1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/api_validate.h"
33 #include "main/varray.h"
34 #include "main/bufferobj.h"
35 #include "main/enums.h"
36 #include "main/macros.h"
38 #include "vbo_context.h"
42 * All vertex buffers should be in an unmapped state when we're about
43 * to draw. This debug function checks that.
46 check_buffers_are_unmapped(const struct gl_client_array
**inputs
)
51 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
53 struct gl_buffer_object
*obj
= inputs
[i
]->BufferObj
;
54 assert(!_mesa_bufferobj_mapped(obj
));
63 * A debug function that may be called from other parts of Mesa as
64 * needed during debugging.
67 vbo_check_buffers_are_unmapped(struct gl_context
*ctx
)
69 struct vbo_context
*vbo
= vbo_context(ctx
);
70 struct vbo_exec_context
*exec
= &vbo
->exec
;
71 /* check the current vertex arrays */
72 check_buffers_are_unmapped(exec
->array
.inputs
);
73 /* check the current glBegin/glVertex/glEnd-style VBO */
74 assert(!_mesa_bufferobj_mapped(exec
->vtx
.bufferobj
));
80 * Compute min and max elements by scanning the index buffer for
81 * glDraw[Range]Elements() calls.
82 * If primitive restart is enabled, we need to ignore restart
83 * indexes when computing min/max.
86 vbo_get_minmax_index(struct gl_context
*ctx
,
87 const struct _mesa_prim
*prim
,
88 const struct _mesa_index_buffer
*ib
,
89 GLuint
*min_index
, GLuint
*max_index
)
91 const GLboolean restart
= ctx
->Array
.PrimitiveRestart
;
92 const GLuint restartIndex
= ctx
->Array
.RestartIndex
;
93 const GLuint count
= prim
->count
;
97 if (_mesa_is_bufferobj(ib
->obj
)) {
99 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
100 GL_READ_ONLY
, ib
->obj
);
101 indices
= ADD_POINTERS(map
, ib
->ptr
);
107 case GL_UNSIGNED_INT
: {
108 const GLuint
*ui_indices
= (const GLuint
*)indices
;
112 for (i
= 0; i
< count
; i
++) {
113 if (ui_indices
[i
] != restartIndex
) {
114 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
115 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
120 for (i
= 0; i
< count
; i
++) {
121 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
122 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
129 case GL_UNSIGNED_SHORT
: {
130 const GLushort
*us_indices
= (const GLushort
*)indices
;
134 for (i
= 0; i
< count
; i
++) {
135 if (us_indices
[i
] != restartIndex
) {
136 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
137 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
142 for (i
= 0; i
< count
; i
++) {
143 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
144 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
151 case GL_UNSIGNED_BYTE
: {
152 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
156 for (i
= 0; i
< count
; i
++) {
157 if (ub_indices
[i
] != restartIndex
) {
158 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
159 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
164 for (i
= 0; i
< count
; i
++) {
165 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
166 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
178 if (_mesa_is_bufferobj(ib
->obj
)) {
179 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, ib
->obj
);
185 * Check that element 'j' of the array has reasonable data.
187 * For debugging purposes; not normally used.
190 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
191 GLuint attrib
, GLuint j
)
193 if (array
->Enabled
) {
194 const void *data
= array
->Ptr
;
195 if (_mesa_is_bufferobj(array
->BufferObj
)) {
196 if (!array
->BufferObj
->Pointer
) {
197 /* need to map now */
198 array
->BufferObj
->Pointer
=
199 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
200 GL_READ_ONLY
, array
->BufferObj
);
202 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
204 switch (array
->Type
) {
207 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
209 for (k
= 0; k
< array
->Size
; k
++) {
210 if (IS_INF_OR_NAN(f
[k
]) ||
211 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
212 printf("Bad array data:\n");
213 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
214 printf(" Array %u at %p\n", attrib
, (void* ) array
);
215 printf(" Type 0x%x, Size %d, Stride %d\n",
216 array
->Type
, array
->Size
, array
->Stride
);
217 printf(" Address/offset %p in Buffer Object %u\n",
218 array
->Ptr
, array
->BufferObj
->Name
);
219 f
[k
] = 1.0; /* XXX replace the bad value! */
221 /*assert(!IS_INF_OR_NAN(f[k]));*/
233 * Unmap the buffer object referenced by given array, if mapped.
236 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
238 if (array
->Enabled
&&
239 _mesa_is_bufferobj(array
->BufferObj
) &&
240 _mesa_bufferobj_mapped(array
->BufferObj
)) {
241 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, array
->BufferObj
);
247 * Examine the array's data for NaNs, etc.
248 * For debug purposes; not normally used.
251 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
252 const void *elements
, GLint basevertex
)
254 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
258 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
259 elemMap
= ctx
->Driver
.MapBuffer(ctx
,
260 GL_ELEMENT_ARRAY_BUFFER_ARB
,
262 ctx
->Array
.ElementArrayBufferObj
);
263 elements
= ADD_POINTERS(elements
, elemMap
);
266 for (i
= 0; i
< count
; i
++) {
271 case GL_UNSIGNED_BYTE
:
272 j
= ((const GLubyte
*) elements
)[i
];
274 case GL_UNSIGNED_SHORT
:
275 j
= ((const GLushort
*) elements
)[i
];
277 case GL_UNSIGNED_INT
:
278 j
= ((const GLuint
*) elements
)[i
];
284 /* check element j of each enabled array */
285 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
286 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
287 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
288 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
289 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
290 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
292 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
293 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
294 VERT_ATTRIB_GENERIC0
+ k
, j
);
298 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
299 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
300 ctx
->Array
.ElementArrayBufferObj
);
303 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
304 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
305 unmap_array_buffer(ctx
, &arrayObj
->Color
);
306 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
307 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
309 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
310 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
316 * Check array data, looking for NaNs, etc.
319 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
326 * Print info/data for glDrawArrays(), for debugging.
329 print_draw_arrays(struct gl_context
*ctx
,
330 GLenum mode
, GLint start
, GLsizei count
)
332 struct vbo_context
*vbo
= vbo_context(ctx
);
333 struct vbo_exec_context
*exec
= &vbo
->exec
;
336 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
339 for (i
= 0; i
< 32; i
++) {
340 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
341 GLuint bufName
= bufObj
->Name
;
342 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
343 printf("attr %2d: size %d stride %d enabled %d "
344 "ptr %p Bufobj %u\n",
346 exec
->array
.inputs
[i
]->Size
,
348 /*exec->array.inputs[i]->Enabled,*/
349 exec
->array
.legacy_array
[i
]->Enabled
,
350 exec
->array
.inputs
[i
]->Ptr
,
354 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
355 GL_READ_ONLY_ARB
, bufObj
);
356 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
357 float *f
= (float *) (p
+ offset
);
360 int n
= (count
* stride
) / 4;
363 printf(" Data at offset %d:\n", offset
);
364 for (i
= 0; i
< n
; i
++) {
365 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
367 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, bufObj
);
374 * Bind the VBO executor to the current vertex array object prior
377 * Just translate the arrayobj into a sane layout.
380 bind_array_obj(struct gl_context
*ctx
)
382 struct vbo_context
*vbo
= vbo_context(ctx
);
383 struct vbo_exec_context
*exec
= &vbo
->exec
;
384 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
387 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
388 * rather than as individual named arrays. Then this function can
391 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
392 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
393 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
394 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
395 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
396 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
397 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
398 if (arrayObj
->PointSize
.Enabled
) {
399 /* this aliases COLOR_INDEX */
400 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
402 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
404 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
405 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
407 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
408 assert(i
< Elements(exec
->array
.generic_array
));
409 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
412 exec
->array
.array_obj
= arrayObj
->Name
;
417 * Set the vbo->exec->inputs[] pointers to point to the enabled
418 * vertex arrays. This depends on the current vertex program/shader
419 * being executed because of whether or not generic vertex arrays
420 * alias the conventional vertex arrays.
421 * For arrays that aren't enabled, we set the input[attrib] pointer
422 * to point at a zero-stride current value "array".
425 recalculate_input_bindings(struct gl_context
*ctx
)
427 struct vbo_context
*vbo
= vbo_context(ctx
);
428 struct vbo_exec_context
*exec
= &vbo
->exec
;
429 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
430 GLbitfield const_inputs
= 0x0;
433 exec
->array
.program_mode
= get_program_mode(ctx
);
434 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
436 switch (exec
->array
.program_mode
) {
438 /* When no vertex program is active (or the vertex program is generated
439 * from fixed-function state). We put the material values into the
440 * generic slots. This is the only situation where material values
441 * are available as per-vertex attributes.
443 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
444 if (exec
->array
.legacy_array
[i
]->Enabled
)
445 inputs
[i
] = exec
->array
.legacy_array
[i
];
447 inputs
[i
] = &vbo
->legacy_currval
[i
];
448 const_inputs
|= 1 << i
;
452 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
453 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
454 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
457 /* Could use just about anything, just to fill in the empty
460 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
461 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
462 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
465 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
466 * because it already takes care of invalidating the state necessary
467 * to revalidate vertex arrays. Not marking the state as dirty also
468 * improves performance (quite significantly in some apps).
470 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
471 ctx
->NewState
|= _NEW_ARRAY
;
475 /* NV_vertex_program - attribute arrays alias and override
476 * conventional, legacy arrays. No materials, and the generic
479 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
480 if (exec
->array
.generic_array
[i
]->Enabled
)
481 inputs
[i
] = exec
->array
.generic_array
[i
];
482 else if (exec
->array
.legacy_array
[i
]->Enabled
)
483 inputs
[i
] = exec
->array
.legacy_array
[i
];
485 inputs
[i
] = &vbo
->legacy_currval
[i
];
486 const_inputs
|= 1 << i
;
490 /* Could use just about anything, just to fill in the empty
493 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
494 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
495 const_inputs
|= 1 << i
;
498 ctx
->NewState
|= _NEW_ARRAY
;
502 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
503 * attribute array aliases and overrides the legacy position array.
505 * Otherwise, legacy attributes available in the legacy slots,
506 * generic attributes in the generic slots and materials are not
507 * available as per-vertex attributes.
509 if (exec
->array
.generic_array
[0]->Enabled
)
510 inputs
[0] = exec
->array
.generic_array
[0];
511 else if (exec
->array
.legacy_array
[0]->Enabled
)
512 inputs
[0] = exec
->array
.legacy_array
[0];
514 inputs
[0] = &vbo
->legacy_currval
[0];
515 const_inputs
|= 1 << 0;
518 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
519 if (exec
->array
.legacy_array
[i
]->Enabled
)
520 inputs
[i
] = exec
->array
.legacy_array
[i
];
522 inputs
[i
] = &vbo
->legacy_currval
[i
];
523 const_inputs
|= 1 << i
;
527 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
528 if (exec
->array
.generic_array
[i
]->Enabled
)
529 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
531 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
532 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
536 ctx
->NewState
|= _NEW_ARRAY
;
540 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
545 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
546 * These will point to the arrays to actually use for drawing. Some will
547 * be user-provided arrays, other will be zero-stride const-valued arrays.
548 * Note that this might set the _NEW_ARRAY dirty flag so state validation
549 * must be done after this call.
552 bind_arrays(struct gl_context
*ctx
)
554 if (!ctx
->Array
.RebindArrays
) {
559 recalculate_input_bindings(ctx
);
560 ctx
->Array
.RebindArrays
= GL_FALSE
;
565 * Helper function called by the other DrawArrays() functions below.
566 * This is where we handle primitive restart for drawing non-indexed
567 * arrays. If primitive restart is enabled, it typically means
568 * splitting one DrawArrays() into two.
571 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
572 GLsizei count
, GLuint numInstances
)
574 struct vbo_context
*vbo
= vbo_context(ctx
);
575 struct vbo_exec_context
*exec
= &vbo
->exec
;
576 struct _mesa_prim prim
[2];
580 /* Again... because we may have changed the bitmask of per-vertex varying
581 * attributes. If we regenerate the fixed-function vertex program now
582 * we may be able to prune down the number of vertex attributes which we
583 * need in the shader.
586 _mesa_update_state(ctx
);
588 /* init most fields to zero */
589 memset(prim
, 0, sizeof(prim
));
593 prim
[0].num_instances
= numInstances
;
595 /* Implement the primitive restart index */
596 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
597 GLuint primCount
= 0;
599 if (ctx
->Array
.RestartIndex
== start
) {
600 /* special case: RestartIndex at beginning */
602 prim
[0].start
= start
+ 1;
603 prim
[0].count
= count
- 1;
607 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
608 /* special case: RestartIndex at end */
610 prim
[0].start
= start
;
611 prim
[0].count
= count
- 1;
616 /* general case: RestartIndex in middle, split into two prims */
617 prim
[0].start
= start
;
618 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
621 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
622 prim
[1].count
= count
- prim
[1].start
;
628 /* draw one or two prims */
629 check_buffers_are_unmapped(exec
->array
.inputs
);
630 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
631 GL_TRUE
, start
, start
+ count
- 1);
635 /* no prim restart */
636 prim
[0].start
= start
;
637 prim
[0].count
= count
;
639 check_buffers_are_unmapped(exec
->array
.inputs
);
640 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
641 GL_TRUE
, start
, start
+ count
- 1);
648 * Called from glDrawArrays when in immediate mode (not display list mode).
650 static void GLAPIENTRY
651 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
653 GET_CURRENT_CONTEXT(ctx
);
655 if (MESA_VERBOSE
& VERBOSE_DRAW
)
656 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
657 _mesa_lookup_enum_by_nr(mode
), start
, count
);
659 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
662 FLUSH_CURRENT( ctx
, 0 );
664 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
669 check_draw_arrays_data(ctx
, start
, count
);
671 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
674 print_draw_arrays(ctx
, mode
, start
, count
);
679 * Called from glDrawArraysInstanced when in immediate mode (not
680 * display list mode).
682 static void GLAPIENTRY
683 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
684 GLsizei numInstances
)
686 GET_CURRENT_CONTEXT(ctx
);
688 if (MESA_VERBOSE
& VERBOSE_DRAW
)
689 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
690 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
692 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
695 FLUSH_CURRENT( ctx
, 0 );
697 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
702 check_draw_arrays_data(ctx
, start
, count
);
704 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
707 print_draw_arrays(ctx
, mode
, start
, count
);
712 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
716 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
718 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
719 GL_ELEMENT_ARRAY_BUFFER_ARB
,
721 ctx
->Array
.ElementArrayBufferObj
);
723 case GL_UNSIGNED_BYTE
:
725 const GLubyte
*us
= (const GLubyte
*) map
;
727 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
728 printf("%02x ", us
[i
]);
735 case GL_UNSIGNED_SHORT
:
737 const GLushort
*us
= (const GLushort
*) map
;
739 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
740 printf("%04x ", us
[i
]);
747 case GL_UNSIGNED_INT
:
749 const GLuint
*us
= (const GLuint
*) map
;
751 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
752 printf("%08x ", us
[i
]);
763 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
764 ctx
->Array
.ElementArrayBufferObj
);
769 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
770 * Do the rendering for a glDrawElements or glDrawRangeElements call after
771 * we've validated buffer bounds, etc.
774 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
775 GLboolean index_bounds_valid
,
776 GLuint start
, GLuint end
,
777 GLsizei count
, GLenum type
,
778 const GLvoid
*indices
,
779 GLint basevertex
, GLint numInstances
)
781 struct vbo_context
*vbo
= vbo_context(ctx
);
782 struct vbo_exec_context
*exec
= &vbo
->exec
;
783 struct _mesa_index_buffer ib
;
784 struct _mesa_prim prim
[1];
786 FLUSH_CURRENT( ctx
, 0 );
788 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
794 /* check for dirty state again */
796 _mesa_update_state( ctx
);
800 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
809 prim
[0].count
= count
;
811 prim
[0].basevertex
= basevertex
;
812 prim
[0].num_instances
= numInstances
;
814 /* Need to give special consideration to rendering a range of
815 * indices starting somewhere above zero. Typically the
816 * application is issuing multiple DrawRangeElements() to draw
817 * successive primitives layed out linearly in the vertex arrays.
818 * Unless the vertex arrays are all in a VBO (or locked as with
819 * CVA), the OpenGL semantics imply that we need to re-read or
820 * re-upload the vertex data on each draw call.
822 * In the case of hardware tnl, we want to avoid starting the
823 * upload at zero, as it will mean every draw call uploads an
824 * increasing amount of not-used vertex data. Worse - in the
825 * software tnl module, all those vertices might be transformed and
826 * lit but never rendered.
828 * If we just upload or transform the vertices in start..end,
829 * however, the indices will be incorrect.
831 * At this level, we don't know exactly what the requirements of
832 * the backend are going to be, though it will likely boil down to
835 * 1) Do nothing, everything is in a VBO and is processed once
838 * 2) Adjust the indices and vertex arrays so that start becomes
841 * Rather than doing anything here, I'll provide a helper function
842 * for the latter case elsewhere.
845 check_buffers_are_unmapped(exec
->array
.inputs
);
846 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
847 index_bounds_valid
, start
, end
);
852 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
854 static void GLAPIENTRY
855 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
856 GLuint start
, GLuint end
,
857 GLsizei count
, GLenum type
,
858 const GLvoid
*indices
,
861 static GLuint warnCount
= 0;
862 GET_CURRENT_CONTEXT(ctx
);
864 if (MESA_VERBOSE
& VERBOSE_DRAW
)
866 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
867 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
868 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
870 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
871 type
, indices
, basevertex
))
874 /* NOTE: It's important that 'end' is a reasonable value.
875 * in _tnl_draw_prims(), we use end to determine how many vertices
876 * to transform. If it's too large, we can unnecessarily split prims
877 * or we can read/write out of memory in several different places!
880 /* Catch/fix some potential user errors */
881 if (type
== GL_UNSIGNED_BYTE
) {
882 start
= MIN2(start
, 0xff);
883 end
= MIN2(end
, 0xff);
885 else if (type
== GL_UNSIGNED_SHORT
) {
886 start
= MIN2(start
, 0xffff);
887 end
= MIN2(end
, 0xffff);
890 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
891 /* the max element is out of bounds of one or more enabled arrays */
894 if (warnCount
< 10) {
895 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
896 "type 0x%x, indices=%p)\n"
897 "\tend is out of bounds (max=%u) "
898 "Element Buffer %u (size %d)\n"
899 "\tThis should probably be fixed in the application.",
900 start
, end
, count
, type
, indices
,
901 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
902 ctx
->Array
.ElementArrayBufferObj
->Name
,
903 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
907 dump_element_buffer(ctx
, type
);
910 _mesa_print_arrays(ctx
);
913 /* 'end' was out of bounds, but now let's check the actual array
914 * indexes to see if any of them are out of bounds.
917 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
918 ctx
->Array
.ElementArrayBufferObj
);
919 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
920 if (warnCount
< 10) {
921 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
922 "count %d, type 0x%x, indices=%p)\n"
923 "\tindex=%u is out of bounds (max=%u) "
924 "Element Buffer %u (size %d)\n"
925 "\tSkipping the glDrawRangeElements() call",
926 start
, end
, count
, type
, indices
, max
,
927 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
928 ctx
->Array
.ElementArrayBufferObj
->Name
,
929 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
932 /* XXX we could also find the min index and compare to 'start'
933 * to see if start is correct. But it's more likely to get the
939 /* Set 'end' to the max possible legal value */
940 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
941 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
949 printf("glDraw[Range]Elements{,BaseVertex}"
950 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
952 start
, end
, type
, count
,
953 ctx
->Array
.ElementArrayBufferObj
->Name
,
958 check_draw_elements_data(ctx
, count
, type
, indices
);
960 (void) check_draw_elements_data
;
963 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
964 count
, type
, indices
, basevertex
, 1);
969 * Called by glDrawRangeElements() in immediate mode.
971 static void GLAPIENTRY
972 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
973 GLsizei count
, GLenum type
, const GLvoid
*indices
)
975 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
976 GET_CURRENT_CONTEXT(ctx
);
978 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
979 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
980 _mesa_lookup_enum_by_nr(type
), indices
);
983 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
989 * Called by glDrawElements() in immediate mode.
991 static void GLAPIENTRY
992 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
993 const GLvoid
*indices
)
995 GET_CURRENT_CONTEXT(ctx
);
997 if (MESA_VERBOSE
& VERBOSE_DRAW
)
998 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
999 _mesa_lookup_enum_by_nr(mode
), count
,
1000 _mesa_lookup_enum_by_nr(type
), indices
);
1002 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
1005 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1006 count
, type
, indices
, 0, 1);
1011 * Called by glDrawElementsBaseVertex() in immediate mode.
1013 static void GLAPIENTRY
1014 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1015 const GLvoid
*indices
, GLint basevertex
)
1017 GET_CURRENT_CONTEXT(ctx
);
1019 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1020 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1021 _mesa_lookup_enum_by_nr(mode
), count
,
1022 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1024 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1028 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1029 count
, type
, indices
, basevertex
, 1);
1034 * Called by glDrawElementsInstanced() in immediate mode.
1036 static void GLAPIENTRY
1037 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1038 const GLvoid
*indices
, GLsizei numInstances
)
1040 GET_CURRENT_CONTEXT(ctx
);
1042 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1043 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1044 _mesa_lookup_enum_by_nr(mode
), count
,
1045 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1047 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1051 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1052 count
, type
, indices
, 0, numInstances
);
1056 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1058 static void GLAPIENTRY
1059 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1060 const GLvoid
*indices
, GLsizei numInstances
,
1063 GET_CURRENT_CONTEXT(ctx
);
1065 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1066 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1067 _mesa_lookup_enum_by_nr(mode
), count
,
1068 _mesa_lookup_enum_by_nr(type
), indices
,
1069 numInstances
, basevertex
);
1071 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1072 numInstances
, basevertex
))
1075 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1076 count
, type
, indices
, basevertex
, numInstances
);
1081 * Inner support for both _mesa_MultiDrawElements() and
1082 * _mesa_MultiDrawRangeElements().
1083 * This does the actual rendering after we've checked array indexes, etc.
1086 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1087 const GLsizei
*count
, GLenum type
,
1088 const GLvoid
**indices
, GLsizei primcount
,
1089 const GLint
*basevertex
)
1091 struct vbo_context
*vbo
= vbo_context(ctx
);
1092 struct vbo_exec_context
*exec
= &vbo
->exec
;
1093 struct _mesa_index_buffer ib
;
1094 struct _mesa_prim
*prim
;
1095 unsigned int index_type_size
= 0;
1096 uintptr_t min_index_ptr
, max_index_ptr
;
1097 GLboolean fallback
= GL_FALSE
;
1103 FLUSH_CURRENT( ctx
, 0 );
1105 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1109 prim
= calloc(1, primcount
* sizeof(*prim
));
1111 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1115 /* Decide if we can do this all as one set of primitives sharing the
1116 * same index buffer, or if we have to reset the index pointer per
1121 /* check for dirty state again */
1123 _mesa_update_state( ctx
);
1126 case GL_UNSIGNED_INT
:
1127 index_type_size
= 4;
1129 case GL_UNSIGNED_SHORT
:
1130 index_type_size
= 2;
1132 case GL_UNSIGNED_BYTE
:
1133 index_type_size
= 1;
1139 min_index_ptr
= (uintptr_t)indices
[0];
1141 for (i
= 0; i
< primcount
; i
++) {
1142 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1143 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1144 index_type_size
* count
[i
]);
1147 /* Check if we can handle this thing as a bunch of index offsets from the
1148 * same index pointer. If we can't, then we have to fall back to doing
1149 * a draw_prims per primitive.
1150 * Check that the difference between each prim's indexes is a multiple of
1151 * the index/element size.
1153 if (index_type_size
!= 1) {
1154 for (i
= 0; i
< primcount
; i
++) {
1155 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1162 /* If the index buffer isn't in a VBO, then treating the application's
1163 * subranges of the index buffer as one large index buffer may lead to
1164 * us reading unmapped memory.
1166 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1170 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1172 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1173 ib
.ptr
= (void *)min_index_ptr
;
1175 for (i
= 0; i
< primcount
; i
++) {
1176 prim
[i
].begin
= (i
== 0);
1177 prim
[i
].end
= (i
== primcount
- 1);
1180 prim
[i
].mode
= mode
;
1181 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1182 prim
[i
].count
= count
[i
];
1183 prim
[i
].indexed
= 1;
1184 prim
[i
].num_instances
= 1;
1185 if (basevertex
!= NULL
)
1186 prim
[i
].basevertex
= basevertex
[i
];
1188 prim
[i
].basevertex
= 0;
1191 check_buffers_are_unmapped(exec
->array
.inputs
);
1192 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1195 /* render one prim at a time */
1196 for (i
= 0; i
< primcount
; i
++) {
1197 ib
.count
= count
[i
];
1199 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1200 ib
.ptr
= indices
[i
];
1206 prim
[0].mode
= mode
;
1208 prim
[0].count
= count
[i
];
1209 prim
[0].indexed
= 1;
1210 prim
[0].num_instances
= 1;
1211 if (basevertex
!= NULL
)
1212 prim
[0].basevertex
= basevertex
[i
];
1214 prim
[0].basevertex
= 0;
1216 check_buffers_are_unmapped(exec
->array
.inputs
);
1217 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1226 static void GLAPIENTRY
1227 vbo_exec_MultiDrawElements(GLenum mode
,
1228 const GLsizei
*count
, GLenum type
,
1229 const GLvoid
**indices
,
1232 GET_CURRENT_CONTEXT(ctx
);
1235 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1237 for (i
= 0; i
< primcount
; i
++) {
1238 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1243 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1248 static void GLAPIENTRY
1249 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1250 const GLsizei
*count
, GLenum type
,
1251 const GLvoid
**indices
,
1253 const GLsizei
*basevertex
)
1255 GET_CURRENT_CONTEXT(ctx
);
1258 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1260 for (i
= 0; i
< primcount
; i
++) {
1261 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1266 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1272 * Plug in the immediate-mode vertex array drawing commands into the
1273 * givven vbo_exec_context object.
1276 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1278 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1279 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1280 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1281 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1282 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1283 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1284 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1285 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1286 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1287 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1292 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1300 * The following functions are only used for OpenGL ES 1/2 support.
1301 * And some aren't even supported (yet) in ES 1/2.
1306 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1308 vbo_exec_DrawArrays(mode
, first
, count
);
1313 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1314 const GLvoid
*indices
)
1316 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1321 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1322 const GLvoid
*indices
, GLint basevertex
)
1324 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1329 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1330 GLenum type
, const GLvoid
*indices
)
1332 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1337 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1338 GLsizei count
, GLenum type
,
1339 const GLvoid
*indices
, GLint basevertex
)
1341 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1342 indices
, basevertex
);
1347 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1348 const GLvoid
**indices
, GLsizei primcount
)
1350 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1355 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1356 const GLsizei
*count
, GLenum type
,
1357 const GLvoid
**indices
, GLsizei primcount
,
1358 const GLint
*basevertex
)
1360 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1361 primcount
, basevertex
);