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
.ArrayObj
->ElementArrayBufferObj
)) {
274 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
275 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
277 ctx
->Array
.ArrayObj
->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(arrayObj
->ElementArrayBufferObj
)) {
314 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->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 GLbitfield64 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_FF_MAX
; 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
|= VERT_BIT(i
);
461 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
462 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->mat_currval
[i
];
463 const_inputs
|= VERT_BIT_GENERIC(i
);
466 /* Could use just about anything, just to fill in the empty
469 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
470 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
471 const_inputs
|= VERT_BIT_GENERIC(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_FF_MAX
; i
++) {
489 if (i
< VERT_ATTRIB_GENERIC_MAX
490 && exec
->array
.generic_array
[i
]->Enabled
)
491 inputs
[i
] = exec
->array
.generic_array
[i
];
492 else if (exec
->array
.legacy_array
[i
]->Enabled
)
493 inputs
[i
] = exec
->array
.legacy_array
[i
];
495 inputs
[i
] = &vbo
->legacy_currval
[i
];
496 const_inputs
|= VERT_BIT_FF(i
);
500 /* Could use just about anything, just to fill in the empty
503 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
504 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
505 const_inputs
|= VERT_BIT_GENERIC(i
);
508 ctx
->NewState
|= _NEW_ARRAY
;
512 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
513 * attribute array aliases and overrides the legacy position array.
515 * Otherwise, legacy attributes available in the legacy slots,
516 * generic attributes in the generic slots and materials are not
517 * available as per-vertex attributes.
519 if (exec
->array
.generic_array
[0]->Enabled
)
520 inputs
[0] = exec
->array
.generic_array
[0];
521 else if (exec
->array
.legacy_array
[0]->Enabled
)
522 inputs
[0] = exec
->array
.legacy_array
[0];
524 inputs
[0] = &vbo
->legacy_currval
[0];
525 const_inputs
|= VERT_BIT_POS
;
528 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
529 if (exec
->array
.legacy_array
[i
]->Enabled
)
530 inputs
[i
] = exec
->array
.legacy_array
[i
];
532 inputs
[i
] = &vbo
->legacy_currval
[i
];
533 const_inputs
|= VERT_BIT_FF(i
);
537 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
538 if (exec
->array
.generic_array
[i
]->Enabled
)
539 inputs
[VERT_ATTRIB_GENERIC(i
)] = exec
->array
.generic_array
[i
];
541 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
542 const_inputs
|= VERT_BIT_GENERIC(i
);
546 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
547 ctx
->NewState
|= _NEW_ARRAY
;
551 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
556 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
557 * These will point to the arrays to actually use for drawing. Some will
558 * be user-provided arrays, other will be zero-stride const-valued arrays.
559 * Note that this might set the _NEW_ARRAY dirty flag so state validation
560 * must be done after this call.
563 vbo_bind_arrays(struct gl_context
*ctx
)
565 if (!ctx
->Array
.RebindArrays
) {
570 recalculate_input_bindings(ctx
);
571 ctx
->Array
.RebindArrays
= GL_FALSE
;
576 * Helper function called by the other DrawArrays() functions below.
577 * This is where we handle primitive restart for drawing non-indexed
578 * arrays. If primitive restart is enabled, it typically means
579 * splitting one DrawArrays() into two.
582 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
583 GLsizei count
, GLuint numInstances
)
585 struct vbo_context
*vbo
= vbo_context(ctx
);
586 struct vbo_exec_context
*exec
= &vbo
->exec
;
587 struct _mesa_prim prim
[2];
589 vbo_bind_arrays(ctx
);
591 /* Again... because we may have changed the bitmask of per-vertex varying
592 * attributes. If we regenerate the fixed-function vertex program now
593 * we may be able to prune down the number of vertex attributes which we
594 * need in the shader.
597 _mesa_update_state(ctx
);
599 /* init most fields to zero */
600 memset(prim
, 0, sizeof(prim
));
604 prim
[0].num_instances
= numInstances
;
606 /* Implement the primitive restart index */
607 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
608 GLuint primCount
= 0;
610 if (ctx
->Array
.RestartIndex
== start
) {
611 /* special case: RestartIndex at beginning */
613 prim
[0].start
= start
+ 1;
614 prim
[0].count
= count
- 1;
618 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
619 /* special case: RestartIndex at end */
621 prim
[0].start
= start
;
622 prim
[0].count
= count
- 1;
627 /* general case: RestartIndex in middle, split into two prims */
628 prim
[0].start
= start
;
629 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
632 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
633 prim
[1].count
= count
- prim
[1].start
;
639 /* draw one or two prims */
640 check_buffers_are_unmapped(exec
->array
.inputs
);
641 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
642 GL_TRUE
, start
, start
+ count
- 1);
646 /* no prim restart */
647 prim
[0].start
= start
;
648 prim
[0].count
= count
;
650 check_buffers_are_unmapped(exec
->array
.inputs
);
651 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
652 GL_TRUE
, start
, start
+ count
- 1);
659 * Called from glDrawArrays when in immediate mode (not display list mode).
661 static void GLAPIENTRY
662 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
664 GET_CURRENT_CONTEXT(ctx
);
666 if (MESA_VERBOSE
& VERBOSE_DRAW
)
667 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
668 _mesa_lookup_enum_by_nr(mode
), start
, count
);
670 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
673 FLUSH_CURRENT( ctx
, 0 );
675 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
680 check_draw_arrays_data(ctx
, start
, count
);
682 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
685 print_draw_arrays(ctx
, mode
, start
, count
);
690 * Called from glDrawArraysInstanced when in immediate mode (not
691 * display list mode).
693 static void GLAPIENTRY
694 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
695 GLsizei numInstances
)
697 GET_CURRENT_CONTEXT(ctx
);
699 if (MESA_VERBOSE
& VERBOSE_DRAW
)
700 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
701 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
703 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
706 FLUSH_CURRENT( ctx
, 0 );
708 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
713 check_draw_arrays_data(ctx
, start
, count
);
715 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
718 print_draw_arrays(ctx
, mode
, start
, count
);
723 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
727 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
730 ctx
->Driver
.MapBufferRange(ctx
, 0,
731 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
733 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
735 case GL_UNSIGNED_BYTE
:
737 const GLubyte
*us
= (const GLubyte
*) map
;
739 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
; i
++) {
740 printf("%02x ", us
[i
]);
747 case GL_UNSIGNED_SHORT
:
749 const GLushort
*us
= (const GLushort
*) map
;
751 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 2; i
++) {
752 printf("%04x ", us
[i
]);
759 case GL_UNSIGNED_INT
:
761 const GLuint
*us
= (const GLuint
*) map
;
763 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 4; i
++) {
764 printf("%08x ", us
[i
]);
775 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
780 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
781 * Do the rendering for a glDrawElements or glDrawRangeElements call after
782 * we've validated buffer bounds, etc.
785 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
786 GLboolean index_bounds_valid
,
787 GLuint start
, GLuint end
,
788 GLsizei count
, GLenum type
,
789 const GLvoid
*indices
,
790 GLint basevertex
, GLint numInstances
)
792 struct vbo_context
*vbo
= vbo_context(ctx
);
793 struct vbo_exec_context
*exec
= &vbo
->exec
;
794 struct _mesa_index_buffer ib
;
795 struct _mesa_prim prim
[1];
797 FLUSH_CURRENT( ctx
, 0 );
799 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
803 vbo_bind_arrays( ctx
);
805 /* check for dirty state again */
807 _mesa_update_state( ctx
);
811 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
820 prim
[0].count
= count
;
822 prim
[0].basevertex
= basevertex
;
823 prim
[0].num_instances
= numInstances
;
825 /* Need to give special consideration to rendering a range of
826 * indices starting somewhere above zero. Typically the
827 * application is issuing multiple DrawRangeElements() to draw
828 * successive primitives layed out linearly in the vertex arrays.
829 * Unless the vertex arrays are all in a VBO (or locked as with
830 * CVA), the OpenGL semantics imply that we need to re-read or
831 * re-upload the vertex data on each draw call.
833 * In the case of hardware tnl, we want to avoid starting the
834 * upload at zero, as it will mean every draw call uploads an
835 * increasing amount of not-used vertex data. Worse - in the
836 * software tnl module, all those vertices might be transformed and
837 * lit but never rendered.
839 * If we just upload or transform the vertices in start..end,
840 * however, the indices will be incorrect.
842 * At this level, we don't know exactly what the requirements of
843 * the backend are going to be, though it will likely boil down to
846 * 1) Do nothing, everything is in a VBO and is processed once
849 * 2) Adjust the indices and vertex arrays so that start becomes
852 * Rather than doing anything here, I'll provide a helper function
853 * for the latter case elsewhere.
856 check_buffers_are_unmapped(exec
->array
.inputs
);
857 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
858 index_bounds_valid
, start
, end
);
863 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
865 static void GLAPIENTRY
866 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
867 GLuint start
, GLuint end
,
868 GLsizei count
, GLenum type
,
869 const GLvoid
*indices
,
872 static GLuint warnCount
= 0;
873 GET_CURRENT_CONTEXT(ctx
);
875 if (MESA_VERBOSE
& VERBOSE_DRAW
)
877 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
878 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
879 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
881 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
882 type
, indices
, basevertex
))
885 /* NOTE: It's important that 'end' is a reasonable value.
886 * in _tnl_draw_prims(), we use end to determine how many vertices
887 * to transform. If it's too large, we can unnecessarily split prims
888 * or we can read/write out of memory in several different places!
891 /* Catch/fix some potential user errors */
892 if (type
== GL_UNSIGNED_BYTE
) {
893 start
= MIN2(start
, 0xff);
894 end
= MIN2(end
, 0xff);
896 else if (type
== GL_UNSIGNED_SHORT
) {
897 start
= MIN2(start
, 0xffff);
898 end
= MIN2(end
, 0xffff);
901 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
902 /* the max element is out of bounds of one or more enabled arrays */
905 if (warnCount
< 10) {
906 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
907 "type 0x%x, indices=%p)\n"
908 "\tend is out of bounds (max=%u) "
909 "Element Buffer %u (size %d)\n"
910 "\tThis should probably be fixed in the application.",
911 start
, end
, count
, type
, indices
,
912 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
913 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
914 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
918 dump_element_buffer(ctx
, type
);
921 _mesa_print_arrays(ctx
);
923 /* 'end' was out of bounds, but now let's check the actual array
924 * indexes to see if any of them are out of bounds.
927 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
928 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
929 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
930 if (warnCount
< 10) {
931 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
932 "count %d, type 0x%x, indices=%p)\n"
933 "\tindex=%u is out of bounds (max=%u) "
934 "Element Buffer %u (size %d)\n"
935 "\tSkipping the glDrawRangeElements() call",
936 start
, end
, count
, type
, indices
, max
,
937 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
938 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
939 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
942 /* XXX we could also find the min index and compare to 'start'
943 * to see if start is correct. But it's more likely to get the
948 /* Set 'end' to the max possible legal value */
949 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
950 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
958 printf("glDraw[Range]Elements{,BaseVertex}"
959 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
961 start
, end
, type
, count
,
962 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
967 check_draw_elements_data(ctx
, count
, type
, indices
);
969 (void) check_draw_elements_data
;
972 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
973 count
, type
, indices
, basevertex
, 1);
978 * Called by glDrawRangeElements() in immediate mode.
980 static void GLAPIENTRY
981 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
982 GLsizei count
, GLenum type
, const GLvoid
*indices
)
984 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
985 GET_CURRENT_CONTEXT(ctx
);
987 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
988 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
989 _mesa_lookup_enum_by_nr(type
), indices
);
992 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
998 * Called by glDrawElements() in immediate mode.
1000 static void GLAPIENTRY
1001 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1002 const GLvoid
*indices
)
1004 GET_CURRENT_CONTEXT(ctx
);
1006 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1007 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
1008 _mesa_lookup_enum_by_nr(mode
), count
,
1009 _mesa_lookup_enum_by_nr(type
), indices
);
1011 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
1014 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1015 count
, type
, indices
, 0, 1);
1020 * Called by glDrawElementsBaseVertex() in immediate mode.
1022 static void GLAPIENTRY
1023 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1024 const GLvoid
*indices
, GLint basevertex
)
1026 GET_CURRENT_CONTEXT(ctx
);
1028 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1029 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1030 _mesa_lookup_enum_by_nr(mode
), count
,
1031 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1033 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1037 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1038 count
, type
, indices
, basevertex
, 1);
1043 * Called by glDrawElementsInstanced() in immediate mode.
1045 static void GLAPIENTRY
1046 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1047 const GLvoid
*indices
, GLsizei numInstances
)
1049 GET_CURRENT_CONTEXT(ctx
);
1051 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1052 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1053 _mesa_lookup_enum_by_nr(mode
), count
,
1054 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1056 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1060 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1061 count
, type
, indices
, 0, numInstances
);
1065 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1067 static void GLAPIENTRY
1068 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1069 const GLvoid
*indices
, GLsizei numInstances
,
1072 GET_CURRENT_CONTEXT(ctx
);
1074 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1075 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1076 _mesa_lookup_enum_by_nr(mode
), count
,
1077 _mesa_lookup_enum_by_nr(type
), indices
,
1078 numInstances
, basevertex
);
1080 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1081 numInstances
, basevertex
))
1084 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1085 count
, type
, indices
, basevertex
, numInstances
);
1090 * Inner support for both _mesa_MultiDrawElements() and
1091 * _mesa_MultiDrawRangeElements().
1092 * This does the actual rendering after we've checked array indexes, etc.
1095 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1096 const GLsizei
*count
, GLenum type
,
1097 const GLvoid
**indices
, GLsizei primcount
,
1098 const GLint
*basevertex
)
1100 struct vbo_context
*vbo
= vbo_context(ctx
);
1101 struct vbo_exec_context
*exec
= &vbo
->exec
;
1102 struct _mesa_index_buffer ib
;
1103 struct _mesa_prim
*prim
;
1104 unsigned int index_type_size
= 0;
1105 uintptr_t min_index_ptr
, max_index_ptr
;
1106 GLboolean fallback
= GL_FALSE
;
1112 FLUSH_CURRENT( ctx
, 0 );
1114 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1118 prim
= calloc(1, primcount
* sizeof(*prim
));
1120 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1124 /* Decide if we can do this all as one set of primitives sharing the
1125 * same index buffer, or if we have to reset the index pointer per
1128 vbo_bind_arrays( ctx
);
1130 /* check for dirty state again */
1132 _mesa_update_state( ctx
);
1135 case GL_UNSIGNED_INT
:
1136 index_type_size
= 4;
1138 case GL_UNSIGNED_SHORT
:
1139 index_type_size
= 2;
1141 case GL_UNSIGNED_BYTE
:
1142 index_type_size
= 1;
1148 min_index_ptr
= (uintptr_t)indices
[0];
1150 for (i
= 0; i
< primcount
; i
++) {
1151 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1152 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1153 index_type_size
* count
[i
]);
1156 /* Check if we can handle this thing as a bunch of index offsets from the
1157 * same index pointer. If we can't, then we have to fall back to doing
1158 * a draw_prims per primitive.
1159 * Check that the difference between each prim's indexes is a multiple of
1160 * the index/element size.
1162 if (index_type_size
!= 1) {
1163 for (i
= 0; i
< primcount
; i
++) {
1164 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1171 /* If the index buffer isn't in a VBO, then treating the application's
1172 * subranges of the index buffer as one large index buffer may lead to
1173 * us reading unmapped memory.
1175 if (!_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
))
1179 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1181 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1182 ib
.ptr
= (void *)min_index_ptr
;
1184 for (i
= 0; i
< primcount
; i
++) {
1185 prim
[i
].begin
= (i
== 0);
1186 prim
[i
].end
= (i
== primcount
- 1);
1189 prim
[i
].mode
= mode
;
1190 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1191 prim
[i
].count
= count
[i
];
1192 prim
[i
].indexed
= 1;
1193 prim
[i
].num_instances
= 1;
1194 if (basevertex
!= NULL
)
1195 prim
[i
].basevertex
= basevertex
[i
];
1197 prim
[i
].basevertex
= 0;
1200 check_buffers_are_unmapped(exec
->array
.inputs
);
1201 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1204 /* render one prim at a time */
1205 for (i
= 0; i
< primcount
; i
++) {
1206 ib
.count
= count
[i
];
1208 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1209 ib
.ptr
= indices
[i
];
1215 prim
[0].mode
= mode
;
1217 prim
[0].count
= count
[i
];
1218 prim
[0].indexed
= 1;
1219 prim
[0].num_instances
= 1;
1220 if (basevertex
!= NULL
)
1221 prim
[0].basevertex
= basevertex
[i
];
1223 prim
[0].basevertex
= 0;
1225 check_buffers_are_unmapped(exec
->array
.inputs
);
1226 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1235 static void GLAPIENTRY
1236 vbo_exec_MultiDrawElements(GLenum mode
,
1237 const GLsizei
*count
, GLenum type
,
1238 const GLvoid
**indices
,
1241 GET_CURRENT_CONTEXT(ctx
);
1244 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1246 for (i
= 0; i
< primcount
; i
++) {
1247 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1252 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1257 static void GLAPIENTRY
1258 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1259 const GLsizei
*count
, GLenum type
,
1260 const GLvoid
**indices
,
1262 const GLsizei
*basevertex
)
1264 GET_CURRENT_CONTEXT(ctx
);
1267 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1269 for (i
= 0; i
< primcount
; i
++) {
1270 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1275 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1281 * Plug in the immediate-mode vertex array drawing commands into the
1282 * givven vbo_exec_context object.
1285 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1287 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1288 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1289 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1290 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1291 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1292 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1293 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1294 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1295 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1296 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1301 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1309 * The following functions are only used for OpenGL ES 1/2 support.
1310 * And some aren't even supported (yet) in ES 1/2.
1315 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1317 vbo_exec_DrawArrays(mode
, first
, count
);
1322 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1323 const GLvoid
*indices
)
1325 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1330 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1331 const GLvoid
*indices
, GLint basevertex
)
1333 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1338 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1339 GLenum type
, const GLvoid
*indices
)
1341 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1346 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1347 GLsizei count
, GLenum type
,
1348 const GLvoid
*indices
, GLint basevertex
)
1350 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1351 indices
, basevertex
);
1356 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1357 const GLvoid
**indices
, GLsizei primcount
)
1359 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1364 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1365 const GLsizei
*count
, GLenum type
,
1366 const GLvoid
**indices
, GLsizei primcount
,
1367 const GLint
*basevertex
)
1369 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1370 primcount
, basevertex
);