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 * Compute min and max elements by scanning the index buffer for
43 * glDraw[Range]Elements() calls.
46 vbo_get_minmax_index(struct gl_context
*ctx
,
47 const struct _mesa_prim
*prim
,
48 const struct _mesa_index_buffer
*ib
,
49 GLuint
*min_index
, GLuint
*max_index
)
52 GLuint count
= prim
->count
;
55 if (_mesa_is_bufferobj(ib
->obj
)) {
57 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
58 GL_READ_ONLY
, ib
->obj
);
59 indices
= ADD_POINTERS(map
, ib
->ptr
);
65 case GL_UNSIGNED_INT
: {
66 const GLuint
*ui_indices
= (const GLuint
*)indices
;
67 GLuint max_ui
= ui_indices
[count
-1];
68 GLuint min_ui
= ui_indices
[0];
69 for (i
= 0; i
< count
; i
++) {
70 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
71 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
77 case GL_UNSIGNED_SHORT
: {
78 const GLushort
*us_indices
= (const GLushort
*)indices
;
79 GLuint max_us
= us_indices
[count
-1];
80 GLuint min_us
= us_indices
[0];
81 for (i
= 0; i
< count
; i
++) {
82 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
83 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
89 case GL_UNSIGNED_BYTE
: {
90 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
91 GLuint max_ub
= ub_indices
[count
-1];
92 GLuint min_ub
= ub_indices
[0];
93 for (i
= 0; i
< count
; i
++) {
94 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
95 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
106 if (_mesa_is_bufferobj(ib
->obj
)) {
107 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, ib
->obj
);
113 * Check that element 'j' of the array has reasonable data.
115 * For debugging purposes; not normally used.
118 check_array_data(struct gl_context
*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 printf("Bad array data:\n");
141 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
142 printf(" Array %u at %p\n", attrib
, (void* ) array
);
143 printf(" Type 0x%x, Size %d, Stride %d\n",
144 array
->Type
, array
->Size
, array
->Stride
);
145 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(struct gl_context
*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.
176 * For debug purposes; not normally used.
179 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
180 const void *elements
, GLint basevertex
)
182 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
186 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
187 elemMap
= ctx
->Driver
.MapBuffer(ctx
,
188 GL_ELEMENT_ARRAY_BUFFER_ARB
,
190 ctx
->Array
.ElementArrayBufferObj
);
191 elements
= ADD_POINTERS(elements
, elemMap
);
194 for (i
= 0; i
< count
; i
++) {
199 case GL_UNSIGNED_BYTE
:
200 j
= ((const GLubyte
*) elements
)[i
];
202 case GL_UNSIGNED_SHORT
:
203 j
= ((const GLushort
*) elements
)[i
];
205 case GL_UNSIGNED_INT
:
206 j
= ((const GLuint
*) elements
)[i
];
212 /* check element j of each enabled array */
213 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
214 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
215 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
216 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
217 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
218 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
220 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
221 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
222 VERT_ATTRIB_GENERIC0
+ k
, j
);
226 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
227 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
228 ctx
->Array
.ElementArrayBufferObj
);
231 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
232 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
233 unmap_array_buffer(ctx
, &arrayObj
->Color
);
234 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
235 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
237 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
238 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
244 * Check array data, looking for NaNs, etc.
247 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
254 * Print info/data for glDrawArrays(), for debugging.
257 print_draw_arrays(struct gl_context
*ctx
, struct vbo_exec_context
*exec
,
258 GLenum mode
, GLint start
, GLsizei count
)
262 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
265 for (i
= 0; i
< 32; i
++) {
266 GLuint bufName
= exec
->array
.inputs
[i
]->BufferObj
->Name
;
267 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
268 printf("attr %2d: size %d stride %d enabled %d "
269 "ptr %p Bufobj %u\n",
271 exec
->array
.inputs
[i
]->Size
,
273 /*exec->array.inputs[i]->Enabled,*/
274 exec
->array
.legacy_array
[i
]->Enabled
,
275 exec
->array
.inputs
[i
]->Ptr
,
279 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, bufName
);
280 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
281 GL_READ_ONLY_ARB
, buf
);
282 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
283 float *f
= (float *) (p
+ offset
);
286 int n
= (count
* stride
) / 4;
289 printf(" Data at offset %d:\n", offset
);
290 for (i
= 0; i
< n
; i
++) {
291 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
293 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, buf
);
300 * Bind the VBO executor to the current vertex array object prior
303 * Just translate the arrayobj into a sane layout.
306 bind_array_obj(struct gl_context
*ctx
)
308 struct vbo_context
*vbo
= vbo_context(ctx
);
309 struct vbo_exec_context
*exec
= &vbo
->exec
;
310 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
313 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
314 * rather than as individual named arrays. Then this function can
317 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
318 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
319 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
320 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
321 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
322 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
323 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
324 if (arrayObj
->PointSize
.Enabled
) {
325 /* this aliases COLOR_INDEX */
326 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
328 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
330 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
331 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
333 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
334 assert(i
< Elements(exec
->array
.generic_array
));
335 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
338 exec
->array
.array_obj
= arrayObj
->Name
;
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 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
356 GLbitfield const_inputs
= 0x0;
359 exec
->array
.program_mode
= get_program_mode(ctx
);
360 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
362 switch (exec
->array
.program_mode
) {
364 /* When no vertex program is active (or the vertex program is generated
365 * from fixed-function state). We put the material values into the
366 * generic slots. This is the only situation where material values
367 * are available as per-vertex attributes.
369 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
370 if (exec
->array
.legacy_array
[i
]->Enabled
)
371 inputs
[i
] = exec
->array
.legacy_array
[i
];
373 inputs
[i
] = &vbo
->legacy_currval
[i
];
374 const_inputs
|= 1 << i
;
378 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
379 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
380 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
383 /* Could use just about anything, just to fill in the empty
386 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
387 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
388 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
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_TEX7
; i
++) {
398 if (exec
->array
.generic_array
[i
]->Enabled
)
399 inputs
[i
] = exec
->array
.generic_array
[i
];
400 else if (exec
->array
.legacy_array
[i
]->Enabled
)
401 inputs
[i
] = exec
->array
.legacy_array
[i
];
403 inputs
[i
] = &vbo
->legacy_currval
[i
];
404 const_inputs
|= 1 << i
;
408 /* Could use just about anything, just to fill in the empty
411 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
412 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
413 const_inputs
|= 1 << i
;
418 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
419 * attribute array aliases and overrides the legacy position array.
421 * Otherwise, legacy attributes available in the legacy slots,
422 * generic attributes in the generic slots and materials are not
423 * available as per-vertex attributes.
425 if (exec
->array
.generic_array
[0]->Enabled
)
426 inputs
[0] = exec
->array
.generic_array
[0];
427 else if (exec
->array
.legacy_array
[0]->Enabled
)
428 inputs
[0] = exec
->array
.legacy_array
[0];
430 inputs
[0] = &vbo
->legacy_currval
[0];
431 const_inputs
|= 1 << 0;
434 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
435 if (exec
->array
.legacy_array
[i
]->Enabled
)
436 inputs
[i
] = exec
->array
.legacy_array
[i
];
438 inputs
[i
] = &vbo
->legacy_currval
[i
];
439 const_inputs
|= 1 << i
;
443 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
444 if (exec
->array
.generic_array
[i
]->Enabled
)
445 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
447 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
448 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
455 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
460 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
461 * These will point to the arrays to actually use for drawing. Some will
462 * be user-provided arrays, other will be zero-stride const-valued arrays.
463 * Note that this might set the _NEW_ARRAY dirty flag so state validation
464 * must be done after this call.
467 bind_arrays(struct gl_context
*ctx
)
470 recalculate_input_bindings(ctx
);
476 * Called from glDrawArrays when in immediate mode (not display list mode).
478 static void GLAPIENTRY
479 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
481 GET_CURRENT_CONTEXT(ctx
);
482 struct vbo_context
*vbo
= vbo_context(ctx
);
483 struct vbo_exec_context
*exec
= &vbo
->exec
;
484 struct _mesa_prim prim
[1];
486 if (MESA_VERBOSE
& VERBOSE_DRAW
)
487 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
488 _mesa_lookup_enum_by_nr(mode
), start
, count
);
490 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
493 FLUSH_CURRENT( ctx
, 0 );
495 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
500 check_draw_arrays_data(ctx
, start
, count
);
502 (void) check_draw_arrays_data
;
507 /* Again... because we may have changed the bitmask of per-vertex varying
508 * attributes. If we regenerate the fixed-function vertex program now
509 * we may be able to prune down the number of vertex attributes which we
510 * need in the shader.
513 _mesa_update_state( ctx
);
520 prim
[0].start
= start
;
521 prim
[0].count
= count
;
523 prim
[0].basevertex
= 0;
524 prim
[0].num_instances
= 1;
526 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
527 GL_TRUE
, start
, start
+ count
- 1 );
530 print_draw_arrays(ctx
, exec
, mode
, start
, count
);
532 (void) print_draw_arrays
;
538 * Called from glDrawArraysInstanced when in immediate mode (not
539 * display list mode).
541 static void GLAPIENTRY
542 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
545 GET_CURRENT_CONTEXT(ctx
);
546 struct vbo_context
*vbo
= vbo_context(ctx
);
547 struct vbo_exec_context
*exec
= &vbo
->exec
;
548 struct _mesa_prim prim
[1];
550 if (MESA_VERBOSE
& VERBOSE_DRAW
)
551 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
552 _mesa_lookup_enum_by_nr(mode
), start
, count
, primcount
);
554 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, primcount
))
557 FLUSH_CURRENT( ctx
, 0 );
559 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
564 check_draw_arrays_data(ctx
, start
, count
);
569 /* Again... because we may have changed the bitmask of per-vertex varying
570 * attributes. If we regenerate the fixed-function vertex program now
571 * we may be able to prune down the number of vertex attributes which we
572 * need in the shader.
575 _mesa_update_state( ctx
);
582 prim
[0].start
= start
;
583 prim
[0].count
= count
;
585 prim
[0].basevertex
= 0;
586 prim
[0].num_instances
= primcount
;
588 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
589 GL_TRUE
, start
, start
+ count
- 1 );
592 print_draw_arrays(ctx
, exec
, mode
, start
, count
);
598 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
602 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
604 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
605 GL_ELEMENT_ARRAY_BUFFER_ARB
,
607 ctx
->Array
.ElementArrayBufferObj
);
609 case GL_UNSIGNED_BYTE
:
611 const GLubyte
*us
= (const GLubyte
*) map
;
613 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
614 printf("%02x ", us
[i
]);
621 case GL_UNSIGNED_SHORT
:
623 const GLushort
*us
= (const GLushort
*) map
;
625 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
626 printf("%04x ", us
[i
]);
633 case GL_UNSIGNED_INT
:
635 const GLuint
*us
= (const GLuint
*) map
;
637 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
638 printf("%08x ", us
[i
]);
649 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
650 ctx
->Array
.ElementArrayBufferObj
);
655 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
656 * Do the rendering for a glDrawElements or glDrawRangeElements call after
657 * we've validated buffer bounds, etc.
660 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
661 GLboolean index_bounds_valid
,
662 GLuint start
, GLuint end
,
663 GLsizei count
, GLenum type
,
664 const GLvoid
*indices
,
665 GLint basevertex
, GLint primcount
)
667 struct vbo_context
*vbo
= vbo_context(ctx
);
668 struct vbo_exec_context
*exec
= &vbo
->exec
;
669 struct _mesa_index_buffer ib
;
670 struct _mesa_prim prim
[1];
672 FLUSH_CURRENT( ctx
, 0 );
674 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
680 /* check for dirty state again */
682 _mesa_update_state( ctx
);
686 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
695 prim
[0].count
= count
;
697 prim
[0].basevertex
= basevertex
;
698 prim
[0].num_instances
= primcount
;
700 /* Need to give special consideration to rendering a range of
701 * indices starting somewhere above zero. Typically the
702 * application is issuing multiple DrawRangeElements() to draw
703 * successive primitives layed out linearly in the vertex arrays.
704 * Unless the vertex arrays are all in a VBO (or locked as with
705 * CVA), the OpenGL semantics imply that we need to re-read or
706 * re-upload the vertex data on each draw call.
708 * In the case of hardware tnl, we want to avoid starting the
709 * upload at zero, as it will mean every draw call uploads an
710 * increasing amount of not-used vertex data. Worse - in the
711 * software tnl module, all those vertices might be transformed and
712 * lit but never rendered.
714 * If we just upload or transform the vertices in start..end,
715 * however, the indices will be incorrect.
717 * At this level, we don't know exactly what the requirements of
718 * the backend are going to be, though it will likely boil down to
721 * 1) Do nothing, everything is in a VBO and is processed once
724 * 2) Adjust the indices and vertex arrays so that start becomes
727 * Rather than doing anything here, I'll provide a helper function
728 * for the latter case elsewhere.
731 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
732 index_bounds_valid
, start
, end
);
737 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
739 static void GLAPIENTRY
740 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
741 GLuint start
, GLuint end
,
742 GLsizei count
, GLenum type
,
743 const GLvoid
*indices
,
746 static GLuint warnCount
= 0;
747 GET_CURRENT_CONTEXT(ctx
);
749 if (MESA_VERBOSE
& VERBOSE_DRAW
)
751 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
752 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
753 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
755 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
756 type
, indices
, basevertex
))
759 /* NOTE: It's important that 'end' is a reasonable value.
760 * in _tnl_draw_prims(), we use end to determine how many vertices
761 * to transform. If it's too large, we can unnecessarily split prims
762 * or we can read/write out of memory in several different places!
765 /* Catch/fix some potential user errors */
766 if (type
== GL_UNSIGNED_BYTE
) {
767 start
= MIN2(start
, 0xff);
768 end
= MIN2(end
, 0xff);
770 else if (type
== GL_UNSIGNED_SHORT
) {
771 start
= MIN2(start
, 0xffff);
772 end
= MIN2(end
, 0xffff);
775 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
776 /* the max element is out of bounds of one or more enabled arrays */
779 if (warnCount
< 10) {
780 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
781 "type 0x%x, indices=%p)\n"
782 "\tend is out of bounds (max=%u) "
783 "Element Buffer %u (size %d)\n"
784 "\tThis should probably be fixed in the application.",
785 start
, end
, count
, type
, indices
,
786 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
787 ctx
->Array
.ElementArrayBufferObj
->Name
,
788 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
792 dump_element_buffer(ctx
, type
);
795 _mesa_print_arrays(ctx
);
798 /* 'end' was out of bounds, but now let's check the actual array
799 * indexes to see if any of them are out of bounds.
802 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
803 ctx
->Array
.ElementArrayBufferObj
);
804 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
805 if (warnCount
< 10) {
806 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
807 "count %d, type 0x%x, indices=%p)\n"
808 "\tindex=%u is out of bounds (max=%u) "
809 "Element Buffer %u (size %d)\n"
810 "\tSkipping the glDrawRangeElements() call",
811 start
, end
, count
, type
, indices
, max
,
812 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
813 ctx
->Array
.ElementArrayBufferObj
->Name
,
814 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
817 /* XXX we could also find the min index and compare to 'start'
818 * to see if start is correct. But it's more likely to get the
824 /* Set 'end' to the max possible legal value */
825 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
826 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
829 printf("glDraw[Range]Elements{,BaseVertex}"
830 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
832 start
, end
, type
, count
,
833 ctx
->Array
.ElementArrayBufferObj
->Name
,
838 check_draw_elements_data(ctx
, count
, type
, indices
);
840 (void) check_draw_elements_data
;
843 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
844 count
, type
, indices
, basevertex
, 1);
849 * Called by glDrawRangeElements() in immediate mode.
851 static void GLAPIENTRY
852 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
853 GLsizei count
, GLenum type
, const GLvoid
*indices
)
855 GET_CURRENT_CONTEXT(ctx
);
857 if (MESA_VERBOSE
& VERBOSE_DRAW
)
859 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
860 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
861 _mesa_lookup_enum_by_nr(type
), indices
);
863 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
869 * Called by glDrawElements() in immediate mode.
871 static void GLAPIENTRY
872 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
873 const GLvoid
*indices
)
875 GET_CURRENT_CONTEXT(ctx
);
877 if (MESA_VERBOSE
& VERBOSE_DRAW
)
878 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
879 _mesa_lookup_enum_by_nr(mode
), count
,
880 _mesa_lookup_enum_by_nr(type
), indices
);
882 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
885 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
886 count
, type
, indices
, 0, 1);
891 * Called by glDrawElementsBaseVertex() in immediate mode.
893 static void GLAPIENTRY
894 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
895 const GLvoid
*indices
, GLint basevertex
)
897 GET_CURRENT_CONTEXT(ctx
);
899 if (MESA_VERBOSE
& VERBOSE_DRAW
)
900 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
901 _mesa_lookup_enum_by_nr(mode
), count
,
902 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
904 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
908 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
909 count
, type
, indices
, basevertex
, 1);
914 * Called by glDrawElementsInstanced() in immediate mode.
916 static void GLAPIENTRY
917 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
918 const GLvoid
*indices
, GLsizei primcount
)
920 GET_CURRENT_CONTEXT(ctx
);
922 if (MESA_VERBOSE
& VERBOSE_DRAW
)
923 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
924 _mesa_lookup_enum_by_nr(mode
), count
,
925 _mesa_lookup_enum_by_nr(type
), indices
, primcount
);
927 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
931 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
932 count
, type
, indices
, 0, primcount
);
937 * Inner support for both _mesa_MultiDrawElements() and
938 * _mesa_MultiDrawRangeElements().
939 * This does the actual rendering after we've checked array indexes, etc.
942 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
943 const GLsizei
*count
, GLenum type
,
944 const GLvoid
**indices
, GLsizei primcount
,
945 const GLint
*basevertex
)
947 struct vbo_context
*vbo
= vbo_context(ctx
);
948 struct vbo_exec_context
*exec
= &vbo
->exec
;
949 struct _mesa_index_buffer ib
;
950 struct _mesa_prim
*prim
;
951 unsigned int index_type_size
= 0;
952 uintptr_t min_index_ptr
, max_index_ptr
;
953 GLboolean fallback
= GL_FALSE
;
959 FLUSH_CURRENT( ctx
, 0 );
961 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
965 prim
= calloc(1, primcount
* sizeof(*prim
));
967 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
971 /* Decide if we can do this all as one set of primitives sharing the
972 * same index buffer, or if we have to reset the index pointer per
977 /* check for dirty state again */
979 _mesa_update_state( ctx
);
982 case GL_UNSIGNED_INT
:
985 case GL_UNSIGNED_SHORT
:
988 case GL_UNSIGNED_BYTE
:
995 min_index_ptr
= (uintptr_t)indices
[0];
997 for (i
= 0; i
< primcount
; i
++) {
998 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
999 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1000 index_type_size
* count
[i
]);
1003 /* Check if we can handle this thing as a bunch of index offsets from the
1004 * same index pointer. If we can't, then we have to fall back to doing
1005 * a draw_prims per primitive.
1007 if (index_type_size
!= 1) {
1008 for (i
= 0; i
< primcount
; i
++) {
1009 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1016 /* If the index buffer isn't in a VBO, then treating the application's
1017 * subranges of the index buffer as one large index buffer may lead to
1018 * us reading unmapped memory.
1020 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1024 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1026 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1027 ib
.ptr
= (void *)min_index_ptr
;
1029 for (i
= 0; i
< primcount
; i
++) {
1030 prim
[i
].begin
= (i
== 0);
1031 prim
[i
].end
= (i
== primcount
- 1);
1034 prim
[i
].mode
= mode
;
1035 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1036 prim
[i
].count
= count
[i
];
1037 prim
[i
].indexed
= 1;
1038 prim
[i
].num_instances
= 1;
1039 if (basevertex
!= NULL
)
1040 prim
[i
].basevertex
= basevertex
[i
];
1042 prim
[i
].basevertex
= 0;
1045 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1048 /* render one prim at a time */
1049 for (i
= 0; i
< primcount
; i
++) {
1050 ib
.count
= count
[i
];
1052 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1053 ib
.ptr
= indices
[i
];
1059 prim
[0].mode
= mode
;
1061 prim
[0].count
= count
[i
];
1062 prim
[0].indexed
= 1;
1063 prim
[0].num_instances
= 1;
1064 if (basevertex
!= NULL
)
1065 prim
[0].basevertex
= basevertex
[i
];
1067 prim
[0].basevertex
= 0;
1069 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1078 static void GLAPIENTRY
1079 vbo_exec_MultiDrawElements(GLenum mode
,
1080 const GLsizei
*count
, GLenum type
,
1081 const GLvoid
**indices
,
1084 GET_CURRENT_CONTEXT(ctx
);
1087 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1089 for (i
= 0; i
< primcount
; i
++) {
1090 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1095 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1100 static void GLAPIENTRY
1101 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1102 const GLsizei
*count
, GLenum type
,
1103 const GLvoid
**indices
,
1105 const GLsizei
*basevertex
)
1107 GET_CURRENT_CONTEXT(ctx
);
1110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1112 for (i
= 0; i
< primcount
; i
++) {
1113 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1118 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1124 * Plug in the immediate-mode vertex array drawing commands into the
1125 * givven vbo_exec_context object.
1128 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1130 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1131 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1132 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1133 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1134 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1135 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1136 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1137 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1138 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1143 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1151 * The following functions are only used for OpenGL ES 1/2 support.
1152 * And some aren't even supported (yet) in ES 1/2.
1157 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1159 vbo_exec_DrawArrays(mode
, first
, count
);
1164 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1165 const GLvoid
*indices
)
1167 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1172 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1173 const GLvoid
*indices
, GLint basevertex
)
1175 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1180 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1181 GLenum type
, const GLvoid
*indices
)
1183 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1188 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1189 GLsizei count
, GLenum type
,
1190 const GLvoid
*indices
, GLint basevertex
)
1192 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1193 indices
, basevertex
);
1198 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1199 const GLvoid
**indices
, GLsizei primcount
)
1201 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1206 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1207 const GLsizei
*count
, GLenum type
,
1208 const GLvoid
**indices
, GLsizei primcount
,
1209 const GLint
*basevertex
)
1211 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1212 primcount
, basevertex
);