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/api_noop.h"
34 #include "main/varray.h"
35 #include "main/bufferobj.h"
36 #include "main/enums.h"
37 #include "main/macros.h"
38 #include "glapi/dispatch.h"
40 #include "vbo_context.h"
44 * Compute min and max elements for glDraw[Range]Elements() calls.
47 vbo_get_minmax_index(GLcontext
*ctx
,
48 const struct _mesa_prim
*prim
,
49 const struct _mesa_index_buffer
*ib
,
50 GLuint
*min_index
, GLuint
*max_index
)
53 GLsizei count
= prim
->count
;
56 if (_mesa_is_bufferobj(ib
->obj
)) {
58 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
59 GL_READ_ONLY
, ib
->obj
);
60 indices
= ADD_POINTERS(map
, ib
->ptr
);
66 case GL_UNSIGNED_INT
: {
67 const GLuint
*ui_indices
= (const GLuint
*)indices
;
68 GLuint max_ui
= ui_indices
[count
-1];
69 GLuint min_ui
= ui_indices
[0];
70 for (i
= 0; i
< count
; i
++) {
71 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
72 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
78 case GL_UNSIGNED_SHORT
: {
79 const GLushort
*us_indices
= (const GLushort
*)indices
;
80 GLuint max_us
= us_indices
[count
-1];
81 GLuint min_us
= us_indices
[0];
82 for (i
= 0; i
< count
; i
++) {
83 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
84 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
90 case GL_UNSIGNED_BYTE
: {
91 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
92 GLuint max_ub
= ub_indices
[count
-1];
93 GLuint min_ub
= ub_indices
[0];
94 for (i
= 0; i
< count
; i
++) {
95 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
96 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
107 if (_mesa_is_bufferobj(ib
->obj
)) {
108 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, ib
->obj
);
114 * Check that element 'j' of the array has reasonable data.
118 check_array_data(GLcontext
*ctx
, struct gl_client_array
*array
,
119 GLuint attrib
, GLuint j
)
121 if (array
->Enabled
) {
122 const void *data
= array
->Ptr
;
123 if (_mesa_is_bufferobj(array
->BufferObj
)) {
124 if (!array
->BufferObj
->Pointer
) {
125 /* need to map now */
126 array
->BufferObj
->Pointer
=
127 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
128 GL_READ_ONLY
, array
->BufferObj
);
130 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
132 switch (array
->Type
) {
135 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
137 for (k
= 0; k
< array
->Size
; k
++) {
138 if (IS_INF_OR_NAN(f
[k
]) ||
139 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
140 _mesa_printf("Bad array data:\n");
141 _mesa_printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
142 _mesa_printf(" Array %u at %p\n", attrib
, (void* ) array
);
143 _mesa_printf(" Type 0x%x, Size %d, Stride %d\n",
144 array
->Type
, array
->Size
, array
->Stride
);
145 _mesa_printf(" Address/offset %p in Buffer Object %u\n",
146 array
->Ptr
, array
->BufferObj
->Name
);
147 f
[k
] = 1.0; /* XXX replace the bad value! */
149 /*assert(!IS_INF_OR_NAN(f[k]));*/
161 * Unmap the buffer object referenced by given array, if mapped.
164 unmap_array_buffer(GLcontext
*ctx
, struct gl_client_array
*array
)
166 if (array
->Enabled
&&
167 _mesa_is_bufferobj(array
->BufferObj
) &&
168 _mesa_bufferobj_mapped(array
->BufferObj
)) {
169 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, array
->BufferObj
);
175 * Examine the array's data for NaNs, etc.
178 check_draw_elements_data(GLcontext
*ctx
, GLsizei count
, GLenum elemType
,
179 const void *elements
, GLint basevertex
)
181 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
185 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
186 elemMap
= ctx
->Driver
.MapBuffer(ctx
,
187 GL_ELEMENT_ARRAY_BUFFER_ARB
,
189 ctx
->Array
.ElementArrayBufferObj
);
190 elements
= ADD_POINTERS(elements
, elemMap
);
193 for (i
= 0; i
< count
; i
++) {
198 case GL_UNSIGNED_BYTE
:
199 j
= ((const GLubyte
*) elements
)[i
];
201 case GL_UNSIGNED_SHORT
:
202 j
= ((const GLushort
*) elements
)[i
];
204 case GL_UNSIGNED_INT
:
205 j
= ((const GLuint
*) elements
)[i
];
211 /* check element j of each enabled array */
212 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
213 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
214 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
215 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
216 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
217 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
219 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
220 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
221 VERT_ATTRIB_GENERIC0
+ k
, j
);
225 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
226 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
227 ctx
->Array
.ElementArrayBufferObj
);
230 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
231 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
232 unmap_array_buffer(ctx
, &arrayObj
->Color
);
233 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
234 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
236 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
237 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
243 * Check array data, looking for NaNs, etc.
246 check_draw_arrays_data(GLcontext
*ctx
, GLint start
, GLsizei count
)
253 * Print info/data for glDrawArrays().
256 print_draw_arrays(GLcontext
*ctx
, struct vbo_exec_context
*exec
,
257 GLenum mode
, GLint start
, GLsizei count
)
261 _mesa_printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
264 for (i
= 0; i
< 32; i
++) {
265 GLuint bufName
= exec
->array
.inputs
[i
]->BufferObj
->Name
;
266 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
267 _mesa_printf("attr %2d: size %d stride %d enabled %d "
268 "ptr %p Bufobj %u\n",
270 exec
->array
.inputs
[i
]->Size
,
272 /*exec->array.inputs[i]->Enabled,*/
273 exec
->array
.legacy_array
[i
]->Enabled
,
274 exec
->array
.inputs
[i
]->Ptr
,
278 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, bufName
);
279 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
280 GL_READ_ONLY_ARB
, buf
);
281 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
282 float *f
= (float *) (p
+ offset
);
285 int n
= (count
* stride
) / 4;
288 _mesa_printf(" Data at offset %d:\n", offset
);
289 for (i
= 0; i
< n
; i
++) {
290 _mesa_printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
292 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, buf
);
299 * Just translate the arrayobj into a sane layout.
302 bind_array_obj(GLcontext
*ctx
)
304 struct vbo_context
*vbo
= vbo_context(ctx
);
305 struct vbo_exec_context
*exec
= &vbo
->exec
;
306 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
309 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
310 * rather than as individual named arrays. Then this function can
313 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
314 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
315 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
316 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
317 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
318 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
319 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
320 if (arrayObj
->PointSize
.Enabled
) {
321 /* this aliases COLOR_INDEX */
322 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
324 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
326 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
327 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
329 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
330 assert(i
< Elements(exec
->array
.generic_array
));
331 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
334 exec
->array
.array_obj
= arrayObj
->Name
;
339 recalculate_input_bindings(GLcontext
*ctx
)
341 struct vbo_context
*vbo
= vbo_context(ctx
);
342 struct vbo_exec_context
*exec
= &vbo
->exec
;
343 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
344 GLbitfield const_inputs
= 0x0;
347 exec
->array
.program_mode
= get_program_mode(ctx
);
348 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
350 switch (exec
->array
.program_mode
) {
352 /* When no vertex program is active (or the vertex program is generated
353 * from fixed-function state). We put the material values into the
354 * generic slots. This is the only situation where material values
355 * are available as per-vertex attributes.
357 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
358 if (exec
->array
.legacy_array
[i
]->Enabled
)
359 inputs
[i
] = exec
->array
.legacy_array
[i
];
361 inputs
[i
] = &vbo
->legacy_currval
[i
];
362 const_inputs
|= 1 << i
;
366 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
367 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
368 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
371 /* Could use just about anything, just to fill in the empty
374 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
375 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
376 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
381 /* NV_vertex_program - attribute arrays alias and override
382 * conventional, legacy arrays. No materials, and the generic
385 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
386 if (exec
->array
.generic_array
[i
]->Enabled
)
387 inputs
[i
] = exec
->array
.generic_array
[i
];
388 else if (exec
->array
.legacy_array
[i
]->Enabled
)
389 inputs
[i
] = exec
->array
.legacy_array
[i
];
391 inputs
[i
] = &vbo
->legacy_currval
[i
];
392 const_inputs
|= 1 << i
;
396 /* Could use just about anything, just to fill in the empty
399 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
400 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
401 const_inputs
|= 1 << i
;
406 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
407 * attribute array aliases and overrides the legacy position array.
409 * Otherwise, legacy attributes available in the legacy slots,
410 * generic attributes in the generic slots and materials are not
411 * available as per-vertex attributes.
413 if (exec
->array
.generic_array
[0]->Enabled
)
414 inputs
[0] = exec
->array
.generic_array
[0];
415 else if (exec
->array
.legacy_array
[0]->Enabled
)
416 inputs
[0] = exec
->array
.legacy_array
[0];
418 inputs
[0] = &vbo
->legacy_currval
[0];
419 const_inputs
|= 1 << 0;
422 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
423 if (exec
->array
.legacy_array
[i
]->Enabled
)
424 inputs
[i
] = exec
->array
.legacy_array
[i
];
426 inputs
[i
] = &vbo
->legacy_currval
[i
];
427 const_inputs
|= 1 << i
;
431 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
432 if (exec
->array
.generic_array
[i
]->Enabled
)
433 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
435 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
436 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
443 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
448 bind_arrays(GLcontext
*ctx
)
451 if (ctx
->Array
.ArrayObj
.Name
!= exec
->array
.array_obj
) {
453 recalculate_input_bindings(ctx
);
455 else if (exec
->array
.program_mode
!= get_program_mode(ctx
) ||
456 exec
->array
.enabled_flags
!= ctx
->Array
.ArrayObj
->_Enabled
) {
457 recalculate_input_bindings(ctx
);
461 recalculate_input_bindings(ctx
);
467 /***********************************************************************
471 static void GLAPIENTRY
472 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
474 GET_CURRENT_CONTEXT(ctx
);
475 struct vbo_context
*vbo
= vbo_context(ctx
);
476 struct vbo_exec_context
*exec
= &vbo
->exec
;
477 struct _mesa_prim prim
[1];
479 if (MESA_VERBOSE
& VERBOSE_DRAW
)
480 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
481 _mesa_lookup_enum_by_nr(mode
), start
, count
);
483 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
486 FLUSH_CURRENT( ctx
, 0 );
489 _mesa_update_state( ctx
);
491 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
496 check_draw_arrays_data(ctx
, start
, count
);
498 (void) check_draw_arrays_data
;
503 /* Again... because we may have changed the bitmask of per-vertex varying
504 * attributes. If we regenerate the fixed-function vertex program now
505 * we may be able to prune down the number of vertex attributes which we
506 * need in the shader.
509 _mesa_update_state( ctx
);
516 prim
[0].start
= start
;
517 prim
[0].count
= count
;
519 prim
[0].basevertex
= 0;
521 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
522 GL_TRUE
, start
, start
+ count
- 1 );
525 print_draw_arrays(ctx
, exec
, mode
, start
, count
);
527 (void) print_draw_arrays
;
533 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
536 dump_element_buffer(GLcontext
*ctx
, GLenum type
)
538 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
539 GL_ELEMENT_ARRAY_BUFFER_ARB
,
541 ctx
->Array
.ElementArrayBufferObj
);
543 case GL_UNSIGNED_BYTE
:
545 const GLubyte
*us
= (const GLubyte
*) map
;
547 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
548 _mesa_printf("%02x ", us
[i
]);
555 case GL_UNSIGNED_SHORT
:
557 const GLushort
*us
= (const GLushort
*) map
;
559 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
560 _mesa_printf("%04x ", us
[i
]);
567 case GL_UNSIGNED_INT
:
569 const GLuint
*us
= (const GLuint
*) map
;
571 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
572 _mesa_printf("%08x ", us
[i
]);
583 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
584 ctx
->Array
.ElementArrayBufferObj
);
588 /* Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements */
590 vbo_validated_drawrangeelements(GLcontext
*ctx
, GLenum mode
,
591 GLboolean index_bounds_valid
,
592 GLuint start
, GLuint end
,
593 GLsizei count
, GLenum type
,
594 const GLvoid
*indices
,
597 struct vbo_context
*vbo
= vbo_context(ctx
);
598 struct vbo_exec_context
*exec
= &vbo
->exec
;
599 struct _mesa_index_buffer ib
;
600 struct _mesa_prim prim
[1];
602 FLUSH_CURRENT( ctx
, 0 );
605 _mesa_update_state( ctx
);
607 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
612 _mesa_update_state( ctx
);
618 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
627 prim
[0].count
= count
;
629 prim
[0].basevertex
= basevertex
;
631 /* Need to give special consideration to rendering a range of
632 * indices starting somewhere above zero. Typically the
633 * application is issuing multiple DrawRangeElements() to draw
634 * successive primitives layed out linearly in the vertex arrays.
635 * Unless the vertex arrays are all in a VBO (or locked as with
636 * CVA), the OpenGL semantics imply that we need to re-read or
637 * re-upload the vertex data on each draw call.
639 * In the case of hardware tnl, we want to avoid starting the
640 * upload at zero, as it will mean every draw call uploads an
641 * increasing amount of not-used vertex data. Worse - in the
642 * software tnl module, all those vertices might be transformed and
643 * lit but never rendered.
645 * If we just upload or transform the vertices in start..end,
646 * however, the indices will be incorrect.
648 * At this level, we don't know exactly what the requirements of
649 * the backend are going to be, though it will likely boil down to
652 * 1) Do nothing, everything is in a VBO and is processed once
655 * 2) Adjust the indices and vertex arrays so that start becomes
658 * Rather than doing anything here, I'll provide a helper function
659 * for the latter case elsewhere.
662 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
663 index_bounds_valid
, start
, end
);
666 static void GLAPIENTRY
667 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
668 GLuint start
, GLuint end
,
669 GLsizei count
, GLenum type
,
670 const GLvoid
*indices
,
673 static GLuint warnCount
= 0;
674 GET_CURRENT_CONTEXT(ctx
);
676 if (MESA_VERBOSE
& VERBOSE_DRAW
)
678 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
679 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
680 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
682 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
683 type
, indices
, basevertex
))
686 /* NOTE: It's important that 'end' is a reasonable value.
687 * in _tnl_draw_prims(), we use end to determine how many vertices
688 * to transform. If it's too large, we can unnecessarily split prims
689 * or we can read/write out of memory in several different places!
692 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
693 /* the max element is out of bounds of one or more enabled arrays */
696 if (warnCount
< 10) {
697 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
698 "type 0x%x, indices=%p)\n"
699 "\tend is out of bounds (max=%u) "
700 "Element Buffer %u (size %d)\n"
701 "\tThis should probably be fixed in the application.",
702 start
, end
, count
, type
, indices
,
703 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
704 ctx
->Array
.ElementArrayBufferObj
->Name
,
705 ctx
->Array
.ElementArrayBufferObj
->Size
);
709 dump_element_buffer(ctx
, type
);
712 _mesa_print_arrays(ctx
);
715 /* 'end' was out of bounds, but now let's check the actual array
716 * indexes to see if any of them are out of bounds. If so, warn
717 * and skip the draw to avoid potential segfault, etc.
720 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
721 ctx
->Array
.ElementArrayBufferObj
);
722 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
723 if (warnCount
< 10) {
724 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
725 "count %d, type 0x%x, indices=%p)\n"
726 "\tindex=%u is out of bounds (max=%u) "
727 "Element Buffer %u (size %d)\n"
728 "\tSkipping the glDrawRangeElements() call",
729 start
, end
, count
, type
, indices
, max
,
730 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
731 ctx
->Array
.ElementArrayBufferObj
->Name
,
732 ctx
->Array
.ElementArrayBufferObj
->Size
);
736 /* XXX we could also find the min index and compare to 'start'
737 * to see if start is correct. But it's more likely to get the
744 _mesa_printf("glDraw[Range]Elements{,BaseVertex}"
745 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
747 start
, end
, type
, count
,
748 ctx
->Array
.ElementArrayBufferObj
->Name
,
753 check_draw_elements_data(ctx
, count
, type
, indices
);
755 (void) check_draw_elements_data
;
758 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
759 count
, type
, indices
, basevertex
);
763 static void GLAPIENTRY
764 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
765 GLsizei count
, GLenum type
, const GLvoid
*indices
)
767 GET_CURRENT_CONTEXT(ctx
);
769 if (MESA_VERBOSE
& VERBOSE_DRAW
)
771 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
772 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
773 _mesa_lookup_enum_by_nr(type
), indices
);
775 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
780 static void GLAPIENTRY
781 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
782 const GLvoid
*indices
)
784 GET_CURRENT_CONTEXT(ctx
);
786 if (MESA_VERBOSE
& VERBOSE_DRAW
)
787 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
788 _mesa_lookup_enum_by_nr(mode
), count
,
789 _mesa_lookup_enum_by_nr(type
), indices
);
791 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
794 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
795 count
, type
, indices
, 0);
799 static void GLAPIENTRY
800 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
801 const GLvoid
*indices
, GLint basevertex
)
803 GET_CURRENT_CONTEXT(ctx
);
805 if (MESA_VERBOSE
& VERBOSE_DRAW
)
806 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
807 _mesa_lookup_enum_by_nr(mode
), count
,
808 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
810 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
814 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
815 count
, type
, indices
, basevertex
);
819 /** Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements */
821 vbo_validated_multidrawelements(GLcontext
*ctx
, GLenum mode
,
822 const GLsizei
*count
, GLenum type
,
823 const GLvoid
**indices
, GLsizei primcount
,
824 const GLint
*basevertex
)
826 struct vbo_context
*vbo
= vbo_context(ctx
);
827 struct vbo_exec_context
*exec
= &vbo
->exec
;
828 struct _mesa_index_buffer ib
;
829 struct _mesa_prim
*prim
;
830 unsigned int index_type_size
= 0;
831 uintptr_t min_index_ptr
, max_index_ptr
;
832 GLboolean fallback
= GL_FALSE
;
838 FLUSH_CURRENT( ctx
, 0 );
841 _mesa_update_state( ctx
);
843 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
848 _mesa_update_state( ctx
);
850 prim
= _mesa_calloc(primcount
* sizeof(*prim
));
852 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
856 /* Decide if we can do this all as one set of primitives sharing the
857 * same index buffer, or if we have to reset the index pointer per primitive.
862 case GL_UNSIGNED_INT
:
865 case GL_UNSIGNED_SHORT
:
868 case GL_UNSIGNED_BYTE
:
875 min_index_ptr
= (uintptr_t)indices
[0];
877 for (i
= 0; i
< primcount
; i
++) {
878 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
879 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
880 index_type_size
* count
[i
]);
883 /* Check if we can handle this thing as a bunch of index offsets from the
884 * same index pointer. If we can't, then we have to fall back to doing
885 * a draw_prims per primitive.
887 if (index_type_size
!= 1) {
888 for (i
= 0; i
< primcount
; i
++) {
889 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
896 /* If the index buffer isn't in a VBO, then treating the application's
897 * subranges of the index buffer as one large index buffer may lead to
898 * us reading unmapped memory.
900 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
904 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
906 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
907 ib
.ptr
= (void *)min_index_ptr
;
909 for (i
= 0; i
< primcount
; i
++) {
910 prim
[i
].begin
= (i
== 0);
911 prim
[i
].end
= (i
== primcount
- 1);
915 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
916 prim
[i
].count
= count
[i
];
918 if (basevertex
!= NULL
)
919 prim
[i
].basevertex
= basevertex
[i
];
921 prim
[i
].basevertex
= 0;
924 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
927 for (i
= 0; i
< primcount
; i
++) {
930 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
940 prim
[0].count
= count
[i
];
942 if (basevertex
!= NULL
)
943 prim
[0].basevertex
= basevertex
[i
];
945 prim
[0].basevertex
= 0;
948 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
955 static void GLAPIENTRY
956 vbo_exec_MultiDrawElements(GLenum mode
,
957 const GLsizei
*count
, GLenum type
,
958 const GLvoid
**indices
,
961 GET_CURRENT_CONTEXT(ctx
);
964 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
966 for (i
= 0; i
< primcount
; i
++) {
967 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
972 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
977 static void GLAPIENTRY
978 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
979 const GLsizei
*count
, GLenum type
,
980 const GLvoid
**indices
,
982 const GLsizei
*basevertex
)
984 GET_CURRENT_CONTEXT(ctx
);
987 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
989 for (i
= 0; i
< primcount
; i
++) {
990 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
995 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1000 /***********************************************************************
1005 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1008 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1009 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1010 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1011 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1012 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1013 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1014 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1016 exec
->vtxfmt
.DrawArrays
= _mesa_noop_DrawArrays
;
1017 exec
->vtxfmt
.DrawElements
= _mesa_noop_DrawElements
;
1018 exec
->vtxfmt
.DrawRangeElements
= _mesa_noop_DrawRangeElements
;
1019 exec
->vtxfmt
.MultiDrawElementsEXT
= _mesa_noop_MultiDrawElements
;
1020 exec
->vtxfmt
.DrawElementsBaseVertex
= _mesa_noop_DrawElementsBaseVertex
;
1021 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= _mesa_noop_DrawRangeElementsBaseVertex
;
1022 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= _mesa_noop_MultiDrawElementsBaseVertex
;
1028 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1034 /* This API entrypoint is not ordinarily used */
1036 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1038 vbo_exec_DrawArrays(mode
, first
, count
);
1042 /* This API entrypoint is not ordinarily used */
1044 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1045 const GLvoid
*indices
)
1047 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1051 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1052 const GLvoid
*indices
, GLint basevertex
)
1054 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1058 /* This API entrypoint is not ordinarily used */
1060 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1061 GLenum type
, const GLvoid
*indices
)
1063 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1068 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1069 GLsizei count
, GLenum type
,
1070 const GLvoid
*indices
, GLint basevertex
)
1072 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1073 indices
, basevertex
);
1077 /* GL_EXT_multi_draw_arrays */
1079 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1080 const GLvoid
**indices
, GLsizei primcount
)
1082 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1087 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1088 const GLsizei
*count
, GLenum type
,
1089 const GLvoid
**indices
, GLsizei primcount
,
1090 const GLint
*basevertex
)
1092 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1093 primcount
, basevertex
);