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"
37 #include "main/transformfeedback.h"
39 #include "vbo_context.h"
43 * All vertex buffers should be in an unmapped state when we're about
44 * to draw. This debug function checks that.
47 check_buffers_are_unmapped(const struct gl_client_array
**inputs
)
52 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
54 struct gl_buffer_object
*obj
= inputs
[i
]->BufferObj
;
55 assert(!_mesa_bufferobj_mapped(obj
));
64 * A debug function that may be called from other parts of Mesa as
65 * needed during debugging.
68 vbo_check_buffers_are_unmapped(struct gl_context
*ctx
)
70 struct vbo_context
*vbo
= vbo_context(ctx
);
71 struct vbo_exec_context
*exec
= &vbo
->exec
;
72 /* check the current vertex arrays */
73 check_buffers_are_unmapped(exec
->array
.inputs
);
74 /* check the current glBegin/glVertex/glEnd-style VBO */
75 assert(!_mesa_bufferobj_mapped(exec
->vtx
.bufferobj
));
79 vbo_sizeof_ib_type(GLenum type
)
83 return sizeof(GLuint
);
84 case GL_UNSIGNED_SHORT
:
85 return sizeof(GLushort
);
86 case GL_UNSIGNED_BYTE
:
87 return sizeof(GLubyte
);
89 assert(!"unsupported index data type");
90 /* In case assert is turned off */
97 * Compute min and max elements by scanning the index buffer for
98 * glDraw[Range]Elements() calls.
99 * If primitive restart is enabled, we need to ignore restart
100 * indexes when computing min/max.
103 vbo_get_minmax_index(struct gl_context
*ctx
,
104 const struct _mesa_prim
*prim
,
105 const struct _mesa_index_buffer
*ib
,
106 GLuint
*min_index
, GLuint
*max_index
,
109 const GLboolean restart
= ctx
->Array
.PrimitiveRestart
;
110 const GLuint restartIndex
= ctx
->Array
.RestartIndex
;
111 const int index_size
= vbo_sizeof_ib_type(ib
->type
);
115 indices
= (char *) ib
->ptr
+ prim
->start
* index_size
;
116 if (_mesa_is_bufferobj(ib
->obj
)) {
117 GLsizeiptr size
= MIN2(count
* index_size
, ib
->obj
->Size
);
118 indices
= ctx
->Driver
.MapBufferRange(ctx
, (GLintptr
) indices
, size
,
119 GL_MAP_READ_BIT
, ib
->obj
);
123 case GL_UNSIGNED_INT
: {
124 const GLuint
*ui_indices
= (const GLuint
*)indices
;
128 for (i
= 0; i
< count
; i
++) {
129 if (ui_indices
[i
] != restartIndex
) {
130 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
131 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
136 for (i
= 0; i
< count
; i
++) {
137 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
138 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
145 case GL_UNSIGNED_SHORT
: {
146 const GLushort
*us_indices
= (const GLushort
*)indices
;
150 for (i
= 0; i
< count
; i
++) {
151 if (us_indices
[i
] != restartIndex
) {
152 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
153 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
158 for (i
= 0; i
< count
; i
++) {
159 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
160 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
167 case GL_UNSIGNED_BYTE
: {
168 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
172 for (i
= 0; i
< count
; i
++) {
173 if (ub_indices
[i
] != restartIndex
) {
174 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
175 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
180 for (i
= 0; i
< count
; i
++) {
181 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
182 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
194 if (_mesa_is_bufferobj(ib
->obj
)) {
195 ctx
->Driver
.UnmapBuffer(ctx
, ib
->obj
);
200 * Compute min and max elements for nr_prims
203 vbo_get_minmax_indices(struct gl_context
*ctx
,
204 const struct _mesa_prim
*prims
,
205 const struct _mesa_index_buffer
*ib
,
210 GLuint tmp_min
, tmp_max
;
217 for (i
= 0; i
< nr_prims
; i
++) {
218 const struct _mesa_prim
*start_prim
;
220 start_prim
= &prims
[i
];
221 count
= start_prim
->count
;
222 /* Do combination if possible to reduce map/unmap count */
223 while ((i
+ 1 < nr_prims
) &&
224 (prims
[i
].start
+ prims
[i
].count
== prims
[i
+1].start
)) {
225 count
+= prims
[i
+1].count
;
228 vbo_get_minmax_index(ctx
, start_prim
, ib
, &tmp_min
, &tmp_max
, count
);
229 *min_index
= MIN2(*min_index
, tmp_min
);
230 *max_index
= MAX2(*max_index
, tmp_max
);
236 * Check that element 'j' of the array has reasonable data.
238 * For debugging purposes; not normally used.
241 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
242 GLuint attrib
, GLuint j
)
244 if (array
->Enabled
) {
245 const void *data
= array
->Ptr
;
246 if (_mesa_is_bufferobj(array
->BufferObj
)) {
247 if (!array
->BufferObj
->Pointer
) {
248 /* need to map now */
249 array
->BufferObj
->Pointer
=
250 ctx
->Driver
.MapBufferRange(ctx
, 0, array
->BufferObj
->Size
,
251 GL_MAP_READ_BIT
, array
->BufferObj
);
253 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
255 switch (array
->Type
) {
258 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
260 for (k
= 0; k
< array
->Size
; k
++) {
261 if (IS_INF_OR_NAN(f
[k
]) ||
262 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
263 printf("Bad array data:\n");
264 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
265 printf(" Array %u at %p\n", attrib
, (void* ) array
);
266 printf(" Type 0x%x, Size %d, Stride %d\n",
267 array
->Type
, array
->Size
, array
->Stride
);
268 printf(" Address/offset %p in Buffer Object %u\n",
269 array
->Ptr
, array
->BufferObj
->Name
);
270 f
[k
] = 1.0; /* XXX replace the bad value! */
272 /*assert(!IS_INF_OR_NAN(f[k]));*/
284 * Unmap the buffer object referenced by given array, if mapped.
287 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
289 if (array
->Enabled
&&
290 _mesa_is_bufferobj(array
->BufferObj
) &&
291 _mesa_bufferobj_mapped(array
->BufferObj
)) {
292 ctx
->Driver
.UnmapBuffer(ctx
, array
->BufferObj
);
298 * Examine the array's data for NaNs, etc.
299 * For debug purposes; not normally used.
302 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
303 const void *elements
, GLint basevertex
)
305 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
309 if (_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
)) {
310 elemMap
= ctx
->Driver
.MapBufferRange(ctx
, 0,
311 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
313 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
314 elements
= ADD_POINTERS(elements
, elemMap
);
317 for (i
= 0; i
< count
; i
++) {
322 case GL_UNSIGNED_BYTE
:
323 j
= ((const GLubyte
*) elements
)[i
];
325 case GL_UNSIGNED_SHORT
:
326 j
= ((const GLushort
*) elements
)[i
];
328 case GL_UNSIGNED_INT
:
329 j
= ((const GLuint
*) elements
)[i
];
335 /* check element j of each enabled array */
336 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
337 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
], k
, j
);
341 if (_mesa_is_bufferobj(arrayObj
->ElementArrayBufferObj
)) {
342 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
345 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
346 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
352 * Check array data, looking for NaNs, etc.
355 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
362 * Print info/data for glDrawArrays(), for debugging.
365 print_draw_arrays(struct gl_context
*ctx
,
366 GLenum mode
, GLint start
, GLsizei count
)
368 struct vbo_context
*vbo
= vbo_context(ctx
);
369 struct vbo_exec_context
*exec
= &vbo
->exec
;
370 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
373 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
376 for (i
= 0; i
< 32; i
++) {
377 struct gl_buffer_object
*bufObj
= exec
->array
.inputs
[i
]->BufferObj
;
378 GLuint bufName
= bufObj
->Name
;
379 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
380 printf("attr %2d: size %d stride %d enabled %d "
381 "ptr %p Bufobj %u\n",
383 exec
->array
.inputs
[i
]->Size
,
385 /*exec->array.inputs[i]->Enabled,*/
386 arrayObj
->VertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
,
387 exec
->array
.inputs
[i
]->Ptr
,
391 GLubyte
*p
= ctx
->Driver
.MapBufferRange(ctx
, 0, bufObj
->Size
,
392 GL_MAP_READ_BIT
, bufObj
);
393 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
394 float *f
= (float *) (p
+ offset
);
397 int n
= (count
* stride
) / 4;
400 printf(" Data at offset %d:\n", offset
);
401 for (i
= 0; i
< n
; i
++) {
402 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
404 ctx
->Driver
.UnmapBuffer(ctx
, bufObj
);
411 * Set the vbo->exec->inputs[] pointers to point to the enabled
412 * vertex arrays. This depends on the current vertex program/shader
413 * being executed because of whether or not generic vertex arrays
414 * alias the conventional vertex arrays.
415 * For arrays that aren't enabled, we set the input[attrib] pointer
416 * to point at a zero-stride current value "array".
419 recalculate_input_bindings(struct gl_context
*ctx
)
421 struct vbo_context
*vbo
= vbo_context(ctx
);
422 struct vbo_exec_context
*exec
= &vbo
->exec
;
423 struct gl_client_array
*vertexAttrib
= ctx
->Array
.ArrayObj
->VertexAttrib
;
424 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
425 GLbitfield64 const_inputs
= 0x0;
428 switch (get_program_mode(ctx
)) {
430 /* When no vertex program is active (or the vertex program is generated
431 * from fixed-function state). We put the material values into the
432 * generic slots. This is the only situation where material values
433 * are available as per-vertex attributes.
435 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
436 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
437 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
439 inputs
[i
] = &vbo
->legacy_currval
[i
];
440 const_inputs
|= VERT_BIT(i
);
444 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
445 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->mat_currval
[i
];
446 const_inputs
|= VERT_BIT_GENERIC(i
);
449 /* Could use just about anything, just to fill in the empty
452 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
453 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
454 const_inputs
|= VERT_BIT_GENERIC(i
);
457 /* There is no need to make _NEW_ARRAY dirty here for the TnL program,
458 * because it already takes care of invalidating the state necessary
459 * to revalidate vertex arrays. Not marking the state as dirty also
460 * improves performance (quite significantly in some apps).
462 if (!ctx
->VertexProgram
._MaintainTnlProgram
)
463 ctx
->NewState
|= _NEW_ARRAY
;
467 /* NV_vertex_program - attribute arrays alias and override
468 * conventional, legacy arrays. No materials, and the generic
471 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
472 if (i
< VERT_ATTRIB_GENERIC_MAX
473 && vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
474 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
475 else if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
476 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
478 inputs
[i
] = &vbo
->legacy_currval
[i
];
479 const_inputs
|= VERT_BIT_FF(i
);
483 /* Could use just about anything, just to fill in the empty
486 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
487 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
488 const_inputs
|= VERT_BIT_GENERIC(i
);
491 ctx
->NewState
|= _NEW_ARRAY
;
495 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
496 * attribute array aliases and overrides the legacy position array.
498 * Otherwise, legacy attributes available in the legacy slots,
499 * generic attributes in the generic slots and materials are not
500 * available as per-vertex attributes.
502 if (vertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
)
503 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_GENERIC0
];
504 else if (vertexAttrib
[VERT_ATTRIB_POS
].Enabled
)
505 inputs
[0] = &vertexAttrib
[VERT_ATTRIB_POS
];
507 inputs
[0] = &vbo
->legacy_currval
[0];
508 const_inputs
|= VERT_BIT_POS
;
511 for (i
= 1; i
< VERT_ATTRIB_FF_MAX
; i
++) {
512 if (vertexAttrib
[VERT_ATTRIB_FF(i
)].Enabled
)
513 inputs
[i
] = &vertexAttrib
[VERT_ATTRIB_FF(i
)];
515 inputs
[i
] = &vbo
->legacy_currval
[i
];
516 const_inputs
|= VERT_BIT_FF(i
);
520 for (i
= 1; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
521 if (vertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
)
522 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vertexAttrib
[VERT_ATTRIB_GENERIC(i
)];
524 inputs
[VERT_ATTRIB_GENERIC(i
)] = &vbo
->generic_currval
[i
];
525 const_inputs
|= VERT_BIT_GENERIC(i
);
529 inputs
[VERT_ATTRIB_GENERIC0
] = inputs
[0];
530 ctx
->NewState
|= _NEW_ARRAY
;
534 _mesa_set_varying_vp_inputs( ctx
, VERT_BIT_ALL
& (~const_inputs
) );
539 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
540 * These will point to the arrays to actually use for drawing. Some will
541 * be user-provided arrays, other will be zero-stride const-valued arrays.
542 * Note that this might set the _NEW_ARRAY dirty flag so state validation
543 * must be done after this call.
546 vbo_bind_arrays(struct gl_context
*ctx
)
548 if (!ctx
->Array
.RebindArrays
) {
552 recalculate_input_bindings(ctx
);
553 ctx
->Array
.RebindArrays
= GL_FALSE
;
558 * Helper function called by the other DrawArrays() functions below.
559 * This is where we handle primitive restart for drawing non-indexed
560 * arrays. If primitive restart is enabled, it typically means
561 * splitting one DrawArrays() into two.
564 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
565 GLsizei count
, GLuint numInstances
)
567 struct vbo_context
*vbo
= vbo_context(ctx
);
568 struct vbo_exec_context
*exec
= &vbo
->exec
;
569 struct _mesa_prim prim
[2];
571 vbo_bind_arrays(ctx
);
573 vbo_draw_method(exec
, DRAW_ARRAYS
);
575 /* Again... because we may have changed the bitmask of per-vertex varying
576 * attributes. If we regenerate the fixed-function vertex program now
577 * we may be able to prune down the number of vertex attributes which we
578 * need in the shader.
581 _mesa_update_state(ctx
);
583 /* init most fields to zero */
584 memset(prim
, 0, sizeof(prim
));
588 prim
[0].num_instances
= numInstances
;
590 /* Implement the primitive restart index */
591 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
592 GLuint primCount
= 0;
594 if (ctx
->Array
.RestartIndex
== start
) {
595 /* special case: RestartIndex at beginning */
597 prim
[0].start
= start
+ 1;
598 prim
[0].count
= count
- 1;
602 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
603 /* special case: RestartIndex at end */
605 prim
[0].start
= start
;
606 prim
[0].count
= count
- 1;
611 /* general case: RestartIndex in middle, split into two prims */
612 prim
[0].start
= start
;
613 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
616 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
617 prim
[1].count
= count
- prim
[1].start
;
623 /* draw one or two prims */
624 check_buffers_are_unmapped(exec
->array
.inputs
);
625 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
626 GL_TRUE
, start
, start
+ count
- 1, NULL
);
630 /* no prim restart */
631 prim
[0].start
= start
;
632 prim
[0].count
= count
;
634 check_buffers_are_unmapped(exec
->array
.inputs
);
635 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
636 GL_TRUE
, start
, start
+ count
- 1,
644 * Called from glDrawArrays when in immediate mode (not display list mode).
646 static void GLAPIENTRY
647 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
649 GET_CURRENT_CONTEXT(ctx
);
651 if (MESA_VERBOSE
& VERBOSE_DRAW
)
652 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
653 _mesa_lookup_enum_by_nr(mode
), start
, count
);
655 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
658 FLUSH_CURRENT( ctx
, 0 );
660 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
665 check_draw_arrays_data(ctx
, start
, count
);
667 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
670 print_draw_arrays(ctx
, mode
, start
, count
);
675 * Called from glDrawArraysInstanced when in immediate mode (not
676 * display list mode).
678 static void GLAPIENTRY
679 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
680 GLsizei numInstances
)
682 GET_CURRENT_CONTEXT(ctx
);
684 if (MESA_VERBOSE
& VERBOSE_DRAW
)
685 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
686 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
688 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
691 FLUSH_CURRENT( ctx
, 0 );
693 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
698 check_draw_arrays_data(ctx
, start
, count
);
700 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
703 print_draw_arrays(ctx
, mode
, start
, count
);
708 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
712 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
715 ctx
->Driver
.MapBufferRange(ctx
, 0,
716 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
,
718 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
720 case GL_UNSIGNED_BYTE
:
722 const GLubyte
*us
= (const GLubyte
*) map
;
724 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
; i
++) {
725 printf("%02x ", us
[i
]);
732 case GL_UNSIGNED_SHORT
:
734 const GLushort
*us
= (const GLushort
*) map
;
736 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 2; i
++) {
737 printf("%04x ", us
[i
]);
744 case GL_UNSIGNED_INT
:
746 const GLuint
*us
= (const GLuint
*) map
;
748 for (i
= 0; i
< ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
/ 4; i
++) {
749 printf("%08x ", us
[i
]);
760 ctx
->Driver
.UnmapBuffer(ctx
, ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
765 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
766 * Do the rendering for a glDrawElements or glDrawRangeElements call after
767 * we've validated buffer bounds, etc.
770 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
771 GLboolean index_bounds_valid
,
772 GLuint start
, GLuint end
,
773 GLsizei count
, GLenum type
,
774 const GLvoid
*indices
,
775 GLint basevertex
, GLint numInstances
)
777 struct vbo_context
*vbo
= vbo_context(ctx
);
778 struct vbo_exec_context
*exec
= &vbo
->exec
;
779 struct _mesa_index_buffer ib
;
780 struct _mesa_prim prim
[1];
782 FLUSH_CURRENT( ctx
, 0 );
784 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
788 vbo_bind_arrays( ctx
);
790 vbo_draw_method(exec
, DRAW_ARRAYS
);
792 /* check for dirty state again */
794 _mesa_update_state( ctx
);
798 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
807 prim
[0].count
= count
;
809 prim
[0].basevertex
= basevertex
;
810 prim
[0].num_instances
= numInstances
;
812 /* Need to give special consideration to rendering a range of
813 * indices starting somewhere above zero. Typically the
814 * application is issuing multiple DrawRangeElements() to draw
815 * successive primitives layed out linearly in the vertex arrays.
816 * Unless the vertex arrays are all in a VBO (or locked as with
817 * CVA), the OpenGL semantics imply that we need to re-read or
818 * re-upload the vertex data on each draw call.
820 * In the case of hardware tnl, we want to avoid starting the
821 * upload at zero, as it will mean every draw call uploads an
822 * increasing amount of not-used vertex data. Worse - in the
823 * software tnl module, all those vertices might be transformed and
824 * lit but never rendered.
826 * If we just upload or transform the vertices in start..end,
827 * however, the indices will be incorrect.
829 * At this level, we don't know exactly what the requirements of
830 * the backend are going to be, though it will likely boil down to
833 * 1) Do nothing, everything is in a VBO and is processed once
836 * 2) Adjust the indices and vertex arrays so that start becomes
839 * Rather than doing anything here, I'll provide a helper function
840 * for the latter case elsewhere.
843 check_buffers_are_unmapped(exec
->array
.inputs
);
844 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
845 index_bounds_valid
, start
, end
, NULL
);
850 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
852 static void GLAPIENTRY
853 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
854 GLuint start
, GLuint end
,
855 GLsizei count
, GLenum type
,
856 const GLvoid
*indices
,
859 static GLuint warnCount
= 0;
860 GET_CURRENT_CONTEXT(ctx
);
862 if (MESA_VERBOSE
& VERBOSE_DRAW
)
864 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
865 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
866 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
868 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
869 type
, indices
, basevertex
))
872 /* NOTE: It's important that 'end' is a reasonable value.
873 * in _tnl_draw_prims(), we use end to determine how many vertices
874 * to transform. If it's too large, we can unnecessarily split prims
875 * or we can read/write out of memory in several different places!
878 /* Catch/fix some potential user errors */
879 if (type
== GL_UNSIGNED_BYTE
) {
880 start
= MIN2(start
, 0xff);
881 end
= MIN2(end
, 0xff);
883 else if (type
== GL_UNSIGNED_SHORT
) {
884 start
= MIN2(start
, 0xffff);
885 end
= MIN2(end
, 0xffff);
888 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
889 /* the max element is out of bounds of one or more enabled arrays */
892 if (warnCount
< 10) {
893 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
894 "type 0x%x, indices=%p)\n"
895 "\tend is out of bounds (max=%u) "
896 "Element Buffer %u (size %d)\n"
897 "\tThis should probably be fixed in the application.",
898 start
, end
, count
, type
, indices
,
899 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
900 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
901 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
905 dump_element_buffer(ctx
, type
);
908 _mesa_print_arrays(ctx
);
910 /* 'end' was out of bounds, but now let's check the actual array
911 * indexes to see if any of them are out of bounds.
914 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
915 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
);
916 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
917 if (warnCount
< 10) {
918 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
919 "count %d, type 0x%x, indices=%p)\n"
920 "\tindex=%u is out of bounds (max=%u) "
921 "Element Buffer %u (size %d)\n"
922 "\tSkipping the glDrawRangeElements() call",
923 start
, end
, count
, type
, indices
, max
,
924 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
925 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
926 (int) ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Size
);
929 /* XXX we could also find the min index and compare to 'start'
930 * to see if start is correct. But it's more likely to get the
935 /* Set 'end' to the max possible legal value */
936 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
937 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
945 printf("glDraw[Range]Elements{,BaseVertex}"
946 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
948 start
, end
, type
, count
,
949 ctx
->Array
.ArrayObj
->ElementArrayBufferObj
->Name
,
954 check_draw_elements_data(ctx
, count
, type
, indices
);
956 (void) check_draw_elements_data
;
959 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
960 count
, type
, indices
, basevertex
, 1);
965 * Called by glDrawRangeElements() in immediate mode.
967 static void GLAPIENTRY
968 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
969 GLsizei count
, GLenum type
, const GLvoid
*indices
)
971 if (MESA_VERBOSE
& VERBOSE_DRAW
) {
972 GET_CURRENT_CONTEXT(ctx
);
974 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
975 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
976 _mesa_lookup_enum_by_nr(type
), indices
);
979 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
985 * Called by glDrawElements() in immediate mode.
987 static void GLAPIENTRY
988 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
989 const GLvoid
*indices
)
991 GET_CURRENT_CONTEXT(ctx
);
993 if (MESA_VERBOSE
& VERBOSE_DRAW
)
994 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
995 _mesa_lookup_enum_by_nr(mode
), count
,
996 _mesa_lookup_enum_by_nr(type
), indices
);
998 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
1001 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1002 count
, type
, indices
, 0, 1);
1007 * Called by glDrawElementsBaseVertex() in immediate mode.
1009 static void GLAPIENTRY
1010 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1011 const GLvoid
*indices
, GLint basevertex
)
1013 GET_CURRENT_CONTEXT(ctx
);
1015 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1016 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
1017 _mesa_lookup_enum_by_nr(mode
), count
,
1018 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
1020 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
1024 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1025 count
, type
, indices
, basevertex
, 1);
1030 * Called by glDrawElementsInstanced() in immediate mode.
1032 static void GLAPIENTRY
1033 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
1034 const GLvoid
*indices
, GLsizei numInstances
)
1036 GET_CURRENT_CONTEXT(ctx
);
1038 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1039 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
1040 _mesa_lookup_enum_by_nr(mode
), count
,
1041 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
1043 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1047 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1048 count
, type
, indices
, 0, numInstances
);
1052 * Called by glDrawElementsInstancedBaseVertex() in immediate mode.
1054 static void GLAPIENTRY
1055 vbo_exec_DrawElementsInstancedBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1056 const GLvoid
*indices
, GLsizei numInstances
,
1059 GET_CURRENT_CONTEXT(ctx
);
1061 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1062 _mesa_debug(ctx
, "glDrawElementsInstancedBaseVertex(%s, %d, %s, %p, %d; %d)\n",
1063 _mesa_lookup_enum_by_nr(mode
), count
,
1064 _mesa_lookup_enum_by_nr(type
), indices
,
1065 numInstances
, basevertex
);
1067 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
1068 numInstances
, basevertex
))
1071 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
1072 count
, type
, indices
, basevertex
, numInstances
);
1077 * Inner support for both _mesa_MultiDrawElements() and
1078 * _mesa_MultiDrawRangeElements().
1079 * This does the actual rendering after we've checked array indexes, etc.
1082 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1083 const GLsizei
*count
, GLenum type
,
1084 const GLvoid
**indices
, GLsizei primcount
,
1085 const GLint
*basevertex
)
1087 struct vbo_context
*vbo
= vbo_context(ctx
);
1088 struct vbo_exec_context
*exec
= &vbo
->exec
;
1089 struct _mesa_index_buffer ib
;
1090 struct _mesa_prim
*prim
;
1091 unsigned int index_type_size
= vbo_sizeof_ib_type(type
);
1092 uintptr_t min_index_ptr
, max_index_ptr
;
1093 GLboolean fallback
= GL_FALSE
;
1099 FLUSH_CURRENT( ctx
, 0 );
1101 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1105 prim
= calloc(1, primcount
* sizeof(*prim
));
1107 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1111 /* Decide if we can do this all as one set of primitives sharing the
1112 * same index buffer, or if we have to reset the index pointer per
1115 vbo_bind_arrays( ctx
);
1117 /* check for dirty state again */
1119 _mesa_update_state( ctx
);
1121 min_index_ptr
= (uintptr_t)indices
[0];
1123 for (i
= 0; i
< primcount
; i
++) {
1124 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1125 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1126 index_type_size
* count
[i
]);
1129 /* Check if we can handle this thing as a bunch of index offsets from the
1130 * same index pointer. If we can't, then we have to fall back to doing
1131 * a draw_prims per primitive.
1132 * Check that the difference between each prim's indexes is a multiple of
1133 * the index/element size.
1135 if (index_type_size
!= 1) {
1136 for (i
= 0; i
< primcount
; i
++) {
1137 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1144 /* If the index buffer isn't in a VBO, then treating the application's
1145 * subranges of the index buffer as one large index buffer may lead to
1146 * us reading unmapped memory.
1148 if (!_mesa_is_bufferobj(ctx
->Array
.ArrayObj
->ElementArrayBufferObj
))
1152 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1154 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1155 ib
.ptr
= (void *)min_index_ptr
;
1157 for (i
= 0; i
< primcount
; i
++) {
1158 prim
[i
].begin
= (i
== 0);
1159 prim
[i
].end
= (i
== primcount
- 1);
1162 prim
[i
].mode
= mode
;
1163 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1164 prim
[i
].count
= count
[i
];
1165 prim
[i
].indexed
= 1;
1166 prim
[i
].num_instances
= 1;
1167 if (basevertex
!= NULL
)
1168 prim
[i
].basevertex
= basevertex
[i
];
1170 prim
[i
].basevertex
= 0;
1173 check_buffers_are_unmapped(exec
->array
.inputs
);
1174 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1175 GL_FALSE
, ~0, ~0, NULL
);
1177 /* render one prim at a time */
1178 for (i
= 0; i
< primcount
; i
++) {
1179 ib
.count
= count
[i
];
1181 ib
.obj
= ctx
->Array
.ArrayObj
->ElementArrayBufferObj
;
1182 ib
.ptr
= indices
[i
];
1188 prim
[0].mode
= mode
;
1190 prim
[0].count
= count
[i
];
1191 prim
[0].indexed
= 1;
1192 prim
[0].num_instances
= 1;
1193 if (basevertex
!= NULL
)
1194 prim
[0].basevertex
= basevertex
[i
];
1196 prim
[0].basevertex
= 0;
1198 check_buffers_are_unmapped(exec
->array
.inputs
);
1199 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1200 GL_FALSE
, ~0, ~0, NULL
);
1208 static void GLAPIENTRY
1209 vbo_exec_MultiDrawElements(GLenum mode
,
1210 const GLsizei
*count
, GLenum type
,
1211 const GLvoid
**indices
,
1214 GET_CURRENT_CONTEXT(ctx
);
1217 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1219 for (i
= 0; i
< primcount
; i
++) {
1220 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1225 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1230 static void GLAPIENTRY
1231 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1232 const GLsizei
*count
, GLenum type
,
1233 const GLvoid
**indices
,
1235 const GLsizei
*basevertex
)
1237 GET_CURRENT_CONTEXT(ctx
);
1240 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1242 for (i
= 0; i
< primcount
; i
++) {
1243 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1248 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1252 #if FEATURE_EXT_transform_feedback
1255 vbo_draw_transform_feedback(struct gl_context
*ctx
, GLenum mode
,
1256 struct gl_transform_feedback_object
*obj
,
1257 GLuint numInstances
)
1259 struct vbo_context
*vbo
= vbo_context(ctx
);
1260 struct vbo_exec_context
*exec
= &vbo
->exec
;
1261 struct _mesa_prim prim
[2];
1263 vbo_bind_arrays(ctx
);
1265 /* Again... because we may have changed the bitmask of per-vertex varying
1266 * attributes. If we regenerate the fixed-function vertex program now
1267 * we may be able to prune down the number of vertex attributes which we
1268 * need in the shader.
1271 _mesa_update_state(ctx
);
1273 /* init most fields to zero */
1274 memset(prim
, 0, sizeof(prim
));
1277 prim
[0].mode
= mode
;
1278 prim
[0].num_instances
= numInstances
;
1280 /* Maybe we should do some primitive splitting for primitive restart
1281 * (like in DrawArrays), but we have no way to know how many vertices
1282 * will be rendered. */
1284 check_buffers_are_unmapped(exec
->array
.inputs
);
1285 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
1286 GL_TRUE
, 0, 0, obj
);
1290 * Like DrawArrays, but take the count from a transform feedback object.
1291 * \param mode GL_POINTS, GL_LINES, GL_TRIANGLE_STRIP, etc.
1292 * \param name the transform feedback object
1293 * User still has to setup of the vertex attribute info with
1294 * glVertexPointer, glColorPointer, etc.
1295 * Part of GL_ARB_transform_feedback2.
1297 static void GLAPIENTRY
1298 vbo_exec_DrawTransformFeedback(GLenum mode
, GLuint name
)
1300 GET_CURRENT_CONTEXT(ctx
);
1301 struct gl_transform_feedback_object
*obj
=
1302 _mesa_lookup_transform_feedback_object(ctx
, name
);
1304 if (MESA_VERBOSE
& VERBOSE_DRAW
)
1305 _mesa_debug(ctx
, "glDrawTransformFeedback(%s, %d)\n",
1306 _mesa_lookup_enum_by_nr(mode
), name
);
1308 if (!_mesa_validate_DrawTransformFeedback(ctx
, mode
, obj
)) {
1312 FLUSH_CURRENT(ctx
, 0);
1314 if (!_mesa_valid_to_render(ctx
, "glDrawTransformFeedback")) {
1318 vbo_draw_transform_feedback(ctx
, mode
, obj
, 1);
1324 * Plug in the immediate-mode vertex array drawing commands into the
1325 * givven vbo_exec_context object.
1328 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1330 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1331 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1332 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1333 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1334 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1335 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1336 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1337 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1338 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1339 exec
->vtxfmt
.DrawElementsInstancedBaseVertex
= vbo_exec_DrawElementsInstancedBaseVertex
;
1340 exec
->vtxfmt
.DrawTransformFeedback
= vbo_exec_DrawTransformFeedback
;
1345 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1353 * The following functions are only used for OpenGL ES 1/2 support.
1354 * And some aren't even supported (yet) in ES 1/2.
1359 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1361 vbo_exec_DrawArrays(mode
, first
, count
);
1366 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1367 const GLvoid
*indices
)
1369 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1374 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1375 const GLvoid
*indices
, GLint basevertex
)
1377 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1382 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1383 GLenum type
, const GLvoid
*indices
)
1385 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1390 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1391 GLsizei count
, GLenum type
,
1392 const GLvoid
*indices
, GLint basevertex
)
1394 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1395 indices
, basevertex
);
1400 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1401 const GLvoid
**indices
, GLsizei primcount
)
1403 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1408 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1409 const GLsizei
*count
, GLenum type
,
1410 const GLvoid
**indices
, GLsizei primcount
,
1411 const GLint
*basevertex
)
1413 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1414 primcount
, basevertex
);
1417 #if FEATURE_EXT_transform_feedback
1420 _mesa_DrawTransformFeedback(GLenum mode
, GLuint name
)
1422 vbo_exec_DrawTransformFeedback(mode
, name
);