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 **************************************************************************/
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/api_validate.h"
33 #include "main/dispatch.h"
34 #include "main/varray.h"
35 #include "main/bufferobj.h"
36 #include "main/enums.h"
37 #include "main/macros.h"
38 #include "main/transformfeedback.h"
39 #include "main/sse_minmax.h"
40 #include "x86/common_x86_asm.h"
42 #include "vbo_context.h"
46 * All vertex buffers should be in an unmapped state when we're about
47 * to draw. This debug function checks that.
50 check_buffers_are_unmapped(const struct gl_client_array
**inputs
)
55 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
57 struct gl_buffer_object
*obj
= inputs
[i
]->BufferObj
;
58 assert(!_mesa_check_disallowed_mapping(obj
));
67 * A debug function that may be called from other parts of Mesa as
68 * needed during debugging.
71 vbo_check_buffers_are_unmapped(struct gl_context
*ctx
)
73 struct vbo_context
*vbo
= vbo_context(ctx
);
74 struct vbo_exec_context
*exec
= &vbo
->exec
;
75 /* check the current vertex arrays */
76 check_buffers_are_unmapped(exec
->array
.inputs
);
77 /* check the current glBegin/glVertex/glEnd-style VBO */
78 assert(!_mesa_check_disallowed_mapping(exec
->vtx
.bufferobj
));
84 * Compute min and max elements by scanning the index buffer for
85 * glDraw[Range]Elements() calls.
86 * If primitive restart is enabled, we need to ignore restart
87 * indexes when computing min/max.
90 vbo_get_minmax_index(struct gl_context
*ctx
,
91 const struct _mesa_prim
*prim
,
92 const struct _mesa_index_buffer
*ib
,
93 GLuint
*min_index
, GLuint
*max_index
,
96 const GLboolean restart
= ctx
->Array
._PrimitiveRestart
;
97 const GLuint restartIndex
= _mesa_primitive_restart_index(ctx
, ib
->type
);
98 const int index_size
= vbo_sizeof_ib_type(ib
->type
);
102 indices
= (char *) ib
->ptr
+ prim
->start
* index_size
;
103 if (_mesa_is_bufferobj(ib
->obj
)) {
104 GLsizeiptr size
= MIN2(count
* index_size
, ib
->obj
->Size
);
105 indices
= ctx
->Driver
.MapBufferRange(ctx
, (GLintptr
) indices
, size
,
106 GL_MAP_READ_BIT
, ib
->obj
,
111 case GL_UNSIGNED_INT
: {
112 const GLuint
*ui_indices
= (const GLuint
*)indices
;
116 for (i
= 0; i
< count
; i
++) {
117 if (ui_indices
[i
] != restartIndex
) {
118 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
119 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
124 #if defined(USE_SSE41)
125 if (cpu_has_sse4_1
) {
126 _mesa_uint_array_min_max(ui_indices
, &min_ui
, &max_ui
, count
);
130 for (i
= 0; i
< count
; i
++) {
131 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
132 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
139 case GL_UNSIGNED_SHORT
: {
140 const GLushort
*us_indices
= (const GLushort
*)indices
;
144 for (i
= 0; i
< count
; i
++) {
145 if (us_indices
[i
] != restartIndex
) {
146 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
147 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
152 for (i
= 0; i
< count
; i
++) {
153 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
154 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
161 case GL_UNSIGNED_BYTE
: {
162 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
166 for (i
= 0; i
< count
; i
++) {
167 if (ub_indices
[i
] != restartIndex
) {
168 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
169 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
174 for (i
= 0; i
< count
; i
++) {
175 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
176 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
184 unreachable("not reached");
187 if (_mesa_is_bufferobj(ib
->obj
)) {
188 ctx
->Driver
.UnmapBuffer(ctx
, ib
->obj
, MAP_INTERNAL
);
193 * Compute min and max elements for nr_prims
196 vbo_get_minmax_indices(struct gl_context
*ctx
,
197 const struct _mesa_prim
*prims
,
198 const struct _mesa_index_buffer
*ib
,
203 GLuint tmp_min
, tmp_max
;
210 for (i
= 0; i
< nr_prims
; i
++) {
211 const struct _mesa_prim
*start_prim
;
213 start_prim
= &prims
[i
];
214 count
= start_prim
->count
;
215 /* Do combination if possible to reduce map/unmap count */
216 while ((i
+ 1 < nr_prims
) &&
217 (prims
[i
].start
+ prims
[i
].count
== prims
[i
+1].start
)) {
218 count
+= prims
[i
+1].count
;
221 vbo_get_minmax_index(ctx
, start_prim
, ib
, &tmp_min
, &tmp_max
, count
);
222 *min_index
= MIN2(*min_index
, tmp_min
);
223 *max_index
= MAX2(*max_index
, tmp_max
);
229 * Check that element 'j' of the array has reasonable data.
231 * For debugging purposes; not normally used.
234 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
235 GLuint attrib
, GLuint j
)
237 if (array
->Enabled
) {
238 const void *data
= array
->Ptr
;
239 if (_mesa_is_bufferobj(array
->BufferObj
)) {
240 if (!array
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
) {
241 /* need to map now */
242 array
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
=
243 ctx
->Driver
.MapBufferRange(ctx
, 0, array
->BufferObj
->Size
,
244 GL_MAP_READ_BIT
, array
->BufferObj
,
247 data
= ADD_POINTERS(data
,
248 array
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
);
250 switch (array
->Type
) {
253 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
255 for (k
= 0; k
< array
->Size
; k
++) {
256 if (IS_INF_OR_NAN(f
[k
]) ||
257 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
258 printf("Bad array data:\n");
259 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
260 printf(" Array %u at %p\n", attrib
, (void* ) array
);
261 printf(" Type 0x%x, Size %d, Stride %d\n",
262 array
->Type
, array
->Size
, array
->Stride
);
263 printf(" Address/offset %p in Buffer Object %u\n",
264 array
->Ptr
, array
->BufferObj
->Name
);
265 f
[k
] = 1.0; /* XXX replace the bad value! */
267 /*assert(!IS_INF_OR_NAN(f[k]));*/
279 * Unmap the buffer object referenced by given array, if mapped.
282 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
284 if (array
->Enabled
&&
285 _mesa_is_bufferobj(array
->BufferObj
) &&
286 _mesa_bufferobj_mapped(array
->BufferObj
, MAP_INTERNAL
)) {
287 ctx
->Driver
.UnmapBuffer(ctx
, array
->BufferObj
, MAP_INTERNAL
);
293 * Examine the array's data for NaNs, etc.
294 * For debug purposes; not normally used.
297 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
298 const void *elements
, GLint basevertex
)
300 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
305 if (_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
)) {
306 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
307 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
309 ctx
->Array
.VAO
->IndexBufferObj
,
311 elements
= ADD_POINTERS(elements
, elemMap
);
314 for (i
= 0; i
< count
; i
++) {
319 case GL_UNSIGNED_BYTE
:
320 j
= ((const GLubyte
*) elements
)[i
];
322 case GL_UNSIGNED_SHORT
:
323 j
= ((const GLushort
*) elements
)[i
];
325 case GL_UNSIGNED_INT
:
326 j
= ((const GLuint
*) elements
)[i
];
332 /* check element j of each enabled array */
333 for (k
= 0; k
< ARRAY_SIZE(vao
->_VertexAttrib
); k
++) {
334 check_array_data(ctx
, &vao
->_VertexAttrib
[k
], k
, j
);
338 if (_mesa_is_bufferobj(vao
->IndexBufferObj
)) {
339 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
343 for (k
= 0; k
< ARRAY_SIZE(vao
->_VertexAttrib
); k
++) {
344 unmap_array_buffer(ctx
, &vao
->_VertexAttrib
[k
]);
350 * Check array data, looking for NaNs, etc.
353 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
360 * Print info/data for glDrawArrays(), for debugging.
363 print_draw_arrays(struct gl_context
*ctx
,
364 GLenum mode
, GLint start
, GLsizei count
)
366 struct vbo_context
*vbo
= vbo_context(ctx
);
367 struct vbo_exec_context
*exec
= &vbo
->exec
;
368 struct gl_vertex_array_object
*vao
= ctx
->Array
.VAO
;
371 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
374 for (i
= 0; i
< 32; i
++) {
375 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
376 GLuint bufName
= bufObj
->Name
;
377 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
378 printf("attr %2d: size %d stride %d enabled %d "
379 "ptr %p Bufobj %u\n",
381 exec
->array
.inputs
[i
]->Size
,
383 /*exec->array.inputs[i]->Enabled,*/
384 vao
->_VertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
,
385 exec
->array
.inputs
[i
]->Ptr
,
389 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
390 GL_MAP_READ_BIT
, bufObj
,
392 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
393 float *f
= (float *) (p
+ offset
);
396 int n
= (count
* stride
) / 4;
399 printf(" Data at offset %d:\n", offset
);
400 for (i
= 0; i
< n
; i
++) {
401 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
403 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
, MAP_INTERNAL
);
410 * Set the vbo->exec->inputs[] pointers to point to the enabled
411 * vertex arrays. This depends on the current vertex program/shader
412 * being executed because of whether or not generic vertex arrays
413 * alias the conventional vertex arrays.
414 * For arrays that aren't enabled, we set the input[attrib] pointer
415 * to point at a zero-stride current value "array".
418 recalculate_input_bindings(struct gl_context
*ctx
)
420 struct vbo_context
*vbo
= vbo_context(ctx
);
421 struct vbo_exec_context
*exec
= &vbo
->exec
;
422 struct gl_client_array
*vertexAttrib
= ctx
->Array
.VAO
->_VertexAttrib
;
423 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
424 GLbitfield64 const_inputs
= 0x0;
427 switch (get_program_mode(ctx
)) {
429 /* When no vertex program is active (or the vertex program is generated
430 * from fixed-function state). We put the material values into the
431 * generic slots. This is the only situation where material values
432 * are available as per-vertex attributes.
434 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
435 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
436 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
438 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
439 const_inputs
|= VERT_BIT(i
);
443 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
444 inputs
[VERT_ATTRIB_GENERIC(i
)] =
445 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+i
];
446 const_inputs
|= VERT_BIT_GENERIC(i
);
449 /* Could use just about anything, just to fill in the empty
452 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
453 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
454 const_inputs
|= VERT_BIT_GENERIC(i
);
459 /* There are no shaders in OpenGL ES 1.x, so this code path should be
460 * impossible to reach. The meta code is careful to not use shaders in
463 assert(ctx
->API
!= API_OPENGLES
);
465 /* In the compatibility profile of desktop OpenGL, the generic[0]
466 * attribute array aliases and overrides the legacy position array.
467 * Otherwise, legacy attributes available in the legacy slots,
468 * generic attributes in the generic slots and materials are not
469 * available as per-vertex attributes.
471 * In all other APIs, only the generic attributes exist, and none of the
472 * slots are considered "magic."
474 if (ctx
->API
== API_OPENGL_COMPAT
) {
475 if (vertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
)
476 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
477 else if (vertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
478 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
480 inputs
[0] = &vbo
->currval
[VBO_ATTRIB_POS
];
481 const_inputs
|= VERT_BIT_POS
;
484 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
485 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
486 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
488 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
489 const_inputs
|= VERT_BIT_FF(i
);
493 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
494 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
495 inputs
[VERT_ATTRIB_GENERIC(i
)] =
496 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
498 inputs
[VERT_ATTRIB_GENERIC(i
)] =
499 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
500 const_inputs
|= VERT_BIT_GENERIC(i
);
504 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
506 /* Other parts of the code assume that inputs[0] through
507 * inputs[VERT_ATTRIB_FF_MAX] will be non-NULL. However, in OpenGL
508 * ES 2.0+ or OpenGL core profile, none of these arrays should ever
511 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
512 assert(!vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
);
514 inputs
[i
] = &vbo
->currval
[VBO_ATTRIB_POS
+i
];
515 const_inputs
|= VERT_BIT_FF(i
);
518 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
519 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
520 inputs
[VERT_ATTRIB_GENERIC(i
)] =
521 &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
523 inputs
[VERT_ATTRIB_GENERIC(i
)] =
524 &vbo
->currval
[VBO_ATTRIB_GENERIC0
+i
];
525 const_inputs
|= VERT_BIT_GENERIC(i
);
533 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
534 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
539 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
540 * These will point to the arrays to actually use for drawing. Some will
541 * be user-provided arrays, other will be zero-stride const-valued arrays.
542 * Note that this might set the _NEW_VARYING_VP_INPUTS dirty flag so state
543 * validation must be done after this call.
546 vbo_bind_arrays(struct gl_context
*ctx
)
548 struct vbo_context
*vbo
= vbo_context(ctx
);
549 struct vbo_exec_context
*exec
= &vbo
->exec
;
551 vbo_draw_method(vbo
, DRAW_ARRAYS
);
553 if (exec
->array
.recalculate_inputs
) {
554 recalculate_input_bindings(ctx
);
555 exec
->array
.recalculate_inputs
= GL_FALSE
;
557 /* Again... because we may have changed the bitmask of per-vertex varying
558 * attributes. If we regenerate the fixed-function vertex program now
559 * we may be able to prune down the number of vertex attributes which we
560 * need in the shader.
563 /* Setting "validating" to TRUE prevents _mesa_update_state from
564 * invalidating what we just did.
566 exec
->validating
= GL_TRUE
;
567 _mesa_update_state(ctx
);
568 exec
->validating
= GL_FALSE
;
574 * Helper function called by the other DrawArrays() functions below.
575 * This is where we handle primitive restart for drawing non-indexed
576 * arrays. If primitive restart is enabled, it typically means
577 * splitting one DrawArrays() into two.
580 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
581 GLsizei count
, GLuint numInstances
, GLuint baseInstance
)
583 struct vbo_context
*vbo
= vbo_context(ctx
);
584 struct vbo_exec_context
*exec
= &vbo
->exec
;
585 struct _mesa_prim prim
[2];
587 vbo_bind_arrays(ctx
);
589 /* init most fields to zero */
590 memset(prim
, 0, sizeof(prim
));
594 prim
[0].num_instances
= numInstances
;
595 prim
[0].base_instance
= baseInstance
;
596 prim
[0].is_indirect
= 0;
598 /* Implement the primitive restart index */
599 if (ctx
->Array
.PrimitiveRestart
&& !ctx
->Array
.PrimitiveRestartFixedIndex
&&
600 ctx
->Array
.RestartIndex
< count
) {
601 GLuint primCount
= 0;
603 if (ctx
->Array
.RestartIndex
== start
) {
604 /* special case: RestartIndex at beginning */
606 prim
[0].start
= start
+ 1;
607 prim
[0].count
= count
- 1;
611 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
612 /* special case: RestartIndex at end */
614 prim
[0].start
= start
;
615 prim
[0].count
= count
- 1;
620 /* general case: RestartIndex in middle, split into two prims */
621 prim
[0].start
= start
;
622 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
625 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
626 prim
[1].count
= count
- prim
[1].start
;
632 /* draw one or two prims */
633 check_buffers_are_unmapped(exec
->array
.inputs
);
634 vbo
->draw_prims(ctx
, prim
, primCount
, NULL
,
635 GL_TRUE
, start
, start
+ count
- 1, NULL
, NULL
);
639 /* no prim restart */
640 prim
[0].start
= start
;
641 prim
[0].count
= count
;
643 check_buffers_are_unmapped(exec
->array
.inputs
);
644 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
645 GL_TRUE
, start
, start
+ count
- 1,
649 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
656 * Execute a glRectf() function.
658 static void GLAPIENTRY
659 vbo_exec_Rectf(GLfloat x1
, GLfloat y1
, GLfloat x2
, GLfloat y2
)
661 GET_CURRENT_CONTEXT(ctx
);
662 ASSERT_OUTSIDE_BEGIN_END(ctx
);
664 CALL_Begin(GET_DISPATCH(), (GL_QUADS
));
665 CALL_Vertex2f(GET_DISPATCH(), (x1
, y1
));
666 CALL_Vertex2f(GET_DISPATCH(), (x2
, y1
));
667 CALL_Vertex2f(GET_DISPATCH(), (x2
, y2
));
668 CALL_Vertex2f(GET_DISPATCH(), (x1
, y2
));
669 CALL_End(GET_DISPATCH(), ());
673 static void GLAPIENTRY
674 vbo_exec_EvalMesh1(GLenum mode
, GLint i1
, GLint i2
)
676 GET_CURRENT_CONTEXT(ctx
);
686 prim
= GL_LINE_STRIP
;
689 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh1(mode)" );
693 /* No effect if vertex maps disabled.
695 if (!ctx
->Eval
.Map1Vertex4
&&
696 !ctx
->Eval
.Map1Vertex3
)
699 du
= ctx
->Eval
.MapGrid1du
;
700 u
= ctx
->Eval
.MapGrid1u1
+ i1
* du
;
702 CALL_Begin(GET_DISPATCH(), (prim
));
703 for (i
=i1
;i
<=i2
;i
++,u
+=du
) {
704 CALL_EvalCoord1f(GET_DISPATCH(), (u
));
706 CALL_End(GET_DISPATCH(), ());
710 static void GLAPIENTRY
711 vbo_exec_EvalMesh2(GLenum mode
, GLint i1
, GLint i2
, GLint j1
, GLint j2
)
713 GET_CURRENT_CONTEXT(ctx
);
714 GLfloat u
, du
, v
, dv
, v1
, u1
;
723 _mesa_error( ctx
, GL_INVALID_ENUM
, "glEvalMesh2(mode)" );
727 /* No effect if vertex maps disabled.
729 if (!ctx
->Eval
.Map2Vertex4
&&
730 !ctx
->Eval
.Map2Vertex3
)
733 du
= ctx
->Eval
.MapGrid2du
;
734 dv
= ctx
->Eval
.MapGrid2dv
;
735 v1
= ctx
->Eval
.MapGrid2v1
+ j1
* dv
;
736 u1
= ctx
->Eval
.MapGrid2u1
+ i1
* du
;
740 CALL_Begin(GET_DISPATCH(), (GL_POINTS
));
741 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
742 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
743 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
746 CALL_End(GET_DISPATCH(), ());
749 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
750 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
751 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
752 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
754 CALL_End(GET_DISPATCH(), ());
756 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
757 CALL_Begin(GET_DISPATCH(), (GL_LINE_STRIP
));
758 for (v
=v1
,j
=j1
;j
<=j2
;j
++,v
+=dv
) {
759 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
761 CALL_End(GET_DISPATCH(), ());
765 for (v
=v1
,j
=j1
;j
<j2
;j
++,v
+=dv
) {
766 CALL_Begin(GET_DISPATCH(), (GL_TRIANGLE_STRIP
));
767 for (u
=u1
,i
=i1
;i
<=i2
;i
++,u
+=du
) {
768 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
));
769 CALL_EvalCoord2f(GET_DISPATCH(), (u
, v
+dv
));
771 CALL_End(GET_DISPATCH(), ());
779 * Called from glDrawArrays when in immediate mode (not display list mode).
781 static void GLAPIENTRY
782 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
784 GET_CURRENT_CONTEXT(ctx
);
786 if (MESA_VERBOSE
& VERBOSE_DRAW
)
787 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
788 _mesa_lookup_enum_by_nr(mode
), start
, count
);
790 if (!_mesa_validate_DrawArrays(ctx
, mode
, count
))
794 check_draw_arrays_data(ctx
, start
, count
);
796 vbo_draw_arrays(ctx
, mode
, start
, count
, 1, 0);
799 print_draw_arrays(ctx
, mode
, start
, count
);
804 * Called from glDrawArraysInstanced when in immediate mode (not
805 * display list mode).
807 static void GLAPIENTRY
808 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
809 GLsizei numInstances
)
811 GET_CURRENT_CONTEXT(ctx
);
813 if (MESA_VERBOSE
& VERBOSE_DRAW
)
814 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
815 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
817 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
821 check_draw_arrays_data(ctx
, start
, count
);
823 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
, 0);
826 print_draw_arrays(ctx
, mode
, start
, count
);
831 * Called from glDrawArraysInstancedBaseInstance when in immediate mode.
833 static void GLAPIENTRY
834 vbo_exec_DrawArraysInstancedBaseInstance(GLenum mode
, GLint first
, GLsizei count
,
835 GLsizei numInstances
, GLuint baseInstance
)
837 GET_CURRENT_CONTEXT(ctx
);
839 if (MESA_VERBOSE
& VERBOSE_DRAW
)
840 _mesa_debug(ctx
, "glDrawArraysInstancedBaseInstance(%s, %d, %d, %d, %d)\n",
841 _mesa_lookup_enum_by_nr(mode
), first
, count
,
842 numInstances
, baseInstance
);
844 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, first
, count
,
849 check_draw_arrays_data(ctx
, first
, count
);
851 vbo_draw_arrays(ctx
, mode
, first
, count
, numInstances
, baseInstance
);
854 print_draw_arrays(ctx
, mode
, first
, count
);
860 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
865 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
868 ctx
->Driver
.MapBufferRange(ctx
, 0,
869 ctx
->Array
.VAO
->IndexBufferObj
->Size
,
871 ctx
->Array
.VAO
->IndexBufferObj
,
874 case GL_UNSIGNED_BYTE
:
876 const GLubyte
*us
= (const GLubyte
*) map
;
878 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
; i
++) {
879 printf("%02x ", us
[i
]);
886 case GL_UNSIGNED_SHORT
:
888 const GLushort
*us
= (const GLushort
*) map
;
890 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 2; i
++) {
891 printf("%04x ", us
[i
]);
898 case GL_UNSIGNED_INT
:
900 const GLuint
*us
= (const GLuint
*) map
;
902 for (i
= 0; i
< ctx
->Array
.VAO
->IndexBufferObj
->Size
/ 4; i
++) {
903 printf("%08x ", us
[i
]);
914 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.VAO
->IndexBufferObj
,
921 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
922 * Do the rendering for a glDrawElements or glDrawRangeElements call after
923 * we've validated buffer bounds, etc.
926 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
927 GLboolean index_bounds_valid
,
928 GLuint start
, GLuint end
,
929 GLsizei count
, GLenum type
,
930 const GLvoid
*indices
,
931 GLint basevertex
, GLuint numInstances
,
934 struct vbo_context
*vbo
= vbo_context(ctx
);
935 struct vbo_exec_context
*exec
= &vbo
->exec
;
936 struct _mesa_index_buffer ib
;
937 struct _mesa_prim prim
[1];
939 vbo_bind_arrays(ctx
);
943 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
952 prim
[0].count
= count
;
954 prim
[0].is_indirect
= 0;
955 prim
[0].basevertex
= basevertex
;
956 prim
[0].num_instances
= numInstances
;
957 prim
[0].base_instance
= baseInstance
;
959 /* Need to give special consideration to rendering a range of
960 * indices starting somewhere above zero. Typically the
961 * application is issuing multiple DrawRangeElements() to draw
962 * successive primitives layed out linearly in the vertex arrays.
963 * Unless the vertex arrays are all in a VBO (or locked as with
964 * CVA), the OpenGL semantics imply that we need to re-read or
965 * re-upload the vertex data on each draw call.
967 * In the case of hardware tnl, we want to avoid starting the
968 * upload at zero, as it will mean every draw call uploads an
969 * increasing amount of not-used vertex data. Worse - in the
970 * software tnl module, all those vertices might be transformed and
971 * lit but never rendered.
973 * If we just upload or transform the vertices in start..end,
974 * however, the indices will be incorrect.
976 * At this level, we don't know exactly what the requirements of
977 * the backend are going to be, though it will likely boil down to
980 * 1) Do nothing, everything is in a VBO and is processed once
983 * 2) Adjust the indices and vertex arrays so that start becomes
986 * Rather than doing anything here, I'll provide a helper function
987 * for the latter case elsewhere.
990 check_buffers_are_unmapped(exec
->array
.inputs
);
991 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
992 index_bounds_valid
, start
, end
, NULL
, NULL
);
994 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1001 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
1003 static void GLAPIENTRY
1004 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
1005 GLuint start
, GLuint end
,
1006 GLsizei count
, GLenum type
,
1007 const GLvoid
*indices
,
1010 static GLuint warnCount
= 0;
1011 GLboolean index_bounds_valid
= GL_TRUE
;
1013 /* This is only useful to catch invalid values in the "end" parameter
1016 GLuint max_element
= 2 * 1000 * 1000 * 1000; /* just a big number */
1018 GET_CURRENT_CONTEXT(ctx
);
1020 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1022 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
1023 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
1024 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1026 if (!_mesa_validate_DrawRangeElements(ctx
, mode
, start
, end
, count
,
1030 if ((int) end
+ basevertex
< 0 ||
1031 start
+ basevertex
>= max_element
) {
1032 /* The application requested we draw using a range of indices that's
1033 * outside the bounds of the current VBO. This is invalid and appears
1034 * to give undefined results. The safest thing to do is to simply
1035 * ignore the range, in case the application botched their range tracking
1036 * but did provide valid indices. Also issue a warning indicating that
1037 * the application is broken.
1039 if (warnCount
++ < 10) {
1040 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
1041 "basevertex %d, count %d, type 0x%x, indices=%p):\n"
1042 "\trange is outside VBO bounds (max=%u); ignoring.\n"
1043 "\tThis should be fixed in the application.",
1044 start
, end
, basevertex
, count
, type
, indices
,
1047 index_bounds_valid
= GL_FALSE
;
1050 /* NOTE: It's important that 'end' is a reasonable value.
1051 * in _tnl_draw_prims(), we use end to determine how many vertices
1052 * to transform. If it's too large, we can unnecessarily split prims
1053 * or we can read/write out of memory in several different places!
1056 /* Catch/fix some potential user errors */
1057 if (type
== GL_UNSIGNED_BYTE
) {
1058 start
= MIN2(start
, 0xff);
1059 end
= MIN2(end
, 0xff);
1061 else if (type
== GL_UNSIGNED_SHORT
) {
1062 start
= MIN2(start
, 0xffff);
1063 end
= MIN2(end
, 0xffff);
1067 printf("glDraw[Range]Elements{,BaseVertex}"
1068 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
1070 start
, end
, type
, count
,
1071 ctx
->Array
.VAO
->IndexBufferObj
->Name
,
1075 if ((int) start
+ basevertex
< 0 ||
1076 end
+ basevertex
>= max_element
)
1077 index_bounds_valid
= GL_FALSE
;
1080 check_draw_elements_data(ctx
, count
, type
, indices
);
1082 (void) check_draw_elements_data
;
1085 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
1086 count
, type
, indices
, basevertex
, 1, 0);
1091 * Called by glDrawRangeElements() in immediate mode.
1093 static void GLAPIENTRY
1094 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
1095 GLsizei count
, GLenum type
, const GLvoid
*indices
)
1097 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
1098 GET_CURRENT_CONTEXT(ctx
);
1100 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
1101 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
1102 _mesa_lookup_enum_by_nr(type
), indices
);
1105 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1111 * Called by glDrawElements() in immediate mode.
1113 static void GLAPIENTRY
1114 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1115 const GLvoid
*indices
)
1117 GET_CURRENT_CONTEXT(ctx
);
1119 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1120 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1121 _mesa_lookup_enum_by_nr(mode
), count
,
1122 _mesa_lookup_enum_by_nr(type
), indices
);
1124 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1127 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1128 count
, type
, indices
, 0, 1, 0);
1133 * Called by glDrawElementsBaseVertex() in immediate mode.
1135 static void GLAPIENTRY
1136 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1137 const GLvoid
*indices
, GLint basevertex
)
1139 GET_CURRENT_CONTEXT(ctx
);
1141 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1142 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1143 _mesa_lookup_enum_by_nr(mode
), count
,
1144 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1146 if (!_mesa_validate_DrawElements(ctx
, mode
, count
, type
, indices
))
1149 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1150 count
, type
, indices
, basevertex
, 1, 0);
1155 * Called by glDrawElementsInstanced() in immediate mode.
1157 static void GLAPIENTRY
1158 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1159 const GLvoid
*indices
, GLsizei numInstances
)
1161 GET_CURRENT_CONTEXT(ctx
);
1163 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1164 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1165 _mesa_lookup_enum_by_nr(mode
), count
,
1166 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1168 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1172 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1173 count
, type
, indices
, 0, numInstances
, 0);
1178 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1180 static void GLAPIENTRY
1181 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1182 const GLvoid
*indices
, GLsizei numInstances
,
1185 GET_CURRENT_CONTEXT(ctx
);
1187 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1188 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1189 _mesa_lookup_enum_by_nr(mode
), count
,
1190 _mesa_lookup_enum_by_nr(type
), indices
,
1191 numInstances
, basevertex
);
1193 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1197 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1198 count
, type
, indices
, basevertex
, numInstances
, 0);
1203 * Called by glDrawElementsInstancedBaseInstance() in immediate mode.
1205 static void GLAPIENTRY
1206 vbo_exec_DrawElementsInstancedBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1207 const GLvoid
*indices
, GLsizei numInstances
,
1208 GLuint baseInstance
)
1210 GET_CURRENT_CONTEXT(ctx
);
1212 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1213 _mesa_debug(ctx
, "glDrawElementsInstancedBaseInstance(%s, %d, %s, %p, %d, %d)\n",
1214 _mesa_lookup_enum_by_nr(mode
), count
,
1215 _mesa_lookup_enum_by_nr(type
), indices
,
1216 numInstances
, baseInstance
);
1218 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1222 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1223 count
, type
, indices
, 0, numInstances
,
1229 * Called by glDrawElementsInstancedBaseVertexBaseInstance() in immediate mode.
1231 static void GLAPIENTRY
1232 vbo_exec_DrawElementsInstancedBaseVertexBaseInstance(GLenum mode
, GLsizei count
, GLenum type
,
1233 const GLvoid
*indices
, GLsizei numInstances
,
1234 GLint basevertex
, GLuint baseInstance
)
1236 GET_CURRENT_CONTEXT(ctx
);
1238 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1239 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertexBaseInstance(%s, %d, %s, %p, %d, %d, %d)\n",
1240 _mesa_lookup_enum_by_nr(mode
), count
,
1241 _mesa_lookup_enum_by_nr(type
), indices
,
1242 numInstances
, basevertex
, baseInstance
);
1244 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1248 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1249 count
, type
, indices
, basevertex
, numInstances
,
1255 * Inner support for both _mesa_MultiDrawElements() and
1256 * _mesa_MultiDrawRangeElements().
1257 * This does the actual rendering after we've checked array indexes, etc.
1260 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1261 const GLsizei
*count
, GLenum type
,
1262 const GLvoid
* const *indices
,
1264 const GLint
*basevertex
)
1266 struct vbo_context
*vbo
= vbo_context(ctx
);
1267 struct vbo_exec_context
*exec
= &vbo
->exec
;
1268 struct _mesa_index_buffer ib
;
1269 struct _mesa_prim
*prim
;
1270 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1271 uintptr_t min_index_ptr
, max_index_ptr
;
1272 GLboolean fallback
= GL_FALSE
;
1278 prim
= calloc(primcount
, sizeof(*prim
));
1280 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1284 vbo_bind_arrays(ctx
);
1286 min_index_ptr
= (uintptr_t)indices
[0];
1288 for (i
= 0; i
< primcount
; i
++) {
1289 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1290 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1291 index_type_size
* count
[i
]);
1294 /* Check if we can handle this thing as a bunch of index offsets from the
1295 * same index pointer. If we can't, then we have to fall back to doing
1296 * a draw_prims per primitive.
1297 * Check that the difference between each prim's indexes is a multiple of
1298 * the index/element size.
1300 if (index_type_size
!= 1) {
1301 for (i
= 0; i
< primcount
; i
++) {
1302 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1309 /* Draw primitives individually if one count is zero, so we can easily skip
1312 for (i
= 0; i
< primcount
; i
++) {
1313 if (count
[i
] == 0) {
1319 /* If the index buffer isn't in a VBO, then treating the application's
1320 * subranges of the index buffer as one large index buffer may lead to
1321 * us reading unmapped memory.
1323 if (!_mesa_is_bufferobj(ctx
->Array
.VAO
->IndexBufferObj
))
1327 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1329 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1330 ib
.ptr
= (void *)min_index_ptr
;
1332 for (i
= 0; i
< primcount
; i
++) {
1333 prim
[i
].begin
= (i
== 0);
1334 prim
[i
].end
= (i
== primcount
- 1);
1337 prim
[i
].mode
= mode
;
1338 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1339 prim
[i
].count
= count
[i
];
1340 prim
[i
].indexed
= 1;
1341 prim
[i
].num_instances
= 1;
1342 prim
[i
].base_instance
= 0;
1343 prim
[i
].is_indirect
= 0;
1344 if (basevertex
!= NULL
)
1345 prim
[i
].basevertex
= basevertex
[i
];
1347 prim
[i
].basevertex
= 0;
1350 check_buffers_are_unmapped(exec
->array
.inputs
);
1351 vbo
->draw_prims(ctx
, prim
, primcount
, &ib
,
1352 false, ~0, ~0, NULL
, NULL
);
1354 /* render one prim at a time */
1355 for (i
= 0; i
< primcount
; i
++) {
1358 ib
.count
= count
[i
];
1360 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1361 ib
.ptr
= indices
[i
];
1367 prim
[0].mode
= mode
;
1369 prim
[0].count
= count
[i
];
1370 prim
[0].indexed
= 1;
1371 prim
[0].num_instances
= 1;
1372 prim
[0].base_instance
= 0;
1373 prim
[0].is_indirect
= 0;
1374 if (basevertex
!= NULL
)
1375 prim
[0].basevertex
= basevertex
[i
];
1377 prim
[0].basevertex
= 0;
1379 check_buffers_are_unmapped(exec
->array
.inputs
);
1380 vbo
->draw_prims(ctx
, prim
, 1, &ib
,
1381 false, ~0, ~0, NULL
, NULL
);
1387 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1393 static void GLAPIENTRY
1394 vbo_exec_MultiDrawElements(GLenum mode
,
1395 const GLsizei
*count
, GLenum type
,
1396 const GLvoid
* const *indices
,
1399 GET_CURRENT_CONTEXT(ctx
);
1401 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1405 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1410 static void GLAPIENTRY
1411 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1412 const GLsizei
*count
, GLenum type
,
1413 const GLvoid
* const *indices
,
1415 const GLsizei
*basevertex
)
1417 GET_CURRENT_CONTEXT(ctx
);
1419 if (!_mesa_validate_MultiDrawElements(ctx
, mode
, count
, type
, indices
,
1423 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1428 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1429 struct gl_transform_feedback_object
*obj
,
1430 GLuint stream
, GLuint numInstances
)
1432 struct vbo_context
*vbo
= vbo_context(ctx
);
1433 struct vbo_exec_context
*exec
= &vbo
->exec
;
1434 struct _mesa_prim prim
[2];
1436 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
, stream
,
1441 if (ctx
->Driver
.GetTransformFeedbackVertexCount
&&
1442 (ctx
->Const
.AlwaysUseGetTransformFeedbackVertexCount
||
1443 !vbo_all_varyings_in_vbos(exec
->array
.inputs
))) {
1444 GLsizei n
= ctx
->Driver
.GetTransformFeedbackVertexCount(ctx
, obj
, stream
);
1445 vbo_draw_arrays(ctx
, mode
, 0, n
, numInstances
, 0);
1449 vbo_bind_arrays(ctx
);
1451 /* init most fields to zero */
1452 memset(prim
, 0, sizeof(prim
));
1455 prim
[0].mode
= mode
;
1456 prim
[0].num_instances
= numInstances
;
1457 prim
[0].base_instance
= 0;
1458 prim
[0].is_indirect
= 0;
1460 /* Maybe we should do some primitive splitting for primitive restart
1461 * (like in DrawArrays), but we have no way to know how many vertices
1462 * will be rendered. */
1464 check_buffers_are_unmapped(exec
->array
.inputs
);
1465 vbo
->draw_prims(ctx
, prim
, 1, NULL
,
1466 GL_TRUE
, 0, 0, obj
, NULL
);
1468 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
1474 * Like DrawArrays, but take the count from a transform feedback object.
1475 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1476 * \param name the transform feedback object
1477 * User still has to setup of the vertex attribute info with
1478 * glVertexPointer, glColorPointer, etc.
1479 * Part of GL_ARB_transform_feedback2.
1481 static void GLAPIENTRY
1482 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1484 GET_CURRENT_CONTEXT(ctx
);
1485 struct gl_transform_feedback_object
*obj
=
1486 _mesa_lookup_transform_feedback_object(ctx
, name
);
1488 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1489 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1490 _mesa_lookup_enum_by_nr(mode
), name
);
1492 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, 1);
1495 static void GLAPIENTRY
1496 vbo_exec_DrawTransformFeedbackStream(GLenum mode
, GLuint name
, GLuint stream
)
1498 GET_CURRENT_CONTEXT(ctx
);
1499 struct gl_transform_feedback_object
*obj
=
1500 _mesa_lookup_transform_feedback_object(ctx
, name
);
1502 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1503 _mesa_debug(ctx
, "glDrawTransformFeedbackStream(%s, %u, %u)\n",
1504 _mesa_lookup_enum_by_nr(mode
), name
, stream
);
1506 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, 1);
1509 static void GLAPIENTRY
1510 vbo_exec_DrawTransformFeedbackInstanced(GLenum mode
, GLuint name
,
1513 GET_CURRENT_CONTEXT(ctx
);
1514 struct gl_transform_feedback_object
*obj
=
1515 _mesa_lookup_transform_feedback_object(ctx
, name
);
1517 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1518 _mesa_debug(ctx
, "glDrawTransformFeedbackInstanced(%s, %d)\n",
1519 _mesa_lookup_enum_by_nr(mode
), name
);
1521 vbo_draw_transform_feedback(ctx
, mode
, obj
, 0, primcount
);
1524 static void GLAPIENTRY
1525 vbo_exec_DrawTransformFeedbackStreamInstanced(GLenum mode
, GLuint name
,
1526 GLuint stream
, GLsizei primcount
)
1528 GET_CURRENT_CONTEXT(ctx
);
1529 struct gl_transform_feedback_object
*obj
=
1530 _mesa_lookup_transform_feedback_object(ctx
, name
);
1532 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1533 _mesa_debug(ctx
, "glDrawTransformFeedbackStreamInstanced"
1534 "(%s, %u, %u, %i)\n",
1535 _mesa_lookup_enum_by_nr(mode
), name
, stream
, primcount
);
1537 vbo_draw_transform_feedback(ctx
, mode
, obj
, stream
, primcount
);
1541 vbo_validated_drawarraysindirect(struct gl_context
*ctx
,
1542 GLenum mode
, const GLvoid
*indirect
)
1544 struct vbo_context
*vbo
= vbo_context(ctx
);
1545 struct vbo_exec_context
*exec
= &vbo
->exec
;
1546 struct _mesa_prim prim
[1];
1548 vbo_bind_arrays(ctx
);
1550 memset(prim
, 0, sizeof(prim
));
1553 prim
[0].mode
= mode
;
1554 prim
[0].is_indirect
= 1;
1555 prim
[0].indirect_offset
= (GLsizeiptr
)indirect
;
1557 /* NOTE: We do NOT want to handle primitive restart here, nor perform any
1558 * other checks that require knowledge of the values in the command buffer.
1559 * That would defeat the whole purpose of this function.
1562 check_buffers_are_unmapped(exec
->array
.inputs
);
1563 vbo
->draw_prims(ctx
, prim
, 1,
1564 NULL
, GL_TRUE
, 0, ~0,
1566 ctx
->DrawIndirectBuffer
);
1568 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1573 vbo_validated_multidrawarraysindirect(struct gl_context
*ctx
,
1575 const GLvoid
*indirect
,
1576 GLsizei primcount
, GLsizei stride
)
1578 struct vbo_context
*vbo
= vbo_context(ctx
);
1579 struct vbo_exec_context
*exec
= &vbo
->exec
;
1580 struct _mesa_prim
*prim
;
1582 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1586 prim
= calloc(primcount
, sizeof(*prim
));
1588 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawArraysIndirect");
1592 vbo_bind_arrays(ctx
);
1595 prim
[primcount
- 1].end
= 1;
1596 for (i
= 0; i
< primcount
; ++i
, offset
+= stride
) {
1597 prim
[i
].mode
= mode
;
1598 prim
[i
].indirect_offset
= offset
;
1599 prim
[i
].is_indirect
= 1;
1602 check_buffers_are_unmapped(exec
->array
.inputs
);
1603 vbo
->draw_prims(ctx
, prim
, primcount
,
1604 NULL
, GL_TRUE
, 0, ~0,
1606 ctx
->DrawIndirectBuffer
);
1610 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1615 vbo_validated_drawelementsindirect(struct gl_context
*ctx
,
1616 GLenum mode
, GLenum type
,
1617 const GLvoid
*indirect
)
1619 struct vbo_context
*vbo
= vbo_context(ctx
);
1620 struct vbo_exec_context
*exec
= &vbo
->exec
;
1621 struct _mesa_index_buffer ib
;
1622 struct _mesa_prim prim
[1];
1624 vbo_bind_arrays(ctx
);
1626 ib
.count
= 0; /* unknown */
1628 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1631 memset(prim
, 0, sizeof(prim
));
1634 prim
[0].mode
= mode
;
1635 prim
[0].indexed
= 1;
1636 prim
[0].indirect_offset
= (GLsizeiptr
)indirect
;
1637 prim
[0].is_indirect
= 1;
1639 check_buffers_are_unmapped(exec
->array
.inputs
);
1640 vbo
->draw_prims(ctx
, prim
, 1,
1641 &ib
, GL_TRUE
, 0, ~0,
1643 ctx
->DrawIndirectBuffer
);
1645 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1650 vbo_validated_multidrawelementsindirect(struct gl_context
*ctx
,
1651 GLenum mode
, GLenum type
,
1652 const GLvoid
*indirect
,
1653 GLsizei primcount
, GLsizei stride
)
1655 struct vbo_context
*vbo
= vbo_context(ctx
);
1656 struct vbo_exec_context
*exec
= &vbo
->exec
;
1657 struct _mesa_index_buffer ib
;
1658 struct _mesa_prim
*prim
;
1660 GLsizeiptr offset
= (GLsizeiptr
)indirect
;
1664 prim
= calloc(primcount
, sizeof(*prim
));
1666 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElementsIndirect");
1670 vbo_bind_arrays(ctx
);
1672 /* NOTE: IndexBufferObj is guaranteed to be a VBO. */
1674 ib
.count
= 0; /* unknown */
1676 ib
.obj
= ctx
->Array
.VAO
->IndexBufferObj
;
1680 prim
[primcount
- 1].end
= 1;
1681 for (i
= 0; i
< primcount
; ++i
, offset
+= stride
) {
1682 prim
[i
].mode
= mode
;
1683 prim
[i
].indexed
= 1;
1684 prim
[i
].indirect_offset
= offset
;
1685 prim
[i
].is_indirect
= 1;
1688 check_buffers_are_unmapped(exec
->array
.inputs
);
1689 vbo
->draw_prims(ctx
, prim
, primcount
,
1690 &ib
, GL_TRUE
, 0, ~0,
1692 ctx
->DrawIndirectBuffer
);
1696 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
)
1701 * Like [Multi]DrawArrays/Elements, but they take most arguments from
1704 static void GLAPIENTRY
1705 vbo_exec_DrawArraysIndirect(GLenum mode
, const GLvoid
*indirect
)
1707 GET_CURRENT_CONTEXT(ctx
);
1709 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1710 _mesa_debug(ctx
, "glDrawArraysIndirect(%s, %p)\n",
1711 _mesa_lookup_enum_by_nr(mode
), indirect
);
1713 if (!_mesa_validate_DrawArraysIndirect(ctx
, mode
, indirect
))
1716 vbo_validated_drawarraysindirect(ctx
, mode
, indirect
);
1719 static void GLAPIENTRY
1720 vbo_exec_DrawElementsIndirect(GLenum mode
, GLenum type
,
1721 const GLvoid
*indirect
)
1723 GET_CURRENT_CONTEXT(ctx
);
1725 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1726 _mesa_debug(ctx
, "glDrawElementsIndirect(%s, %s, %p)\n",
1727 _mesa_lookup_enum_by_nr(mode
),
1728 _mesa_lookup_enum_by_nr(type
), indirect
);
1730 if (!_mesa_validate_DrawElementsIndirect(ctx
, mode
, type
, indirect
))
1733 vbo_validated_drawelementsindirect(ctx
, mode
, type
, indirect
);
1736 static void GLAPIENTRY
1737 vbo_exec_MultiDrawArraysIndirect(GLenum mode
,
1738 const GLvoid
*indirect
,
1739 GLsizei primcount
, GLsizei stride
)
1741 GET_CURRENT_CONTEXT(ctx
);
1743 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1744 _mesa_debug(ctx
, "glMultiDrawArraysIndirect(%s, %p, %i, %i)\n",
1745 _mesa_lookup_enum_by_nr(mode
), indirect
, primcount
, stride
);
1747 /* If <stride> is zero, the array elements are treated as tightly packed. */
1749 stride
= 4 * sizeof(GLuint
); /* sizeof(DrawArraysIndirectCommand) */
1751 if (!_mesa_validate_MultiDrawArraysIndirect(ctx
, mode
,
1756 vbo_validated_multidrawarraysindirect(ctx
, mode
,
1761 static void GLAPIENTRY
1762 vbo_exec_MultiDrawElementsIndirect(GLenum mode
, GLenum type
,
1763 const GLvoid
*indirect
,
1764 GLsizei primcount
, GLsizei stride
)
1766 GET_CURRENT_CONTEXT(ctx
);
1768 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1769 _mesa_debug(ctx
, "glMultiDrawElementsIndirect(%s, %s, %p, %i, %i)\n",
1770 _mesa_lookup_enum_by_nr(mode
),
1771 _mesa_lookup_enum_by_nr(type
), indirect
, primcount
, stride
);
1773 /* If <stride> is zero, the array elements are treated as tightly packed. */
1775 stride
= 5 * sizeof(GLuint
); /* sizeof(DrawElementsIndirectCommand) */
1777 if (!_mesa_validate_MultiDrawElementsIndirect(ctx
, mode
, type
,
1782 vbo_validated_multidrawelementsindirect(ctx
, mode
, type
,
1788 * Initialize the dispatch table with the VBO functions for drawing.
1791 vbo_initialize_exec_dispatch(const struct gl_context
*ctx
,
1792 struct _glapi_table
*exec
)
1794 SET_DrawArrays(exec
, vbo_exec_DrawArrays
);
1795 SET_DrawElements(exec
, vbo_exec_DrawElements
);
1797 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1798 SET_DrawRangeElements(exec
, vbo_exec_DrawRangeElements
);
1801 SET_MultiDrawElementsEXT(exec
, vbo_exec_MultiDrawElements
);
1803 if (ctx
->API
== API_OPENGL_COMPAT
) {
1804 SET_Rectf(exec
, vbo_exec_Rectf
);
1805 SET_EvalMesh1(exec
, vbo_exec_EvalMesh1
);
1806 SET_EvalMesh2(exec
, vbo_exec_EvalMesh2
);
1809 if (_mesa_is_desktop_gl(ctx
)) {
1810 SET_DrawElementsBaseVertex(exec
, vbo_exec_DrawElementsBaseVertex
);
1811 SET_DrawRangeElementsBaseVertex(exec
, vbo_exec_DrawRangeElementsBaseVertex
);
1812 SET_MultiDrawElementsBaseVertex(exec
, vbo_exec_MultiDrawElementsBaseVertex
);
1813 SET_DrawArraysInstancedBaseInstance(exec
, vbo_exec_DrawArraysInstancedBaseInstance
);
1814 SET_DrawElementsInstancedBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseInstance
);
1815 SET_DrawElementsInstancedBaseVertex(exec
, vbo_exec_DrawElementsInstancedBaseVertex
);
1816 SET_DrawElementsInstancedBaseVertexBaseInstance(exec
, vbo_exec_DrawElementsInstancedBaseVertexBaseInstance
);
1819 if (ctx
->API
== API_OPENGL_CORE
) {
1820 SET_DrawArraysIndirect(exec
, vbo_exec_DrawArraysIndirect
);
1821 SET_DrawElementsIndirect(exec
, vbo_exec_DrawElementsIndirect
);
1822 SET_MultiDrawArraysIndirect(exec
, vbo_exec_MultiDrawArraysIndirect
);
1823 SET_MultiDrawElementsIndirect(exec
, vbo_exec_MultiDrawElementsIndirect
);
1826 if (_mesa_is_desktop_gl(ctx
) || _mesa_is_gles3(ctx
)) {
1827 SET_DrawArraysInstancedARB(exec
, vbo_exec_DrawArraysInstanced
);
1828 SET_DrawElementsInstancedARB(exec
, vbo_exec_DrawElementsInstanced
);
1831 if (_mesa_is_desktop_gl(ctx
)) {
1832 SET_DrawTransformFeedback(exec
, vbo_exec_DrawTransformFeedback
);
1833 SET_DrawTransformFeedbackStream(exec
, vbo_exec_DrawTransformFeedbackStream
);
1834 SET_DrawTransformFeedbackInstanced(exec
, vbo_exec_DrawTransformFeedbackInstanced
);
1835 SET_DrawTransformFeedbackStreamInstanced(exec
, vbo_exec_DrawTransformFeedbackStreamInstanced
);
1842 * The following functions are only used for OpenGL ES 1/2 support.
1843 * And some aren't even supported (yet) in ES 1/2.
1848 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1850 vbo_exec_DrawArrays(mode
, first
, count
);
1854 _mesa_DrawArraysInstanced(GLenum mode
, GLint first
, GLsizei count
,
1857 vbo_exec_DrawArraysInstanced(mode
, first
, count
, primcount
);
1861 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1862 const GLvoid
*indices
)
1864 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1869 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1870 const GLvoid
*indices
, GLint basevertex
)
1872 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1877 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1878 GLenum type
, const GLvoid
*indices
)
1880 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1885 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1886 GLsizei count
, GLenum type
,
1887 const GLvoid
*indices
, GLint basevertex
)
1889 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1890 indices
, basevertex
);
1895 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1896 const GLvoid
**indices
, GLsizei primcount
)
1898 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1903 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1904 const GLsizei
*count
, GLenum type
,
1905 const GLvoid
**indices
, GLsizei primcount
,
1906 const GLint
*basevertex
)
1908 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1909 primcount
, basevertex
);
1913 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1915 vbo_exec_DrawTransformFeedback(mode
, name
);