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 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
301 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
], k
, j
);
305 if (_mesa_is_bufferobj(arrayObj
->ElementArrayBufferObj
)) {
306 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
309 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
310 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
316 * Check array data, looking for NaNs, etc.
319 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
326 * Print info/data for glDrawArrays(), for debugging.
329 print_draw_arrays(struct gl_context
*ctx
,
330 GLenum mode
, GLint start
, GLsizei count
)
332 struct vbo_context
*vbo
= vbo_context(ctx
);
333 struct vbo_exec_context
*exec
= &vbo
->exec
;
336 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
339 for (i
= 0; i
< 32; i
++) {
340 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
341 GLuint bufName
= bufObj
->Name
;
342 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
343 printf("attr %2d: size %d stride %d enabled %d "
344 "ptr %p Bufobj %u\n",
346 exec
->array
.inputs
[i
]->Size
,
348 /*exec->array.inputs[i]->Enabled,*/
349 exec
->array
.legacy_array
[i
]->Enabled
,
350 exec
->array
.inputs
[i
]->Ptr
,
354 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
355 GL_MAP_READ_BIT
, bufObj
);
356 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
357 float *f
= (float *) (p
+ offset
);
360 int n
= (count
* stride
) / 4;
363 printf(" Data at offset %d:\n", offset
);
364 for (i
= 0; i
< n
; i
++) {
365 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
367 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
374 * Bind the VBO executor to the current vertex array object prior
377 * Just translate the arrayobj into a sane layout.
380 bind_array_obj(struct gl_context
*ctx
)
382 struct vbo_context
*vbo
= vbo_context(ctx
);
383 struct vbo_exec_context
*exec
= &vbo
->exec
;
384 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
387 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++)
388 exec
->array
.legacy_array
[i
] = &arrayObj
->VertexAttrib
[VERT_ATTRIB_FF(i
)];
390 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
391 assert(i
< Elements(exec
->array
.generic_array
));
392 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
398 * Set the vbo->exec->inputs[] pointers to point to the enabled
399 * vertex arrays. This depends on the current vertex program/shader
400 * being executed because of whether or not generic vertex arrays
401 * alias the conventional vertex arrays.
402 * For arrays that aren't enabled, we set the input[attrib] pointer
403 * to point at a zero-stride current value "array".
406 recalculate_input_bindings(struct gl_context
*ctx
)
408 struct vbo_context
*vbo
= vbo_context(ctx
);
409 struct vbo_exec_context
*exec
= &vbo
->exec
;
410 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
411 GLbitfield64 const_inputs
= 0x0;
414 switch (get_program_mode(ctx
)) {
416 /* When no vertex program is active (or the vertex program is generated
417 * from fixed-function state). We put the material values into the
418 * generic slots. This is the only situation where material values
419 * are available as per-vertex attributes.
421 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
422 if (exec
->array
.legacy_array
[i
]->Enabled
)
423 inputs
[i
] = exec
->array
.legacy_array
[i
];
425 inputs
[i
] = &vbo
->legacy_currval
[i
];
426 const_inputs
|= VERT_BIT(i
);
430 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
431 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->mat_currval
[i
];
432 const_inputs
|= VERT_BIT_GENERIC(i
);
435 /* Could use just about anything, just to fill in the empty
438 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
439 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
440 const_inputs
|= VERT_BIT_GENERIC(i
);
443 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
444 * because it already takes care of invalidating the state necessary
445 * to revalidate vertex arrays. Not marking the state as dirty also
446 * improves performance (quite significantly in some apps).
448 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
449 ctx
->NewState
|= _NEW_ARRAY
;
453 /* NV_vertex_program - attribute arrays alias and override
454 * conventional, legacy arrays. No materials, and the generic
457 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
458 if (i
< VERT_ATTRIB_GENERIC_MAX
459 && exec
->array
.generic_array
[i
]->Enabled
)
460 inputs
[i
] = exec
->array
.generic_array
[i
];
461 else if (exec
->array
.legacy_array
[i
]->Enabled
)
462 inputs
[i
] = exec
->array
.legacy_array
[i
];
464 inputs
[i
] = &vbo
->legacy_currval
[i
];
465 const_inputs
|= VERT_BIT_FF(i
);
469 /* Could use just about anything, just to fill in the empty
472 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
473 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
474 const_inputs
|= VERT_BIT_GENERIC(i
);
477 ctx
->NewState
|= _NEW_ARRAY
;
481 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
482 * attribute array aliases and overrides the legacy position array.
484 * Otherwise, legacy attributes available in the legacy slots,
485 * generic attributes in the generic slots and materials are not
486 * available as per-vertex attributes.
488 if (exec
->array
.generic_array
[0]->Enabled
)
489 inputs
[0] = exec
->array
.generic_array
[0];
490 else if (exec
->array
.legacy_array
[0]->Enabled
)
491 inputs
[0] = exec
->array
.legacy_array
[0];
493 inputs
[0] = &vbo
->legacy_currval
[0];
494 const_inputs
|= VERT_BIT_POS
;
497 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
498 if (exec
->array
.legacy_array
[i
]->Enabled
)
499 inputs
[i
] = exec
->array
.legacy_array
[i
];
501 inputs
[i
] = &vbo
->legacy_currval
[i
];
502 const_inputs
|= VERT_BIT_FF(i
);
506 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
507 if (exec
->array
.generic_array
[i
]->Enabled
)
508 inputs
[VERT_ATTRIB_GENERIC(i
)] = exec
->array
.generic_array
[i
];
510 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
511 const_inputs
|= VERT_BIT_GENERIC(i
);
515 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
516 ctx
->NewState
|= _NEW_ARRAY
;
520 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
525 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
526 * These will point to the arrays to actually use for drawing. Some will
527 * be user-provided arrays, other will be zero-stride const-valued arrays.
528 * Note that this might set the _NEW_ARRAY dirty flag so state validation
529 * must be done after this call.
532 vbo_bind_arrays(struct gl_context
*ctx
)
534 if (!ctx
->Array
.RebindArrays
) {
539 recalculate_input_bindings(ctx
);
540 ctx
->Array
.RebindArrays
= GL_FALSE
;
545 * Helper function called by the other DrawArrays() functions below.
546 * This is where we handle primitive restart for drawing non-indexed
547 * arrays. If primitive restart is enabled, it typically means
548 * splitting one DrawArrays() into two.
551 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
552 GLsizei count
, GLuint numInstances
)
554 struct vbo_context
*vbo
= vbo_context(ctx
);
555 struct vbo_exec_context
*exec
= &vbo
->exec
;
556 struct _mesa_prim prim
[2];
558 vbo_bind_arrays(ctx
);
560 /* Again... because we may have changed the bitmask of per-vertex varying
561 * attributes. If we regenerate the fixed-function vertex program now
562 * we may be able to prune down the number of vertex attributes which we
563 * need in the shader.
566 _mesa_update_state(ctx
);
568 /* init most fields to zero */
569 memset(prim
, 0, sizeof(prim
));
573 prim
[0].num_instances
= numInstances
;
575 /* Implement the primitive restart index */
576 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
577 GLuint primCount
= 0;
579 if (ctx
->Array
.RestartIndex
== start
) {
580 /* special case: RestartIndex at beginning */
582 prim
[0].start
= start
+ 1;
583 prim
[0].count
= count
- 1;
587 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
588 /* special case: RestartIndex at end */
590 prim
[0].start
= start
;
591 prim
[0].count
= count
- 1;
596 /* general case: RestartIndex in middle, split into two prims */
597 prim
[0].start
= start
;
598 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
601 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
602 prim
[1].count
= count
- prim
[1].start
;
608 /* draw one or two prims */
609 check_buffers_are_unmapped(exec
->array
.inputs
);
610 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
611 GL_TRUE
, start
, start
+ count
- 1);
615 /* no prim restart */
616 prim
[0].start
= start
;
617 prim
[0].count
= count
;
619 check_buffers_are_unmapped(exec
->array
.inputs
);
620 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
621 GL_TRUE
, start
, start
+ count
- 1);
628 * Called from glDrawArrays when in immediate mode (not display list mode).
630 static void GLAPIENTRY
631 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
633 GET_CURRENT_CONTEXT(ctx
);
635 if (MESA_VERBOSE
& VERBOSE_DRAW
)
636 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
637 _mesa_lookup_enum_by_nr(mode
), start
, count
);
639 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
642 FLUSH_CURRENT( ctx
, 0 );
644 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
649 check_draw_arrays_data(ctx
, start
, count
);
651 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
654 print_draw_arrays(ctx
, mode
, start
, count
);
659 * Called from glDrawArraysInstanced when in immediate mode (not
660 * display list mode).
662 static void GLAPIENTRY
663 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
664 GLsizei numInstances
)
666 GET_CURRENT_CONTEXT(ctx
);
668 if (MESA_VERBOSE
& VERBOSE_DRAW
)
669 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
670 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
672 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
675 FLUSH_CURRENT( ctx
, 0 );
677 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
682 check_draw_arrays_data(ctx
, start
, count
);
684 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
687 print_draw_arrays(ctx
, mode
, start
, count
);
692 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
696 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
699 ctx
->Driver
.MapBufferRange(ctx
, 0,
700 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
702 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
704 case GL_UNSIGNED_BYTE
:
706 const GLubyte
*us
= (const GLubyte
*) map
;
708 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
; i
++) {
709 printf("%02x ", us
[i
]);
716 case GL_UNSIGNED_SHORT
:
718 const GLushort
*us
= (const GLushort
*) map
;
720 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 2; i
++) {
721 printf("%04x ", us
[i
]);
728 case GL_UNSIGNED_INT
:
730 const GLuint
*us
= (const GLuint
*) map
;
732 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 4; i
++) {
733 printf("%08x ", us
[i
]);
744 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
749 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
750 * Do the rendering for a glDrawElements or glDrawRangeElements call after
751 * we've validated buffer bounds, etc.
754 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
755 GLboolean index_bounds_valid
,
756 GLuint start
, GLuint end
,
757 GLsizei count
, GLenum type
,
758 const GLvoid
*indices
,
759 GLint basevertex
, GLint numInstances
)
761 struct vbo_context
*vbo
= vbo_context(ctx
);
762 struct vbo_exec_context
*exec
= &vbo
->exec
;
763 struct _mesa_index_buffer ib
;
764 struct _mesa_prim prim
[1];
766 FLUSH_CURRENT( ctx
, 0 );
768 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
772 vbo_bind_arrays( ctx
);
774 /* check for dirty state again */
776 _mesa_update_state( ctx
);
780 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
789 prim
[0].count
= count
;
791 prim
[0].basevertex
= basevertex
;
792 prim
[0].num_instances
= numInstances
;
794 /* Need to give special consideration to rendering a range of
795 * indices starting somewhere above zero. Typically the
796 * application is issuing multiple DrawRangeElements() to draw
797 * successive primitives layed out linearly in the vertex arrays.
798 * Unless the vertex arrays are all in a VBO (or locked as with
799 * CVA), the OpenGL semantics imply that we need to re-read or
800 * re-upload the vertex data on each draw call.
802 * In the case of hardware tnl, we want to avoid starting the
803 * upload at zero, as it will mean every draw call uploads an
804 * increasing amount of not-used vertex data. Worse - in the
805 * software tnl module, all those vertices might be transformed and
806 * lit but never rendered.
808 * If we just upload or transform the vertices in start..end,
809 * however, the indices will be incorrect.
811 * At this level, we don't know exactly what the requirements of
812 * the backend are going to be, though it will likely boil down to
815 * 1) Do nothing, everything is in a VBO and is processed once
818 * 2) Adjust the indices and vertex arrays so that start becomes
821 * Rather than doing anything here, I'll provide a helper function
822 * for the latter case elsewhere.
825 check_buffers_are_unmapped(exec
->array
.inputs
);
826 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
827 index_bounds_valid
, start
, end
);
832 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
834 static void GLAPIENTRY
835 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
836 GLuint start
, GLuint end
,
837 GLsizei count
, GLenum type
,
838 const GLvoid
*indices
,
841 static GLuint warnCount
= 0;
842 GET_CURRENT_CONTEXT(ctx
);
844 if (MESA_VERBOSE
& VERBOSE_DRAW
)
846 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
847 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
848 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
850 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
851 type
, indices
, basevertex
))
854 /* NOTE: It's important that 'end' is a reasonable value.
855 * in _tnl_draw_prims(), we use end to determine how many vertices
856 * to transform. If it's too large, we can unnecessarily split prims
857 * or we can read/write out of memory in several different places!
860 /* Catch/fix some potential user errors */
861 if (type
== GL_UNSIGNED_BYTE
) {
862 start
= MIN2(start
, 0xff);
863 end
= MIN2(end
, 0xff);
865 else if (type
== GL_UNSIGNED_SHORT
) {
866 start
= MIN2(start
, 0xffff);
867 end
= MIN2(end
, 0xffff);
870 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
871 /* the max element is out of bounds of one or more enabled arrays */
874 if (warnCount
< 10) {
875 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
876 "type 0x%x, indices=%p)\n"
877 "\tend is out of bounds (max=%u) "
878 "Element Buffer %u (size %d)\n"
879 "\tThis should probably be fixed in the application.",
880 start
, end
, count
, type
, indices
,
881 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
882 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
883 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
887 dump_element_buffer(ctx
, type
);
890 _mesa_print_arrays(ctx
);
892 /* 'end' was out of bounds, but now let's check the actual array
893 * indexes to see if any of them are out of bounds.
896 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
897 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
898 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
899 if (warnCount
< 10) {
900 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
901 "count %d, type 0x%x, indices=%p)\n"
902 "\tindex=%u is out of bounds (max=%u) "
903 "Element Buffer %u (size %d)\n"
904 "\tSkipping the glDrawRangeElements() call",
905 start
, end
, count
, type
, indices
, max
,
906 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
907 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
908 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
911 /* XXX we could also find the min index and compare to 'start'
912 * to see if start is correct. But it's more likely to get the
917 /* Set 'end' to the max possible legal value */
918 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
919 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
927 printf("glDraw[Range]Elements{,BaseVertex}"
928 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
930 start
, end
, type
, count
,
931 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
936 check_draw_elements_data(ctx
, count
, type
, indices
);
938 (void) check_draw_elements_data
;
941 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
942 count
, type
, indices
, basevertex
, 1);
947 * Called by glDrawRangeElements() in immediate mode.
949 static void GLAPIENTRY
950 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
951 GLsizei count
, GLenum type
, const GLvoid
*indices
)
953 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
954 GET_CURRENT_CONTEXT(ctx
);
956 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
957 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
958 _mesa_lookup_enum_by_nr(type
), indices
);
961 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
967 * Called by glDrawElements() in immediate mode.
969 static void GLAPIENTRY
970 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
971 const GLvoid
*indices
)
973 GET_CURRENT_CONTEXT(ctx
);
975 if (MESA_VERBOSE
& VERBOSE_DRAW
)
976 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
977 _mesa_lookup_enum_by_nr(mode
), count
,
978 _mesa_lookup_enum_by_nr(type
), indices
);
980 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
983 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
984 count
, type
, indices
, 0, 1);
989 * Called by glDrawElementsBaseVertex() in immediate mode.
991 static void GLAPIENTRY
992 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
993 const GLvoid
*indices
, GLint basevertex
)
995 GET_CURRENT_CONTEXT(ctx
);
997 if (MESA_VERBOSE
& VERBOSE_DRAW
)
998 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
999 _mesa_lookup_enum_by_nr(mode
), count
,
1000 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1002 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1006 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1007 count
, type
, indices
, basevertex
, 1);
1012 * Called by glDrawElementsInstanced() in immediate mode.
1014 static void GLAPIENTRY
1015 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1016 const GLvoid
*indices
, GLsizei numInstances
)
1018 GET_CURRENT_CONTEXT(ctx
);
1020 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1021 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1022 _mesa_lookup_enum_by_nr(mode
), count
,
1023 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1025 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1029 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1030 count
, type
, indices
, 0, numInstances
);
1034 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1036 static void GLAPIENTRY
1037 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1038 const GLvoid
*indices
, GLsizei numInstances
,
1041 GET_CURRENT_CONTEXT(ctx
);
1043 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1044 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1045 _mesa_lookup_enum_by_nr(mode
), count
,
1046 _mesa_lookup_enum_by_nr(type
), indices
,
1047 numInstances
, basevertex
);
1049 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1050 numInstances
, basevertex
))
1053 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1054 count
, type
, indices
, basevertex
, numInstances
);
1059 * Inner support for both _mesa_MultiDrawElements() and
1060 * _mesa_MultiDrawRangeElements().
1061 * This does the actual rendering after we've checked array indexes, etc.
1064 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1065 const GLsizei
*count
, GLenum type
,
1066 const GLvoid
**indices
, GLsizei primcount
,
1067 const GLint
*basevertex
)
1069 struct vbo_context
*vbo
= vbo_context(ctx
);
1070 struct vbo_exec_context
*exec
= &vbo
->exec
;
1071 struct _mesa_index_buffer ib
;
1072 struct _mesa_prim
*prim
;
1073 unsigned int index_type_size
= 0;
1074 uintptr_t min_index_ptr
, max_index_ptr
;
1075 GLboolean fallback
= GL_FALSE
;
1081 FLUSH_CURRENT( ctx
, 0 );
1083 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1087 prim
= calloc(1, primcount
* sizeof(*prim
));
1089 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1093 /* Decide if we can do this all as one set of primitives sharing the
1094 * same index buffer, or if we have to reset the index pointer per
1097 vbo_bind_arrays( ctx
);
1099 /* check for dirty state again */
1101 _mesa_update_state( ctx
);
1104 case GL_UNSIGNED_INT
:
1105 index_type_size
= 4;
1107 case GL_UNSIGNED_SHORT
:
1108 index_type_size
= 2;
1110 case GL_UNSIGNED_BYTE
:
1111 index_type_size
= 1;
1117 min_index_ptr
= (uintptr_t)indices
[0];
1119 for (i
= 0; i
< primcount
; i
++) {
1120 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1121 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1122 index_type_size
* count
[i
]);
1125 /* Check if we can handle this thing as a bunch of index offsets from the
1126 * same index pointer. If we can't, then we have to fall back to doing
1127 * a draw_prims per primitive.
1128 * Check that the difference between each prim's indexes is a multiple of
1129 * the index/element size.
1131 if (index_type_size
!= 1) {
1132 for (i
= 0; i
< primcount
; i
++) {
1133 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1140 /* If the index buffer isn't in a VBO, then treating the application's
1141 * subranges of the index buffer as one large index buffer may lead to
1142 * us reading unmapped memory.
1144 if (!_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
))
1148 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1150 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1151 ib
.ptr
= (void *)min_index_ptr
;
1153 for (i
= 0; i
< primcount
; i
++) {
1154 prim
[i
].begin
= (i
== 0);
1155 prim
[i
].end
= (i
== primcount
- 1);
1158 prim
[i
].mode
= mode
;
1159 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1160 prim
[i
].count
= count
[i
];
1161 prim
[i
].indexed
= 1;
1162 prim
[i
].num_instances
= 1;
1163 if (basevertex
!= NULL
)
1164 prim
[i
].basevertex
= basevertex
[i
];
1166 prim
[i
].basevertex
= 0;
1169 check_buffers_are_unmapped(exec
->array
.inputs
);
1170 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1173 /* render one prim at a time */
1174 for (i
= 0; i
< primcount
; i
++) {
1175 ib
.count
= count
[i
];
1177 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1178 ib
.ptr
= indices
[i
];
1184 prim
[0].mode
= mode
;
1186 prim
[0].count
= count
[i
];
1187 prim
[0].indexed
= 1;
1188 prim
[0].num_instances
= 1;
1189 if (basevertex
!= NULL
)
1190 prim
[0].basevertex
= basevertex
[i
];
1192 prim
[0].basevertex
= 0;
1194 check_buffers_are_unmapped(exec
->array
.inputs
);
1195 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1204 static void GLAPIENTRY
1205 vbo_exec_MultiDrawElements(GLenum mode
,
1206 const GLsizei
*count
, GLenum type
,
1207 const GLvoid
**indices
,
1210 GET_CURRENT_CONTEXT(ctx
);
1213 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1215 for (i
= 0; i
< primcount
; i
++) {
1216 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1221 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1226 static void GLAPIENTRY
1227 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1228 const GLsizei
*count
, GLenum type
,
1229 const GLvoid
**indices
,
1231 const GLsizei
*basevertex
)
1233 GET_CURRENT_CONTEXT(ctx
);
1236 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1238 for (i
= 0; i
< primcount
; i
++) {
1239 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1244 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1250 * Plug in the immediate-mode vertex array drawing commands into the
1251 * givven vbo_exec_context object.
1254 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1256 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1257 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1258 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1259 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1260 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1261 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1262 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1263 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1264 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1265 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1270 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1278 * The following functions are only used for OpenGL ES 1/2 support.
1279 * And some aren't even supported (yet) in ES 1/2.
1284 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1286 vbo_exec_DrawArrays(mode
, first
, count
);
1291 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1292 const GLvoid
*indices
)
1294 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1299 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1300 const GLvoid
*indices
, GLint basevertex
)
1302 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1307 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1308 GLenum type
, const GLvoid
*indices
)
1310 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1315 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1316 GLsizei count
, GLenum type
,
1317 const GLvoid
*indices
, GLint basevertex
)
1319 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1320 indices
, basevertex
);
1325 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1326 const GLvoid
**indices
, GLsizei primcount
)
1328 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1333 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1334 const GLsizei
*count
, GLenum type
,
1335 const GLvoid
**indices
, GLsizei primcount
,
1336 const GLint
*basevertex
)
1338 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1339 primcount
, basevertex
);