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
)) {
101 case GL_UNSIGNED_INT
:
102 map_size
= count
* sizeof(GLuint
);
104 case GL_UNSIGNED_SHORT
:
105 map_size
= count
* sizeof(GLushort
);
107 case GL_UNSIGNED_BYTE
:
108 map_size
= count
* sizeof(GLubyte
);
115 indices
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
, map_size
,
116 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
);
200 * Check that element 'j' of the array has reasonable data.
202 * For debugging purposes; not normally used.
205 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
206 GLuint attrib
, GLuint j
)
208 if (array
->Enabled
) {
209 const void *data
= array
->Ptr
;
210 if (_mesa_is_bufferobj(array
->BufferObj
)) {
211 if (!array
->BufferObj
->Pointer
) {
212 /* need to map now */
213 array
->BufferObj
->Pointer
=
214 ctx
->Driver
.MapBufferRange(ctx
, 0, array
->BufferObj
->Size
,
215 GL_MAP_READ_BIT
, array
->BufferObj
);
217 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
219 switch (array
->Type
) {
222 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
224 for (k
= 0; k
< array
->Size
; k
++) {
225 if (IS_INF_OR_NAN(f
[k
]) ||
226 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
227 printf("Bad array data:\n");
228 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
229 printf(" Array %u at %p\n", attrib
, (void* ) array
);
230 printf(" Type 0x%x, Size %d, Stride %d\n",
231 array
->Type
, array
->Size
, array
->Stride
);
232 printf(" Address/offset %p in Buffer Object %u\n",
233 array
->Ptr
, array
->BufferObj
->Name
);
234 f
[k
] = 1.0; /* XXX replace the bad value! */
236 /*assert(!IS_INF_OR_NAN(f[k]));*/
248 * Unmap the buffer object referenced by given array, if mapped.
251 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
253 if (array
->Enabled
&&
254 _mesa_is_bufferobj(array
->BufferObj
) &&
255 _mesa_bufferobj_mapped(array
->BufferObj
)) {
256 ctx
->Driver
.UnmapBuffer(ctx
, array
->BufferObj
);
262 * Examine the array's data for NaNs, etc.
263 * For debug purposes; not normally used.
266 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
267 const void *elements
, GLint basevertex
)
269 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
273 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
274 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
275 ctx
->Array
.ElementArrayBufferObj
->Size
,
277 ctx
->Array
.ElementArrayBufferObj
);
278 elements
= ADD_POINTERS(elements
, elemMap
);
281 for (i
= 0; i
< count
; i
++) {
286 case GL_UNSIGNED_BYTE
:
287 j
= ((const GLubyte
*) elements
)[i
];
289 case GL_UNSIGNED_SHORT
:
290 j
= ((const GLushort
*) elements
)[i
];
292 case GL_UNSIGNED_INT
:
293 j
= ((const GLuint
*) elements
)[i
];
299 /* check element j of each enabled array */
300 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
301 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
302 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
303 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
304 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
305 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
307 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
308 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
309 VERT_ATTRIB_GENERIC0
+ k
, j
);
313 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
314 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ElementArrayBufferObj
);
317 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
318 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
319 unmap_array_buffer(ctx
, &arrayObj
->Color
);
320 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
321 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
323 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
324 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
330 * Check array data, looking for NaNs, etc.
333 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
340 * Print info/data for glDrawArrays(), for debugging.
343 print_draw_arrays(struct gl_context
*ctx
,
344 GLenum mode
, GLint start
, GLsizei count
)
346 struct vbo_context
*vbo
= vbo_context(ctx
);
347 struct vbo_exec_context
*exec
= &vbo
->exec
;
350 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
353 for (i
= 0; i
< 32; i
++) {
354 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
355 GLuint bufName
= bufObj
->Name
;
356 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
357 printf("attr %2d: size %d stride %d enabled %d "
358 "ptr %p Bufobj %u\n",
360 exec
->array
.inputs
[i
]->Size
,
362 /*exec->array.inputs[i]->Enabled,*/
363 exec
->array
.legacy_array
[i
]->Enabled
,
364 exec
->array
.inputs
[i
]->Ptr
,
368 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
369 GL_MAP_READ_BIT
, bufObj
);
370 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
371 float *f
= (float *) (p
+ offset
);
374 int n
= (count
* stride
) / 4;
377 printf(" Data at offset %d:\n", offset
);
378 for (i
= 0; i
< n
; i
++) {
379 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
381 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
388 * Bind the VBO executor to the current vertex array object prior
391 * Just translate the arrayobj into a sane layout.
394 bind_array_obj(struct gl_context
*ctx
)
396 struct vbo_context
*vbo
= vbo_context(ctx
);
397 struct vbo_exec_context
*exec
= &vbo
->exec
;
398 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
401 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
402 * rather than as individual named arrays. Then this function can
405 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
406 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
407 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
408 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
409 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
410 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
411 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
412 if (arrayObj
->PointSize
.Enabled
) {
413 /* this aliases COLOR_INDEX */
414 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
416 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
418 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
419 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
421 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
422 assert(i
< Elements(exec
->array
.generic_array
));
423 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
429 * Set the vbo->exec->inputs[] pointers to point to the enabled
430 * vertex arrays. This depends on the current vertex program/shader
431 * being executed because of whether or not generic vertex arrays
432 * alias the conventional vertex arrays.
433 * For arrays that aren't enabled, we set the input[attrib] pointer
434 * to point at a zero-stride current value "array".
437 recalculate_input_bindings(struct gl_context
*ctx
)
439 struct vbo_context
*vbo
= vbo_context(ctx
);
440 struct vbo_exec_context
*exec
= &vbo
->exec
;
441 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
442 GLbitfield const_inputs
= 0x0;
445 switch (get_program_mode(ctx
)) {
447 /* When no vertex program is active (or the vertex program is generated
448 * from fixed-function state). We put the material values into the
449 * generic slots. This is the only situation where material values
450 * are available as per-vertex attributes.
452 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
453 if (exec
->array
.legacy_array
[i
]->Enabled
)
454 inputs
[i
] = exec
->array
.legacy_array
[i
];
456 inputs
[i
] = &vbo
->legacy_currval
[i
];
457 const_inputs
|= 1 << i
;
461 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
462 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
463 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
466 /* Could use just about anything, just to fill in the empty
469 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
470 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
471 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
474 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
475 * because it already takes care of invalidating the state necessary
476 * to revalidate vertex arrays. Not marking the state as dirty also
477 * improves performance (quite significantly in some apps).
479 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
480 ctx
->NewState
|= _NEW_ARRAY
;
484 /* NV_vertex_program - attribute arrays alias and override
485 * conventional, legacy arrays. No materials, and the generic
488 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
489 if (exec
->array
.generic_array
[i
]->Enabled
)
490 inputs
[i
] = exec
->array
.generic_array
[i
];
491 else if (exec
->array
.legacy_array
[i
]->Enabled
)
492 inputs
[i
] = exec
->array
.legacy_array
[i
];
494 inputs
[i
] = &vbo
->legacy_currval
[i
];
495 const_inputs
|= 1 << i
;
499 /* Could use just about anything, just to fill in the empty
502 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
503 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
504 const_inputs
|= 1 << i
;
507 ctx
->NewState
|= _NEW_ARRAY
;
511 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
512 * attribute array aliases and overrides the legacy position array.
514 * Otherwise, legacy attributes available in the legacy slots,
515 * generic attributes in the generic slots and materials are not
516 * available as per-vertex attributes.
518 if (exec
->array
.generic_array
[0]->Enabled
)
519 inputs
[0] = exec
->array
.generic_array
[0];
520 else if (exec
->array
.legacy_array
[0]->Enabled
)
521 inputs
[0] = exec
->array
.legacy_array
[0];
523 inputs
[0] = &vbo
->legacy_currval
[0];
524 const_inputs
|= 1 << 0;
527 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
528 if (exec
->array
.legacy_array
[i
]->Enabled
)
529 inputs
[i
] = exec
->array
.legacy_array
[i
];
531 inputs
[i
] = &vbo
->legacy_currval
[i
];
532 const_inputs
|= 1 << i
;
536 for (i
= 1; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
537 if (exec
->array
.generic_array
[i
]->Enabled
)
538 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
540 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
541 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
545 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
546 ctx
->NewState
|= _NEW_ARRAY
;
550 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
555 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
556 * These will point to the arrays to actually use for drawing. Some will
557 * be user-provided arrays, other will be zero-stride const-valued arrays.
558 * Note that this might set the _NEW_ARRAY dirty flag so state validation
559 * must be done after this call.
562 vbo_bind_arrays(struct gl_context
*ctx
)
564 if (!ctx
->Array
.RebindArrays
) {
569 recalculate_input_bindings(ctx
);
570 ctx
->Array
.RebindArrays
= GL_FALSE
;
575 * Helper function called by the other DrawArrays() functions below.
576 * This is where we handle primitive restart for drawing non-indexed
577 * arrays. If primitive restart is enabled, it typically means
578 * splitting one DrawArrays() into two.
581 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
582 GLsizei count
, GLuint numInstances
)
584 struct vbo_context
*vbo
= vbo_context(ctx
);
585 struct vbo_exec_context
*exec
= &vbo
->exec
;
586 struct _mesa_prim prim
[2];
588 vbo_bind_arrays(ctx
);
590 /* Again... because we may have changed the bitmask of per-vertex varying
591 * attributes. If we regenerate the fixed-function vertex program now
592 * we may be able to prune down the number of vertex attributes which we
593 * need in the shader.
596 _mesa_update_state(ctx
);
598 /* init most fields to zero */
599 memset(prim
, 0, sizeof(prim
));
603 prim
[0].num_instances
= numInstances
;
605 /* Implement the primitive restart index */
606 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
607 GLuint primCount
= 0;
609 if (ctx
->Array
.RestartIndex
== start
) {
610 /* special case: RestartIndex at beginning */
612 prim
[0].start
= start
+ 1;
613 prim
[0].count
= count
- 1;
617 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
618 /* special case: RestartIndex at end */
620 prim
[0].start
= start
;
621 prim
[0].count
= count
- 1;
626 /* general case: RestartIndex in middle, split into two prims */
627 prim
[0].start
= start
;
628 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
631 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
632 prim
[1].count
= count
- prim
[1].start
;
638 /* draw one or two prims */
639 check_buffers_are_unmapped(exec
->array
.inputs
);
640 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
641 GL_TRUE
, start
, start
+ count
- 1);
645 /* no prim restart */
646 prim
[0].start
= start
;
647 prim
[0].count
= count
;
649 check_buffers_are_unmapped(exec
->array
.inputs
);
650 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
651 GL_TRUE
, start
, start
+ count
- 1);
658 * Called from glDrawArrays when in immediate mode (not display list mode).
660 static void GLAPIENTRY
661 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
663 GET_CURRENT_CONTEXT(ctx
);
665 if (MESA_VERBOSE
& VERBOSE_DRAW
)
666 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
667 _mesa_lookup_enum_by_nr(mode
), start
, count
);
669 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
672 FLUSH_CURRENT( ctx
, 0 );
674 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
679 check_draw_arrays_data(ctx
, start
, count
);
681 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
684 print_draw_arrays(ctx
, mode
, start
, count
);
689 * Called from glDrawArraysInstanced when in immediate mode (not
690 * display list mode).
692 static void GLAPIENTRY
693 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
694 GLsizei numInstances
)
696 GET_CURRENT_CONTEXT(ctx
);
698 if (MESA_VERBOSE
& VERBOSE_DRAW
)
699 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
700 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
702 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
705 FLUSH_CURRENT( ctx
, 0 );
707 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
712 check_draw_arrays_data(ctx
, start
, count
);
714 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
717 print_draw_arrays(ctx
, mode
, start
, count
);
722 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
726 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
729 ctx
->Driver
.MapBufferRange(ctx
, 0,
730 ctx
->Array
.ElementArrayBufferObj
->Size
,
732 ctx
->Array
.ElementArrayBufferObj
);
734 case GL_UNSIGNED_BYTE
:
736 const GLubyte
*us
= (const GLubyte
*) map
;
738 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
739 printf("%02x ", us
[i
]);
746 case GL_UNSIGNED_SHORT
:
748 const GLushort
*us
= (const GLushort
*) map
;
750 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
751 printf("%04x ", us
[i
]);
758 case GL_UNSIGNED_INT
:
760 const GLuint
*us
= (const GLuint
*) map
;
762 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
763 printf("%08x ", us
[i
]);
774 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ElementArrayBufferObj
);
779 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
780 * Do the rendering for a glDrawElements or glDrawRangeElements call after
781 * we've validated buffer bounds, etc.
784 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
785 GLboolean index_bounds_valid
,
786 GLuint start
, GLuint end
,
787 GLsizei count
, GLenum type
,
788 const GLvoid
*indices
,
789 GLint basevertex
, GLint numInstances
)
791 struct vbo_context
*vbo
= vbo_context(ctx
);
792 struct vbo_exec_context
*exec
= &vbo
->exec
;
793 struct _mesa_index_buffer ib
;
794 struct _mesa_prim prim
[1];
796 FLUSH_CURRENT( ctx
, 0 );
798 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
802 vbo_bind_arrays( ctx
);
804 /* check for dirty state again */
806 _mesa_update_state( ctx
);
810 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
819 prim
[0].count
= count
;
821 prim
[0].basevertex
= basevertex
;
822 prim
[0].num_instances
= numInstances
;
824 /* Need to give special consideration to rendering a range of
825 * indices starting somewhere above zero. Typically the
826 * application is issuing multiple DrawRangeElements() to draw
827 * successive primitives layed out linearly in the vertex arrays.
828 * Unless the vertex arrays are all in a VBO (or locked as with
829 * CVA), the OpenGL semantics imply that we need to re-read or
830 * re-upload the vertex data on each draw call.
832 * In the case of hardware tnl, we want to avoid starting the
833 * upload at zero, as it will mean every draw call uploads an
834 * increasing amount of not-used vertex data. Worse - in the
835 * software tnl module, all those vertices might be transformed and
836 * lit but never rendered.
838 * If we just upload or transform the vertices in start..end,
839 * however, the indices will be incorrect.
841 * At this level, we don't know exactly what the requirements of
842 * the backend are going to be, though it will likely boil down to
845 * 1) Do nothing, everything is in a VBO and is processed once
848 * 2) Adjust the indices and vertex arrays so that start becomes
851 * Rather than doing anything here, I'll provide a helper function
852 * for the latter case elsewhere.
855 check_buffers_are_unmapped(exec
->array
.inputs
);
856 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
857 index_bounds_valid
, start
, end
);
862 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
864 static void GLAPIENTRY
865 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
866 GLuint start
, GLuint end
,
867 GLsizei count
, GLenum type
,
868 const GLvoid
*indices
,
871 static GLuint warnCount
= 0;
872 GET_CURRENT_CONTEXT(ctx
);
874 if (MESA_VERBOSE
& VERBOSE_DRAW
)
876 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
877 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
878 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
880 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
881 type
, indices
, basevertex
))
884 /* NOTE: It's important that 'end' is a reasonable value.
885 * in _tnl_draw_prims(), we use end to determine how many vertices
886 * to transform. If it's too large, we can unnecessarily split prims
887 * or we can read/write out of memory in several different places!
890 /* Catch/fix some potential user errors */
891 if (type
== GL_UNSIGNED_BYTE
) {
892 start
= MIN2(start
, 0xff);
893 end
= MIN2(end
, 0xff);
895 else if (type
== GL_UNSIGNED_SHORT
) {
896 start
= MIN2(start
, 0xffff);
897 end
= MIN2(end
, 0xffff);
900 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
901 /* the max element is out of bounds of one or more enabled arrays */
904 if (warnCount
< 10) {
905 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
906 "type 0x%x, indices=%p)\n"
907 "\tend is out of bounds (max=%u) "
908 "Element Buffer %u (size %d)\n"
909 "\tThis should probably be fixed in the application.",
910 start
, end
, count
, type
, indices
,
911 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
912 ctx
->Array
.ElementArrayBufferObj
->Name
,
913 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
917 dump_element_buffer(ctx
, type
);
920 _mesa_print_arrays(ctx
);
922 /* 'end' was out of bounds, but now let's check the actual array
923 * indexes to see if any of them are out of bounds.
926 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
927 ctx
->Array
.ElementArrayBufferObj
);
928 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
929 if (warnCount
< 10) {
930 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
931 "count %d, type 0x%x, indices=%p)\n"
932 "\tindex=%u is out of bounds (max=%u) "
933 "Element Buffer %u (size %d)\n"
934 "\tSkipping the glDrawRangeElements() call",
935 start
, end
, count
, type
, indices
, max
,
936 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
937 ctx
->Array
.ElementArrayBufferObj
->Name
,
938 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
941 /* XXX we could also find the min index and compare to 'start'
942 * to see if start is correct. But it's more likely to get the
947 /* Set 'end' to the max possible legal value */
948 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
949 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
957 printf("glDraw[Range]Elements{,BaseVertex}"
958 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
960 start
, end
, type
, count
,
961 ctx
->Array
.ElementArrayBufferObj
->Name
,
966 check_draw_elements_data(ctx
, count
, type
, indices
);
968 (void) check_draw_elements_data
;
971 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
972 count
, type
, indices
, basevertex
, 1);
977 * Called by glDrawRangeElements() in immediate mode.
979 static void GLAPIENTRY
980 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
981 GLsizei count
, GLenum type
, const GLvoid
*indices
)
983 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
984 GET_CURRENT_CONTEXT(ctx
);
986 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
987 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
988 _mesa_lookup_enum_by_nr(type
), indices
);
991 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
997 * Called by glDrawElements() in immediate mode.
999 static void GLAPIENTRY
1000 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1001 const GLvoid
*indices
)
1003 GET_CURRENT_CONTEXT(ctx
);
1005 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1006 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1007 _mesa_lookup_enum_by_nr(mode
), count
,
1008 _mesa_lookup_enum_by_nr(type
), indices
);
1010 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
1013 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1014 count
, type
, indices
, 0, 1);
1019 * Called by glDrawElementsBaseVertex() in immediate mode.
1021 static void GLAPIENTRY
1022 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1023 const GLvoid
*indices
, GLint basevertex
)
1025 GET_CURRENT_CONTEXT(ctx
);
1027 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1028 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1029 _mesa_lookup_enum_by_nr(mode
), count
,
1030 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1032 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1036 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1037 count
, type
, indices
, basevertex
, 1);
1042 * Called by glDrawElementsInstanced() in immediate mode.
1044 static void GLAPIENTRY
1045 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1046 const GLvoid
*indices
, GLsizei numInstances
)
1048 GET_CURRENT_CONTEXT(ctx
);
1050 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1051 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1052 _mesa_lookup_enum_by_nr(mode
), count
,
1053 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1055 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1059 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1060 count
, type
, indices
, 0, numInstances
);
1064 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1066 static void GLAPIENTRY
1067 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1068 const GLvoid
*indices
, GLsizei numInstances
,
1071 GET_CURRENT_CONTEXT(ctx
);
1073 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1074 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1075 _mesa_lookup_enum_by_nr(mode
), count
,
1076 _mesa_lookup_enum_by_nr(type
), indices
,
1077 numInstances
, basevertex
);
1079 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1080 numInstances
, basevertex
))
1083 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1084 count
, type
, indices
, basevertex
, numInstances
);
1089 * Inner support for both _mesa_MultiDrawElements() and
1090 * _mesa_MultiDrawRangeElements().
1091 * This does the actual rendering after we've checked array indexes, etc.
1094 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1095 const GLsizei
*count
, GLenum type
,
1096 const GLvoid
**indices
, GLsizei primcount
,
1097 const GLint
*basevertex
)
1099 struct vbo_context
*vbo
= vbo_context(ctx
);
1100 struct vbo_exec_context
*exec
= &vbo
->exec
;
1101 struct _mesa_index_buffer ib
;
1102 struct _mesa_prim
*prim
;
1103 unsigned int index_type_size
= 0;
1104 uintptr_t min_index_ptr
, max_index_ptr
;
1105 GLboolean fallback
= GL_FALSE
;
1111 FLUSH_CURRENT( ctx
, 0 );
1113 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1117 prim
= calloc(1, primcount
* sizeof(*prim
));
1119 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1123 /* Decide if we can do this all as one set of primitives sharing the
1124 * same index buffer, or if we have to reset the index pointer per
1127 vbo_bind_arrays( ctx
);
1129 /* check for dirty state again */
1131 _mesa_update_state( ctx
);
1134 case GL_UNSIGNED_INT
:
1135 index_type_size
= 4;
1137 case GL_UNSIGNED_SHORT
:
1138 index_type_size
= 2;
1140 case GL_UNSIGNED_BYTE
:
1141 index_type_size
= 1;
1147 min_index_ptr
= (uintptr_t)indices
[0];
1149 for (i
= 0; i
< primcount
; i
++) {
1150 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1151 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1152 index_type_size
* count
[i
]);
1155 /* Check if we can handle this thing as a bunch of index offsets from the
1156 * same index pointer. If we can't, then we have to fall back to doing
1157 * a draw_prims per primitive.
1158 * Check that the difference between each prim's indexes is a multiple of
1159 * the index/element size.
1161 if (index_type_size
!= 1) {
1162 for (i
= 0; i
< primcount
; i
++) {
1163 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1170 /* If the index buffer isn't in a VBO, then treating the application's
1171 * subranges of the index buffer as one large index buffer may lead to
1172 * us reading unmapped memory.
1174 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1178 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1180 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1181 ib
.ptr
= (void *)min_index_ptr
;
1183 for (i
= 0; i
< primcount
; i
++) {
1184 prim
[i
].begin
= (i
== 0);
1185 prim
[i
].end
= (i
== primcount
- 1);
1188 prim
[i
].mode
= mode
;
1189 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1190 prim
[i
].count
= count
[i
];
1191 prim
[i
].indexed
= 1;
1192 prim
[i
].num_instances
= 1;
1193 if (basevertex
!= NULL
)
1194 prim
[i
].basevertex
= basevertex
[i
];
1196 prim
[i
].basevertex
= 0;
1199 check_buffers_are_unmapped(exec
->array
.inputs
);
1200 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1203 /* render one prim at a time */
1204 for (i
= 0; i
< primcount
; i
++) {
1205 ib
.count
= count
[i
];
1207 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1208 ib
.ptr
= indices
[i
];
1214 prim
[0].mode
= mode
;
1216 prim
[0].count
= count
[i
];
1217 prim
[0].indexed
= 1;
1218 prim
[0].num_instances
= 1;
1219 if (basevertex
!= NULL
)
1220 prim
[0].basevertex
= basevertex
[i
];
1222 prim
[0].basevertex
= 0;
1224 check_buffers_are_unmapped(exec
->array
.inputs
);
1225 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1234 static void GLAPIENTRY
1235 vbo_exec_MultiDrawElements(GLenum mode
,
1236 const GLsizei
*count
, GLenum type
,
1237 const GLvoid
**indices
,
1240 GET_CURRENT_CONTEXT(ctx
);
1243 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1245 for (i
= 0; i
< primcount
; i
++) {
1246 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1251 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1256 static void GLAPIENTRY
1257 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1258 const GLsizei
*count
, GLenum type
,
1259 const GLvoid
**indices
,
1261 const GLsizei
*basevertex
)
1263 GET_CURRENT_CONTEXT(ctx
);
1266 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1268 for (i
= 0; i
< primcount
; i
++) {
1269 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1274 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1280 * Plug in the immediate-mode vertex array drawing commands into the
1281 * givven vbo_exec_context object.
1284 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1286 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1287 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1288 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1289 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1290 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1291 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1292 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1293 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1294 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1295 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1300 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1308 * The following functions are only used for OpenGL ES 1/2 support.
1309 * And some aren't even supported (yet) in ES 1/2.
1314 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1316 vbo_exec_DrawArrays(mode
, first
, count
);
1321 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1322 const GLvoid
*indices
)
1324 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1329 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1330 const GLvoid
*indices
, GLint basevertex
)
1332 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1337 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1338 GLenum type
, const GLvoid
*indices
)
1340 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1345 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1346 GLsizei count
, GLenum type
,
1347 const GLvoid
*indices
, GLint basevertex
)
1349 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1350 indices
, basevertex
);
1355 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1356 const GLvoid
**indices
, GLsizei primcount
)
1358 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1363 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1364 const GLsizei
*count
, GLenum type
,
1365 const GLvoid
**indices
, GLsizei primcount
,
1366 const GLint
*basevertex
)
1368 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1369 primcount
, basevertex
);