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"
38 #include "vbo_context.h"
42 * All vertex buffers should be in an unmapped state when we're about
43 * to draw. This debug function checks that.
46 check_buffers_are_unmapped(const struct gl_client_array
**inputs
)
51 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
53 struct gl_buffer_object
*obj
= inputs
[i
]->BufferObj
;
54 assert(!_mesa_bufferobj_mapped(obj
));
63 * A debug function that may be called from other parts of Mesa as
64 * needed during debugging.
67 vbo_check_buffers_are_unmapped(struct gl_context
*ctx
)
69 struct vbo_context
*vbo
= vbo_context(ctx
);
70 struct vbo_exec_context
*exec
= &vbo
->exec
;
71 /* check the current vertex arrays */
72 check_buffers_are_unmapped(exec
->array
.inputs
);
73 /* check the current glBegin/glVertex/glEnd-style VBO */
74 assert(!_mesa_bufferobj_mapped(exec
->vtx
.bufferobj
));
80 * Compute min and max elements by scanning the index buffer for
81 * glDraw[Range]Elements() calls.
82 * If primitive restart is enabled, we need to ignore restart
83 * indexes when computing min/max.
86 vbo_get_minmax_index(struct gl_context
*ctx
,
87 const struct _mesa_prim
*prim
,
88 const struct _mesa_index_buffer
*ib
,
89 GLuint
*min_index
, GLuint
*max_index
)
91 const GLboolean restart
= ctx
->Array
.PrimitiveRestart
;
92 const GLuint restartIndex
= ctx
->Array
.RestartIndex
;
93 const GLuint count
= prim
->count
;
97 if (_mesa_is_bufferobj(ib
->obj
)) {
99 ctx
->Driver
.MapBuffer(ctx
, GL_READ_ONLY
, ib
->obj
);
100 indices
= ADD_POINTERS(map
, ib
->ptr
);
106 case GL_UNSIGNED_INT
: {
107 const GLuint
*ui_indices
= (const GLuint
*)indices
;
111 for (i
= 0; i
< count
; i
++) {
112 if (ui_indices
[i
] != restartIndex
) {
113 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
114 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
119 for (i
= 0; i
< count
; i
++) {
120 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
121 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
128 case GL_UNSIGNED_SHORT
: {
129 const GLushort
*us_indices
= (const GLushort
*)indices
;
133 for (i
= 0; i
< count
; i
++) {
134 if (us_indices
[i
] != restartIndex
) {
135 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
136 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
141 for (i
= 0; i
< count
; i
++) {
142 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
143 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
150 case GL_UNSIGNED_BYTE
: {
151 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
155 for (i
= 0; i
< count
; i
++) {
156 if (ub_indices
[i
] != restartIndex
) {
157 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
158 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
163 for (i
= 0; i
< count
; i
++) {
164 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
165 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
177 if (_mesa_is_bufferobj(ib
->obj
)) {
178 ctx
->Driver
.UnmapBuffer(ctx
, ib
->obj
);
184 * Check that element 'j' of the array has reasonable data.
186 * For debugging purposes; not normally used.
189 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
190 GLuint attrib
, GLuint j
)
192 if (array
->Enabled
) {
193 const void *data
= array
->Ptr
;
194 if (_mesa_is_bufferobj(array
->BufferObj
)) {
195 if (!array
->BufferObj
->Pointer
) {
196 /* need to map now */
197 array
->BufferObj
->Pointer
=
198 ctx
->Driver
.MapBuffer(ctx
, GL_READ_ONLY
, array
->BufferObj
);
200 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
202 switch (array
->Type
) {
205 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
207 for (k
= 0; k
< array
->Size
; k
++) {
208 if (IS_INF_OR_NAN(f
[k
]) ||
209 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
210 printf("Bad array data:\n");
211 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
212 printf(" Array %u at %p\n", attrib
, (void* ) array
);
213 printf(" Type 0x%x, Size %d, Stride %d\n",
214 array
->Type
, array
->Size
, array
->Stride
);
215 printf(" Address/offset %p in Buffer Object %u\n",
216 array
->Ptr
, array
->BufferObj
->Name
);
217 f
[k
] = 1.0; /* XXX replace the bad value! */
219 /*assert(!IS_INF_OR_NAN(f[k]));*/
231 * Unmap the buffer object referenced by given array, if mapped.
234 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
236 if (array
->Enabled
&&
237 _mesa_is_bufferobj(array
->BufferObj
) &&
238 _mesa_bufferobj_mapped(array
->BufferObj
)) {
239 ctx
->Driver
.UnmapBuffer(ctx
, array
->BufferObj
);
245 * Examine the array's data for NaNs, etc.
246 * For debug purposes; not normally used.
249 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
250 const void *elements
, GLint basevertex
)
252 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
256 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
257 elemMap
= ctx
->Driver
.MapBuffer(ctx
,
259 ctx
->Array
.ElementArrayBufferObj
);
260 elements
= ADD_POINTERS(elements
, elemMap
);
263 for (i
= 0; i
< count
; i
++) {
268 case GL_UNSIGNED_BYTE
:
269 j
= ((const GLubyte
*) elements
)[i
];
271 case GL_UNSIGNED_SHORT
:
272 j
= ((const GLushort
*) elements
)[i
];
274 case GL_UNSIGNED_INT
:
275 j
= ((const GLuint
*) elements
)[i
];
281 /* check element j of each enabled array */
282 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
283 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
284 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
285 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
286 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
287 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
289 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
290 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
291 VERT_ATTRIB_GENERIC0
+ k
, j
);
295 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
296 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ElementArrayBufferObj
);
299 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
300 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
301 unmap_array_buffer(ctx
, &arrayObj
->Color
);
302 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
303 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
305 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
306 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
312 * Check array data, looking for NaNs, etc.
315 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
322 * Print info/data for glDrawArrays(), for debugging.
325 print_draw_arrays(struct gl_context
*ctx
,
326 GLenum mode
, GLint start
, GLsizei count
)
328 struct vbo_context
*vbo
= vbo_context(ctx
);
329 struct vbo_exec_context
*exec
= &vbo
->exec
;
332 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
335 for (i
= 0; i
< 32; i
++) {
336 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
337 GLuint bufName
= bufObj
->Name
;
338 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
339 printf("attr %2d: size %d stride %d enabled %d "
340 "ptr %p Bufobj %u\n",
342 exec
->array
.inputs
[i
]->Size
,
344 /*exec->array.inputs[i]->Enabled,*/
345 exec
->array
.legacy_array
[i
]->Enabled
,
346 exec
->array
.inputs
[i
]->Ptr
,
350 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_READ_ONLY_ARB
, bufObj
);
351 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
352 float *f
= (float *) (p
+ offset
);
355 int n
= (count
* stride
) / 4;
358 printf(" Data at offset %d:\n", offset
);
359 for (i
= 0; i
< n
; i
++) {
360 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
362 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
369 * Bind the VBO executor to the current vertex array object prior
372 * Just translate the arrayobj into a sane layout.
375 bind_array_obj(struct gl_context
*ctx
)
377 struct vbo_context
*vbo
= vbo_context(ctx
);
378 struct vbo_exec_context
*exec
= &vbo
->exec
;
379 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
382 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
383 * rather than as individual named arrays. Then this function can
386 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
387 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
388 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
389 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
390 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
391 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
392 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
393 if (arrayObj
->PointSize
.Enabled
) {
394 /* this aliases COLOR_INDEX */
395 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
397 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
399 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
400 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
402 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
403 assert(i
< Elements(exec
->array
.generic_array
));
404 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
407 exec
->array
.array_obj
= arrayObj
->Name
;
412 * Set the vbo->exec->inputs[] pointers to point to the enabled
413 * vertex arrays. This depends on the current vertex program/shader
414 * being executed because of whether or not generic vertex arrays
415 * alias the conventional vertex arrays.
416 * For arrays that aren't enabled, we set the input[attrib] pointer
417 * to point at a zero-stride current value "array".
420 recalculate_input_bindings(struct gl_context
*ctx
)
422 struct vbo_context
*vbo
= vbo_context(ctx
);
423 struct vbo_exec_context
*exec
= &vbo
->exec
;
424 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
425 GLbitfield const_inputs
= 0x0;
428 exec
->array
.program_mode
= get_program_mode(ctx
);
429 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
431 switch (exec
->array
.program_mode
) {
433 /* When no vertex program is active (or the vertex program is generated
434 * from fixed-function state). We put the material values into the
435 * generic slots. This is the only situation where material values
436 * are available as per-vertex attributes.
438 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
439 if (exec
->array
.legacy_array
[i
]->Enabled
)
440 inputs
[i
] = exec
->array
.legacy_array
[i
];
442 inputs
[i
] = &vbo
->legacy_currval
[i
];
443 const_inputs
|= 1 << i
;
447 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
448 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
449 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
452 /* Could use just about anything, just to fill in the empty
455 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
456 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
457 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
460 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
461 * because it already takes care of invalidating the state necessary
462 * to revalidate vertex arrays. Not marking the state as dirty also
463 * improves performance (quite significantly in some apps).
465 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
466 ctx
->NewState
|= _NEW_ARRAY
;
470 /* NV_vertex_program - attribute arrays alias and override
471 * conventional, legacy arrays. No materials, and the generic
474 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
475 if (exec
->array
.generic_array
[i
]->Enabled
)
476 inputs
[i
] = exec
->array
.generic_array
[i
];
477 else if (exec
->array
.legacy_array
[i
]->Enabled
)
478 inputs
[i
] = exec
->array
.legacy_array
[i
];
480 inputs
[i
] = &vbo
->legacy_currval
[i
];
481 const_inputs
|= 1 << i
;
485 /* Could use just about anything, just to fill in the empty
488 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
489 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
490 const_inputs
|= 1 << i
;
493 ctx
->NewState
|= _NEW_ARRAY
;
497 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
498 * attribute array aliases and overrides the legacy position array.
500 * Otherwise, legacy attributes available in the legacy slots,
501 * generic attributes in the generic slots and materials are not
502 * available as per-vertex attributes.
504 if (exec
->array
.generic_array
[0]->Enabled
)
505 inputs
[0] = exec
->array
.generic_array
[0];
506 else if (exec
->array
.legacy_array
[0]->Enabled
)
507 inputs
[0] = exec
->array
.legacy_array
[0];
509 inputs
[0] = &vbo
->legacy_currval
[0];
510 const_inputs
|= 1 << 0;
513 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
514 if (exec
->array
.legacy_array
[i
]->Enabled
)
515 inputs
[i
] = exec
->array
.legacy_array
[i
];
517 inputs
[i
] = &vbo
->legacy_currval
[i
];
518 const_inputs
|= 1 << i
;
522 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
523 if (exec
->array
.generic_array
[i
]->Enabled
)
524 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
526 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
527 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
531 ctx
->NewState
|= _NEW_ARRAY
;
535 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
540 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
541 * These will point to the arrays to actually use for drawing. Some will
542 * be user-provided arrays, other will be zero-stride const-valued arrays.
543 * Note that this might set the _NEW_ARRAY dirty flag so state validation
544 * must be done after this call.
547 bind_arrays(struct gl_context
*ctx
)
549 if (!ctx
->Array
.RebindArrays
) {
554 recalculate_input_bindings(ctx
);
555 ctx
->Array
.RebindArrays
= GL_FALSE
;
560 * Helper function called by the other DrawArrays() functions below.
561 * This is where we handle primitive restart for drawing non-indexed
562 * arrays. If primitive restart is enabled, it typically means
563 * splitting one DrawArrays() into two.
566 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
567 GLsizei count
, GLuint numInstances
)
569 struct vbo_context
*vbo
= vbo_context(ctx
);
570 struct vbo_exec_context
*exec
= &vbo
->exec
;
571 struct _mesa_prim prim
[2];
575 /* Again... because we may have changed the bitmask of per-vertex varying
576 * attributes. If we regenerate the fixed-function vertex program now
577 * we may be able to prune down the number of vertex attributes which we
578 * need in the shader.
581 _mesa_update_state(ctx
);
583 /* init most fields to zero */
584 memset(prim
, 0, sizeof(prim
));
588 prim
[0].num_instances
= numInstances
;
590 /* Implement the primitive restart index */
591 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
592 GLuint primCount
= 0;
594 if (ctx
->Array
.RestartIndex
== start
) {
595 /* special case: RestartIndex at beginning */
597 prim
[0].start
= start
+ 1;
598 prim
[0].count
= count
- 1;
602 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
603 /* special case: RestartIndex at end */
605 prim
[0].start
= start
;
606 prim
[0].count
= count
- 1;
611 /* general case: RestartIndex in middle, split into two prims */
612 prim
[0].start
= start
;
613 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
616 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
617 prim
[1].count
= count
- prim
[1].start
;
623 /* draw one or two prims */
624 check_buffers_are_unmapped(exec
->array
.inputs
);
625 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
626 GL_TRUE
, start
, start
+ count
- 1);
630 /* no prim restart */
631 prim
[0].start
= start
;
632 prim
[0].count
= count
;
634 check_buffers_are_unmapped(exec
->array
.inputs
);
635 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
636 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
)
713 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
715 ctx
->Array
.ElementArrayBufferObj
);
717 case GL_UNSIGNED_BYTE
:
719 const GLubyte
*us
= (const GLubyte
*) map
;
721 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
722 printf("%02x ", us
[i
]);
729 case GL_UNSIGNED_SHORT
:
731 const GLushort
*us
= (const GLushort
*) map
;
733 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
734 printf("%04x ", us
[i
]);
741 case GL_UNSIGNED_INT
:
743 const GLuint
*us
= (const GLuint
*) map
;
745 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
746 printf("%08x ", us
[i
]);
757 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ElementArrayBufferObj
);
762 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
763 * Do the rendering for a glDrawElements or glDrawRangeElements call after
764 * we've validated buffer bounds, etc.
767 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
768 GLboolean index_bounds_valid
,
769 GLuint start
, GLuint end
,
770 GLsizei count
, GLenum type
,
771 const GLvoid
*indices
,
772 GLint basevertex
, GLint numInstances
)
774 struct vbo_context
*vbo
= vbo_context(ctx
);
775 struct vbo_exec_context
*exec
= &vbo
->exec
;
776 struct _mesa_index_buffer ib
;
777 struct _mesa_prim prim
[1];
779 FLUSH_CURRENT( ctx
, 0 );
781 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
787 /* check for dirty state again */
789 _mesa_update_state( ctx
);
793 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
802 prim
[0].count
= count
;
804 prim
[0].basevertex
= basevertex
;
805 prim
[0].num_instances
= numInstances
;
807 /* Need to give special consideration to rendering a range of
808 * indices starting somewhere above zero. Typically the
809 * application is issuing multiple DrawRangeElements() to draw
810 * successive primitives layed out linearly in the vertex arrays.
811 * Unless the vertex arrays are all in a VBO (or locked as with
812 * CVA), the OpenGL semantics imply that we need to re-read or
813 * re-upload the vertex data on each draw call.
815 * In the case of hardware tnl, we want to avoid starting the
816 * upload at zero, as it will mean every draw call uploads an
817 * increasing amount of not-used vertex data. Worse - in the
818 * software tnl module, all those vertices might be transformed and
819 * lit but never rendered.
821 * If we just upload or transform the vertices in start..end,
822 * however, the indices will be incorrect.
824 * At this level, we don't know exactly what the requirements of
825 * the backend are going to be, though it will likely boil down to
828 * 1) Do nothing, everything is in a VBO and is processed once
831 * 2) Adjust the indices and vertex arrays so that start becomes
834 * Rather than doing anything here, I'll provide a helper function
835 * for the latter case elsewhere.
838 check_buffers_are_unmapped(exec
->array
.inputs
);
839 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
840 index_bounds_valid
, start
, end
);
845 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
847 static void GLAPIENTRY
848 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
849 GLuint start
, GLuint end
,
850 GLsizei count
, GLenum type
,
851 const GLvoid
*indices
,
854 static GLuint warnCount
= 0;
855 GET_CURRENT_CONTEXT(ctx
);
857 if (MESA_VERBOSE
& VERBOSE_DRAW
)
859 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
860 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
861 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
863 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
864 type
, indices
, basevertex
))
867 /* NOTE: It's important that 'end' is a reasonable value.
868 * in _tnl_draw_prims(), we use end to determine how many vertices
869 * to transform. If it's too large, we can unnecessarily split prims
870 * or we can read/write out of memory in several different places!
873 /* Catch/fix some potential user errors */
874 if (type
== GL_UNSIGNED_BYTE
) {
875 start
= MIN2(start
, 0xff);
876 end
= MIN2(end
, 0xff);
878 else if (type
== GL_UNSIGNED_SHORT
) {
879 start
= MIN2(start
, 0xffff);
880 end
= MIN2(end
, 0xffff);
883 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
884 /* the max element is out of bounds of one or more enabled arrays */
887 if (warnCount
< 10) {
888 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
889 "type 0x%x, indices=%p)\n"
890 "\tend is out of bounds (max=%u) "
891 "Element Buffer %u (size %d)\n"
892 "\tThis should probably be fixed in the application.",
893 start
, end
, count
, type
, indices
,
894 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
895 ctx
->Array
.ElementArrayBufferObj
->Name
,
896 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
900 dump_element_buffer(ctx
, type
);
903 _mesa_print_arrays(ctx
);
905 /* 'end' was out of bounds, but now let's check the actual array
906 * indexes to see if any of them are out of bounds.
909 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
910 ctx
->Array
.ElementArrayBufferObj
);
911 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
912 if (warnCount
< 10) {
913 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
914 "count %d, type 0x%x, indices=%p)\n"
915 "\tindex=%u is out of bounds (max=%u) "
916 "Element Buffer %u (size %d)\n"
917 "\tSkipping the glDrawRangeElements() call",
918 start
, end
, count
, type
, indices
, max
,
919 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
920 ctx
->Array
.ElementArrayBufferObj
->Name
,
921 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
924 /* XXX we could also find the min index and compare to 'start'
925 * to see if start is correct. But it's more likely to get the
930 /* Set 'end' to the max possible legal value */
931 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
932 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
940 printf("glDraw[Range]Elements{,BaseVertex}"
941 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
943 start
, end
, type
, count
,
944 ctx
->Array
.ElementArrayBufferObj
->Name
,
949 check_draw_elements_data(ctx
, count
, type
, indices
);
951 (void) check_draw_elements_data
;
954 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
955 count
, type
, indices
, basevertex
, 1);
960 * Called by glDrawRangeElements() in immediate mode.
962 static void GLAPIENTRY
963 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
964 GLsizei count
, GLenum type
, const GLvoid
*indices
)
966 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
967 GET_CURRENT_CONTEXT(ctx
);
969 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
970 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
971 _mesa_lookup_enum_by_nr(type
), indices
);
974 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
980 * Called by glDrawElements() in immediate mode.
982 static void GLAPIENTRY
983 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
984 const GLvoid
*indices
)
986 GET_CURRENT_CONTEXT(ctx
);
988 if (MESA_VERBOSE
& VERBOSE_DRAW
)
989 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
990 _mesa_lookup_enum_by_nr(mode
), count
,
991 _mesa_lookup_enum_by_nr(type
), indices
);
993 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
996 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
997 count
, type
, indices
, 0, 1);
1002 * Called by glDrawElementsBaseVertex() in immediate mode.
1004 static void GLAPIENTRY
1005 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1006 const GLvoid
*indices
, GLint basevertex
)
1008 GET_CURRENT_CONTEXT(ctx
);
1010 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1011 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1012 _mesa_lookup_enum_by_nr(mode
), count
,
1013 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1015 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1019 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1020 count
, type
, indices
, basevertex
, 1);
1025 * Called by glDrawElementsInstanced() in immediate mode.
1027 static void GLAPIENTRY
1028 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1029 const GLvoid
*indices
, GLsizei numInstances
)
1031 GET_CURRENT_CONTEXT(ctx
);
1033 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1034 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1035 _mesa_lookup_enum_by_nr(mode
), count
,
1036 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1038 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1042 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1043 count
, type
, indices
, 0, numInstances
);
1047 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1049 static void GLAPIENTRY
1050 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1051 const GLvoid
*indices
, GLsizei numInstances
,
1054 GET_CURRENT_CONTEXT(ctx
);
1056 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1057 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1058 _mesa_lookup_enum_by_nr(mode
), count
,
1059 _mesa_lookup_enum_by_nr(type
), indices
,
1060 numInstances
, basevertex
);
1062 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1063 numInstances
, basevertex
))
1066 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1067 count
, type
, indices
, basevertex
, numInstances
);
1072 * Inner support for both _mesa_MultiDrawElements() and
1073 * _mesa_MultiDrawRangeElements().
1074 * This does the actual rendering after we've checked array indexes, etc.
1077 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1078 const GLsizei
*count
, GLenum type
,
1079 const GLvoid
**indices
, GLsizei primcount
,
1080 const GLint
*basevertex
)
1082 struct vbo_context
*vbo
= vbo_context(ctx
);
1083 struct vbo_exec_context
*exec
= &vbo
->exec
;
1084 struct _mesa_index_buffer ib
;
1085 struct _mesa_prim
*prim
;
1086 unsigned int index_type_size
= 0;
1087 uintptr_t min_index_ptr
, max_index_ptr
;
1088 GLboolean fallback
= GL_FALSE
;
1094 FLUSH_CURRENT( ctx
, 0 );
1096 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1100 prim
= calloc(1, primcount
* sizeof(*prim
));
1102 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1106 /* Decide if we can do this all as one set of primitives sharing the
1107 * same index buffer, or if we have to reset the index pointer per
1112 /* check for dirty state again */
1114 _mesa_update_state( ctx
);
1117 case GL_UNSIGNED_INT
:
1118 index_type_size
= 4;
1120 case GL_UNSIGNED_SHORT
:
1121 index_type_size
= 2;
1123 case GL_UNSIGNED_BYTE
:
1124 index_type_size
= 1;
1130 min_index_ptr
= (uintptr_t)indices
[0];
1132 for (i
= 0; i
< primcount
; i
++) {
1133 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1134 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1135 index_type_size
* count
[i
]);
1138 /* Check if we can handle this thing as a bunch of index offsets from the
1139 * same index pointer. If we can't, then we have to fall back to doing
1140 * a draw_prims per primitive.
1141 * Check that the difference between each prim's indexes is a multiple of
1142 * the index/element size.
1144 if (index_type_size
!= 1) {
1145 for (i
= 0; i
< primcount
; i
++) {
1146 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1153 /* If the index buffer isn't in a VBO, then treating the application's
1154 * subranges of the index buffer as one large index buffer may lead to
1155 * us reading unmapped memory.
1157 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1161 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1163 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1164 ib
.ptr
= (void *)min_index_ptr
;
1166 for (i
= 0; i
< primcount
; i
++) {
1167 prim
[i
].begin
= (i
== 0);
1168 prim
[i
].end
= (i
== primcount
- 1);
1171 prim
[i
].mode
= mode
;
1172 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1173 prim
[i
].count
= count
[i
];
1174 prim
[i
].indexed
= 1;
1175 prim
[i
].num_instances
= 1;
1176 if (basevertex
!= NULL
)
1177 prim
[i
].basevertex
= basevertex
[i
];
1179 prim
[i
].basevertex
= 0;
1182 check_buffers_are_unmapped(exec
->array
.inputs
);
1183 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1186 /* render one prim at a time */
1187 for (i
= 0; i
< primcount
; i
++) {
1188 ib
.count
= count
[i
];
1190 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1191 ib
.ptr
= indices
[i
];
1197 prim
[0].mode
= mode
;
1199 prim
[0].count
= count
[i
];
1200 prim
[0].indexed
= 1;
1201 prim
[0].num_instances
= 1;
1202 if (basevertex
!= NULL
)
1203 prim
[0].basevertex
= basevertex
[i
];
1205 prim
[0].basevertex
= 0;
1207 check_buffers_are_unmapped(exec
->array
.inputs
);
1208 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1217 static void GLAPIENTRY
1218 vbo_exec_MultiDrawElements(GLenum mode
,
1219 const GLsizei
*count
, GLenum type
,
1220 const GLvoid
**indices
,
1223 GET_CURRENT_CONTEXT(ctx
);
1226 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1228 for (i
= 0; i
< primcount
; i
++) {
1229 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1234 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1239 static void GLAPIENTRY
1240 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1241 const GLsizei
*count
, GLenum type
,
1242 const GLvoid
**indices
,
1244 const GLsizei
*basevertex
)
1246 GET_CURRENT_CONTEXT(ctx
);
1249 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1251 for (i
= 0; i
< primcount
; i
++) {
1252 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1257 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1263 * Plug in the immediate-mode vertex array drawing commands into the
1264 * givven vbo_exec_context object.
1267 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1269 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1270 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1271 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1272 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1273 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1274 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1275 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1276 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1277 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1278 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1283 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1291 * The following functions are only used for OpenGL ES 1/2 support.
1292 * And some aren't even supported (yet) in ES 1/2.
1297 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1299 vbo_exec_DrawArrays(mode
, first
, count
);
1304 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1305 const GLvoid
*indices
)
1307 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1312 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1313 const GLvoid
*indices
, GLint basevertex
)
1315 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1320 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1321 GLenum type
, const GLvoid
*indices
)
1323 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1328 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1329 GLsizei count
, GLenum type
,
1330 const GLvoid
*indices
, GLint basevertex
)
1332 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1333 indices
, basevertex
);
1338 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1339 const GLvoid
**indices
, GLsizei primcount
)
1341 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1346 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1347 const GLsizei
*count
, GLenum type
,
1348 const GLvoid
**indices
, GLsizei primcount
,
1349 const GLint
*basevertex
)
1351 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1352 primcount
, basevertex
);