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"
39 #include "vbo_context.h"
43 * Compute min and max elements by scanning the index buffer for
44 * 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 GLuint 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.
116 * For debugging purposes; not normally used.
119 check_array_data(GLcontext
*ctx
, struct gl_client_array
*array
,
120 GLuint attrib
, GLuint j
)
122 if (array
->Enabled
) {
123 const void *data
= array
->Ptr
;
124 if (_mesa_is_bufferobj(array
->BufferObj
)) {
125 if (!array
->BufferObj
->Pointer
) {
126 /* need to map now */
127 array
->BufferObj
->Pointer
=
128 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
129 GL_READ_ONLY
, array
->BufferObj
);
131 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
133 switch (array
->Type
) {
136 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
138 for (k
= 0; k
< array
->Size
; k
++) {
139 if (IS_INF_OR_NAN(f
[k
]) ||
140 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
141 printf("Bad array data:\n");
142 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
143 printf(" Array %u at %p\n", attrib
, (void* ) array
);
144 printf(" Type 0x%x, Size %d, Stride %d\n",
145 array
->Type
, array
->Size
, array
->Stride
);
146 printf(" Address/offset %p in Buffer Object %u\n",
147 array
->Ptr
, array
->BufferObj
->Name
);
148 f
[k
] = 1.0; /* XXX replace the bad value! */
150 /*assert(!IS_INF_OR_NAN(f[k]));*/
162 * Unmap the buffer object referenced by given array, if mapped.
165 unmap_array_buffer(GLcontext
*ctx
, struct gl_client_array
*array
)
167 if (array
->Enabled
&&
168 _mesa_is_bufferobj(array
->BufferObj
) &&
169 _mesa_bufferobj_mapped(array
->BufferObj
)) {
170 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, array
->BufferObj
);
176 * Examine the array's data for NaNs, etc.
177 * For debug purposes; not normally used.
180 check_draw_elements_data(GLcontext
*ctx
, GLsizei count
, GLenum elemType
,
181 const void *elements
, GLint basevertex
)
183 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
187 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
188 elemMap
= ctx
->Driver
.MapBuffer(ctx
,
189 GL_ELEMENT_ARRAY_BUFFER_ARB
,
191 ctx
->Array
.ElementArrayBufferObj
);
192 elements
= ADD_POINTERS(elements
, elemMap
);
195 for (i
= 0; i
< count
; i
++) {
200 case GL_UNSIGNED_BYTE
:
201 j
= ((const GLubyte
*) elements
)[i
];
203 case GL_UNSIGNED_SHORT
:
204 j
= ((const GLushort
*) elements
)[i
];
206 case GL_UNSIGNED_INT
:
207 j
= ((const GLuint
*) elements
)[i
];
213 /* check element j of each enabled array */
214 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
215 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
216 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
217 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
218 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
219 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
221 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
222 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
223 VERT_ATTRIB_GENERIC0
+ k
, j
);
227 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
228 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
229 ctx
->Array
.ElementArrayBufferObj
);
232 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
233 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
234 unmap_array_buffer(ctx
, &arrayObj
->Color
);
235 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
236 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
238 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
239 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
245 * Check array data, looking for NaNs, etc.
248 check_draw_arrays_data(GLcontext
*ctx
, GLint start
, GLsizei count
)
255 * Print info/data for glDrawArrays(), for debugging.
258 print_draw_arrays(GLcontext
*ctx
, struct vbo_exec_context
*exec
,
259 GLenum mode
, GLint start
, GLsizei count
)
263 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
266 for (i
= 0; i
< 32; i
++) {
267 GLuint bufName
= exec
->array
.inputs
[i
]->BufferObj
->Name
;
268 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
269 printf("attr %2d: size %d stride %d enabled %d "
270 "ptr %p Bufobj %u\n",
272 exec
->array
.inputs
[i
]->Size
,
274 /*exec->array.inputs[i]->Enabled,*/
275 exec
->array
.legacy_array
[i
]->Enabled
,
276 exec
->array
.inputs
[i
]->Ptr
,
280 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, bufName
);
281 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
282 GL_READ_ONLY_ARB
, buf
);
283 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
284 float *f
= (float *) (p
+ offset
);
287 int n
= (count
* stride
) / 4;
290 printf(" Data at offset %d:\n", offset
);
291 for (i
= 0; i
< n
; i
++) {
292 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
294 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, buf
);
301 * Bind the VBO executor to the current vertex array object prior
304 * Just translate the arrayobj into a sane layout.
307 bind_array_obj(GLcontext
*ctx
)
309 struct vbo_context
*vbo
= vbo_context(ctx
);
310 struct vbo_exec_context
*exec
= &vbo
->exec
;
311 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
314 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
315 * rather than as individual named arrays. Then this function can
318 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
319 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
320 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
321 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
322 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
323 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
324 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
325 if (arrayObj
->PointSize
.Enabled
) {
326 /* this aliases COLOR_INDEX */
327 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
329 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
331 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
332 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
334 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
335 assert(i
< Elements(exec
->array
.generic_array
));
336 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
339 exec
->array
.array_obj
= arrayObj
->Name
;
344 * Set the vbo->exec->inputs[] pointers to point to the enabled
345 * vertex arrays. This depends on the current vertex program/shader
346 * being executed because of whether or not generic vertex arrays
347 * alias the conventional vertex arrays.
348 * For arrays that aren't enabled, we set the input[attrib] pointer
349 * to point at a zero-stride current value "array".
352 recalculate_input_bindings(GLcontext
*ctx
)
354 struct vbo_context
*vbo
= vbo_context(ctx
);
355 struct vbo_exec_context
*exec
= &vbo
->exec
;
356 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
357 GLbitfield const_inputs
= 0x0;
360 exec
->array
.program_mode
= get_program_mode(ctx
);
361 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
363 switch (exec
->array
.program_mode
) {
365 /* When no vertex program is active (or the vertex program is generated
366 * from fixed-function state). We put the material values into the
367 * generic slots. This is the only situation where material values
368 * are available as per-vertex attributes.
370 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
371 if (exec
->array
.legacy_array
[i
]->Enabled
)
372 inputs
[i
] = exec
->array
.legacy_array
[i
];
374 inputs
[i
] = &vbo
->legacy_currval
[i
];
375 const_inputs
|= 1 << i
;
379 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
380 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
381 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
384 /* Could use just about anything, just to fill in the empty
387 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
388 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
389 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
394 /* NV_vertex_program - attribute arrays alias and override
395 * conventional, legacy arrays. No materials, and the generic
398 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
399 if (exec
->array
.generic_array
[i
]->Enabled
)
400 inputs
[i
] = exec
->array
.generic_array
[i
];
401 else if (exec
->array
.legacy_array
[i
]->Enabled
)
402 inputs
[i
] = exec
->array
.legacy_array
[i
];
404 inputs
[i
] = &vbo
->legacy_currval
[i
];
405 const_inputs
|= 1 << i
;
409 /* Could use just about anything, just to fill in the empty
412 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
413 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
414 const_inputs
|= 1 << i
;
419 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
420 * attribute array aliases and overrides the legacy position array.
422 * Otherwise, legacy attributes available in the legacy slots,
423 * generic attributes in the generic slots and materials are not
424 * available as per-vertex attributes.
426 if (exec
->array
.generic_array
[0]->Enabled
)
427 inputs
[0] = exec
->array
.generic_array
[0];
428 else if (exec
->array
.legacy_array
[0]->Enabled
)
429 inputs
[0] = exec
->array
.legacy_array
[0];
431 inputs
[0] = &vbo
->legacy_currval
[0];
432 const_inputs
|= 1 << 0;
435 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
436 if (exec
->array
.legacy_array
[i
]->Enabled
)
437 inputs
[i
] = exec
->array
.legacy_array
[i
];
439 inputs
[i
] = &vbo
->legacy_currval
[i
];
440 const_inputs
|= 1 << i
;
444 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
445 if (exec
->array
.generic_array
[i
]->Enabled
)
446 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
448 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
449 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
456 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
461 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
462 * These will point to the arrays to actually use for drawing. Some will
463 * be user-provided arrays, other will be zero-stride const-valued arrays.
464 * Note that this might set the _NEW_ARRAY dirty flag so state validation
465 * must be done after this call.
468 bind_arrays(GLcontext
*ctx
)
471 if (ctx
->Array
.ArrayObj
.Name
!= exec
->array
.array_obj
) {
473 recalculate_input_bindings(ctx
);
475 else if (exec
->array
.program_mode
!= get_program_mode(ctx
) ||
476 exec
->array
.enabled_flags
!= ctx
->Array
.ArrayObj
->_Enabled
) {
477 recalculate_input_bindings(ctx
);
481 recalculate_input_bindings(ctx
);
488 * Called from glDrawArrays when in immediate mode (not display list mode).
490 static void GLAPIENTRY
491 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
493 GET_CURRENT_CONTEXT(ctx
);
494 struct vbo_context
*vbo
= vbo_context(ctx
);
495 struct vbo_exec_context
*exec
= &vbo
->exec
;
496 struct _mesa_prim prim
[1];
498 if (MESA_VERBOSE
& VERBOSE_DRAW
)
499 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
500 _mesa_lookup_enum_by_nr(mode
), start
, count
);
502 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
505 FLUSH_CURRENT( ctx
, 0 );
507 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
512 check_draw_arrays_data(ctx
, start
, count
);
514 (void) check_draw_arrays_data
;
519 /* Again... because we may have changed the bitmask of per-vertex varying
520 * attributes. If we regenerate the fixed-function vertex program now
521 * we may be able to prune down the number of vertex attributes which we
522 * need in the shader.
525 _mesa_update_state( ctx
);
532 prim
[0].start
= start
;
533 prim
[0].count
= count
;
535 prim
[0].basevertex
= 0;
536 prim
[0].num_instances
= 1;
538 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
539 GL_TRUE
, start
, start
+ count
- 1 );
542 print_draw_arrays(ctx
, exec
, mode
, start
, count
);
544 (void) print_draw_arrays
;
550 * Called from glDrawArraysInstanced when in immediate mode (not
551 * display list mode).
553 static void GLAPIENTRY
554 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
557 GET_CURRENT_CONTEXT(ctx
);
558 struct vbo_context
*vbo
= vbo_context(ctx
);
559 struct vbo_exec_context
*exec
= &vbo
->exec
;
560 struct _mesa_prim prim
[1];
562 if (MESA_VERBOSE
& VERBOSE_DRAW
)
563 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
564 _mesa_lookup_enum_by_nr(mode
), start
, count
, primcount
);
566 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, primcount
))
569 FLUSH_CURRENT( ctx
, 0 );
571 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
576 check_draw_arrays_data(ctx
, start
, count
);
581 /* Again... because we may have changed the bitmask of per-vertex varying
582 * attributes. If we regenerate the fixed-function vertex program now
583 * we may be able to prune down the number of vertex attributes which we
584 * need in the shader.
587 _mesa_update_state( ctx
);
594 prim
[0].start
= start
;
595 prim
[0].count
= count
;
597 prim
[0].basevertex
= 0;
598 prim
[0].num_instances
= primcount
;
600 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
601 GL_TRUE
, start
, start
+ count
- 1 );
604 print_draw_arrays(ctx
, exec
, mode
, start
, count
);
610 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
614 dump_element_buffer(GLcontext
*ctx
, GLenum type
)
616 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
617 GL_ELEMENT_ARRAY_BUFFER_ARB
,
619 ctx
->Array
.ElementArrayBufferObj
);
621 case GL_UNSIGNED_BYTE
:
623 const GLubyte
*us
= (const GLubyte
*) map
;
625 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
626 printf("%02x ", us
[i
]);
633 case GL_UNSIGNED_SHORT
:
635 const GLushort
*us
= (const GLushort
*) map
;
637 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
638 printf("%04x ", us
[i
]);
645 case GL_UNSIGNED_INT
:
647 const GLuint
*us
= (const GLuint
*) map
;
649 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
650 printf("%08x ", us
[i
]);
661 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
662 ctx
->Array
.ElementArrayBufferObj
);
667 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
668 * Do the rendering for a glDrawElements or glDrawRangeElements call after
669 * we've validated buffer bounds, etc.
672 vbo_validated_drawrangeelements(GLcontext
*ctx
, GLenum mode
,
673 GLboolean index_bounds_valid
,
674 GLuint start
, GLuint end
,
675 GLsizei count
, GLenum type
,
676 const GLvoid
*indices
,
677 GLint basevertex
, GLint primcount
)
679 struct vbo_context
*vbo
= vbo_context(ctx
);
680 struct vbo_exec_context
*exec
= &vbo
->exec
;
681 struct _mesa_index_buffer ib
;
682 struct _mesa_prim prim
[1];
684 FLUSH_CURRENT( ctx
, 0 );
686 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
692 /* check for dirty state again */
694 _mesa_update_state( ctx
);
698 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
707 prim
[0].count
= count
;
709 prim
[0].basevertex
= basevertex
;
710 prim
[0].num_instances
= primcount
;
712 /* Need to give special consideration to rendering a range of
713 * indices starting somewhere above zero. Typically the
714 * application is issuing multiple DrawRangeElements() to draw
715 * successive primitives layed out linearly in the vertex arrays.
716 * Unless the vertex arrays are all in a VBO (or locked as with
717 * CVA), the OpenGL semantics imply that we need to re-read or
718 * re-upload the vertex data on each draw call.
720 * In the case of hardware tnl, we want to avoid starting the
721 * upload at zero, as it will mean every draw call uploads an
722 * increasing amount of not-used vertex data. Worse - in the
723 * software tnl module, all those vertices might be transformed and
724 * lit but never rendered.
726 * If we just upload or transform the vertices in start..end,
727 * however, the indices will be incorrect.
729 * At this level, we don't know exactly what the requirements of
730 * the backend are going to be, though it will likely boil down to
733 * 1) Do nothing, everything is in a VBO and is processed once
736 * 2) Adjust the indices and vertex arrays so that start becomes
739 * Rather than doing anything here, I'll provide a helper function
740 * for the latter case elsewhere.
743 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
744 index_bounds_valid
, start
, end
);
749 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
751 static void GLAPIENTRY
752 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
753 GLuint start
, GLuint end
,
754 GLsizei count
, GLenum type
,
755 const GLvoid
*indices
,
758 static GLuint warnCount
= 0;
759 GET_CURRENT_CONTEXT(ctx
);
761 if (MESA_VERBOSE
& VERBOSE_DRAW
)
763 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
764 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
765 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
767 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
768 type
, indices
, basevertex
))
771 /* NOTE: It's important that 'end' is a reasonable value.
772 * in _tnl_draw_prims(), we use end to determine how many vertices
773 * to transform. If it's too large, we can unnecessarily split prims
774 * or we can read/write out of memory in several different places!
777 /* Catch/fix some potential user errors */
778 if (type
== GL_UNSIGNED_BYTE
) {
779 start
= MIN2(start
, 0xff);
780 end
= MIN2(end
, 0xff);
782 else if (type
== GL_UNSIGNED_SHORT
) {
783 start
= MIN2(start
, 0xffff);
784 end
= MIN2(end
, 0xffff);
787 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
788 /* the max element is out of bounds of one or more enabled arrays */
791 if (warnCount
< 10) {
792 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
793 "type 0x%x, indices=%p)\n"
794 "\tend is out of bounds (max=%u) "
795 "Element Buffer %u (size %d)\n"
796 "\tThis should probably be fixed in the application.",
797 start
, end
, count
, type
, indices
,
798 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
799 ctx
->Array
.ElementArrayBufferObj
->Name
,
800 ctx
->Array
.ElementArrayBufferObj
->Size
);
804 dump_element_buffer(ctx
, type
);
807 _mesa_print_arrays(ctx
);
810 /* 'end' was out of bounds, but now let's check the actual array
811 * indexes to see if any of them are out of bounds.
814 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
815 ctx
->Array
.ElementArrayBufferObj
);
816 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
817 if (warnCount
< 10) {
818 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
819 "count %d, type 0x%x, indices=%p)\n"
820 "\tindex=%u is out of bounds (max=%u) "
821 "Element Buffer %u (size %d)\n"
822 "\tSkipping the glDrawRangeElements() call",
823 start
, end
, count
, type
, indices
, max
,
824 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
825 ctx
->Array
.ElementArrayBufferObj
->Name
,
826 ctx
->Array
.ElementArrayBufferObj
->Size
);
829 /* XXX we could also find the min index and compare to 'start'
830 * to see if start is correct. But it's more likely to get the
836 /* Set 'end' to the max possible legal value */
837 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
838 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
841 printf("glDraw[Range]Elements{,BaseVertex}"
842 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
844 start
, end
, type
, count
,
845 ctx
->Array
.ElementArrayBufferObj
->Name
,
850 check_draw_elements_data(ctx
, count
, type
, indices
);
852 (void) check_draw_elements_data
;
855 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
856 count
, type
, indices
, basevertex
, 1);
861 * Called by glDrawRangeElements() in immediate mode.
863 static void GLAPIENTRY
864 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
865 GLsizei count
, GLenum type
, const GLvoid
*indices
)
867 GET_CURRENT_CONTEXT(ctx
);
869 if (MESA_VERBOSE
& VERBOSE_DRAW
)
871 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
872 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
873 _mesa_lookup_enum_by_nr(type
), indices
);
875 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
881 * Called by glDrawElements() in immediate mode.
883 static void GLAPIENTRY
884 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
885 const GLvoid
*indices
)
887 GET_CURRENT_CONTEXT(ctx
);
889 if (MESA_VERBOSE
& VERBOSE_DRAW
)
890 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
891 _mesa_lookup_enum_by_nr(mode
), count
,
892 _mesa_lookup_enum_by_nr(type
), indices
);
894 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
897 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
898 count
, type
, indices
, 0, 1);
903 * Called by glDrawElementsBaseVertex() in immediate mode.
905 static void GLAPIENTRY
906 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
907 const GLvoid
*indices
, GLint basevertex
)
909 GET_CURRENT_CONTEXT(ctx
);
911 if (MESA_VERBOSE
& VERBOSE_DRAW
)
912 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
913 _mesa_lookup_enum_by_nr(mode
), count
,
914 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
916 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
920 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
921 count
, type
, indices
, basevertex
, 1);
926 * Called by glDrawElementsInstanced() in immediate mode.
928 static void GLAPIENTRY
929 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
930 const GLvoid
*indices
, GLsizei primcount
)
932 GET_CURRENT_CONTEXT(ctx
);
934 if (MESA_VERBOSE
& VERBOSE_DRAW
)
935 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
936 _mesa_lookup_enum_by_nr(mode
), count
,
937 _mesa_lookup_enum_by_nr(type
), indices
, primcount
);
939 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
943 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
944 count
, type
, indices
, 0, primcount
);
949 * Inner support for both _mesa_MultiDrawElements() and
950 * _mesa_MultiDrawRangeElements().
951 * This does the actual rendering after we've checked array indexes, etc.
954 vbo_validated_multidrawelements(GLcontext
*ctx
, GLenum mode
,
955 const GLsizei
*count
, GLenum type
,
956 const GLvoid
**indices
, GLsizei primcount
,
957 const GLint
*basevertex
)
959 struct vbo_context
*vbo
= vbo_context(ctx
);
960 struct vbo_exec_context
*exec
= &vbo
->exec
;
961 struct _mesa_index_buffer ib
;
962 struct _mesa_prim
*prim
;
963 unsigned int index_type_size
= 0;
964 uintptr_t min_index_ptr
, max_index_ptr
;
965 GLboolean fallback
= GL_FALSE
;
971 FLUSH_CURRENT( ctx
, 0 );
973 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
977 prim
= calloc(1, primcount
* sizeof(*prim
));
979 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
983 /* Decide if we can do this all as one set of primitives sharing the
984 * same index buffer, or if we have to reset the index pointer per
989 /* check for dirty state again */
991 _mesa_update_state( ctx
);
994 case GL_UNSIGNED_INT
:
997 case GL_UNSIGNED_SHORT
:
1000 case GL_UNSIGNED_BYTE
:
1001 index_type_size
= 1;
1007 min_index_ptr
= (uintptr_t)indices
[0];
1009 for (i
= 0; i
< primcount
; i
++) {
1010 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1011 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1012 index_type_size
* count
[i
]);
1015 /* Check if we can handle this thing as a bunch of index offsets from the
1016 * same index pointer. If we can't, then we have to fall back to doing
1017 * a draw_prims per primitive.
1019 if (index_type_size
!= 1) {
1020 for (i
= 0; i
< primcount
; i
++) {
1021 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1028 /* If the index buffer isn't in a VBO, then treating the application's
1029 * subranges of the index buffer as one large index buffer may lead to
1030 * us reading unmapped memory.
1032 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1036 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1038 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1039 ib
.ptr
= (void *)min_index_ptr
;
1041 for (i
= 0; i
< primcount
; i
++) {
1042 prim
[i
].begin
= (i
== 0);
1043 prim
[i
].end
= (i
== primcount
- 1);
1046 prim
[i
].mode
= mode
;
1047 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1048 prim
[i
].count
= count
[i
];
1049 prim
[i
].indexed
= 1;
1050 prim
[i
].num_instances
= 1;
1051 if (basevertex
!= NULL
)
1052 prim
[i
].basevertex
= basevertex
[i
];
1054 prim
[i
].basevertex
= 0;
1057 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1060 /* render one prim at a time */
1061 for (i
= 0; i
< primcount
; i
++) {
1062 ib
.count
= count
[i
];
1064 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1065 ib
.ptr
= indices
[i
];
1071 prim
[0].mode
= mode
;
1073 prim
[0].count
= count
[i
];
1074 prim
[0].indexed
= 1;
1075 prim
[0].num_instances
= 1;
1076 if (basevertex
!= NULL
)
1077 prim
[0].basevertex
= basevertex
[i
];
1079 prim
[0].basevertex
= 0;
1081 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1090 static void GLAPIENTRY
1091 vbo_exec_MultiDrawElements(GLenum mode
,
1092 const GLsizei
*count
, GLenum type
,
1093 const GLvoid
**indices
,
1096 GET_CURRENT_CONTEXT(ctx
);
1099 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1101 for (i
= 0; i
< primcount
; i
++) {
1102 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1107 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1112 static void GLAPIENTRY
1113 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1114 const GLsizei
*count
, GLenum type
,
1115 const GLvoid
**indices
,
1117 const GLsizei
*basevertex
)
1119 GET_CURRENT_CONTEXT(ctx
);
1122 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1124 for (i
= 0; i
< primcount
; i
++) {
1125 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1130 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1136 * Plug in the immediate-mode vertex array drawing commands into the
1137 * givven vbo_exec_context object.
1140 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1143 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1144 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1145 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1146 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1147 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1148 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1149 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1150 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1151 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1153 exec
->vtxfmt
.DrawArrays
= _mesa_noop_DrawArrays
;
1154 exec
->vtxfmt
.DrawElements
= _mesa_noop_DrawElements
;
1155 exec
->vtxfmt
.DrawRangeElements
= _mesa_noop_DrawRangeElements
;
1156 exec
->vtxfmt
.MultiDrawElementsEXT
= _mesa_noop_MultiDrawElements
;
1157 exec
->vtxfmt
.DrawElementsBaseVertex
= _mesa_noop_DrawElementsBaseVertex
;
1158 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= _mesa_noop_DrawRangeElementsBaseVertex
;
1159 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= _mesa_noop_MultiDrawElementsBaseVertex
;
1165 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1173 * The following functions are only used for OpenGL ES 1/2 support.
1174 * And some aren't even supported (yet) in ES 1/2.
1179 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1181 vbo_exec_DrawArrays(mode
, first
, count
);
1186 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1187 const GLvoid
*indices
)
1189 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1194 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1195 const GLvoid
*indices
, GLint basevertex
)
1197 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1202 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1203 GLenum type
, const GLvoid
*indices
)
1205 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1210 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1211 GLsizei count
, GLenum type
,
1212 const GLvoid
*indices
, GLint basevertex
)
1214 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1215 indices
, basevertex
);
1220 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1221 const GLvoid
**indices
, GLsizei primcount
)
1223 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1228 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1229 const GLsizei
*count
, GLenum type
,
1230 const GLvoid
**indices
, GLsizei primcount
,
1231 const GLint
*basevertex
)
1233 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1234 primcount
, basevertex
);