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
.MapBufferRange(ctx
, 0, ib
->obj
->Size
, GL_MAP_READ_BIT
,
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
, 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
.MapBufferRange(ctx
, 0, array
->BufferObj
->Size
,
200 GL_MAP_READ_BIT
, 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
, 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
.MapBufferRange(ctx
, 0,
260 ctx
->Array
.ElementArrayBufferObj
->Size
,
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
, ctx
->Array
.ElementArrayBufferObj
);
302 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
303 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
304 unmap_array_buffer(ctx
, &arrayObj
->Color
);
305 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
306 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
308 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
309 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
315 * Check array data, looking for NaNs, etc.
318 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
325 * Print info/data for glDrawArrays(), for debugging.
328 print_draw_arrays(struct gl_context
*ctx
,
329 GLenum mode
, GLint start
, GLsizei count
)
331 struct vbo_context
*vbo
= vbo_context(ctx
);
332 struct vbo_exec_context
*exec
= &vbo
->exec
;
335 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
338 for (i
= 0; i
< 32; i
++) {
339 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
340 GLuint bufName
= bufObj
->Name
;
341 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
342 printf("attr %2d: size %d stride %d enabled %d "
343 "ptr %p Bufobj %u\n",
345 exec
->array
.inputs
[i
]->Size
,
347 /*exec->array.inputs[i]->Enabled,*/
348 exec
->array
.legacy_array
[i
]->Enabled
,
349 exec
->array
.inputs
[i
]->Ptr
,
353 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
354 GL_MAP_READ_BIT
, bufObj
);
355 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
356 float *f
= (float *) (p
+ offset
);
359 int n
= (count
* stride
) / 4;
362 printf(" Data at offset %d:\n", offset
);
363 for (i
= 0; i
< n
; i
++) {
364 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
366 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
373 * Bind the VBO executor to the current vertex array object prior
376 * Just translate the arrayobj into a sane layout.
379 bind_array_obj(struct gl_context
*ctx
)
381 struct vbo_context
*vbo
= vbo_context(ctx
);
382 struct vbo_exec_context
*exec
= &vbo
->exec
;
383 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
386 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
387 * rather than as individual named arrays. Then this function can
390 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
391 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
392 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
393 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
394 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
395 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
396 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
397 if (arrayObj
->PointSize
.Enabled
) {
398 /* this aliases COLOR_INDEX */
399 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
401 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
403 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
404 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
406 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
407 assert(i
< Elements(exec
->array
.generic_array
));
408 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
411 exec
->array
.array_obj
= arrayObj
->Name
;
416 * Set the vbo->exec->inputs[] pointers to point to the enabled
417 * vertex arrays. This depends on the current vertex program/shader
418 * being executed because of whether or not generic vertex arrays
419 * alias the conventional vertex arrays.
420 * For arrays that aren't enabled, we set the input[attrib] pointer
421 * to point at a zero-stride current value "array".
424 recalculate_input_bindings(struct gl_context
*ctx
)
426 struct vbo_context
*vbo
= vbo_context(ctx
);
427 struct vbo_exec_context
*exec
= &vbo
->exec
;
428 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
429 GLbitfield const_inputs
= 0x0;
432 exec
->array
.program_mode
= get_program_mode(ctx
);
433 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
435 switch (exec
->array
.program_mode
) {
437 /* When no vertex program is active (or the vertex program is generated
438 * from fixed-function state). We put the material values into the
439 * generic slots. This is the only situation where material values
440 * are available as per-vertex attributes.
442 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
443 if (exec
->array
.legacy_array
[i
]->Enabled
)
444 inputs
[i
] = exec
->array
.legacy_array
[i
];
446 inputs
[i
] = &vbo
->legacy_currval
[i
];
447 const_inputs
|= 1 << i
;
451 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
452 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
453 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
456 /* Could use just about anything, just to fill in the empty
459 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
460 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
461 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
464 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
465 * because it already takes care of invalidating the state necessary
466 * to revalidate vertex arrays. Not marking the state as dirty also
467 * improves performance (quite significantly in some apps).
469 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
470 ctx
->NewState
|= _NEW_ARRAY
;
474 /* NV_vertex_program - attribute arrays alias and override
475 * conventional, legacy arrays. No materials, and the generic
478 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
479 if (exec
->array
.generic_array
[i
]->Enabled
)
480 inputs
[i
] = exec
->array
.generic_array
[i
];
481 else if (exec
->array
.legacy_array
[i
]->Enabled
)
482 inputs
[i
] = exec
->array
.legacy_array
[i
];
484 inputs
[i
] = &vbo
->legacy_currval
[i
];
485 const_inputs
|= 1 << i
;
489 /* Could use just about anything, just to fill in the empty
492 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
493 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
494 const_inputs
|= 1 << i
;
497 ctx
->NewState
|= _NEW_ARRAY
;
501 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
502 * attribute array aliases and overrides the legacy position array.
504 * Otherwise, legacy attributes available in the legacy slots,
505 * generic attributes in the generic slots and materials are not
506 * available as per-vertex attributes.
508 if (exec
->array
.generic_array
[0]->Enabled
)
509 inputs
[0] = exec
->array
.generic_array
[0];
510 else if (exec
->array
.legacy_array
[0]->Enabled
)
511 inputs
[0] = exec
->array
.legacy_array
[0];
513 inputs
[0] = &vbo
->legacy_currval
[0];
514 const_inputs
|= 1 << 0;
517 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
518 if (exec
->array
.legacy_array
[i
]->Enabled
)
519 inputs
[i
] = exec
->array
.legacy_array
[i
];
521 inputs
[i
] = &vbo
->legacy_currval
[i
];
522 const_inputs
|= 1 << i
;
526 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
527 if (exec
->array
.generic_array
[i
]->Enabled
)
528 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
530 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
531 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
535 ctx
->NewState
|= _NEW_ARRAY
;
539 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
544 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
545 * These will point to the arrays to actually use for drawing. Some will
546 * be user-provided arrays, other will be zero-stride const-valued arrays.
547 * Note that this might set the _NEW_ARRAY dirty flag so state validation
548 * must be done after this call.
551 bind_arrays(struct gl_context
*ctx
)
553 if (!ctx
->Array
.RebindArrays
) {
558 recalculate_input_bindings(ctx
);
559 ctx
->Array
.RebindArrays
= GL_FALSE
;
564 * Helper function called by the other DrawArrays() functions below.
565 * This is where we handle primitive restart for drawing non-indexed
566 * arrays. If primitive restart is enabled, it typically means
567 * splitting one DrawArrays() into two.
570 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
571 GLsizei count
, GLuint numInstances
)
573 struct vbo_context
*vbo
= vbo_context(ctx
);
574 struct vbo_exec_context
*exec
= &vbo
->exec
;
575 struct _mesa_prim prim
[2];
579 /* Again... because we may have changed the bitmask of per-vertex varying
580 * attributes. If we regenerate the fixed-function vertex program now
581 * we may be able to prune down the number of vertex attributes which we
582 * need in the shader.
585 _mesa_update_state(ctx
);
587 /* init most fields to zero */
588 memset(prim
, 0, sizeof(prim
));
592 prim
[0].num_instances
= numInstances
;
594 /* Implement the primitive restart index */
595 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
596 GLuint primCount
= 0;
598 if (ctx
->Array
.RestartIndex
== start
) {
599 /* special case: RestartIndex at beginning */
601 prim
[0].start
= start
+ 1;
602 prim
[0].count
= count
- 1;
606 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
607 /* special case: RestartIndex at end */
609 prim
[0].start
= start
;
610 prim
[0].count
= count
- 1;
615 /* general case: RestartIndex in middle, split into two prims */
616 prim
[0].start
= start
;
617 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
620 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
621 prim
[1].count
= count
- prim
[1].start
;
627 /* draw one or two prims */
628 check_buffers_are_unmapped(exec
->array
.inputs
);
629 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
630 GL_TRUE
, start
, start
+ count
- 1);
634 /* no prim restart */
635 prim
[0].start
= start
;
636 prim
[0].count
= count
;
638 check_buffers_are_unmapped(exec
->array
.inputs
);
639 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
640 GL_TRUE
, start
, start
+ count
- 1);
647 * Called from glDrawArrays when in immediate mode (not display list mode).
649 static void GLAPIENTRY
650 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
652 GET_CURRENT_CONTEXT(ctx
);
654 if (MESA_VERBOSE
& VERBOSE_DRAW
)
655 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
656 _mesa_lookup_enum_by_nr(mode
), start
, count
);
658 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
661 FLUSH_CURRENT( ctx
, 0 );
663 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
668 check_draw_arrays_data(ctx
, start
, count
);
670 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
673 print_draw_arrays(ctx
, mode
, start
, count
);
678 * Called from glDrawArraysInstanced when in immediate mode (not
679 * display list mode).
681 static void GLAPIENTRY
682 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
683 GLsizei numInstances
)
685 GET_CURRENT_CONTEXT(ctx
);
687 if (MESA_VERBOSE
& VERBOSE_DRAW
)
688 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
689 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
691 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
694 FLUSH_CURRENT( ctx
, 0 );
696 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
701 check_draw_arrays_data(ctx
, start
, count
);
703 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
706 print_draw_arrays(ctx
, mode
, start
, count
);
711 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
715 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
718 ctx
->Driver
.MapBufferRange(ctx
, 0,
719 ctx
->Array
.ElementArrayBufferObj
->Size
,
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
, ctx
->Array
.ElementArrayBufferObj
);
768 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
769 * Do the rendering for a glDrawElements or glDrawRangeElements call after
770 * we've validated buffer bounds, etc.
773 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
774 GLboolean index_bounds_valid
,
775 GLuint start
, GLuint end
,
776 GLsizei count
, GLenum type
,
777 const GLvoid
*indices
,
778 GLint basevertex
, GLint numInstances
)
780 struct vbo_context
*vbo
= vbo_context(ctx
);
781 struct vbo_exec_context
*exec
= &vbo
->exec
;
782 struct _mesa_index_buffer ib
;
783 struct _mesa_prim prim
[1];
785 FLUSH_CURRENT( ctx
, 0 );
787 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
793 /* check for dirty state again */
795 _mesa_update_state( ctx
);
799 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
808 prim
[0].count
= count
;
810 prim
[0].basevertex
= basevertex
;
811 prim
[0].num_instances
= numInstances
;
813 /* Need to give special consideration to rendering a range of
814 * indices starting somewhere above zero. Typically the
815 * application is issuing multiple DrawRangeElements() to draw
816 * successive primitives layed out linearly in the vertex arrays.
817 * Unless the vertex arrays are all in a VBO (or locked as with
818 * CVA), the OpenGL semantics imply that we need to re-read or
819 * re-upload the vertex data on each draw call.
821 * In the case of hardware tnl, we want to avoid starting the
822 * upload at zero, as it will mean every draw call uploads an
823 * increasing amount of not-used vertex data. Worse - in the
824 * software tnl module, all those vertices might be transformed and
825 * lit but never rendered.
827 * If we just upload or transform the vertices in start..end,
828 * however, the indices will be incorrect.
830 * At this level, we don't know exactly what the requirements of
831 * the backend are going to be, though it will likely boil down to
834 * 1) Do nothing, everything is in a VBO and is processed once
837 * 2) Adjust the indices and vertex arrays so that start becomes
840 * Rather than doing anything here, I'll provide a helper function
841 * for the latter case elsewhere.
844 check_buffers_are_unmapped(exec
->array
.inputs
);
845 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
846 index_bounds_valid
, start
, end
);
851 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
853 static void GLAPIENTRY
854 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
855 GLuint start
, GLuint end
,
856 GLsizei count
, GLenum type
,
857 const GLvoid
*indices
,
860 static GLuint warnCount
= 0;
861 GET_CURRENT_CONTEXT(ctx
);
863 if (MESA_VERBOSE
& VERBOSE_DRAW
)
865 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
866 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
867 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
869 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
870 type
, indices
, basevertex
))
873 /* NOTE: It's important that 'end' is a reasonable value.
874 * in _tnl_draw_prims(), we use end to determine how many vertices
875 * to transform. If it's too large, we can unnecessarily split prims
876 * or we can read/write out of memory in several different places!
879 /* Catch/fix some potential user errors */
880 if (type
== GL_UNSIGNED_BYTE
) {
881 start
= MIN2(start
, 0xff);
882 end
= MIN2(end
, 0xff);
884 else if (type
== GL_UNSIGNED_SHORT
) {
885 start
= MIN2(start
, 0xffff);
886 end
= MIN2(end
, 0xffff);
889 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
890 /* the max element is out of bounds of one or more enabled arrays */
893 if (warnCount
< 10) {
894 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
895 "type 0x%x, indices=%p)\n"
896 "\tend is out of bounds (max=%u) "
897 "Element Buffer %u (size %d)\n"
898 "\tThis should probably be fixed in the application.",
899 start
, end
, count
, type
, indices
,
900 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
901 ctx
->Array
.ElementArrayBufferObj
->Name
,
902 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
906 dump_element_buffer(ctx
, type
);
909 _mesa_print_arrays(ctx
);
911 /* 'end' was out of bounds, but now let's check the actual array
912 * indexes to see if any of them are out of bounds.
915 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
916 ctx
->Array
.ElementArrayBufferObj
);
917 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
918 if (warnCount
< 10) {
919 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
920 "count %d, type 0x%x, indices=%p)\n"
921 "\tindex=%u is out of bounds (max=%u) "
922 "Element Buffer %u (size %d)\n"
923 "\tSkipping the glDrawRangeElements() call",
924 start
, end
, count
, type
, indices
, max
,
925 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
926 ctx
->Array
.ElementArrayBufferObj
->Name
,
927 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
930 /* XXX we could also find the min index and compare to 'start'
931 * to see if start is correct. But it's more likely to get the
936 /* Set 'end' to the max possible legal value */
937 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
938 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
946 printf("glDraw[Range]Elements{,BaseVertex}"
947 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
949 start
, end
, type
, count
,
950 ctx
->Array
.ElementArrayBufferObj
->Name
,
955 check_draw_elements_data(ctx
, count
, type
, indices
);
957 (void) check_draw_elements_data
;
960 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
961 count
, type
, indices
, basevertex
, 1);
966 * Called by glDrawRangeElements() in immediate mode.
968 static void GLAPIENTRY
969 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
970 GLsizei count
, GLenum type
, const GLvoid
*indices
)
972 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
973 GET_CURRENT_CONTEXT(ctx
);
975 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
976 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
977 _mesa_lookup_enum_by_nr(type
), indices
);
980 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
986 * Called by glDrawElements() in immediate mode.
988 static void GLAPIENTRY
989 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
990 const GLvoid
*indices
)
992 GET_CURRENT_CONTEXT(ctx
);
994 if (MESA_VERBOSE
& VERBOSE_DRAW
)
995 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
996 _mesa_lookup_enum_by_nr(mode
), count
,
997 _mesa_lookup_enum_by_nr(type
), indices
);
999 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
1002 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1003 count
, type
, indices
, 0, 1);
1008 * Called by glDrawElementsBaseVertex() in immediate mode.
1010 static void GLAPIENTRY
1011 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1012 const GLvoid
*indices
, GLint basevertex
)
1014 GET_CURRENT_CONTEXT(ctx
);
1016 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1017 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1018 _mesa_lookup_enum_by_nr(mode
), count
,
1019 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1021 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1025 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1026 count
, type
, indices
, basevertex
, 1);
1031 * Called by glDrawElementsInstanced() in immediate mode.
1033 static void GLAPIENTRY
1034 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1035 const GLvoid
*indices
, GLsizei numInstances
)
1037 GET_CURRENT_CONTEXT(ctx
);
1039 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1040 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1041 _mesa_lookup_enum_by_nr(mode
), count
,
1042 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1044 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1048 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1049 count
, type
, indices
, 0, numInstances
);
1053 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1055 static void GLAPIENTRY
1056 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1057 const GLvoid
*indices
, GLsizei numInstances
,
1060 GET_CURRENT_CONTEXT(ctx
);
1062 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1063 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1064 _mesa_lookup_enum_by_nr(mode
), count
,
1065 _mesa_lookup_enum_by_nr(type
), indices
,
1066 numInstances
, basevertex
);
1068 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1069 numInstances
, basevertex
))
1072 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1073 count
, type
, indices
, basevertex
, numInstances
);
1078 * Inner support for both _mesa_MultiDrawElements() and
1079 * _mesa_MultiDrawRangeElements().
1080 * This does the actual rendering after we've checked array indexes, etc.
1083 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1084 const GLsizei
*count
, GLenum type
,
1085 const GLvoid
**indices
, GLsizei primcount
,
1086 const GLint
*basevertex
)
1088 struct vbo_context
*vbo
= vbo_context(ctx
);
1089 struct vbo_exec_context
*exec
= &vbo
->exec
;
1090 struct _mesa_index_buffer ib
;
1091 struct _mesa_prim
*prim
;
1092 unsigned int index_type_size
= 0;
1093 uintptr_t min_index_ptr
, max_index_ptr
;
1094 GLboolean fallback
= GL_FALSE
;
1100 FLUSH_CURRENT( ctx
, 0 );
1102 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1106 prim
= calloc(1, primcount
* sizeof(*prim
));
1108 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1112 /* Decide if we can do this all as one set of primitives sharing the
1113 * same index buffer, or if we have to reset the index pointer per
1118 /* check for dirty state again */
1120 _mesa_update_state( ctx
);
1123 case GL_UNSIGNED_INT
:
1124 index_type_size
= 4;
1126 case GL_UNSIGNED_SHORT
:
1127 index_type_size
= 2;
1129 case GL_UNSIGNED_BYTE
:
1130 index_type_size
= 1;
1136 min_index_ptr
= (uintptr_t)indices
[0];
1138 for (i
= 0; i
< primcount
; i
++) {
1139 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1140 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1141 index_type_size
* count
[i
]);
1144 /* Check if we can handle this thing as a bunch of index offsets from the
1145 * same index pointer. If we can't, then we have to fall back to doing
1146 * a draw_prims per primitive.
1147 * Check that the difference between each prim's indexes is a multiple of
1148 * the index/element size.
1150 if (index_type_size
!= 1) {
1151 for (i
= 0; i
< primcount
; i
++) {
1152 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1159 /* If the index buffer isn't in a VBO, then treating the application's
1160 * subranges of the index buffer as one large index buffer may lead to
1161 * us reading unmapped memory.
1163 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1167 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1169 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1170 ib
.ptr
= (void *)min_index_ptr
;
1172 for (i
= 0; i
< primcount
; i
++) {
1173 prim
[i
].begin
= (i
== 0);
1174 prim
[i
].end
= (i
== primcount
- 1);
1177 prim
[i
].mode
= mode
;
1178 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1179 prim
[i
].count
= count
[i
];
1180 prim
[i
].indexed
= 1;
1181 prim
[i
].num_instances
= 1;
1182 if (basevertex
!= NULL
)
1183 prim
[i
].basevertex
= basevertex
[i
];
1185 prim
[i
].basevertex
= 0;
1188 check_buffers_are_unmapped(exec
->array
.inputs
);
1189 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1192 /* render one prim at a time */
1193 for (i
= 0; i
< primcount
; i
++) {
1194 ib
.count
= count
[i
];
1196 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1197 ib
.ptr
= indices
[i
];
1203 prim
[0].mode
= mode
;
1205 prim
[0].count
= count
[i
];
1206 prim
[0].indexed
= 1;
1207 prim
[0].num_instances
= 1;
1208 if (basevertex
!= NULL
)
1209 prim
[0].basevertex
= basevertex
[i
];
1211 prim
[0].basevertex
= 0;
1213 check_buffers_are_unmapped(exec
->array
.inputs
);
1214 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1223 static void GLAPIENTRY
1224 vbo_exec_MultiDrawElements(GLenum mode
,
1225 const GLsizei
*count
, GLenum type
,
1226 const GLvoid
**indices
,
1229 GET_CURRENT_CONTEXT(ctx
);
1232 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1234 for (i
= 0; i
< primcount
; i
++) {
1235 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1240 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1245 static void GLAPIENTRY
1246 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1247 const GLsizei
*count
, GLenum type
,
1248 const GLvoid
**indices
,
1250 const GLsizei
*basevertex
)
1252 GET_CURRENT_CONTEXT(ctx
);
1255 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1257 for (i
= 0; i
< primcount
; i
++) {
1258 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1263 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1269 * Plug in the immediate-mode vertex array drawing commands into the
1270 * givven vbo_exec_context object.
1273 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1275 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1276 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1277 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1278 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1279 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1280 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1281 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1282 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1283 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1284 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1289 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1297 * The following functions are only used for OpenGL ES 1/2 support.
1298 * And some aren't even supported (yet) in ES 1/2.
1303 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1305 vbo_exec_DrawArrays(mode
, first
, count
);
1310 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1311 const GLvoid
*indices
)
1313 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1318 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1319 const GLvoid
*indices
, GLint basevertex
)
1321 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1326 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1327 GLenum type
, const GLvoid
*indices
)
1329 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1334 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1335 GLsizei count
, GLenum type
,
1336 const GLvoid
*indices
, GLint basevertex
)
1338 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1339 indices
, basevertex
);
1344 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1345 const GLvoid
**indices
, GLsizei primcount
)
1347 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1352 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1353 const GLsizei
*count
, GLenum type
,
1354 const GLvoid
**indices
, GLsizei primcount
,
1355 const GLint
*basevertex
)
1357 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1358 primcount
, basevertex
);