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 * All vertex buffers should be in an unmapped state when we're about
50 check_input_buffers_are_unmapped(const struct gl_vertex_array_object
*vao
)
52 /* Walk the enabled arrays that have a vbo attached */
53 GLbitfield64 mask
= vao
->_Enabled
& vao
->VertexAttribBufferMask
;
56 int i
= ffsll(mask
) - 1;
57 const struct gl_vertex_attrib_array
*attrib_array
=
58 &vao
->VertexAttrib
[i
];
59 const struct gl_vertex_buffer_binding
*buffer_binding
=
60 &vao
->VertexBinding
[attrib_array
->VertexBinding
];
62 /* Only enabled arrays shall appear in the _Enabled bitmask */
63 assert(attrib_array
->Enabled
);
64 /* We have already masked with vao->VertexAttribBufferMask */
65 assert(_mesa_is_bufferobj(buffer_binding
->BufferObj
));
67 /* Bail out once we find the first disallowed mapping */
68 if (_mesa_check_disallowed_mapping(buffer_binding
->BufferObj
))
71 /* We have handled everything that is bound to this buffer_binding. */
72 mask
&= ~buffer_binding
->_BoundArrays
;
80 * A debug function that may be called from other parts of Mesa as
81 * needed during debugging.
84 check_buffers_are_unmapped(struct gl_context
*ctx
)
86 struct vbo_context
*vbo
= vbo_context(ctx
);
87 struct vbo_exec_context
*exec
= &vbo
->exec
;
89 /* check the current vertex arrays */
90 return !_mesa_check_disallowed_mapping(exec
->vtx
.bufferobj
) &&
91 check_input_buffers_are_unmapped(ctx
->Array
.VAO
);
96 * Check that element 'j' of the array has reasonable data.
98 * For debugging purposes; not normally used.
101 check_array_data(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
102 GLuint attrib
, GLuint j
)
104 const struct gl_vertex_attrib_array
*array
= &vao
->VertexAttrib
[attrib
];
105 if (array
->Enabled
) {
106 const struct gl_vertex_buffer_binding
*binding
=
107 &vao
->VertexBinding
[array
->VertexBinding
];
108 struct gl_buffer_object
*bo
= binding
->BufferObj
;
109 const void *data
= array
->Ptr
;
110 if (_mesa_is_bufferobj(bo
)) {
111 if (!bo
->Mappings
[MAP_INTERNAL
].Pointer
) {
112 /* need to map now */
113 bo
->Mappings
[MAP_INTERNAL
].Pointer
=
114 ctx
->Driver
.MapBufferRange(ctx
, 0, bo
->Size
,
118 data
= ADD_POINTERS(_mesa_vertex_attrib_address(array
, binding
),
119 bo
->Mappings
[MAP_INTERNAL
].Pointer
);
121 switch (array
->Type
) {
124 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ binding
->Stride
* j
);
126 for (k
= 0; k
< array
->Size
; k
++) {
127 if (IS_INF_OR_NAN(f
[k
]) ||
128 f
[k
] >= 1.0e20F
|| f
[k
] <= -1.0e10F
) {
129 printf("Bad array data:\n");
130 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
131 printf(" Array %u at %p\n", attrib
, (void* ) array
);
132 printf(" Type 0x%x, Size %d, Stride %d\n",
133 array
->Type
, array
->Size
, binding
->Stride
);
134 printf(" Address/offset %p in Buffer Object %u\n",
135 array
->Ptr
, bo
->Name
);
136 f
[k
] = 1.0F
; /* XXX replace the bad value! */
138 /*assert(!IS_INF_OR_NAN(f[k]));*/
150 * Unmap the buffer object referenced by given array, if mapped.
153 unmap_array_buffer(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
156 const struct gl_vertex_attrib_array
*array
= &vao
->VertexAttrib
[attrib
];
157 if (array
->Enabled
) {
158 const struct gl_vertex_buffer_binding
*binding
=
159 &vao
->VertexBinding
[array
->VertexBinding
];
160 struct gl_buffer_object
*bo
= binding
->BufferObj
;
161 if (_mesa_is_bufferobj(bo
) && _mesa_bufferobj_mapped(bo
, MAP_INTERNAL
)) {
162 ctx
->Driver
.UnmapBuffer(ctx
, bo
, MAP_INTERNAL
);
169 * Examine the array's data for NaNs, etc.
170 * For debug purposes; not normally used.
173 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
174 const void *elements
, GLint basevertex
)
176 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
181 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
182 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
183 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
185 ctx
->Array
.VAO
->IndexBufferObj
,
187 elements
= ADD_POINTERS(elements
, elemMap
);
190 for (i
= 0; i
< count
; i
++) {
195 case GL_UNSIGNED_BYTE
:
196 j
= ((const GLubyte
*) elements
)[i
];
198 case GL_UNSIGNED_SHORT
:
199 j
= ((const GLushort
*) elements
)[i
];
201 case GL_UNSIGNED_INT
:
202 j
= ((const GLuint
*) elements
)[i
];
208 /* check element j of each enabled array */
209 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
210 check_array_data(ctx
, vao
, k
, j
);
214 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
215 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
219 for (k
= 0; k
< VERT_ATTRIB_MAX
; k
++) {
220 unmap_array_buffer(ctx
, vao
, k
);
226 * Check array data, looking for NaNs, etc.
229 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
236 * Print info/data for glDrawArrays(), for debugging.
239 print_draw_arrays(struct gl_context
*ctx
,
240 GLenum mode
, GLint start
, GLsizei count
)
242 const struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
244 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
248 for (i
= 0; i
< VERT_ATTRIB_MAX
; ++i
) {
249 const struct gl_vertex_attrib_array
*array
= &vao
->VertexAttrib
[i
];
253 const struct gl_vertex_buffer_binding
*binding
=
254 &vao
->VertexBinding
[array
->VertexBinding
];
255 struct gl_buffer_object
*bufObj
= binding
->BufferObj
;
257 printf("attr %s: size %d stride %d enabled %d "
258 "ptr %p Bufobj %u\n",
259 gl_vert_attrib_name((gl_vert_attrib
)i
),
260 array
->Size
, binding
->Stride
, array
->Enabled
,
261 array
->Ptr
, bufObj
->Name
);
263 if (_mesa_is_bufferobj(bufObj
)) {
264 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
265 GL_MAP_READ_BIT
, bufObj
,
267 int offset
= (int) (GLintptr
)
268 _mesa_vertex_attrib_address(array
, binding
);
269 float *f
= (float *) (p
+ offset
);
272 int n
= (count
* binding
->Stride
) / 4;
275 printf(" Data at offset %d:\n", offset
);
276 for (i
= 0; i
< n
; i
++) {
277 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
279 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
286 * Set the vbo->exec->inputs[] pointers to point to the enabled
287 * vertex arrays. This depends on the current vertex program/shader
288 * being executed because of whether or not generic vertex arrays
289 * alias the conventional vertex arrays.
290 * For arrays that aren't enabled, we set the input[attrib] pointer
291 * to point at a zero-stride current value "array".
294 recalculate_input_bindings(struct gl_context
*ctx
)
296 struct vbo_context
*vbo
= vbo_context(ctx
);
297 struct vbo_exec_context
*exec
= &vbo
->exec
;
298 struct gl_client_array
*vertexAttrib
= ctx
->Array
.VAO
->_VertexAttrib
;
299 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
300 GLbitfield64 const_inputs
= 0x0;
303 switch (get_program_mode(ctx
)) {
305 /* When no vertex program is active (or the vertex program is generated
306 * from fixed-function state). We put the material values into the
307 * generic slots. This is the only situation where material values
308 * are available as per-vertex attributes.
310 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
311 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
312 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
314 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
315 const_inputs
|= VERT_BIT(i
);
319 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
320 inputs
[VERT_ATTRIB_GENERIC(i
)] =
321 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+i
];
322 const_inputs
|= VERT_BIT_GENERIC(i
);
325 /* Could use just about anything, just to fill in the empty
328 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
329 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
330 const_inputs
|= VERT_BIT_GENERIC(i
);
335 /* There are no shaders in OpenGL ES 1.x, so this code path should be
336 * impossible to reach. The meta code is careful to not use shaders in
339 assert(ctx
->API
!= API_OPENGLES
);
341 /* In the compatibility profile of desktop OpenGL, the generic[0]
342 * attribute array aliases and overrides the legacy position array.
343 * Otherwise, legacy attributes available in the legacy slots,
344 * generic attributes in the generic slots and materials are not
345 * available as per-vertex attributes.
347 * In all other APIs, only the generic attributes exist, and none of the
348 * slots are considered "magic."
350 if (ctx
->API
== API_OPENGL_COMPAT
) {
351 if (vertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
)
352 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
353 else if (vertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
354 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
356 inputs
[0] = &vbo
->currval
[VBO_ATTRIB_POS
];
357 const_inputs
|= VERT_BIT_POS
;
360 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
361 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
362 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
364 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
365 const_inputs
|= VERT_BIT_FF(i
);
369 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
370 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
371 inputs
[VERT_ATTRIB_GENERIC(i
)] =
372 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
374 inputs
[VERT_ATTRIB_GENERIC(i
)] =
375 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
376 const_inputs
|= VERT_BIT_GENERIC(i
);
380 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
382 /* Other parts of the code assume that inputs[0] through
383 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
384 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
387 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
388 assert(!vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
);
390 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
391 const_inputs
|= VERT_BIT_FF(i
);
394 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
395 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
396 inputs
[VERT_ATTRIB_GENERIC(i
)] =
397 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
399 inputs
[VERT_ATTRIB_GENERIC(i
)] =
400 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
401 const_inputs
|= VERT_BIT_GENERIC(i
);
409 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
410 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
415 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
416 * These will point to the arrays to actually use for drawing. Some will
417 * be user-provided arrays, other will be zero-stride const-valued arrays.
418 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
419 * validation must be done after this call.
422 vbo_bind_arrays(struct gl_context
*ctx
)
424 struct vbo_context
*vbo
= vbo_context(ctx
);
425 struct vbo_exec_context
*exec
= &vbo
->exec
;
427 vbo_draw_method(vbo
, DRAW_ARRAYS
);
429 if (exec
->array
.recalculate_inputs
) {
430 recalculate_input_bindings(ctx
);
431 exec
->array
.recalculate_inputs
= GL_FALSE
;
433 /* Again... because we may have changed the bitmask of per-vertex varying
434 * attributes. If we regenerate the fixed-function vertex program now
435 * we may be able to prune down the number of vertex attributes which we
436 * need in the shader.
439 /* Setting "validating" to TRUE prevents _mesa_update_state from
440 * invalidating what we just did.
442 exec
->validating
= GL_TRUE
;
443 _mesa_update_state(ctx
);
444 exec
->validating
= GL_FALSE
;
448 if (!check_buffers_are_unmapped(ctx
)) {
449 _mesa_error(ctx
, GL_INVALID_OPERATION
,
450 "draw call (vertex buffers are mapped)");
458 * Helper function called by the other DrawArrays() functions below.
459 * This is where we handle primitive restart for drawing non-indexed
460 * arrays. If primitive restart is enabled, it typically means
461 * splitting one DrawArrays() into two.
464 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
465 GLsizei count
, GLuint numInstances
, GLuint baseInstance
)
467 struct vbo_context
*vbo
= vbo_context(ctx
);
468 struct _mesa_prim prim
[2];
470 if (!vbo_bind_arrays(ctx
))
473 /* init most fields to zero */
474 memset(prim
, 0, sizeof(prim
));
478 prim
[0].num_instances
= numInstances
;
479 prim
[0].base_instance
= baseInstance
;
480 prim
[0].is_indirect
= 0;
482 /* Implement the primitive restart index */
483 if (ctx
->Array
.PrimitiveRestart
&& !ctx
->Array
.PrimitiveRestartFixedIndex
&&
484 ctx
->Array
.RestartIndex
< count
) {
485 GLuint primCount
= 0;
487 if (ctx
->Array
.RestartIndex
== start
) {
488 /* special case: RestartIndex at beginning */
490 prim
[0].start
= start
+ 1;
491 prim
[0].count
= count
- 1;
495 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
496 /* special case: RestartIndex at end */
498 prim
[0].start
= start
;
499 prim
[0].count
= count
- 1;
504 /* general case: RestartIndex in middle, split into two prims */
505 prim
[0].start
= start
;
506 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
509 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
510 prim
[1].count
= count
- prim
[1].start
;
516 /* draw one or two prims */
517 vbo
->draw_prims(ctx
, prim
, primCount
, NULL
,
518 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
522 /* no prim restart */
523 prim
[0].start
= start
;
524 prim
[0].count
= count
;
526 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
527 GL_TRUE
, start
, start
+ count
- 1,
531 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
538 * Execute a glRectf() function.
540 static void GLAPIENTRY
541 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
543 GET_CURRENT_CONTEXT(ctx
);
544 ASSERT_OUTSIDE_BEGIN_END(ctx
);
546 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
547 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
548 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
549 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
550 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
551 CALL_End(GET_DISPATCH(), ());
555 static void GLAPIENTRY
556 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
558 GET_CURRENT_CONTEXT(ctx
);
568 prim
= GL_LINE_STRIP
;
571 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)" );
575 /* No effect if vertex maps disabled.
577 if (!ctx
->Eval
.Map1Vertex4
&&
578 !ctx
->Eval
.Map1Vertex3
)
581 du
= ctx
->Eval
.MapGrid1du
;
582 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
584 CALL_Begin(GET_DISPATCH(), (prim
));
585 for (i
=i1
;i
<=i2
;i
++,u
+=du
) {
586 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
588 CALL_End(GET_DISPATCH(), ());
592 static void GLAPIENTRY
593 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
595 GET_CURRENT_CONTEXT(ctx
);
596 GLfloat u
, du
, v
, dv
, v1
, u1
;
605 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)" );
609 /* No effect if vertex maps disabled.
611 if (!ctx
->Eval
.Map2Vertex4
&&
612 !ctx
->Eval
.Map2Vertex3
)
615 du
= ctx
->Eval
.MapGrid2du
;
616 dv
= ctx
->Eval
.MapGrid2dv
;
617 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
618 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
622 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
623 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
624 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
625 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
628 CALL_End(GET_DISPATCH(), ());
631 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
632 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
633 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
634 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
636 CALL_End(GET_DISPATCH(), ());
638 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
639 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
640 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
641 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
643 CALL_End(GET_DISPATCH(), ());
647 for (v
=v1
,j
=j1
;j
<j2
;j
++,v
+=dv
) {
648 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
649 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
650 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
651 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+dv
));
653 CALL_End(GET_DISPATCH(), ());
661 * Called from glDrawArrays when in immediate mode (not display list mode).
663 static void GLAPIENTRY
664 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
666 GET_CURRENT_CONTEXT(ctx
);
668 if (MESA_VERBOSE
& VERBOSE_DRAW
)
669 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
670 _mesa_enum_to_string(mode
), start
, count
);
672 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
676 check_draw_arrays_data(ctx
, start
, count
);
678 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0);
681 print_draw_arrays(ctx
, mode
, start
, count
);
686 * Called from glDrawArraysInstanced when in immediate mode (not
687 * display list mode).
689 static void GLAPIENTRY
690 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
691 GLsizei numInstances
)
693 GET_CURRENT_CONTEXT(ctx
);
695 if (MESA_VERBOSE
& VERBOSE_DRAW
)
696 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
697 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
699 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
703 check_draw_arrays_data(ctx
, start
, count
);
705 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0);
708 print_draw_arrays(ctx
, mode
, start
, count
);
713 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
715 static void GLAPIENTRY
716 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
, GLsizei count
,
717 GLsizei numInstances
, GLuint baseInstance
)
719 GET_CURRENT_CONTEXT(ctx
);
721 if (MESA_VERBOSE
& VERBOSE_DRAW
)
722 _mesa_debug(ctx
, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
723 _mesa_enum_to_string(mode
), first
, count
,
724 numInstances
, baseInstance
);
726 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
731 check_draw_arrays_data(ctx
, first
, count
);
733 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
);
736 print_draw_arrays(ctx
, mode
, first
, count
);
742 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
747 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
750 ctx
->Driver
.MapBufferRange(ctx
, 0,
751 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
753 ctx
->Array
.VAO
->IndexBufferObj
,
756 case GL_UNSIGNED_BYTE
:
758 const GLubyte
*us
= (const GLubyte
*) map
;
760 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
761 printf("%02x ", us
[i
]);
768 case GL_UNSIGNED_SHORT
:
770 const GLushort
*us
= (const GLushort
*) map
;
772 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
773 printf("%04x ", us
[i
]);
780 case GL_UNSIGNED_INT
:
782 const GLuint
*us
= (const GLuint
*) map
;
784 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
785 printf("%08x ", us
[i
]);
796 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
803 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
804 * Do the rendering for a glDrawElements or glDrawRangeElements call after
805 * we've validated buffer bounds, etc.
808 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
809 GLboolean index_bounds_valid
,
810 GLuint start
, GLuint end
,
811 GLsizei count
, GLenum type
,
812 const GLvoid
*indices
,
813 GLint basevertex
, GLuint numInstances
,
816 struct vbo_context
*vbo
= vbo_context(ctx
);
817 struct _mesa_index_buffer ib
;
818 struct _mesa_prim prim
[1];
820 if (!vbo_bind_arrays(ctx
))
825 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
834 prim
[0].count
= count
;
836 prim
[0].is_indirect
= 0;
837 prim
[0].basevertex
= basevertex
;
838 prim
[0].num_instances
= numInstances
;
839 prim
[0].base_instance
= baseInstance
;
841 /* Need to give special consideration to rendering a range of
842 * indices starting somewhere above zero. Typically the
843 * application is issuing multiple DrawRangeElements() to draw
844 * successive primitives layed out linearly in the vertex arrays.
845 * Unless the vertex arrays are all in a VBO (or locked as with
846 * CVA), the OpenGL semantics imply that we need to re-read or
847 * re-upload the vertex data on each draw call.
849 * In the case of hardware tnl, we want to avoid starting the
850 * upload at zero, as it will mean every draw call uploads an
851 * increasing amount of not-used vertex data. Worse - in the
852 * software tnl module, all those vertices might be transformed and
853 * lit but never rendered.
855 * If we just upload or transform the vertices in start..end,
856 * however, the indices will be incorrect.
858 * At this level, we don't know exactly what the requirements of
859 * the backend are going to be, though it will likely boil down to
862 * 1) Do nothing, everything is in a VBO and is processed once
865 * 2) Adjust the indices and vertex arrays so that start becomes
868 * Rather than doing anything here, I'll provide a helper function
869 * for the latter case elsewhere.
872 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
873 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
875 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
882 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
884 static void GLAPIENTRY
885 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
886 GLuint start
, GLuint end
,
887 GLsizei count
, GLenum type
,
888 const GLvoid
*indices
,
891 static GLuint warnCount
= 0;
892 GLboolean index_bounds_valid
= GL_TRUE
;
894 /* This is only useful to catch invalid values in the "end" parameter
897 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
899 GET_CURRENT_CONTEXT(ctx
);
901 if (MESA_VERBOSE
& VERBOSE_DRAW
)
903 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
904 _mesa_enum_to_string(mode
), start
, end
, count
,
905 _mesa_enum_to_string(type
), indices
, basevertex
);
907 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
911 if ((int) end
+ basevertex
< 0 ||
912 start
+ basevertex
>= max_element
) {
913 /* The application requested we draw using a range of indices that's
914 * outside the bounds of the current VBO. This is invalid and appears
915 * to give undefined results. The safest thing to do is to simply
916 * ignore the range, in case the application botched their range tracking
917 * but did provide valid indices. Also issue a warning indicating that
918 * the application is broken.
920 if (warnCount
++ < 10) {
921 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
922 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
923 "\trange is outside VBO bounds (max=%u); ignoring.\n"
924 "\tThis should be fixed in the application.",
925 start
, end
, basevertex
, count
, type
, indices
,
928 index_bounds_valid
= GL_FALSE
;
931 /* NOTE: It's important that 'end' is a reasonable value.
932 * in _tnl_draw_prims(), we use end to determine how many vertices
933 * to transform. If it's too large, we can unnecessarily split prims
934 * or we can read/write out of memory in several different places!
937 /* Catch/fix some potential user errors */
938 if (type
== GL_UNSIGNED_BYTE
) {
939 start
= MIN2(start
, 0xff);
940 end
= MIN2(end
, 0xff);
942 else if (type
== GL_UNSIGNED_SHORT
) {
943 start
= MIN2(start
, 0xffff);
944 end
= MIN2(end
, 0xffff);
948 printf("glDraw[Range]Elements{,BaseVertex}"
949 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
951 start
, end
, type
, count
,
952 ctx
->Array
.VAO
->IndexBufferObj
->Name
,
956 if ((int) start
+ basevertex
< 0 ||
957 end
+ basevertex
>= max_element
)
958 index_bounds_valid
= GL_FALSE
;
961 check_draw_elements_data(ctx
, count
, type
, indices
);
963 (void) check_draw_elements_data
;
966 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
967 count
, type
, indices
, basevertex
, 1, 0);
972 * Called by glDrawRangeElements() in immediate mode.
974 static void GLAPIENTRY
975 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
976 GLsizei count
, GLenum type
, const GLvoid
*indices
)
978 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
979 GET_CURRENT_CONTEXT(ctx
);
981 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
982 _mesa_enum_to_string(mode
), start
, end
, count
,
983 _mesa_enum_to_string(type
), indices
);
986 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
992 * Called by glDrawElements() in immediate mode.
994 static void GLAPIENTRY
995 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
996 const GLvoid
*indices
)
998 GET_CURRENT_CONTEXT(ctx
);
1000 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1001 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1002 _mesa_enum_to_string(mode
), count
,
1003 _mesa_enum_to_string(type
), indices
);
1005 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1008 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1009 count
, type
, indices
, 0, 1, 0);
1014 * Called by glDrawElementsBaseVertex() in immediate mode.
1016 static void GLAPIENTRY
1017 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1018 const GLvoid
*indices
, GLint basevertex
)
1020 GET_CURRENT_CONTEXT(ctx
);
1022 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1023 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1024 _mesa_enum_to_string(mode
), count
,
1025 _mesa_enum_to_string(type
), indices
, basevertex
);
1027 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1030 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1031 count
, type
, indices
, basevertex
, 1, 0);
1036 * Called by glDrawElementsInstanced() in immediate mode.
1038 static void GLAPIENTRY
1039 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1040 const GLvoid
*indices
, GLsizei numInstances
)
1042 GET_CURRENT_CONTEXT(ctx
);
1044 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1045 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1046 _mesa_enum_to_string(mode
), count
,
1047 _mesa_enum_to_string(type
), indices
, numInstances
);
1049 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1053 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1054 count
, type
, indices
, 0, numInstances
, 0);
1059 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1061 static void GLAPIENTRY
1062 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1063 const GLvoid
*indices
, GLsizei numInstances
,
1066 GET_CURRENT_CONTEXT(ctx
);
1068 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1069 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1070 _mesa_enum_to_string(mode
), count
,
1071 _mesa_enum_to_string(type
), indices
,
1072 numInstances
, basevertex
);
1074 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1078 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1079 count
, type
, indices
, basevertex
, numInstances
, 0);
1084 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1086 static void GLAPIENTRY
1087 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1088 const GLvoid
*indices
, GLsizei numInstances
,
1089 GLuint baseInstance
)
1091 GET_CURRENT_CONTEXT(ctx
);
1093 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1094 _mesa_debug(ctx
, "glDrawElementsInstancedBaseInstance(%s, %d, %s, %p, %d, %d)\n",
1095 _mesa_enum_to_string(mode
), count
,
1096 _mesa_enum_to_string(type
), indices
,
1097 numInstances
, baseInstance
);
1099 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1103 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1104 count
, type
, indices
, 0, numInstances
,
1110 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1112 static void GLAPIENTRY
1113 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1114 const GLvoid
*indices
, GLsizei numInstances
,
1115 GLint basevertex
, GLuint baseInstance
)
1117 GET_CURRENT_CONTEXT(ctx
);
1119 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1120 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertexBaseInstance(%s, %d, %s, %p, %d, %d, %d)\n",
1121 _mesa_enum_to_string(mode
), count
,
1122 _mesa_enum_to_string(type
), indices
,
1123 numInstances
, basevertex
, baseInstance
);
1125 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1129 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1130 count
, type
, indices
, basevertex
, numInstances
,
1136 * Inner support for both _mesa_MultiDrawElements() and
1137 * _mesa_MultiDrawRangeElements().
1138 * This does the actual rendering after we've checked array indexes, etc.
1141 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1142 const GLsizei
*count
, GLenum type
,
1143 const GLvoid
* const *indices
,
1145 const GLint
*basevertex
)
1147 struct vbo_context
*vbo
= vbo_context(ctx
);
1148 struct _mesa_index_buffer ib
;
1149 struct _mesa_prim
*prim
;
1150 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1151 uintptr_t min_index_ptr
, max_index_ptr
;
1152 GLboolean fallback
= GL_FALSE
;
1158 prim
= calloc(primcount
, sizeof(*prim
));
1160 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1164 if (!vbo_bind_arrays(ctx
)) {
1169 min_index_ptr
= (uintptr_t)indices
[0];
1171 for (i
= 0; i
< primcount
; i
++) {
1172 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1173 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1174 index_type_size
* count
[i
]);
1177 /* Check if we can handle this thing as a bunch of index offsets from the
1178 * same index pointer. If we can't, then we have to fall back to doing
1179 * a draw_prims per primitive.
1180 * Check that the difference between each prim's indexes is a multiple of
1181 * the index/element size.
1183 if (index_type_size
!= 1) {
1184 for (i
= 0; i
< primcount
; i
++) {
1185 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1192 /* Draw primitives individually if one count is zero, so we can easily skip
1195 for (i
= 0; i
< primcount
; i
++) {
1196 if (count
[i
] == 0) {
1202 /* If the index buffer isn't in a VBO, then treating the application's
1203 * subranges of the index buffer as one large index buffer may lead to
1204 * us reading unmapped memory.
1206 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1210 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1212 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1213 ib
.ptr
= (void *)min_index_ptr
;
1215 for (i
= 0; i
< primcount
; i
++) {
1216 prim
[i
].begin
= (i
== 0);
1217 prim
[i
].end
= (i
== primcount
- 1);
1220 prim
[i
].mode
= mode
;
1221 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1222 prim
[i
].count
= count
[i
];
1223 prim
[i
].indexed
= 1;
1224 prim
[i
].num_instances
= 1;
1225 prim
[i
].base_instance
= 0;
1226 prim
[i
].draw_id
= i
;
1227 prim
[i
].is_indirect
= 0;
1228 if (basevertex
!= NULL
)
1229 prim
[i
].basevertex
= basevertex
[i
];
1231 prim
[i
].basevertex
= 0;
1234 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1235 false, ~0, ~0, NULL
, 0, NULL
);
1237 /* render one prim at a time */
1238 for (i
= 0; i
< primcount
; i
++) {
1241 ib
.count
= count
[i
];
1243 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1244 ib
.ptr
= indices
[i
];
1250 prim
[0].mode
= mode
;
1252 prim
[0].count
= count
[i
];
1253 prim
[0].indexed
= 1;
1254 prim
[0].num_instances
= 1;
1255 prim
[0].base_instance
= 0;
1256 prim
[0].draw_id
= i
;
1257 prim
[0].is_indirect
= 0;
1258 if (basevertex
!= NULL
)
1259 prim
[0].basevertex
= basevertex
[i
];
1261 prim
[0].basevertex
= 0;
1263 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
1264 false, ~0, ~0, NULL
, 0, NULL
);
1270 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1276 static void GLAPIENTRY
1277 vbo_exec_MultiDrawElements(GLenum mode
,
1278 const GLsizei
*count
, GLenum type
,
1279 const GLvoid
* const *indices
,
1282 GET_CURRENT_CONTEXT(ctx
);
1284 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1288 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1293 static void GLAPIENTRY
1294 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1295 const GLsizei
*count
, GLenum type
,
1296 const GLvoid
* const *indices
,
1298 const GLsizei
*basevertex
)
1300 GET_CURRENT_CONTEXT(ctx
);
1302 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1306 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1311 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1312 struct gl_transform_feedback_object
*obj
,
1313 GLuint stream
, GLuint numInstances
)
1315 struct vbo_context
*vbo
= vbo_context(ctx
);
1316 struct _mesa_prim prim
[2];
1318 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1323 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1324 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1325 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1326 GLsizei n
= ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1327 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0);
1331 if (!vbo_bind_arrays(ctx
))
1334 /* init most fields to zero */
1335 memset(prim
, 0, sizeof(prim
));
1338 prim
[0].mode
= mode
;
1339 prim
[0].num_instances
= numInstances
;
1340 prim
[0].base_instance
= 0;
1341 prim
[0].is_indirect
= 0;
1343 /* Maybe we should do some primitive splitting for primitive restart
1344 * (like in DrawArrays), but we have no way to know how many vertices
1345 * will be rendered. */
1347 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
1348 GL_FALSE
, ~0, ~0, obj
, stream
, NULL
);
1350 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1356 * Like DrawArrays, but take the count from a transform feedback object.
1357 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1358 * \param name the transform feedback object
1359 * User still has to setup of the vertex attribute info with
1360 * glVertexPointer, glColorPointer, etc.
1361 * Part of GL_ARB_transform_feedback2.
1363 static void GLAPIENTRY
1364 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1366 GET_CURRENT_CONTEXT(ctx
);
1367 struct gl_transform_feedback_object
*obj
=
1368 _mesa_lookup_transform_feedback_object(ctx
, name
);
1370 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1371 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1372 _mesa_enum_to_string(mode
), name
);
1374 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1377 static void GLAPIENTRY
1378 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1380 GET_CURRENT_CONTEXT(ctx
);
1381 struct gl_transform_feedback_object
*obj
=
1382 _mesa_lookup_transform_feedback_object(ctx
, name
);
1384 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1385 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1386 _mesa_enum_to_string(mode
), name
, stream
);
1388 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1391 static void GLAPIENTRY
1392 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1395 GET_CURRENT_CONTEXT(ctx
);
1396 struct gl_transform_feedback_object
*obj
=
1397 _mesa_lookup_transform_feedback_object(ctx
, name
);
1399 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1400 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1401 _mesa_enum_to_string(mode
), name
);
1403 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1406 static void GLAPIENTRY
1407 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1408 GLuint stream
, GLsizei primcount
)
1410 GET_CURRENT_CONTEXT(ctx
);
1411 struct gl_transform_feedback_object
*obj
=
1412 _mesa_lookup_transform_feedback_object(ctx
, name
);
1414 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1415 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1416 "(%s, %u, %u, %i)\n",
1417 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1419 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1423 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1424 GLenum mode
, const GLvoid
*indirect
)
1426 struct vbo_context
*vbo
= vbo_context(ctx
);
1428 if (!vbo_bind_arrays(ctx
))
1431 vbo
->draw_indirect_prims(ctx
, mode
,
1432 ctx
->DrawIndirectBuffer
, (GLsizeiptr
)indirect
,
1433 1 /* draw_count */, 16 /* stride */,
1436 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1441 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1443 const GLvoid
*indirect
,
1444 GLsizei primcount
, GLsizei stride
)
1446 struct vbo_context
*vbo
= vbo_context(ctx
);
1447 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1452 if (!vbo_bind_arrays(ctx
))
1455 vbo
->draw_indirect_prims(ctx
, mode
,
1456 ctx
->DrawIndirectBuffer
, offset
,
1460 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1465 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1466 GLenum mode
, GLenum type
,
1467 const GLvoid
*indirect
)
1469 struct vbo_context
*vbo
= vbo_context(ctx
);
1470 struct _mesa_index_buffer ib
;
1472 if (!vbo_bind_arrays(ctx
))
1475 ib
.count
= 0; /* unknown */
1477 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1480 vbo
->draw_indirect_prims(ctx
, mode
,
1481 ctx
->DrawIndirectBuffer
, (GLsizeiptr
)indirect
,
1482 1 /* draw_count */, 20 /* stride */,
1486 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1491 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1492 GLenum mode
, GLenum type
,
1493 const GLvoid
*indirect
,
1494 GLsizei primcount
, GLsizei stride
)
1496 struct vbo_context
*vbo
= vbo_context(ctx
);
1497 struct _mesa_index_buffer ib
;
1498 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1503 if (!vbo_bind_arrays(ctx
))
1506 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1508 ib
.count
= 0; /* unknown */
1510 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1513 vbo
->draw_indirect_prims(ctx
, mode
,
1514 ctx
->DrawIndirectBuffer
, offset
,
1519 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1524 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1527 static void GLAPIENTRY
1528 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1530 GET_CURRENT_CONTEXT(ctx
);
1532 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1533 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1534 _mesa_enum_to_string(mode
), indirect
);
1536 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1539 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1542 static void GLAPIENTRY
1543 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
,
1544 const GLvoid
*indirect
)
1546 GET_CURRENT_CONTEXT(ctx
);
1548 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1549 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1550 _mesa_enum_to_string(mode
),
1551 _mesa_enum_to_string(type
), indirect
);
1553 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1556 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1559 static void GLAPIENTRY
1560 vbo_exec_MultiDrawArraysIndirect(GLenum mode
,
1561 const GLvoid
*indirect
,
1562 GLsizei primcount
, GLsizei stride
)
1564 GET_CURRENT_CONTEXT(ctx
);
1566 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1567 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1568 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1570 /* If <stride> is zero, the array elements are treated as tightly packed. */
1572 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1574 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
,
1579 vbo_validated_multidrawarraysindirect(ctx
, mode
,
1584 static void GLAPIENTRY
1585 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1586 const GLvoid
*indirect
,
1587 GLsizei primcount
, GLsizei stride
)
1589 GET_CURRENT_CONTEXT(ctx
);
1591 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1592 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1593 _mesa_enum_to_string(mode
),
1594 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1596 /* If <stride> is zero, the array elements are treated as tightly packed. */
1598 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1600 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
,
1605 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
,
1611 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1615 GLsizei maxdrawcount
,
1618 struct vbo_context
*vbo
= vbo_context(ctx
);
1619 GLsizeiptr offset
= indirect
;
1621 if (maxdrawcount
== 0)
1624 if (!vbo_bind_arrays(ctx
))
1627 vbo
->draw_indirect_prims(ctx
, mode
,
1628 ctx
->DrawIndirectBuffer
, offset
,
1629 maxdrawcount
, stride
,
1630 ctx
->ParameterBuffer
, drawcount
,
1633 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1638 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1639 GLenum mode
, GLenum type
,
1642 GLsizei maxdrawcount
,
1645 struct vbo_context
*vbo
= vbo_context(ctx
);
1646 struct _mesa_index_buffer ib
;
1647 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1649 if (maxdrawcount
== 0)
1652 if (!vbo_bind_arrays(ctx
))
1655 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1657 ib
.count
= 0; /* unknown */
1659 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1662 vbo
->draw_indirect_prims(ctx
, mode
,
1663 ctx
->DrawIndirectBuffer
, offset
,
1664 maxdrawcount
, stride
,
1665 ctx
->ParameterBuffer
, drawcount
,
1668 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1672 static void GLAPIENTRY
1673 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
,
1676 GLsizei maxdrawcount
, GLsizei stride
)
1678 GET_CURRENT_CONTEXT(ctx
);
1680 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1681 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1682 "(%s, %lx, %lx, %i, %i)\n",
1683 _mesa_enum_to_string(mode
), indirect
,
1684 drawcount
, maxdrawcount
, stride
);
1686 /* If <stride> is zero, the array elements are treated as tightly packed. */
1688 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1690 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1691 indirect
, drawcount
,
1692 maxdrawcount
, stride
))
1695 vbo_validated_multidrawarraysindirectcount(ctx
, mode
,
1696 indirect
, drawcount
,
1697 maxdrawcount
, stride
);
1700 static void GLAPIENTRY
1701 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1704 GLsizei maxdrawcount
, GLsizei stride
)
1706 GET_CURRENT_CONTEXT(ctx
);
1708 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1709 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1710 "(%s, %s, %lx, %lx, %i, %i)\n",
1711 _mesa_enum_to_string(mode
),
1712 _mesa_enum_to_string(type
), indirect
,
1713 drawcount
, maxdrawcount
, stride
);
1715 /* If <stride> is zero, the array elements are treated as tightly packed. */
1717 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1719 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1720 indirect
, drawcount
,
1721 maxdrawcount
, stride
))
1724 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
,
1725 indirect
, drawcount
,
1726 maxdrawcount
, stride
);
1731 * Initialize the dispatch table with the VBO functions for drawing.
1734 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1735 struct _glapi_table
*exec
)
1737 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1738 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1740 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1741 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1744 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1746 if (ctx
->API
== API_OPENGL_COMPAT
) {
1747 SET_Rectf(exec
, vbo_exec_Rectf
);
1748 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1749 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1752 if (ctx
->API
!= API_OPENGLES
&&
1753 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1754 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1755 SET_MultiDrawElementsBaseVertex(exec
, vbo_exec_MultiDrawElementsBaseVertex
);
1757 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1758 SET_DrawRangeElementsBaseVertex(exec
, vbo_exec_DrawRangeElementsBaseVertex
);
1759 SET_DrawElementsInstancedBaseVertex(exec
, vbo_exec_DrawElementsInstancedBaseVertex
);
1763 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1764 SET_DrawArraysInstancedBaseInstance(exec
, vbo_exec_DrawArraysInstancedBaseInstance
);
1765 SET_DrawElementsInstancedBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseInstance
);
1766 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1769 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1770 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1771 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1774 if (ctx
->API
== API_OPENGL_CORE
) {
1775 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1776 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1777 SET_MultiDrawArraysIndirectCountARB(exec
, vbo_exec_MultiDrawArraysIndirectCount
);
1778 SET_MultiDrawElementsIndirectCountARB(exec
, vbo_exec_MultiDrawElementsIndirectCount
);
1781 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1782 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1783 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1786 if (_mesa_is_desktop_gl(ctx
)) {
1787 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1788 SET_DrawTransformFeedbackStream(exec
, vbo_exec_DrawTransformFeedbackStream
);
1789 SET_DrawTransformFeedbackInstanced(exec
, vbo_exec_DrawTransformFeedbackInstanced
);
1790 SET_DrawTransformFeedbackStreamInstanced(exec
, vbo_exec_DrawTransformFeedbackStreamInstanced
);
1797 * The following functions are only used for OpenGL ES 1/2 support.
1798 * And some aren't even supported (yet) in ES 1/2.
1803 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1805 vbo_exec_DrawArrays(mode
, first
, count
);
1809 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
1812 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
1816 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1817 const GLvoid
*indices
)
1819 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1824 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1825 const GLvoid
*indices
, GLint basevertex
)
1827 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1832 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1833 GLenum type
, const GLvoid
*indices
)
1835 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1840 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1841 GLsizei count
, GLenum type
,
1842 const GLvoid
*indices
, GLint basevertex
)
1844 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1845 indices
, basevertex
);
1850 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1851 const GLvoid
**indices
, GLsizei primcount
)
1853 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1858 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1859 const GLsizei
*count
, GLenum type
,
1860 const GLvoid
**indices
, GLsizei primcount
,
1861 const GLint
*basevertex
)
1863 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1864 primcount
, basevertex
);
1868 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1870 vbo_exec_DrawTransformFeedback(mode
, name
);