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 const struct gl_vertex_attrib_array
*array
= ctx
->Array
.VAO
->VertexAttrib
;
299 struct gl_client_array
*vertexAttrib
= ctx
->Array
.VAO
->_VertexAttrib
;
300 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
301 GLbitfield64 const_inputs
= 0x0;
304 switch (get_program_mode(ctx
)) {
306 /* When no vertex program is active (or the vertex program is generated
307 * from fixed-function state). We put the material values into the
308 * generic slots. This is the only situation where material values
309 * are available as per-vertex attributes.
311 for (i
= 0; 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(i
);
320 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
321 inputs
[VERT_ATTRIB_GENERIC(i
)] =
322 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+i
];
323 const_inputs
|= VERT_BIT_GENERIC(i
);
326 /* Could use just about anything, just to fill in the empty
329 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
330 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
331 const_inputs
|= VERT_BIT_GENERIC(i
);
336 /* There are no shaders in OpenGL ES 1.x, so this code path should be
337 * impossible to reach. The meta code is careful to not use shaders in
340 assert(ctx
->API
!= API_OPENGLES
);
342 /* In the compatibility profile of desktop OpenGL, the generic[0]
343 * attribute array aliases and overrides the legacy position array.
344 * Otherwise, legacy attributes available in the legacy slots,
345 * generic attributes in the generic slots and materials are not
346 * available as per-vertex attributes.
348 * In all other APIs, only the generic attributes exist, and none of the
349 * slots are considered "magic."
351 if (ctx
->API
== API_OPENGL_COMPAT
) {
352 if (array
[VERT_ATTRIB_GENERIC0
].Enabled
)
353 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
354 else if (array
[VERT_ATTRIB_POS
].Enabled
)
355 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
357 inputs
[0] = &vbo
->currval
[VBO_ATTRIB_POS
];
358 const_inputs
|= VERT_BIT_POS
;
361 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
362 if (array
[VERT_ATTRIB_FF(i
)].Enabled
)
363 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
365 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
366 const_inputs
|= VERT_BIT_FF(i
);
370 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
371 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
372 inputs
[VERT_ATTRIB_GENERIC(i
)] =
373 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
375 inputs
[VERT_ATTRIB_GENERIC(i
)] =
376 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
377 const_inputs
|= VERT_BIT_GENERIC(i
);
381 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
383 /* Other parts of the code assume that inputs[0] through
384 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
385 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
388 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
389 assert(!array
[VERT_ATTRIB_FF(i
)].Enabled
);
391 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
392 const_inputs
|= VERT_BIT_FF(i
);
395 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
396 if (array
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
397 inputs
[VERT_ATTRIB_GENERIC(i
)] =
398 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
400 inputs
[VERT_ATTRIB_GENERIC(i
)] =
401 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
402 const_inputs
|= VERT_BIT_GENERIC(i
);
410 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
411 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
416 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
417 * These will point to the arrays to actually use for drawing. Some will
418 * be user-provided arrays, other will be zero-stride const-valued arrays.
419 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
420 * validation must be done after this call.
423 vbo_bind_arrays(struct gl_context
*ctx
)
425 struct vbo_context
*vbo
= vbo_context(ctx
);
426 struct vbo_exec_context
*exec
= &vbo
->exec
;
428 vbo_draw_method(vbo
, DRAW_ARRAYS
);
430 if (exec
->array
.recalculate_inputs
) {
431 recalculate_input_bindings(ctx
);
432 exec
->array
.recalculate_inputs
= GL_FALSE
;
434 /* Again... because we may have changed the bitmask of per-vertex varying
435 * attributes. If we regenerate the fixed-function vertex program now
436 * we may be able to prune down the number of vertex attributes which we
437 * need in the shader.
440 /* Setting "validating" to TRUE prevents _mesa_update_state from
441 * invalidating what we just did.
443 exec
->validating
= GL_TRUE
;
444 _mesa_update_state(ctx
);
445 exec
->validating
= GL_FALSE
;
449 if (!check_buffers_are_unmapped(ctx
)) {
450 _mesa_error(ctx
, GL_INVALID_OPERATION
,
451 "draw call (vertex buffers are mapped)");
459 * Helper function called by the other DrawArrays() functions below.
460 * This is where we handle primitive restart for drawing non-indexed
461 * arrays. If primitive restart is enabled, it typically means
462 * splitting one DrawArrays() into two.
465 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
466 GLsizei count
, GLuint numInstances
, GLuint baseInstance
)
468 struct vbo_context
*vbo
= vbo_context(ctx
);
469 struct _mesa_prim prim
[2];
471 if (!vbo_bind_arrays(ctx
))
474 /* init most fields to zero */
475 memset(prim
, 0, sizeof(prim
));
479 prim
[0].num_instances
= numInstances
;
480 prim
[0].base_instance
= baseInstance
;
481 prim
[0].is_indirect
= 0;
483 /* Implement the primitive restart index */
484 if (ctx
->Array
.PrimitiveRestart
&& !ctx
->Array
.PrimitiveRestartFixedIndex
&&
485 ctx
->Array
.RestartIndex
< count
) {
486 GLuint primCount
= 0;
488 if (ctx
->Array
.RestartIndex
== start
) {
489 /* special case: RestartIndex at beginning */
491 prim
[0].start
= start
+ 1;
492 prim
[0].count
= count
- 1;
496 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
497 /* special case: RestartIndex at end */
499 prim
[0].start
= start
;
500 prim
[0].count
= count
- 1;
505 /* general case: RestartIndex in middle, split into two prims */
506 prim
[0].start
= start
;
507 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
510 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
511 prim
[1].count
= count
- prim
[1].start
;
517 /* draw one or two prims */
518 vbo
->draw_prims(ctx
, prim
, primCount
, NULL
,
519 GL_TRUE
, start
, start
+ count
- 1, NULL
, 0, NULL
);
523 /* no prim restart */
524 prim
[0].start
= start
;
525 prim
[0].count
= count
;
527 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
528 GL_TRUE
, start
, start
+ count
- 1,
532 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
539 * Execute a glRectf() function.
541 static void GLAPIENTRY
542 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
544 GET_CURRENT_CONTEXT(ctx
);
545 ASSERT_OUTSIDE_BEGIN_END(ctx
);
547 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
548 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
549 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
550 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
551 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
552 CALL_End(GET_DISPATCH(), ());
556 static void GLAPIENTRY
557 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
559 GET_CURRENT_CONTEXT(ctx
);
569 prim
= GL_LINE_STRIP
;
572 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)" );
576 /* No effect if vertex maps disabled.
578 if (!ctx
->Eval
.Map1Vertex4
&&
579 !ctx
->Eval
.Map1Vertex3
)
582 du
= ctx
->Eval
.MapGrid1du
;
583 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
585 CALL_Begin(GET_DISPATCH(), (prim
));
586 for (i
=i1
;i
<=i2
;i
++,u
+=du
) {
587 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
589 CALL_End(GET_DISPATCH(), ());
593 static void GLAPIENTRY
594 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
596 GET_CURRENT_CONTEXT(ctx
);
597 GLfloat u
, du
, v
, dv
, v1
, u1
;
606 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)" );
610 /* No effect if vertex maps disabled.
612 if (!ctx
->Eval
.Map2Vertex4
&&
613 !ctx
->Eval
.Map2Vertex3
)
616 du
= ctx
->Eval
.MapGrid2du
;
617 dv
= ctx
->Eval
.MapGrid2dv
;
618 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
619 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
623 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
624 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
625 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
626 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
629 CALL_End(GET_DISPATCH(), ());
632 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
633 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
634 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
635 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
637 CALL_End(GET_DISPATCH(), ());
639 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
640 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
641 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
642 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
644 CALL_End(GET_DISPATCH(), ());
648 for (v
=v1
,j
=j1
;j
<j2
;j
++,v
+=dv
) {
649 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
650 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
651 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
652 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+dv
));
654 CALL_End(GET_DISPATCH(), ());
662 * Called from glDrawArrays when in immediate mode (not display list mode).
664 static void GLAPIENTRY
665 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
667 GET_CURRENT_CONTEXT(ctx
);
669 if (MESA_VERBOSE
& VERBOSE_DRAW
)
670 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
671 _mesa_enum_to_string(mode
), start
, count
);
673 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
677 check_draw_arrays_data(ctx
, start
, count
);
679 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0);
682 print_draw_arrays(ctx
, mode
, start
, count
);
687 * Called from glDrawArraysInstanced when in immediate mode (not
688 * display list mode).
690 static void GLAPIENTRY
691 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
692 GLsizei numInstances
)
694 GET_CURRENT_CONTEXT(ctx
);
696 if (MESA_VERBOSE
& VERBOSE_DRAW
)
697 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
698 _mesa_enum_to_string(mode
), start
, count
, numInstances
);
700 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
704 check_draw_arrays_data(ctx
, start
, count
);
706 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0);
709 print_draw_arrays(ctx
, mode
, start
, count
);
714 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
716 static void GLAPIENTRY
717 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
, GLsizei count
,
718 GLsizei numInstances
, GLuint baseInstance
)
720 GET_CURRENT_CONTEXT(ctx
);
722 if (MESA_VERBOSE
& VERBOSE_DRAW
)
723 _mesa_debug(ctx
, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
724 _mesa_enum_to_string(mode
), first
, count
,
725 numInstances
, baseInstance
);
727 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
732 check_draw_arrays_data(ctx
, first
, count
);
734 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
);
737 print_draw_arrays(ctx
, mode
, first
, count
);
743 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
748 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
751 ctx
->Driver
.MapBufferRange(ctx
, 0,
752 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
754 ctx
->Array
.VAO
->IndexBufferObj
,
757 case GL_UNSIGNED_BYTE
:
759 const GLubyte
*us
= (const GLubyte
*) map
;
761 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
762 printf("%02x ", us
[i
]);
769 case GL_UNSIGNED_SHORT
:
771 const GLushort
*us
= (const GLushort
*) map
;
773 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
774 printf("%04x ", us
[i
]);
781 case GL_UNSIGNED_INT
:
783 const GLuint
*us
= (const GLuint
*) map
;
785 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
786 printf("%08x ", us
[i
]);
797 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
804 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
805 * Do the rendering for a glDrawElements or glDrawRangeElements call after
806 * we've validated buffer bounds, etc.
809 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
810 GLboolean index_bounds_valid
,
811 GLuint start
, GLuint end
,
812 GLsizei count
, GLenum type
,
813 const GLvoid
*indices
,
814 GLint basevertex
, GLuint numInstances
,
817 struct vbo_context
*vbo
= vbo_context(ctx
);
818 struct _mesa_index_buffer ib
;
819 struct _mesa_prim prim
[1];
821 if (!vbo_bind_arrays(ctx
))
826 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
835 prim
[0].count
= count
;
837 prim
[0].is_indirect
= 0;
838 prim
[0].basevertex
= basevertex
;
839 prim
[0].num_instances
= numInstances
;
840 prim
[0].base_instance
= baseInstance
;
842 /* Need to give special consideration to rendering a range of
843 * indices starting somewhere above zero. Typically the
844 * application is issuing multiple DrawRangeElements() to draw
845 * successive primitives layed out linearly in the vertex arrays.
846 * Unless the vertex arrays are all in a VBO (or locked as with
847 * CVA), the OpenGL semantics imply that we need to re-read or
848 * re-upload the vertex data on each draw call.
850 * In the case of hardware tnl, we want to avoid starting the
851 * upload at zero, as it will mean every draw call uploads an
852 * increasing amount of not-used vertex data. Worse - in the
853 * software tnl module, all those vertices might be transformed and
854 * lit but never rendered.
856 * If we just upload or transform the vertices in start..end,
857 * however, the indices will be incorrect.
859 * At this level, we don't know exactly what the requirements of
860 * the backend are going to be, though it will likely boil down to
863 * 1) Do nothing, everything is in a VBO and is processed once
866 * 2) Adjust the indices and vertex arrays so that start becomes
869 * Rather than doing anything here, I'll provide a helper function
870 * for the latter case elsewhere.
873 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
874 index_bounds_valid
, start
, end
, NULL
, 0, NULL
);
876 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
883 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
885 static void GLAPIENTRY
886 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
887 GLuint start
, GLuint end
,
888 GLsizei count
, GLenum type
,
889 const GLvoid
*indices
,
892 static GLuint warnCount
= 0;
893 GLboolean index_bounds_valid
= GL_TRUE
;
895 /* This is only useful to catch invalid values in the "end" parameter
898 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
900 GET_CURRENT_CONTEXT(ctx
);
902 if (MESA_VERBOSE
& VERBOSE_DRAW
)
904 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
905 _mesa_enum_to_string(mode
), start
, end
, count
,
906 _mesa_enum_to_string(type
), indices
, basevertex
);
908 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
912 if ((int) end
+ basevertex
< 0 ||
913 start
+ basevertex
>= max_element
) {
914 /* The application requested we draw using a range of indices that's
915 * outside the bounds of the current VBO. This is invalid and appears
916 * to give undefined results. The safest thing to do is to simply
917 * ignore the range, in case the application botched their range tracking
918 * but did provide valid indices. Also issue a warning indicating that
919 * the application is broken.
921 if (warnCount
++ < 10) {
922 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
923 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
924 "\trange is outside VBO bounds (max=%u); ignoring.\n"
925 "\tThis should be fixed in the application.",
926 start
, end
, basevertex
, count
, type
, indices
,
929 index_bounds_valid
= GL_FALSE
;
932 /* NOTE: It's important that 'end' is a reasonable value.
933 * in _tnl_draw_prims(), we use end to determine how many vertices
934 * to transform. If it's too large, we can unnecessarily split prims
935 * or we can read/write out of memory in several different places!
938 /* Catch/fix some potential user errors */
939 if (type
== GL_UNSIGNED_BYTE
) {
940 start
= MIN2(start
, 0xff);
941 end
= MIN2(end
, 0xff);
943 else if (type
== GL_UNSIGNED_SHORT
) {
944 start
= MIN2(start
, 0xffff);
945 end
= MIN2(end
, 0xffff);
949 printf("glDraw[Range]Elements{,BaseVertex}"
950 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
952 start
, end
, type
, count
,
953 ctx
->Array
.VAO
->IndexBufferObj
->Name
,
957 if ((int) start
+ basevertex
< 0 ||
958 end
+ basevertex
>= max_element
)
959 index_bounds_valid
= GL_FALSE
;
962 check_draw_elements_data(ctx
, count
, type
, indices
);
964 (void) check_draw_elements_data
;
967 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
968 count
, type
, indices
, basevertex
, 1, 0);
973 * Called by glDrawRangeElements() in immediate mode.
975 static void GLAPIENTRY
976 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
977 GLsizei count
, GLenum type
, const GLvoid
*indices
)
979 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
980 GET_CURRENT_CONTEXT(ctx
);
982 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
983 _mesa_enum_to_string(mode
), start
, end
, count
,
984 _mesa_enum_to_string(type
), indices
);
987 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
993 * Called by glDrawElements() in immediate mode.
995 static void GLAPIENTRY
996 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
997 const GLvoid
*indices
)
999 GET_CURRENT_CONTEXT(ctx
);
1001 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1002 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1003 _mesa_enum_to_string(mode
), count
,
1004 _mesa_enum_to_string(type
), indices
);
1006 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1009 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1010 count
, type
, indices
, 0, 1, 0);
1015 * Called by glDrawElementsBaseVertex() in immediate mode.
1017 static void GLAPIENTRY
1018 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1019 const GLvoid
*indices
, GLint basevertex
)
1021 GET_CURRENT_CONTEXT(ctx
);
1023 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1024 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1025 _mesa_enum_to_string(mode
), count
,
1026 _mesa_enum_to_string(type
), indices
, basevertex
);
1028 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1031 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1032 count
, type
, indices
, basevertex
, 1, 0);
1037 * Called by glDrawElementsInstanced() in immediate mode.
1039 static void GLAPIENTRY
1040 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1041 const GLvoid
*indices
, GLsizei numInstances
)
1043 GET_CURRENT_CONTEXT(ctx
);
1045 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1046 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1047 _mesa_enum_to_string(mode
), count
,
1048 _mesa_enum_to_string(type
), indices
, numInstances
);
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
, 0);
1060 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1062 static void GLAPIENTRY
1063 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1064 const GLvoid
*indices
, GLsizei numInstances
,
1067 GET_CURRENT_CONTEXT(ctx
);
1069 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1070 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1071 _mesa_enum_to_string(mode
), count
,
1072 _mesa_enum_to_string(type
), indices
,
1073 numInstances
, basevertex
);
1075 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1079 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1080 count
, type
, indices
, basevertex
, numInstances
, 0);
1085 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1087 static void GLAPIENTRY
1088 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1089 const GLvoid
*indices
, GLsizei numInstances
,
1090 GLuint baseInstance
)
1092 GET_CURRENT_CONTEXT(ctx
);
1094 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1095 _mesa_debug(ctx
, "glDrawElementsInstancedBaseInstance(%s, %d, %s, %p, %d, %d)\n",
1096 _mesa_enum_to_string(mode
), count
,
1097 _mesa_enum_to_string(type
), indices
,
1098 numInstances
, baseInstance
);
1100 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1104 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1105 count
, type
, indices
, 0, numInstances
,
1111 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1113 static void GLAPIENTRY
1114 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1115 const GLvoid
*indices
, GLsizei numInstances
,
1116 GLint basevertex
, GLuint baseInstance
)
1118 GET_CURRENT_CONTEXT(ctx
);
1120 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1121 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertexBaseInstance(%s, %d, %s, %p, %d, %d, %d)\n",
1122 _mesa_enum_to_string(mode
), count
,
1123 _mesa_enum_to_string(type
), indices
,
1124 numInstances
, basevertex
, baseInstance
);
1126 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1130 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1131 count
, type
, indices
, basevertex
, numInstances
,
1137 * Inner support for both _mesa_MultiDrawElements() and
1138 * _mesa_MultiDrawRangeElements().
1139 * This does the actual rendering after we've checked array indexes, etc.
1142 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1143 const GLsizei
*count
, GLenum type
,
1144 const GLvoid
* const *indices
,
1146 const GLint
*basevertex
)
1148 struct vbo_context
*vbo
= vbo_context(ctx
);
1149 struct _mesa_index_buffer ib
;
1150 struct _mesa_prim
*prim
;
1151 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1152 uintptr_t min_index_ptr
, max_index_ptr
;
1153 GLboolean fallback
= GL_FALSE
;
1159 prim
= calloc(primcount
, sizeof(*prim
));
1161 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1165 if (!vbo_bind_arrays(ctx
)) {
1170 min_index_ptr
= (uintptr_t)indices
[0];
1172 for (i
= 0; i
< primcount
; i
++) {
1173 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1174 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1175 index_type_size
* count
[i
]);
1178 /* Check if we can handle this thing as a bunch of index offsets from the
1179 * same index pointer. If we can't, then we have to fall back to doing
1180 * a draw_prims per primitive.
1181 * Check that the difference between each prim's indexes is a multiple of
1182 * the index/element size.
1184 if (index_type_size
!= 1) {
1185 for (i
= 0; i
< primcount
; i
++) {
1186 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1193 /* Draw primitives individually if one count is zero, so we can easily skip
1196 for (i
= 0; i
< primcount
; i
++) {
1197 if (count
[i
] == 0) {
1203 /* If the index buffer isn't in a VBO, then treating the application's
1204 * subranges of the index buffer as one large index buffer may lead to
1205 * us reading unmapped memory.
1207 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1211 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1213 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1214 ib
.ptr
= (void *)min_index_ptr
;
1216 for (i
= 0; i
< primcount
; i
++) {
1217 prim
[i
].begin
= (i
== 0);
1218 prim
[i
].end
= (i
== primcount
- 1);
1221 prim
[i
].mode
= mode
;
1222 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1223 prim
[i
].count
= count
[i
];
1224 prim
[i
].indexed
= 1;
1225 prim
[i
].num_instances
= 1;
1226 prim
[i
].base_instance
= 0;
1227 prim
[i
].draw_id
= i
;
1228 prim
[i
].is_indirect
= 0;
1229 if (basevertex
!= NULL
)
1230 prim
[i
].basevertex
= basevertex
[i
];
1232 prim
[i
].basevertex
= 0;
1235 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1236 false, ~0, ~0, NULL
, 0, NULL
);
1238 /* render one prim at a time */
1239 for (i
= 0; i
< primcount
; i
++) {
1242 ib
.count
= count
[i
];
1244 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1245 ib
.ptr
= indices
[i
];
1251 prim
[0].mode
= mode
;
1253 prim
[0].count
= count
[i
];
1254 prim
[0].indexed
= 1;
1255 prim
[0].num_instances
= 1;
1256 prim
[0].base_instance
= 0;
1257 prim
[0].draw_id
= i
;
1258 prim
[0].is_indirect
= 0;
1259 if (basevertex
!= NULL
)
1260 prim
[0].basevertex
= basevertex
[i
];
1262 prim
[0].basevertex
= 0;
1264 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
1265 false, ~0, ~0, NULL
, 0, NULL
);
1271 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1277 static void GLAPIENTRY
1278 vbo_exec_MultiDrawElements(GLenum mode
,
1279 const GLsizei
*count
, GLenum type
,
1280 const GLvoid
* const *indices
,
1283 GET_CURRENT_CONTEXT(ctx
);
1285 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1289 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1294 static void GLAPIENTRY
1295 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1296 const GLsizei
*count
, GLenum type
,
1297 const GLvoid
* const *indices
,
1299 const GLsizei
*basevertex
)
1301 GET_CURRENT_CONTEXT(ctx
);
1303 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1307 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1312 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1313 struct gl_transform_feedback_object
*obj
,
1314 GLuint stream
, GLuint numInstances
)
1316 struct vbo_context
*vbo
= vbo_context(ctx
);
1317 struct _mesa_prim prim
[2];
1319 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1324 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1325 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1326 !_mesa_all_varyings_in_vbos(ctx
->Array
.VAO
))) {
1327 GLsizei n
= ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1328 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0);
1332 if (!vbo_bind_arrays(ctx
))
1335 /* init most fields to zero */
1336 memset(prim
, 0, sizeof(prim
));
1339 prim
[0].mode
= mode
;
1340 prim
[0].num_instances
= numInstances
;
1341 prim
[0].base_instance
= 0;
1342 prim
[0].is_indirect
= 0;
1344 /* Maybe we should do some primitive splitting for primitive restart
1345 * (like in DrawArrays), but we have no way to know how many vertices
1346 * will be rendered. */
1348 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
1349 GL_FALSE
, ~0, ~0, obj
, stream
, NULL
);
1351 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1357 * Like DrawArrays, but take the count from a transform feedback object.
1358 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1359 * \param name the transform feedback object
1360 * User still has to setup of the vertex attribute info with
1361 * glVertexPointer, glColorPointer, etc.
1362 * Part of GL_ARB_transform_feedback2.
1364 static void GLAPIENTRY
1365 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1367 GET_CURRENT_CONTEXT(ctx
);
1368 struct gl_transform_feedback_object
*obj
=
1369 _mesa_lookup_transform_feedback_object(ctx
, name
);
1371 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1372 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1373 _mesa_enum_to_string(mode
), name
);
1375 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1378 static void GLAPIENTRY
1379 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1381 GET_CURRENT_CONTEXT(ctx
);
1382 struct gl_transform_feedback_object
*obj
=
1383 _mesa_lookup_transform_feedback_object(ctx
, name
);
1385 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1386 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1387 _mesa_enum_to_string(mode
), name
, stream
);
1389 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1392 static void GLAPIENTRY
1393 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1396 GET_CURRENT_CONTEXT(ctx
);
1397 struct gl_transform_feedback_object
*obj
=
1398 _mesa_lookup_transform_feedback_object(ctx
, name
);
1400 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1401 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1402 _mesa_enum_to_string(mode
), name
);
1404 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1407 static void GLAPIENTRY
1408 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1409 GLuint stream
, GLsizei primcount
)
1411 GET_CURRENT_CONTEXT(ctx
);
1412 struct gl_transform_feedback_object
*obj
=
1413 _mesa_lookup_transform_feedback_object(ctx
, name
);
1415 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1416 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1417 "(%s, %u, %u, %i)\n",
1418 _mesa_enum_to_string(mode
), name
, stream
, primcount
);
1420 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1424 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1425 GLenum mode
, const GLvoid
*indirect
)
1427 struct vbo_context
*vbo
= vbo_context(ctx
);
1429 if (!vbo_bind_arrays(ctx
))
1432 vbo
->draw_indirect_prims(ctx
, mode
,
1433 ctx
->DrawIndirectBuffer
, (GLsizeiptr
)indirect
,
1434 1 /* draw_count */, 16 /* stride */,
1437 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1442 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1444 const GLvoid
*indirect
,
1445 GLsizei primcount
, GLsizei stride
)
1447 struct vbo_context
*vbo
= vbo_context(ctx
);
1448 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1453 if (!vbo_bind_arrays(ctx
))
1456 vbo
->draw_indirect_prims(ctx
, mode
,
1457 ctx
->DrawIndirectBuffer
, offset
,
1461 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1466 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1467 GLenum mode
, GLenum type
,
1468 const GLvoid
*indirect
)
1470 struct vbo_context
*vbo
= vbo_context(ctx
);
1471 struct _mesa_index_buffer ib
;
1473 if (!vbo_bind_arrays(ctx
))
1476 ib
.count
= 0; /* unknown */
1478 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1481 vbo
->draw_indirect_prims(ctx
, mode
,
1482 ctx
->DrawIndirectBuffer
, (GLsizeiptr
)indirect
,
1483 1 /* draw_count */, 20 /* stride */,
1487 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1492 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1493 GLenum mode
, GLenum type
,
1494 const GLvoid
*indirect
,
1495 GLsizei primcount
, GLsizei stride
)
1497 struct vbo_context
*vbo
= vbo_context(ctx
);
1498 struct _mesa_index_buffer ib
;
1499 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1504 if (!vbo_bind_arrays(ctx
))
1507 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1509 ib
.count
= 0; /* unknown */
1511 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1514 vbo
->draw_indirect_prims(ctx
, mode
,
1515 ctx
->DrawIndirectBuffer
, offset
,
1520 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1525 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1528 static void GLAPIENTRY
1529 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1531 GET_CURRENT_CONTEXT(ctx
);
1533 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1534 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1535 _mesa_enum_to_string(mode
), indirect
);
1537 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1540 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1543 static void GLAPIENTRY
1544 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
,
1545 const GLvoid
*indirect
)
1547 GET_CURRENT_CONTEXT(ctx
);
1549 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1550 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1551 _mesa_enum_to_string(mode
),
1552 _mesa_enum_to_string(type
), indirect
);
1554 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1557 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1560 static void GLAPIENTRY
1561 vbo_exec_MultiDrawArraysIndirect(GLenum mode
,
1562 const GLvoid
*indirect
,
1563 GLsizei primcount
, GLsizei stride
)
1565 GET_CURRENT_CONTEXT(ctx
);
1567 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1568 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1569 _mesa_enum_to_string(mode
), indirect
, primcount
, stride
);
1571 /* If <stride> is zero, the array elements are treated as tightly packed. */
1573 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1575 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
,
1580 vbo_validated_multidrawarraysindirect(ctx
, mode
,
1585 static void GLAPIENTRY
1586 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1587 const GLvoid
*indirect
,
1588 GLsizei primcount
, GLsizei stride
)
1590 GET_CURRENT_CONTEXT(ctx
);
1592 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1593 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1594 _mesa_enum_to_string(mode
),
1595 _mesa_enum_to_string(type
), indirect
, primcount
, stride
);
1597 /* If <stride> is zero, the array elements are treated as tightly packed. */
1599 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1601 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
,
1606 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
,
1612 vbo_validated_multidrawarraysindirectcount(struct gl_context
*ctx
,
1616 GLsizei maxdrawcount
,
1619 struct vbo_context
*vbo
= vbo_context(ctx
);
1620 GLsizeiptr offset
= indirect
;
1622 if (maxdrawcount
== 0)
1625 if (!vbo_bind_arrays(ctx
))
1628 vbo
->draw_indirect_prims(ctx
, mode
,
1629 ctx
->DrawIndirectBuffer
, offset
,
1630 maxdrawcount
, stride
,
1631 ctx
->ParameterBuffer
, drawcount
,
1634 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1639 vbo_validated_multidrawelementsindirectcount(struct gl_context
*ctx
,
1640 GLenum mode
, GLenum type
,
1643 GLsizei maxdrawcount
,
1646 struct vbo_context
*vbo
= vbo_context(ctx
);
1647 struct _mesa_index_buffer ib
;
1648 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1650 if (maxdrawcount
== 0)
1653 if (!vbo_bind_arrays(ctx
))
1656 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1658 ib
.count
= 0; /* unknown */
1660 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1663 vbo
->draw_indirect_prims(ctx
, mode
,
1664 ctx
->DrawIndirectBuffer
, offset
,
1665 maxdrawcount
, stride
,
1666 ctx
->ParameterBuffer
, drawcount
,
1669 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1673 static void GLAPIENTRY
1674 vbo_exec_MultiDrawArraysIndirectCount(GLenum mode
,
1677 GLsizei maxdrawcount
, GLsizei stride
)
1679 GET_CURRENT_CONTEXT(ctx
);
1681 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1682 _mesa_debug(ctx
, "glMultiDrawArraysIndirectCountARB"
1683 "(%s, %lx, %lx, %i, %i)\n",
1684 _mesa_enum_to_string(mode
), indirect
,
1685 drawcount
, maxdrawcount
, stride
);
1687 /* If <stride> is zero, the array elements are treated as tightly packed. */
1689 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1691 if (!_mesa_validate_MultiDrawArraysIndirectCount(ctx
, mode
,
1692 indirect
, drawcount
,
1693 maxdrawcount
, stride
))
1696 vbo_validated_multidrawarraysindirectcount(ctx
, mode
,
1697 indirect
, drawcount
,
1698 maxdrawcount
, stride
);
1701 static void GLAPIENTRY
1702 vbo_exec_MultiDrawElementsIndirectCount(GLenum mode
, GLenum type
,
1705 GLsizei maxdrawcount
, GLsizei stride
)
1707 GET_CURRENT_CONTEXT(ctx
);
1709 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1710 _mesa_debug(ctx
, "glMultiDrawElementsIndirectCountARB"
1711 "(%s, %s, %lx, %lx, %i, %i)\n",
1712 _mesa_enum_to_string(mode
),
1713 _mesa_enum_to_string(type
), indirect
,
1714 drawcount
, maxdrawcount
, stride
);
1716 /* If <stride> is zero, the array elements are treated as tightly packed. */
1718 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1720 if (!_mesa_validate_MultiDrawElementsIndirectCount(ctx
, mode
, type
,
1721 indirect
, drawcount
,
1722 maxdrawcount
, stride
))
1725 vbo_validated_multidrawelementsindirectcount(ctx
, mode
, type
,
1726 indirect
, drawcount
,
1727 maxdrawcount
, stride
);
1732 * Initialize the dispatch table with the VBO functions for drawing.
1735 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1736 struct _glapi_table
*exec
)
1738 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1739 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1741 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1742 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1745 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1747 if (ctx
->API
== API_OPENGL_COMPAT
) {
1748 SET_Rectf(exec
, vbo_exec_Rectf
);
1749 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1750 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1753 if (ctx
->API
!= API_OPENGLES
&&
1754 ctx
->Extensions
.ARB_draw_elements_base_vertex
) {
1755 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1756 SET_MultiDrawElementsBaseVertex(exec
, vbo_exec_MultiDrawElementsBaseVertex
);
1758 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1759 SET_DrawRangeElementsBaseVertex(exec
, vbo_exec_DrawRangeElementsBaseVertex
);
1760 SET_DrawElementsInstancedBaseVertex(exec
, vbo_exec_DrawElementsInstancedBaseVertex
);
1764 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1765 SET_DrawArraysInstancedBaseInstance(exec
, vbo_exec_DrawArraysInstancedBaseInstance
);
1766 SET_DrawElementsInstancedBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseInstance
);
1767 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1770 if (ctx
->API
== API_OPENGL_CORE
|| _mesa_is_gles31(ctx
)) {
1771 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1772 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1775 if (ctx
->API
== API_OPENGL_CORE
) {
1776 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1777 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1778 SET_MultiDrawArraysIndirectCountARB(exec
, vbo_exec_MultiDrawArraysIndirectCount
);
1779 SET_MultiDrawElementsIndirectCountARB(exec
, vbo_exec_MultiDrawElementsIndirectCount
);
1782 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1783 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1784 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1787 if (_mesa_is_desktop_gl(ctx
)) {
1788 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1789 SET_DrawTransformFeedbackStream(exec
, vbo_exec_DrawTransformFeedbackStream
);
1790 SET_DrawTransformFeedbackInstanced(exec
, vbo_exec_DrawTransformFeedbackInstanced
);
1791 SET_DrawTransformFeedbackStreamInstanced(exec
, vbo_exec_DrawTransformFeedbackStreamInstanced
);
1798 * The following functions are only used for OpenGL ES 1/2 support.
1799 * And some aren't even supported (yet) in ES 1/2.
1804 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1806 vbo_exec_DrawArrays(mode
, first
, count
);
1810 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
1813 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
1817 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1818 const GLvoid
*indices
)
1820 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1825 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1826 const GLvoid
*indices
, GLint basevertex
)
1828 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1833 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1834 GLenum type
, const GLvoid
*indices
)
1836 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1841 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1842 GLsizei count
, GLenum type
,
1843 const GLvoid
*indices
, GLint basevertex
)
1845 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1846 indices
, basevertex
);
1851 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1852 const GLvoid
**indices
, GLsizei primcount
)
1854 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1859 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1860 const GLsizei
*count
, GLenum type
,
1861 const GLvoid
**indices
, GLsizei primcount
,
1862 const GLint
*basevertex
)
1864 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1865 primcount
, basevertex
);
1869 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1871 vbo_exec_DrawTransformFeedback(mode
, name
);