1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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/varray.h"
34 #include "main/bufferobj.h"
35 #include "main/enums.h"
36 #include "main/macros.h"
37 #include "main/transformfeedback.h"
39 #include "vbo_context.h"
43 * All vertex buffers should be in an unmapped state when we're about
44 * to draw. This debug function checks that.
47 check_buffers_are_unmapped(const struct gl_client_array
**inputs
)
52 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
54 struct gl_buffer_object
*obj
= inputs
[i
]->BufferObj
;
55 assert(!_mesa_bufferobj_mapped(obj
));
64 * A debug function that may be called from other parts of Mesa as
65 * needed during debugging.
68 vbo_check_buffers_are_unmapped(struct gl_context
*ctx
)
70 struct vbo_context
*vbo
= vbo_context(ctx
);
71 struct vbo_exec_context
*exec
= &vbo
->exec
;
72 /* check the current vertex arrays */
73 check_buffers_are_unmapped(exec
->array
.inputs
);
74 /* check the current glBegin/glVertex/glEnd-style VBO */
75 assert(!_mesa_bufferobj_mapped(exec
->vtx
.bufferobj
));
79 vbo_sizeof_ib_type(GLenum type
)
83 return sizeof(GLuint
);
84 case GL_UNSIGNED_SHORT
:
85 return sizeof(GLushort
);
86 case GL_UNSIGNED_BYTE
:
87 return sizeof(GLubyte
);
89 assert(!"unsupported index data type");
90 /* In case assert is turned off */
97 * Compute min and max elements by scanning the index buffer for
98 * glDraw[Range]Elements() calls.
99 * If primitive restart is enabled, we need to ignore restart
100 * indexes when computing min/max.
103 vbo_get_minmax_index(struct gl_context
*ctx
,
104 const struct _mesa_prim
*prim
,
105 const struct _mesa_index_buffer
*ib
,
106 GLuint
*min_index
, GLuint
*max_index
,
109 const GLboolean restart
= ctx
->Array
.PrimitiveRestart
;
110 const GLuint restartIndex
= ctx
->Array
.RestartIndex
;
114 indices
= (void *)ib
->ptr
+ prim
->start
* vbo_sizeof_ib_type(ib
->type
);
115 if (_mesa_is_bufferobj(ib
->obj
)) {
116 GLsizeiptr size
= MIN2(count
* vbo_sizeof_ib_type(ib
->type
), ib
->obj
->Size
);
117 indices
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) indices
, size
,
118 GL_MAP_READ_BIT
, ib
->obj
);
122 case GL_UNSIGNED_INT
: {
123 const GLuint
*ui_indices
= (const GLuint
*)indices
;
127 for (i
= 0; i
< count
; i
++) {
128 if (ui_indices
[i
] != restartIndex
) {
129 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
130 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
135 for (i
= 0; i
< count
; i
++) {
136 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
137 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
144 case GL_UNSIGNED_SHORT
: {
145 const GLushort
*us_indices
= (const GLushort
*)indices
;
149 for (i
= 0; i
< count
; i
++) {
150 if (us_indices
[i
] != restartIndex
) {
151 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
152 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
157 for (i
= 0; i
< count
; i
++) {
158 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
159 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
166 case GL_UNSIGNED_BYTE
: {
167 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
171 for (i
= 0; i
< count
; i
++) {
172 if (ub_indices
[i
] != restartIndex
) {
173 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
174 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
179 for (i
= 0; i
< count
; i
++) {
180 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
181 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
193 if (_mesa_is_bufferobj(ib
->obj
)) {
194 ctx
->Driver
.UnmapBuffer(ctx
, ib
->obj
);
199 * Compute min and max elements for nr_prims
202 vbo_get_minmax_indices(struct gl_context
*ctx
,
203 const struct _mesa_prim
*prims
,
204 const struct _mesa_index_buffer
*ib
,
209 GLuint tmp_min
, tmp_max
;
216 for (i
= 0; i
< nr_prims
; i
++) {
217 const struct _mesa_prim
*start_prim
;
219 start_prim
= &prims
[i
];
220 count
= start_prim
->count
;
221 /* Do combination if possible to reduce map/unmap count */
222 while ((i
+ 1 < nr_prims
) &&
223 (prims
[i
].start
+ prims
[i
].count
== prims
[i
+1].start
)) {
224 count
+= prims
[i
+1].count
;
227 vbo_get_minmax_index(ctx
, start_prim
, ib
, &tmp_min
, &tmp_max
, count
);
228 *min_index
= MIN2(*min_index
, tmp_min
);
229 *max_index
= MAX2(*max_index
, tmp_max
);
235 * Check that element 'j' of the array has reasonable data.
237 * For debugging purposes; not normally used.
240 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
241 GLuint attrib
, GLuint j
)
243 if (array
->Enabled
) {
244 const void *data
= array
->Ptr
;
245 if (_mesa_is_bufferobj(array
->BufferObj
)) {
246 if (!array
->BufferObj
->Pointer
) {
247 /* need to map now */
248 array
->BufferObj
->Pointer
=
249 ctx
->Driver
.MapBufferRange(ctx
, 0, array
->BufferObj
->Size
,
250 GL_MAP_READ_BIT
, array
->BufferObj
);
252 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
254 switch (array
->Type
) {
257 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
259 for (k
= 0; k
< array
->Size
; k
++) {
260 if (IS_INF_OR_NAN(f
[k
]) ||
261 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
262 printf("Bad array data:\n");
263 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
264 printf(" Array %u at %p\n", attrib
, (void* ) array
);
265 printf(" Type 0x%x, Size %d, Stride %d\n",
266 array
->Type
, array
->Size
, array
->Stride
);
267 printf(" Address/offset %p in Buffer Object %u\n",
268 array
->Ptr
, array
->BufferObj
->Name
);
269 f
[k
] = 1.0; /* XXX replace the bad value! */
271 /*assert(!IS_INF_OR_NAN(f[k]));*/
283 * Unmap the buffer object referenced by given array, if mapped.
286 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
288 if (array
->Enabled
&&
289 _mesa_is_bufferobj(array
->BufferObj
) &&
290 _mesa_bufferobj_mapped(array
->BufferObj
)) {
291 ctx
->Driver
.UnmapBuffer(ctx
, array
->BufferObj
);
297 * Examine the array's data for NaNs, etc.
298 * For debug purposes; not normally used.
301 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
302 const void *elements
, GLint basevertex
)
304 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
308 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
309 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
310 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
312 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
313 elements
= ADD_POINTERS(elements
, elemMap
);
316 for (i
= 0; i
< count
; i
++) {
321 case GL_UNSIGNED_BYTE
:
322 j
= ((const GLubyte
*) elements
)[i
];
324 case GL_UNSIGNED_SHORT
:
325 j
= ((const GLushort
*) elements
)[i
];
327 case GL_UNSIGNED_INT
:
328 j
= ((const GLuint
*) elements
)[i
];
334 /* check element j of each enabled array */
335 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
336 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
], k
, j
);
340 if (_mesa_is_bufferobj(arrayObj
->ElementArrayBufferObj
)) {
341 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
344 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
345 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
351 * Check array data, looking for NaNs, etc.
354 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
361 * Print info/data for glDrawArrays(), for debugging.
364 print_draw_arrays(struct gl_context
*ctx
,
365 GLenum mode
, GLint start
, GLsizei count
)
367 struct vbo_context
*vbo
= vbo_context(ctx
);
368 struct vbo_exec_context
*exec
= &vbo
->exec
;
369 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
372 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
375 for (i
= 0; i
< 32; i
++) {
376 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
377 GLuint bufName
= bufObj
->Name
;
378 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
379 printf("attr %2d: size %d stride %d enabled %d "
380 "ptr %p Bufobj %u\n",
382 exec
->array
.inputs
[i
]->Size
,
384 /*exec->array.inputs[i]->Enabled,*/
385 arrayObj
->VertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
,
386 exec
->array
.inputs
[i
]->Ptr
,
390 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
391 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
);
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
.ArrayObj
->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
->legacy_currval
[i
];
439 const_inputs
|= VERT_BIT(i
);
443 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
444 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->mat_currval
[i
];
445 const_inputs
|= VERT_BIT_GENERIC(i
);
448 /* Could use just about anything, just to fill in the empty
451 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
452 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
453 const_inputs
|= VERT_BIT_GENERIC(i
);
456 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
457 * because it already takes care of invalidating the state necessary
458 * to revalidate vertex arrays. Not marking the state as dirty also
459 * improves performance (quite significantly in some apps).
461 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
462 ctx
->NewState
|= _NEW_ARRAY
;
466 /* NV_vertex_program - attribute arrays alias and override
467 * conventional, legacy arrays. No materials, and the generic
470 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
471 if (i
< VERT_ATTRIB_GENERIC_MAX
472 && vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
473 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
474 else if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
475 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
477 inputs
[i
] = &vbo
->legacy_currval
[i
];
478 const_inputs
|= VERT_BIT_FF(i
);
482 /* Could use just about anything, just to fill in the empty
485 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
486 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
487 const_inputs
|= VERT_BIT_GENERIC(i
);
490 ctx
->NewState
|= _NEW_ARRAY
;
494 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
495 * attribute array aliases and overrides the legacy position array.
497 * Otherwise, legacy attributes available in the legacy slots,
498 * generic attributes in the generic slots and materials are not
499 * available as per-vertex attributes.
501 if (vertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
)
502 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
503 else if (vertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
504 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
506 inputs
[0] = &vbo
->legacy_currval
[0];
507 const_inputs
|= VERT_BIT_POS
;
510 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
511 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
512 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
514 inputs
[i
] = &vbo
->legacy_currval
[i
];
515 const_inputs
|= VERT_BIT_FF(i
);
519 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
520 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
521 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
523 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
524 const_inputs
|= VERT_BIT_GENERIC(i
);
528 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
529 ctx
->NewState
|= _NEW_ARRAY
;
533 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
538 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
539 * These will point to the arrays to actually use for drawing. Some will
540 * be user-provided arrays, other will be zero-stride const-valued arrays.
541 * Note that this might set the _NEW_ARRAY dirty flag so state validation
542 * must be done after this call.
545 vbo_bind_arrays(struct gl_context
*ctx
)
547 if (!ctx
->Array
.RebindArrays
) {
551 recalculate_input_bindings(ctx
);
552 ctx
->Array
.RebindArrays
= GL_FALSE
;
557 * Helper function called by the other DrawArrays() functions below.
558 * This is where we handle primitive restart for drawing non-indexed
559 * arrays. If primitive restart is enabled, it typically means
560 * splitting one DrawArrays() into two.
563 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
564 GLsizei count
, GLuint numInstances
)
566 struct vbo_context
*vbo
= vbo_context(ctx
);
567 struct vbo_exec_context
*exec
= &vbo
->exec
;
568 struct _mesa_prim prim
[2];
570 vbo_bind_arrays(ctx
);
572 vbo_draw_method(exec
, DRAW_ARRAYS
);
574 /* Again... because we may have changed the bitmask of per-vertex varying
575 * attributes. If we regenerate the fixed-function vertex program now
576 * we may be able to prune down the number of vertex attributes which we
577 * need in the shader.
580 _mesa_update_state(ctx
);
582 /* init most fields to zero */
583 memset(prim
, 0, sizeof(prim
));
587 prim
[0].num_instances
= numInstances
;
589 /* Implement the primitive restart index */
590 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
591 GLuint primCount
= 0;
593 if (ctx
->Array
.RestartIndex
== start
) {
594 /* special case: RestartIndex at beginning */
596 prim
[0].start
= start
+ 1;
597 prim
[0].count
= count
- 1;
601 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
602 /* special case: RestartIndex at end */
604 prim
[0].start
= start
;
605 prim
[0].count
= count
- 1;
610 /* general case: RestartIndex in middle, split into two prims */
611 prim
[0].start
= start
;
612 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
615 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
616 prim
[1].count
= count
- prim
[1].start
;
622 /* draw one or two prims */
623 check_buffers_are_unmapped(exec
->array
.inputs
);
624 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
625 GL_TRUE
, start
, start
+ count
- 1, NULL
);
629 /* no prim restart */
630 prim
[0].start
= start
;
631 prim
[0].count
= count
;
633 check_buffers_are_unmapped(exec
->array
.inputs
);
634 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
635 GL_TRUE
, start
, start
+ count
- 1,
643 * Called from glDrawArrays when in immediate mode (not display list mode).
645 static void GLAPIENTRY
646 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
648 GET_CURRENT_CONTEXT(ctx
);
650 if (MESA_VERBOSE
& VERBOSE_DRAW
)
651 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
652 _mesa_lookup_enum_by_nr(mode
), start
, count
);
654 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
657 FLUSH_CURRENT( ctx
, 0 );
659 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
664 check_draw_arrays_data(ctx
, start
, count
);
666 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
669 print_draw_arrays(ctx
, mode
, start
, count
);
674 * Called from glDrawArraysInstanced when in immediate mode (not
675 * display list mode).
677 static void GLAPIENTRY
678 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
679 GLsizei numInstances
)
681 GET_CURRENT_CONTEXT(ctx
);
683 if (MESA_VERBOSE
& VERBOSE_DRAW
)
684 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
685 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
687 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
690 FLUSH_CURRENT( ctx
, 0 );
692 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
697 check_draw_arrays_data(ctx
, start
, count
);
699 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
702 print_draw_arrays(ctx
, mode
, start
, count
);
707 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
711 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
714 ctx
->Driver
.MapBufferRange(ctx
, 0,
715 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
717 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
719 case GL_UNSIGNED_BYTE
:
721 const GLubyte
*us
= (const GLubyte
*) map
;
723 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
; i
++) {
724 printf("%02x ", us
[i
]);
731 case GL_UNSIGNED_SHORT
:
733 const GLushort
*us
= (const GLushort
*) map
;
735 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 2; i
++) {
736 printf("%04x ", us
[i
]);
743 case GL_UNSIGNED_INT
:
745 const GLuint
*us
= (const GLuint
*) map
;
747 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 4; i
++) {
748 printf("%08x ", us
[i
]);
759 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
764 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
765 * Do the rendering for a glDrawElements or glDrawRangeElements call after
766 * we've validated buffer bounds, etc.
769 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
770 GLboolean index_bounds_valid
,
771 GLuint start
, GLuint end
,
772 GLsizei count
, GLenum type
,
773 const GLvoid
*indices
,
774 GLint basevertex
, GLint numInstances
)
776 struct vbo_context
*vbo
= vbo_context(ctx
);
777 struct vbo_exec_context
*exec
= &vbo
->exec
;
778 struct _mesa_index_buffer ib
;
779 struct _mesa_prim prim
[1];
781 FLUSH_CURRENT( ctx
, 0 );
783 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
787 vbo_bind_arrays( ctx
);
789 vbo_draw_method(exec
, DRAW_ARRAYS
);
791 /* check for dirty state again */
793 _mesa_update_state( ctx
);
797 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
806 prim
[0].count
= count
;
808 prim
[0].basevertex
= basevertex
;
809 prim
[0].num_instances
= numInstances
;
811 /* Need to give special consideration to rendering a range of
812 * indices starting somewhere above zero. Typically the
813 * application is issuing multiple DrawRangeElements() to draw
814 * successive primitives layed out linearly in the vertex arrays.
815 * Unless the vertex arrays are all in a VBO (or locked as with
816 * CVA), the OpenGL semantics imply that we need to re-read or
817 * re-upload the vertex data on each draw call.
819 * In the case of hardware tnl, we want to avoid starting the
820 * upload at zero, as it will mean every draw call uploads an
821 * increasing amount of not-used vertex data. Worse - in the
822 * software tnl module, all those vertices might be transformed and
823 * lit but never rendered.
825 * If we just upload or transform the vertices in start..end,
826 * however, the indices will be incorrect.
828 * At this level, we don't know exactly what the requirements of
829 * the backend are going to be, though it will likely boil down to
832 * 1) Do nothing, everything is in a VBO and is processed once
835 * 2) Adjust the indices and vertex arrays so that start becomes
838 * Rather than doing anything here, I'll provide a helper function
839 * for the latter case elsewhere.
842 check_buffers_are_unmapped(exec
->array
.inputs
);
843 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
844 index_bounds_valid
, start
, end
, NULL
);
849 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
851 static void GLAPIENTRY
852 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
853 GLuint start
, GLuint end
,
854 GLsizei count
, GLenum type
,
855 const GLvoid
*indices
,
858 static GLuint warnCount
= 0;
859 GET_CURRENT_CONTEXT(ctx
);
861 if (MESA_VERBOSE
& VERBOSE_DRAW
)
863 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
864 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
865 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
867 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
868 type
, indices
, basevertex
))
871 /* NOTE: It's important that 'end' is a reasonable value.
872 * in _tnl_draw_prims(), we use end to determine how many vertices
873 * to transform. If it's too large, we can unnecessarily split prims
874 * or we can read/write out of memory in several different places!
877 /* Catch/fix some potential user errors */
878 if (type
== GL_UNSIGNED_BYTE
) {
879 start
= MIN2(start
, 0xff);
880 end
= MIN2(end
, 0xff);
882 else if (type
== GL_UNSIGNED_SHORT
) {
883 start
= MIN2(start
, 0xffff);
884 end
= MIN2(end
, 0xffff);
887 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
888 /* the max element is out of bounds of one or more enabled arrays */
891 if (warnCount
< 10) {
892 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
893 "type 0x%x, indices=%p)\n"
894 "\tend is out of bounds (max=%u) "
895 "Element Buffer %u (size %d)\n"
896 "\tThis should probably be fixed in the application.",
897 start
, end
, count
, type
, indices
,
898 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
899 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
900 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
904 dump_element_buffer(ctx
, type
);
907 _mesa_print_arrays(ctx
);
909 /* 'end' was out of bounds, but now let's check the actual array
910 * indexes to see if any of them are out of bounds.
913 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
914 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
915 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
916 if (warnCount
< 10) {
917 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
918 "count %d, type 0x%x, indices=%p)\n"
919 "\tindex=%u is out of bounds (max=%u) "
920 "Element Buffer %u (size %d)\n"
921 "\tSkipping the glDrawRangeElements() call",
922 start
, end
, count
, type
, indices
, max
,
923 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
924 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
925 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
928 /* XXX we could also find the min index and compare to 'start'
929 * to see if start is correct. But it's more likely to get the
934 /* Set 'end' to the max possible legal value */
935 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
936 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
944 printf("glDraw[Range]Elements{,BaseVertex}"
945 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
947 start
, end
, type
, count
,
948 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
953 check_draw_elements_data(ctx
, count
, type
, indices
);
955 (void) check_draw_elements_data
;
958 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
959 count
, type
, indices
, basevertex
, 1);
964 * Called by glDrawRangeElements() in immediate mode.
966 static void GLAPIENTRY
967 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
968 GLsizei count
, GLenum type
, const GLvoid
*indices
)
970 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
971 GET_CURRENT_CONTEXT(ctx
);
973 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
974 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
975 _mesa_lookup_enum_by_nr(type
), indices
);
978 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
984 * Called by glDrawElements() in immediate mode.
986 static void GLAPIENTRY
987 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
988 const GLvoid
*indices
)
990 GET_CURRENT_CONTEXT(ctx
);
992 if (MESA_VERBOSE
& VERBOSE_DRAW
)
993 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
994 _mesa_lookup_enum_by_nr(mode
), count
,
995 _mesa_lookup_enum_by_nr(type
), indices
);
997 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
1000 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1001 count
, type
, indices
, 0, 1);
1006 * Called by glDrawElementsBaseVertex() in immediate mode.
1008 static void GLAPIENTRY
1009 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1010 const GLvoid
*indices
, GLint basevertex
)
1012 GET_CURRENT_CONTEXT(ctx
);
1014 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1015 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1016 _mesa_lookup_enum_by_nr(mode
), count
,
1017 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1019 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1023 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1024 count
, type
, indices
, basevertex
, 1);
1029 * Called by glDrawElementsInstanced() in immediate mode.
1031 static void GLAPIENTRY
1032 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1033 const GLvoid
*indices
, GLsizei numInstances
)
1035 GET_CURRENT_CONTEXT(ctx
);
1037 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1038 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1039 _mesa_lookup_enum_by_nr(mode
), count
,
1040 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1042 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1046 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1047 count
, type
, indices
, 0, numInstances
);
1051 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1053 static void GLAPIENTRY
1054 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1055 const GLvoid
*indices
, GLsizei numInstances
,
1058 GET_CURRENT_CONTEXT(ctx
);
1060 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1061 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1062 _mesa_lookup_enum_by_nr(mode
), count
,
1063 _mesa_lookup_enum_by_nr(type
), indices
,
1064 numInstances
, basevertex
);
1066 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1067 numInstances
, basevertex
))
1070 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1071 count
, type
, indices
, basevertex
, numInstances
);
1076 * Inner support for both _mesa_MultiDrawElements() and
1077 * _mesa_MultiDrawRangeElements().
1078 * This does the actual rendering after we've checked array indexes, etc.
1081 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1082 const GLsizei
*count
, GLenum type
,
1083 const GLvoid
**indices
, GLsizei primcount
,
1084 const GLint
*basevertex
)
1086 struct vbo_context
*vbo
= vbo_context(ctx
);
1087 struct vbo_exec_context
*exec
= &vbo
->exec
;
1088 struct _mesa_index_buffer ib
;
1089 struct _mesa_prim
*prim
;
1090 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1091 uintptr_t min_index_ptr
, max_index_ptr
;
1092 GLboolean fallback
= GL_FALSE
;
1098 FLUSH_CURRENT( ctx
, 0 );
1100 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1104 prim
= calloc(1, primcount
* sizeof(*prim
));
1106 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1110 /* Decide if we can do this all as one set of primitives sharing the
1111 * same index buffer, or if we have to reset the index pointer per
1114 vbo_bind_arrays( ctx
);
1116 /* check for dirty state again */
1118 _mesa_update_state( ctx
);
1120 min_index_ptr
= (uintptr_t)indices
[0];
1122 for (i
= 0; i
< primcount
; i
++) {
1123 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1124 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1125 index_type_size
* count
[i
]);
1128 /* Check if we can handle this thing as a bunch of index offsets from the
1129 * same index pointer. If we can't, then we have to fall back to doing
1130 * a draw_prims per primitive.
1131 * Check that the difference between each prim's indexes is a multiple of
1132 * the index/element size.
1134 if (index_type_size
!= 1) {
1135 for (i
= 0; i
< primcount
; i
++) {
1136 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1143 /* If the index buffer isn't in a VBO, then treating the application's
1144 * subranges of the index buffer as one large index buffer may lead to
1145 * us reading unmapped memory.
1147 if (!_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
))
1151 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1153 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1154 ib
.ptr
= (void *)min_index_ptr
;
1156 for (i
= 0; i
< primcount
; i
++) {
1157 prim
[i
].begin
= (i
== 0);
1158 prim
[i
].end
= (i
== primcount
- 1);
1161 prim
[i
].mode
= mode
;
1162 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1163 prim
[i
].count
= count
[i
];
1164 prim
[i
].indexed
= 1;
1165 prim
[i
].num_instances
= 1;
1166 if (basevertex
!= NULL
)
1167 prim
[i
].basevertex
= basevertex
[i
];
1169 prim
[i
].basevertex
= 0;
1172 check_buffers_are_unmapped(exec
->array
.inputs
);
1173 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1174 GL_FALSE
, ~0, ~0, NULL
);
1176 /* render one prim at a time */
1177 for (i
= 0; i
< primcount
; i
++) {
1178 ib
.count
= count
[i
];
1180 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1181 ib
.ptr
= indices
[i
];
1187 prim
[0].mode
= mode
;
1189 prim
[0].count
= count
[i
];
1190 prim
[0].indexed
= 1;
1191 prim
[0].num_instances
= 1;
1192 if (basevertex
!= NULL
)
1193 prim
[0].basevertex
= basevertex
[i
];
1195 prim
[0].basevertex
= 0;
1197 check_buffers_are_unmapped(exec
->array
.inputs
);
1198 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1199 GL_FALSE
, ~0, ~0, NULL
);
1207 static void GLAPIENTRY
1208 vbo_exec_MultiDrawElements(GLenum mode
,
1209 const GLsizei
*count
, GLenum type
,
1210 const GLvoid
**indices
,
1213 GET_CURRENT_CONTEXT(ctx
);
1216 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1218 for (i
= 0; i
< primcount
; i
++) {
1219 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1224 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1229 static void GLAPIENTRY
1230 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1231 const GLsizei
*count
, GLenum type
,
1232 const GLvoid
**indices
,
1234 const GLsizei
*basevertex
)
1236 GET_CURRENT_CONTEXT(ctx
);
1239 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1241 for (i
= 0; i
< primcount
; i
++) {
1242 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1247 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1251 #if FEATURE_EXT_transform_feedback
1254 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1255 struct gl_transform_feedback_object
*obj
,
1256 GLuint numInstances
)
1258 struct vbo_context
*vbo
= vbo_context(ctx
);
1259 struct vbo_exec_context
*exec
= &vbo
->exec
;
1260 struct _mesa_prim prim
[2];
1262 vbo_bind_arrays(ctx
);
1264 /* Again... because we may have changed the bitmask of per-vertex varying
1265 * attributes. If we regenerate the fixed-function vertex program now
1266 * we may be able to prune down the number of vertex attributes which we
1267 * need in the shader.
1270 _mesa_update_state(ctx
);
1272 /* init most fields to zero */
1273 memset(prim
, 0, sizeof(prim
));
1276 prim
[0].mode
= mode
;
1277 prim
[0].num_instances
= numInstances
;
1279 /* Maybe we should do some primitive splitting for primitive restart
1280 * (like in DrawArrays), but we have no way to know how many vertices
1281 * will be rendered. */
1283 check_buffers_are_unmapped(exec
->array
.inputs
);
1284 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
1285 GL_TRUE
, 0, 0, obj
);
1289 * Like DrawArrays, but take the count from a transform feedback object.
1290 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1291 * \param name the transform feedback object
1292 * User still has to setup of the vertex attribute info with
1293 * glVertexPointer, glColorPointer, etc.
1294 * Part of GL_ARB_transform_feedback2.
1296 static void GLAPIENTRY
1297 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1299 GET_CURRENT_CONTEXT(ctx
);
1300 struct gl_transform_feedback_object
*obj
=
1301 _mesa_lookup_transform_feedback_object(ctx
, name
);
1303 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1304 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1305 _mesa_lookup_enum_by_nr(mode
), name
);
1307 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
)) {
1311 FLUSH_CURRENT(ctx
, 0);
1313 if (!_mesa_valid_to_render(ctx
, "glDrawTransformFeedback")) {
1317 vbo_draw_transform_feedback(ctx
, mode
, obj
, 1);
1323 * Plug in the immediate-mode vertex array drawing commands into the
1324 * givven vbo_exec_context object.
1327 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1329 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1330 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1331 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1332 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1333 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1334 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1335 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1336 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1337 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1338 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1339 exec
->vtxfmt
.DrawTransformFeedback
= vbo_exec_DrawTransformFeedback
;
1344 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1352 * The following functions are only used for OpenGL ES 1/2 support.
1353 * And some aren't even supported (yet) in ES 1/2.
1358 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1360 vbo_exec_DrawArrays(mode
, first
, count
);
1365 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1366 const GLvoid
*indices
)
1368 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1373 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1374 const GLvoid
*indices
, GLint basevertex
)
1376 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1381 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1382 GLenum type
, const GLvoid
*indices
)
1384 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1389 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1390 GLsizei count
, GLenum type
,
1391 const GLvoid
*indices
, GLint basevertex
)
1393 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1394 indices
, basevertex
);
1399 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1400 const GLvoid
**indices
, GLsizei primcount
)
1402 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1407 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1408 const GLsizei
*count
, GLenum type
,
1409 const GLvoid
**indices
, GLsizei primcount
,
1410 const GLint
*basevertex
)
1412 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1413 primcount
, basevertex
);
1416 #if FEATURE_EXT_transform_feedback
1419 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1421 vbo_exec_DrawTransformFeedback(mode
, name
);