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
));
78 vbo_sizeof_ib_type(GLenum type
)
82 return sizeof(GLuint
);
83 case GL_UNSIGNED_SHORT
:
84 return sizeof(GLushort
);
85 case GL_UNSIGNED_BYTE
:
86 return sizeof(GLubyte
);
88 assert(!"unsupported index data type");
89 /* In case assert is turned off */
96 * Compute min and max elements by scanning the index buffer for
97 * glDraw[Range]Elements() calls.
98 * If primitive restart is enabled, we need to ignore restart
99 * indexes when computing min/max.
102 vbo_get_minmax_index(struct gl_context
*ctx
,
103 const struct _mesa_prim
*prim
,
104 const struct _mesa_index_buffer
*ib
,
105 GLuint
*min_index
, GLuint
*max_index
)
107 const GLuint count
= prim
->count
;
111 if (_mesa_is_bufferobj(ib
->obj
)) {
112 indices
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
113 count
* vbo_sizeof_ib_type(ib
->type
),
114 GL_MAP_READ_BIT
, ib
->obj
);
120 case GL_UNSIGNED_INT
: {
121 const GLuint
*ui_indices
= (const GLuint
*)indices
;
124 for (i
= 0; i
< count
; i
++) {
125 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
126 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
132 case GL_UNSIGNED_SHORT
: {
133 const GLushort
*us_indices
= (const GLushort
*)indices
;
136 for (i
= 0; i
< count
; i
++) {
137 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
138 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
144 case GL_UNSIGNED_BYTE
: {
145 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
148 for (i
= 0; i
< count
; i
++) {
149 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
150 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
161 if (_mesa_is_bufferobj(ib
->obj
)) {
162 ctx
->Driver
.UnmapBuffer(ctx
, ib
->obj
);
168 * Check that element 'j' of the array has reasonable data.
170 * For debugging purposes; not normally used.
173 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
174 GLuint attrib
, GLuint j
)
176 if (array
->Enabled
) {
177 const void *data
= array
->Ptr
;
178 if (_mesa_is_bufferobj(array
->BufferObj
)) {
179 if (!array
->BufferObj
->Pointer
) {
180 /* need to map now */
181 array
->BufferObj
->Pointer
=
182 ctx
->Driver
.MapBufferRange(ctx
, 0, array
->BufferObj
->Size
,
183 GL_MAP_READ_BIT
, array
->BufferObj
);
185 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
187 switch (array
->Type
) {
190 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
192 for (k
= 0; k
< array
->Size
; k
++) {
193 if (IS_INF_OR_NAN(f
[k
]) ||
194 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
195 printf("Bad array data:\n");
196 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
197 printf(" Array %u at %p\n", attrib
, (void* ) array
);
198 printf(" Type 0x%x, Size %d, Stride %d\n",
199 array
->Type
, array
->Size
, array
->Stride
);
200 printf(" Address/offset %p in Buffer Object %u\n",
201 array
->Ptr
, array
->BufferObj
->Name
);
202 f
[k
] = 1.0; /* XXX replace the bad value! */
204 /*assert(!IS_INF_OR_NAN(f[k]));*/
216 * Unmap the buffer object referenced by given array, if mapped.
219 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
221 if (array
->Enabled
&&
222 _mesa_is_bufferobj(array
->BufferObj
) &&
223 _mesa_bufferobj_mapped(array
->BufferObj
)) {
224 ctx
->Driver
.UnmapBuffer(ctx
, array
->BufferObj
);
230 * Examine the array's data for NaNs, etc.
231 * For debug purposes; not normally used.
234 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
235 const void *elements
, GLint basevertex
)
237 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
241 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
242 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
243 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
245 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
246 elements
= ADD_POINTERS(elements
, elemMap
);
249 for (i
= 0; i
< count
; i
++) {
254 case GL_UNSIGNED_BYTE
:
255 j
= ((const GLubyte
*) elements
)[i
];
257 case GL_UNSIGNED_SHORT
:
258 j
= ((const GLushort
*) elements
)[i
];
260 case GL_UNSIGNED_INT
:
261 j
= ((const GLuint
*) elements
)[i
];
267 /* check element j of each enabled array */
268 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
269 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
], k
, j
);
273 if (_mesa_is_bufferobj(arrayObj
->ElementArrayBufferObj
)) {
274 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
277 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
278 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
284 * Check array data, looking for NaNs, etc.
287 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
294 * Print info/data for glDrawArrays(), for debugging.
297 print_draw_arrays(struct gl_context
*ctx
,
298 GLenum mode
, GLint start
, GLsizei count
)
300 struct vbo_context
*vbo
= vbo_context(ctx
);
301 struct vbo_exec_context
*exec
= &vbo
->exec
;
302 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
305 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
308 for (i
= 0; i
< 32; i
++) {
309 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
310 GLuint bufName
= bufObj
->Name
;
311 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
312 printf("attr %2d: size %d stride %d enabled %d "
313 "ptr %p Bufobj %u\n",
315 exec
->array
.inputs
[i
]->Size
,
317 /*exec->array.inputs[i]->Enabled,*/
318 arrayObj
->VertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
,
319 exec
->array
.inputs
[i
]->Ptr
,
323 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
324 GL_MAP_READ_BIT
, bufObj
);
325 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
326 float *f
= (float *) (p
+ offset
);
329 int n
= (count
* stride
) / 4;
332 printf(" Data at offset %d:\n", offset
);
333 for (i
= 0; i
< n
; i
++) {
334 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
336 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
343 * Set the vbo->exec->inputs[] pointers to point to the enabled
344 * vertex arrays. This depends on the current vertex program/shader
345 * being executed because of whether or not generic vertex arrays
346 * alias the conventional vertex arrays.
347 * For arrays that aren't enabled, we set the input[attrib] pointer
348 * to point at a zero-stride current value "array".
351 recalculate_input_bindings(struct gl_context
*ctx
)
353 struct vbo_context
*vbo
= vbo_context(ctx
);
354 struct vbo_exec_context
*exec
= &vbo
->exec
;
355 struct gl_client_array
*vertexAttrib
= ctx
->Array
.ArrayObj
->VertexAttrib
;
356 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
357 GLbitfield64 const_inputs
= 0x0;
360 switch (get_program_mode(ctx
)) {
362 /* When no vertex program is active (or the vertex program is generated
363 * from fixed-function state). We put the material values into the
364 * generic slots. This is the only situation where material values
365 * are available as per-vertex attributes.
367 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
368 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
369 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
371 inputs
[i
] = &vbo
->legacy_currval
[i
];
372 const_inputs
|= VERT_BIT(i
);
376 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
377 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->mat_currval
[i
];
378 const_inputs
|= VERT_BIT_GENERIC(i
);
381 /* Could use just about anything, just to fill in the empty
384 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
385 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
386 const_inputs
|= VERT_BIT_GENERIC(i
);
389 ctx
->NewState
|= _NEW_ARRAY
;
393 /* NV_vertex_program - attribute arrays alias and override
394 * conventional, legacy arrays. No materials, and the generic
397 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
398 if (i
< VERT_ATTRIB_GENERIC_MAX
399 && vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
400 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
401 else if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
402 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
404 inputs
[i
] = &vbo
->legacy_currval
[i
];
405 const_inputs
|= VERT_BIT_FF(i
);
409 /* Could use just about anything, just to fill in the empty
412 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
413 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
414 const_inputs
|= VERT_BIT_GENERIC(i
);
417 ctx
->NewState
|= _NEW_ARRAY
;
421 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
422 * attribute array aliases and overrides the legacy position array.
424 * Otherwise, legacy attributes available in the legacy slots,
425 * generic attributes in the generic slots and materials are not
426 * available as per-vertex attributes.
428 if (vertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
)
429 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
430 else if (vertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
431 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
433 inputs
[0] = &vbo
->legacy_currval
[0];
434 const_inputs
|= VERT_BIT_POS
;
437 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
438 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
439 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
441 inputs
[i
] = &vbo
->legacy_currval
[i
];
442 const_inputs
|= VERT_BIT_FF(i
);
446 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
447 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
448 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
450 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
451 const_inputs
|= VERT_BIT_GENERIC(i
);
455 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
456 ctx
->NewState
|= _NEW_ARRAY
;
460 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
465 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
466 * These will point to the arrays to actually use for drawing. Some will
467 * be user-provided arrays, other will be zero-stride const-valued arrays.
468 * Note that this might set the _NEW_ARRAY dirty flag so state validation
469 * must be done after this call.
472 vbo_bind_arrays(struct gl_context
*ctx
)
474 if (!ctx
->Array
.RebindArrays
) {
478 recalculate_input_bindings(ctx
);
479 ctx
->Array
.RebindArrays
= GL_FALSE
;
484 * Helper function called by the other DrawArrays() functions below.
485 * This is where we handle primitive restart for drawing non-indexed
486 * arrays. If primitive restart is enabled, it typically means
487 * splitting one DrawArrays() into two.
490 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
491 GLsizei count
, GLuint numInstances
)
493 struct vbo_context
*vbo
= vbo_context(ctx
);
494 struct vbo_exec_context
*exec
= &vbo
->exec
;
495 struct _mesa_prim prim
[2];
497 vbo_bind_arrays(ctx
);
499 vbo_draw_method(exec
, DRAW_ARRAYS
);
501 /* Again... because we may have changed the bitmask of per-vertex varying
502 * attributes. If we regenerate the fixed-function vertex program now
503 * we may be able to prune down the number of vertex attributes which we
504 * need in the shader.
507 _mesa_update_state(ctx
);
509 /* init most fields to zero */
510 memset(prim
, 0, sizeof(prim
));
514 prim
[0].num_instances
= numInstances
;
516 /* no prim restart */
517 prim
[0].start
= start
;
518 prim
[0].count
= count
;
520 check_buffers_are_unmapped(exec
->array
.inputs
);
521 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
522 GL_TRUE
, start
, start
+ count
- 1);
528 * Called from glDrawArrays when in immediate mode (not display list mode).
530 static void GLAPIENTRY
531 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
533 GET_CURRENT_CONTEXT(ctx
);
535 if (MESA_VERBOSE
& VERBOSE_DRAW
)
536 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
537 _mesa_lookup_enum_by_nr(mode
), start
, count
);
539 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
542 FLUSH_CURRENT( ctx
, 0 );
544 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
549 check_draw_arrays_data(ctx
, start
, count
);
551 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
554 print_draw_arrays(ctx
, mode
, start
, count
);
559 * Called from glDrawArraysInstanced when in immediate mode (not
560 * display list mode).
562 static void GLAPIENTRY
563 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
564 GLsizei numInstances
)
566 GET_CURRENT_CONTEXT(ctx
);
568 if (MESA_VERBOSE
& VERBOSE_DRAW
)
569 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
570 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
572 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
575 FLUSH_CURRENT( ctx
, 0 );
577 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
582 check_draw_arrays_data(ctx
, start
, count
);
584 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
587 print_draw_arrays(ctx
, mode
, start
, count
);
592 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
597 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
600 ctx
->Driver
.MapBufferRange(ctx
, 0,
601 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
603 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
605 case GL_UNSIGNED_BYTE
:
607 const GLubyte
*us
= (const GLubyte
*) map
;
609 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
; i
++) {
610 printf("%02x ", us
[i
]);
617 case GL_UNSIGNED_SHORT
:
619 const GLushort
*us
= (const GLushort
*) map
;
621 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 2; i
++) {
622 printf("%04x ", us
[i
]);
629 case GL_UNSIGNED_INT
:
631 const GLuint
*us
= (const GLuint
*) map
;
633 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 4; i
++) {
634 printf("%08x ", us
[i
]);
645 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
651 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
652 * Do the rendering for a glDrawElements or glDrawRangeElements call after
653 * we've validated buffer bounds, etc.
656 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
657 GLboolean index_bounds_valid
,
658 GLuint start
, GLuint end
,
659 GLsizei count
, GLenum type
,
660 const GLvoid
*indices
, GLint numInstances
)
662 struct vbo_context
*vbo
= vbo_context(ctx
);
663 struct vbo_exec_context
*exec
= &vbo
->exec
;
664 struct _mesa_index_buffer ib
;
665 struct _mesa_prim prim
[1];
667 FLUSH_CURRENT( ctx
, 0 );
669 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
673 vbo_bind_arrays( ctx
);
675 vbo_draw_method(exec
, DRAW_ARRAYS
);
677 /* check for dirty state again */
679 _mesa_update_state( ctx
);
683 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
692 prim
[0].count
= count
;
694 prim
[0].num_instances
= numInstances
;
696 /* Need to give special consideration to rendering a range of
697 * indices starting somewhere above zero. Typically the
698 * application is issuing multiple DrawRangeElements() to draw
699 * successive primitives layed out linearly in the vertex arrays.
700 * Unless the vertex arrays are all in a VBO (or locked as with
701 * CVA), the OpenGL semantics imply that we need to re-read or
702 * re-upload the vertex data on each draw call.
704 * In the case of hardware tnl, we want to avoid starting the
705 * upload at zero, as it will mean every draw call uploads an
706 * increasing amount of not-used vertex data. Worse - in the
707 * software tnl module, all those vertices might be transformed and
708 * lit but never rendered.
710 * If we just upload or transform the vertices in start..end,
711 * however, the indices will be incorrect.
713 * At this level, we don't know exactly what the requirements of
714 * the backend are going to be, though it will likely boil down to
717 * 1) Do nothing, everything is in a VBO and is processed once
720 * 2) Adjust the indices and vertex arrays so that start becomes
723 * Rather than doing anything here, I'll provide a helper function
724 * for the latter case elsewhere.
727 check_buffers_are_unmapped(exec
->array
.inputs
);
728 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
729 index_bounds_valid
, start
, end
);
733 * Called by glDrawRangeElements() in immediate mode.
735 static void GLAPIENTRY
736 vbo_exec_DrawRangeElements(GLenum mode
,
737 GLuint start
, GLuint end
,
738 GLsizei count
, GLenum type
,
739 const GLvoid
*indices
)
741 static GLuint warnCount
= 0;
742 GLboolean index_bounds_valid
= GL_TRUE
;
743 GET_CURRENT_CONTEXT(ctx
);
745 if (MESA_VERBOSE
& VERBOSE_DRAW
)
747 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
748 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
749 _mesa_lookup_enum_by_nr(type
), indices
);
751 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
756 end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
757 /* The application requested we draw using a range of indices that's
758 * outside the bounds of the current VBO. This is invalid and appears
759 * to give undefined results. The safest thing to do is to simply
760 * ignore the range, in case the application botched their range tracking
761 * but did provide valid indices. Also issue a warning indicating that
762 * the application is broken.
764 if (warnCount
++ < 10) {
765 _mesa_warning(ctx
, "glDrawRangeElements(start %u, end %u, "
766 "count %d, type 0x%x, indices=%p):\n"
767 "\trange is outside VBO bounds (max=%u); ignoring.\n"
768 "\tThis should be fixed in the application.",
769 start
, end
, count
, type
, indices
,
770 ctx
->Array
.ArrayObj
->_MaxElement
- 1);
772 index_bounds_valid
= GL_FALSE
;
775 /* NOTE: It's important that 'end' is a reasonable value.
776 * in _tnl_draw_prims(), we use end to determine how many vertices
777 * to transform. If it's too large, we can unnecessarily split prims
778 * or we can read/write out of memory in several different places!
781 /* Catch/fix some potential user errors */
782 if (type
== GL_UNSIGNED_BYTE
) {
783 start
= MIN2(start
, 0xff);
784 end
= MIN2(end
, 0xff);
786 else if (type
== GL_UNSIGNED_SHORT
) {
787 start
= MIN2(start
, 0xffff);
788 end
= MIN2(end
, 0xffff);
792 printf("glDraw[Range]Elements"
793 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u\n",
794 start
, end
, type
, count
,
795 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
);
799 check_draw_elements_data(ctx
, count
, type
, indices
);
801 (void) check_draw_elements_data
;
804 vbo_validated_drawrangeelements(ctx
, mode
, index_bounds_valid
, start
, end
,
805 count
, type
, indices
, 1);
810 * Called by glDrawElements() in immediate mode.
812 static void GLAPIENTRY
813 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
814 const GLvoid
*indices
)
816 GET_CURRENT_CONTEXT(ctx
);
818 if (MESA_VERBOSE
& VERBOSE_DRAW
)
819 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
820 _mesa_lookup_enum_by_nr(mode
), count
,
821 _mesa_lookup_enum_by_nr(type
), indices
);
823 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
826 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
827 count
, type
, indices
, 1);
832 * Called by glDrawElementsInstanced() in immediate mode.
834 static void GLAPIENTRY
835 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
836 const GLvoid
*indices
, GLsizei numInstances
)
838 GET_CURRENT_CONTEXT(ctx
);
840 if (MESA_VERBOSE
& VERBOSE_DRAW
)
841 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
842 _mesa_lookup_enum_by_nr(mode
), count
,
843 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
845 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
849 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
850 count
, type
, indices
, numInstances
);
855 * Inner support for both _mesa_MultiDrawElements() and
856 * _mesa_MultiDrawRangeElements().
857 * This does the actual rendering after we've checked array indexes, etc.
860 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
861 const GLsizei
*count
, GLenum type
,
862 const GLvoid
**indices
, GLsizei primcount
)
864 struct vbo_context
*vbo
= vbo_context(ctx
);
865 struct vbo_exec_context
*exec
= &vbo
->exec
;
866 struct _mesa_index_buffer ib
;
867 struct _mesa_prim
*prim
;
868 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
869 uintptr_t min_index_ptr
, max_index_ptr
;
870 GLboolean fallback
= GL_FALSE
;
876 FLUSH_CURRENT( ctx
, 0 );
878 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
882 prim
= calloc(1, primcount
* sizeof(*prim
));
884 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
888 /* Decide if we can do this all as one set of primitives sharing the
889 * same index buffer, or if we have to reset the index pointer per
892 vbo_bind_arrays( ctx
);
894 /* check for dirty state again */
896 _mesa_update_state( ctx
);
898 min_index_ptr
= (uintptr_t)indices
[0];
900 for (i
= 0; i
< primcount
; i
++) {
901 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
902 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
903 index_type_size
* count
[i
]);
906 /* Check if we can handle this thing as a bunch of index offsets from the
907 * same index pointer. If we can't, then we have to fall back to doing
908 * a draw_prims per primitive.
909 * Check that the difference between each prim's indexes is a multiple of
910 * the index/element size.
912 if (index_type_size
!= 1) {
913 for (i
= 0; i
< primcount
; i
++) {
914 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
921 /* If the index buffer isn't in a VBO, then treating the application's
922 * subranges of the index buffer as one large index buffer may lead to
923 * us reading unmapped memory.
925 if (!_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
))
929 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
931 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
932 ib
.ptr
= (void *)min_index_ptr
;
934 for (i
= 0; i
< primcount
; i
++) {
935 prim
[i
].begin
= (i
== 0);
936 prim
[i
].end
= (i
== primcount
- 1);
940 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
941 prim
[i
].count
= count
[i
];
943 prim
[i
].num_instances
= 1;
946 check_buffers_are_unmapped(exec
->array
.inputs
);
947 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
950 /* render one prim at a time */
951 for (i
= 0; i
< primcount
; i
++) {
954 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
963 prim
[0].count
= count
[i
];
965 prim
[0].num_instances
= 1;
967 check_buffers_are_unmapped(exec
->array
.inputs
);
968 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
977 static void GLAPIENTRY
978 vbo_exec_MultiDrawElements(GLenum mode
,
979 const GLsizei
*count
, GLenum type
,
980 const GLvoid
**indices
,
983 GET_CURRENT_CONTEXT(ctx
);
986 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
988 for (i
= 0; i
< primcount
; i
++) {
989 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
]))
993 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
);
997 * Plug in the immediate-mode vertex array drawing commands into the
998 * givven vbo_exec_context object.
1001 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1003 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1004 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1005 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1006 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1007 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1008 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1013 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1021 * The following functions are only used for OpenGL ES 1/2 support.
1022 * And some aren't even supported (yet) in ES 1/2.
1027 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1029 vbo_exec_DrawArrays(mode
, first
, count
);
1034 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1035 const GLvoid
*indices
)
1037 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1042 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1043 GLenum type
, const GLvoid
*indices
)
1045 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1050 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1051 const GLvoid
**indices
, GLsizei primcount
)
1053 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);