1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/api_validate.h"
33 #include "main/varray.h"
34 #include "main/bufferobj.h"
35 #include "main/enums.h"
36 #include "main/macros.h"
38 #include "vbo_context.h"
42 * Compute min and max elements by scanning the index buffer for
43 * glDraw[Range]Elements() calls.
44 * If primitive restart is enabled, we need to ignore restart
45 * indexes when computing min/max.
48 vbo_get_minmax_index(struct gl_context
*ctx
,
49 const struct _mesa_prim
*prim
,
50 const struct _mesa_index_buffer
*ib
,
51 GLuint
*min_index
, GLuint
*max_index
)
53 const GLboolean restart
= ctx
->Array
.PrimitiveRestart
;
54 const GLuint restartIndex
= ctx
->Array
.RestartIndex
;
55 const GLuint count
= prim
->count
;
59 if (_mesa_is_bufferobj(ib
->obj
)) {
61 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
62 GL_READ_ONLY
, ib
->obj
);
63 indices
= ADD_POINTERS(map
, ib
->ptr
);
69 case GL_UNSIGNED_INT
: {
70 const GLuint
*ui_indices
= (const GLuint
*)indices
;
74 for (i
= 0; i
< count
; i
++) {
75 if (ui_indices
[i
] != restartIndex
) {
76 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
77 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
82 for (i
= 0; i
< count
; i
++) {
83 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
84 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
91 case GL_UNSIGNED_SHORT
: {
92 const GLushort
*us_indices
= (const GLushort
*)indices
;
96 for (i
= 0; i
< count
; i
++) {
97 if (us_indices
[i
] != restartIndex
) {
98 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
99 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
104 for (i
= 0; i
< count
; i
++) {
105 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
106 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
113 case GL_UNSIGNED_BYTE
: {
114 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
118 for (i
= 0; i
< count
; i
++) {
119 if (ub_indices
[i
] != restartIndex
) {
120 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
121 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
126 for (i
= 0; i
< count
; i
++) {
127 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
128 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
140 if (_mesa_is_bufferobj(ib
->obj
)) {
141 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, ib
->obj
);
147 * Check that element 'j' of the array has reasonable data.
149 * For debugging purposes; not normally used.
152 check_array_data(struct gl_context
*ctx
, struct gl_client_array
*array
,
153 GLuint attrib
, GLuint j
)
155 if (array
->Enabled
) {
156 const void *data
= array
->Ptr
;
157 if (_mesa_is_bufferobj(array
->BufferObj
)) {
158 if (!array
->BufferObj
->Pointer
) {
159 /* need to map now */
160 array
->BufferObj
->Pointer
=
161 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
162 GL_READ_ONLY
, array
->BufferObj
);
164 data
= ADD_POINTERS(data
, array
->BufferObj
->Pointer
);
166 switch (array
->Type
) {
169 GLfloat
*f
= (GLfloat
*) ((GLubyte
*) data
+ array
->StrideB
* j
);
171 for (k
= 0; k
< array
->Size
; k
++) {
172 if (IS_INF_OR_NAN(f
[k
]) ||
173 f
[k
] >= 1.0e20
|| f
[k
] <= -1.0e10
) {
174 printf("Bad array data:\n");
175 printf(" Element[%u].%u = %f\n", j
, k
, f
[k
]);
176 printf(" Array %u at %p\n", attrib
, (void* ) array
);
177 printf(" Type 0x%x, Size %d, Stride %d\n",
178 array
->Type
, array
->Size
, array
->Stride
);
179 printf(" Address/offset %p in Buffer Object %u\n",
180 array
->Ptr
, array
->BufferObj
->Name
);
181 f
[k
] = 1.0; /* XXX replace the bad value! */
183 /*assert(!IS_INF_OR_NAN(f[k]));*/
195 * Unmap the buffer object referenced by given array, if mapped.
198 unmap_array_buffer(struct gl_context
*ctx
, struct gl_client_array
*array
)
200 if (array
->Enabled
&&
201 _mesa_is_bufferobj(array
->BufferObj
) &&
202 _mesa_bufferobj_mapped(array
->BufferObj
)) {
203 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, array
->BufferObj
);
209 * Examine the array's data for NaNs, etc.
210 * For debug purposes; not normally used.
213 check_draw_elements_data(struct gl_context
*ctx
, GLsizei count
, GLenum elemType
,
214 const void *elements
, GLint basevertex
)
216 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
220 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
221 elemMap
= ctx
->Driver
.MapBuffer(ctx
,
222 GL_ELEMENT_ARRAY_BUFFER_ARB
,
224 ctx
->Array
.ElementArrayBufferObj
);
225 elements
= ADD_POINTERS(elements
, elemMap
);
228 for (i
= 0; i
< count
; i
++) {
233 case GL_UNSIGNED_BYTE
:
234 j
= ((const GLubyte
*) elements
)[i
];
236 case GL_UNSIGNED_SHORT
:
237 j
= ((const GLushort
*) elements
)[i
];
239 case GL_UNSIGNED_INT
:
240 j
= ((const GLuint
*) elements
)[i
];
246 /* check element j of each enabled array */
247 check_array_data(ctx
, &arrayObj
->Vertex
, VERT_ATTRIB_POS
, j
);
248 check_array_data(ctx
, &arrayObj
->Normal
, VERT_ATTRIB_NORMAL
, j
);
249 check_array_data(ctx
, &arrayObj
->Color
, VERT_ATTRIB_COLOR0
, j
);
250 check_array_data(ctx
, &arrayObj
->SecondaryColor
, VERT_ATTRIB_COLOR1
, j
);
251 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
252 check_array_data(ctx
, &arrayObj
->TexCoord
[k
], VERT_ATTRIB_TEX0
+ k
, j
);
254 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
255 check_array_data(ctx
, &arrayObj
->VertexAttrib
[k
],
256 VERT_ATTRIB_GENERIC0
+ k
, j
);
260 if (_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
)) {
261 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
262 ctx
->Array
.ElementArrayBufferObj
);
265 unmap_array_buffer(ctx
, &arrayObj
->Vertex
);
266 unmap_array_buffer(ctx
, &arrayObj
->Normal
);
267 unmap_array_buffer(ctx
, &arrayObj
->Color
);
268 for (k
= 0; k
< Elements(arrayObj
->TexCoord
); k
++) {
269 unmap_array_buffer(ctx
, &arrayObj
->TexCoord
[k
]);
271 for (k
= 0; k
< Elements(arrayObj
->VertexAttrib
); k
++) {
272 unmap_array_buffer(ctx
, &arrayObj
->VertexAttrib
[k
]);
278 * Check array data, looking for NaNs, etc.
281 check_draw_arrays_data(struct gl_context
*ctx
, GLint start
, GLsizei count
)
288 * Print info/data for glDrawArrays(), for debugging.
291 print_draw_arrays(struct gl_context
*ctx
,
292 GLenum mode
, GLint start
, GLsizei count
)
294 struct vbo_context
*vbo
= vbo_context(ctx
);
295 struct vbo_exec_context
*exec
= &vbo
->exec
;
298 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
301 for (i
= 0; i
< 32; i
++) {
302 GLuint bufName
= exec
->array
.inputs
[i
]->BufferObj
->Name
;
303 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
304 printf("attr %2d: size %d stride %d enabled %d "
305 "ptr %p Bufobj %u\n",
307 exec
->array
.inputs
[i
]->Size
,
309 /*exec->array.inputs[i]->Enabled,*/
310 exec
->array
.legacy_array
[i
]->Enabled
,
311 exec
->array
.inputs
[i
]->Ptr
,
315 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, bufName
);
316 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
317 GL_READ_ONLY_ARB
, buf
);
318 int offset
= (int) (GLintptr
) exec
->array
.inputs
[i
]->Ptr
;
319 float *f
= (float *) (p
+ offset
);
322 int n
= (count
* stride
) / 4;
325 printf(" Data at offset %d:\n", offset
);
326 for (i
= 0; i
< n
; i
++) {
327 printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
329 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, buf
);
336 * Bind the VBO executor to the current vertex array object prior
339 * Just translate the arrayobj into a sane layout.
342 bind_array_obj(struct gl_context
*ctx
)
344 struct vbo_context
*vbo
= vbo_context(ctx
);
345 struct vbo_exec_context
*exec
= &vbo
->exec
;
346 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
349 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
350 * rather than as individual named arrays. Then this function can
353 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
354 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
355 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
356 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
357 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
358 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
359 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
360 if (arrayObj
->PointSize
.Enabled
) {
361 /* this aliases COLOR_INDEX */
362 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
364 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
366 for (i
= 0; i
< Elements(arrayObj
->TexCoord
); i
++)
367 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
369 for (i
= 0; i
< Elements(arrayObj
->VertexAttrib
); i
++) {
370 assert(i
< Elements(exec
->array
.generic_array
));
371 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
374 exec
->array
.array_obj
= arrayObj
->Name
;
379 * Set the vbo->exec->inputs[] pointers to point to the enabled
380 * vertex arrays. This depends on the current vertex program/shader
381 * being executed because of whether or not generic vertex arrays
382 * alias the conventional vertex arrays.
383 * For arrays that aren't enabled, we set the input[attrib] pointer
384 * to point at a zero-stride current value "array".
387 recalculate_input_bindings(struct gl_context
*ctx
)
389 struct vbo_context
*vbo
= vbo_context(ctx
);
390 struct vbo_exec_context
*exec
= &vbo
->exec
;
391 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
392 GLbitfield const_inputs
= 0x0;
395 exec
->array
.program_mode
= get_program_mode(ctx
);
396 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
398 switch (exec
->array
.program_mode
) {
400 /* When no vertex program is active (or the vertex program is generated
401 * from fixed-function state). We put the material values into the
402 * generic slots. This is the only situation where material values
403 * are available as per-vertex attributes.
405 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
406 if (exec
->array
.legacy_array
[i
]->Enabled
)
407 inputs
[i
] = exec
->array
.legacy_array
[i
];
409 inputs
[i
] = &vbo
->legacy_currval
[i
];
410 const_inputs
|= 1 << i
;
414 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
415 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
416 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
419 /* Could use just about anything, just to fill in the empty
422 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
423 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
424 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
429 /* NV_vertex_program - attribute arrays alias and override
430 * conventional, legacy arrays. No materials, and the generic
433 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
434 if (exec
->array
.generic_array
[i
]->Enabled
)
435 inputs
[i
] = exec
->array
.generic_array
[i
];
436 else if (exec
->array
.legacy_array
[i
]->Enabled
)
437 inputs
[i
] = exec
->array
.legacy_array
[i
];
439 inputs
[i
] = &vbo
->legacy_currval
[i
];
440 const_inputs
|= 1 << i
;
444 /* Could use just about anything, just to fill in the empty
447 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
448 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
449 const_inputs
|= 1 << i
;
454 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
455 * attribute array aliases and overrides the legacy position array.
457 * Otherwise, legacy attributes available in the legacy slots,
458 * generic attributes in the generic slots and materials are not
459 * available as per-vertex attributes.
461 if (exec
->array
.generic_array
[0]->Enabled
)
462 inputs
[0] = exec
->array
.generic_array
[0];
463 else if (exec
->array
.legacy_array
[0]->Enabled
)
464 inputs
[0] = exec
->array
.legacy_array
[0];
466 inputs
[0] = &vbo
->legacy_currval
[0];
467 const_inputs
|= 1 << 0;
470 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
471 if (exec
->array
.legacy_array
[i
]->Enabled
)
472 inputs
[i
] = exec
->array
.legacy_array
[i
];
474 inputs
[i
] = &vbo
->legacy_currval
[i
];
475 const_inputs
|= 1 << i
;
479 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
480 if (exec
->array
.generic_array
[i
]->Enabled
)
481 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
483 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
484 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
491 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
496 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
497 * These will point to the arrays to actually use for drawing. Some will
498 * be user-provided arrays, other will be zero-stride const-valued arrays.
499 * Note that this might set the _NEW_ARRAY dirty flag so state validation
500 * must be done after this call.
503 bind_arrays(struct gl_context
*ctx
)
506 recalculate_input_bindings(ctx
);
511 * Helper function called by the other DrawArrays() functions below.
512 * This is where we handle primitive restart for drawing non-indexed
513 * arrays. If primitive restart is enabled, it typically means
514 * splitting one DrawArrays() into two.
517 vbo_draw_arrays(struct gl_context
*ctx
, GLenum mode
, GLint start
,
518 GLsizei count
, GLuint numInstances
)
520 struct vbo_context
*vbo
= vbo_context(ctx
);
521 struct vbo_exec_context
*exec
= &vbo
->exec
;
522 struct _mesa_prim prim
[2];
526 /* Again... because we may have changed the bitmask of per-vertex varying
527 * attributes. If we regenerate the fixed-function vertex program now
528 * we may be able to prune down the number of vertex attributes which we
529 * need in the shader.
532 _mesa_update_state(ctx
);
539 prim
[0].start
= 0; /* filled in below */
540 prim
[0].count
= 0; /* filled in below */
542 prim
[0].basevertex
= 0;
543 prim
[0].num_instances
= numInstances
;
545 /* Implement the primitive restart index */
546 if (ctx
->Array
.PrimitiveRestart
&& ctx
->Array
.RestartIndex
< count
) {
547 GLuint primCount
= 0;
549 if (ctx
->Array
.RestartIndex
== start
) {
550 /* special case: RestartIndex at beginning */
552 prim
[0].start
= start
+ 1;
553 prim
[0].count
= count
- 1;
557 else if (ctx
->Array
.RestartIndex
== start
+ count
- 1) {
558 /* special case: RestartIndex at end */
560 prim
[0].start
= start
;
561 prim
[0].count
= count
- 1;
566 /* general case: RestartIndex in middle, split into two prims */
567 prim
[0].start
= start
;
568 prim
[0].count
= ctx
->Array
.RestartIndex
- start
;
571 prim
[1].start
= ctx
->Array
.RestartIndex
+ 1;
572 prim
[1].count
= count
- prim
[1].start
;
578 /* draw one or two prims */
579 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primCount
, NULL
,
580 GL_TRUE
, start
, start
+ count
- 1);
584 /* no prim restart */
585 prim
[0].start
= start
;
586 prim
[0].count
= count
;
588 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
589 GL_TRUE
, start
, start
+ count
- 1);
596 * Called from glDrawArrays when in immediate mode (not display list mode).
598 static void GLAPIENTRY
599 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
601 GET_CURRENT_CONTEXT(ctx
);
603 if (MESA_VERBOSE
& VERBOSE_DRAW
)
604 _mesa_debug(ctx
, "glDrawArrays(%s, %d, %d)\n",
605 _mesa_lookup_enum_by_nr(mode
), start
, count
);
607 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
610 FLUSH_CURRENT( ctx
, 0 );
612 if (!_mesa_valid_to_render(ctx
, "glDrawArrays")) {
617 check_draw_arrays_data(ctx
, start
, count
);
619 vbo_draw_arrays(ctx
, mode
, start
, count
, 1);
622 print_draw_arrays(ctx
, mode
, start
, count
);
627 * Called from glDrawArraysInstanced when in immediate mode (not
628 * display list mode).
630 static void GLAPIENTRY
631 vbo_exec_DrawArraysInstanced(GLenum mode
, GLint start
, GLsizei count
,
632 GLsizei numInstances
)
634 GET_CURRENT_CONTEXT(ctx
);
636 if (MESA_VERBOSE
& VERBOSE_DRAW
)
637 _mesa_debug(ctx
, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
638 _mesa_lookup_enum_by_nr(mode
), start
, count
, numInstances
);
640 if (!_mesa_validate_DrawArraysInstanced(ctx
, mode
, start
, count
, numInstances
))
643 FLUSH_CURRENT( ctx
, 0 );
645 if (!_mesa_valid_to_render(ctx
, "glDrawArraysInstanced")) {
650 check_draw_arrays_data(ctx
, start
, count
);
652 vbo_draw_arrays(ctx
, mode
, start
, count
, numInstances
);
655 print_draw_arrays(ctx
, mode
, start
, count
);
660 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
664 dump_element_buffer(struct gl_context
*ctx
, GLenum type
)
666 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
667 GL_ELEMENT_ARRAY_BUFFER_ARB
,
669 ctx
->Array
.ElementArrayBufferObj
);
671 case GL_UNSIGNED_BYTE
:
673 const GLubyte
*us
= (const GLubyte
*) map
;
675 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
; i
++) {
676 printf("%02x ", us
[i
]);
683 case GL_UNSIGNED_SHORT
:
685 const GLushort
*us
= (const GLushort
*) map
;
687 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 2; i
++) {
688 printf("%04x ", us
[i
]);
695 case GL_UNSIGNED_INT
:
697 const GLuint
*us
= (const GLuint
*) map
;
699 for (i
= 0; i
< ctx
->Array
.ElementArrayBufferObj
->Size
/ 4; i
++) {
700 printf("%08x ", us
[i
]);
711 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
,
712 ctx
->Array
.ElementArrayBufferObj
);
717 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
718 * Do the rendering for a glDrawElements or glDrawRangeElements call after
719 * we've validated buffer bounds, etc.
722 vbo_validated_drawrangeelements(struct gl_context
*ctx
, GLenum mode
,
723 GLboolean index_bounds_valid
,
724 GLuint start
, GLuint end
,
725 GLsizei count
, GLenum type
,
726 const GLvoid
*indices
,
727 GLint basevertex
, GLint numInstances
)
729 struct vbo_context
*vbo
= vbo_context(ctx
);
730 struct vbo_exec_context
*exec
= &vbo
->exec
;
731 struct _mesa_index_buffer ib
;
732 struct _mesa_prim prim
[1];
734 FLUSH_CURRENT( ctx
, 0 );
736 if (!_mesa_valid_to_render(ctx
, "glDraw[Range]Elements")) {
742 /* check for dirty state again */
744 _mesa_update_state( ctx
);
748 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
757 prim
[0].count
= count
;
759 prim
[0].basevertex
= basevertex
;
760 prim
[0].num_instances
= numInstances
;
762 /* Need to give special consideration to rendering a range of
763 * indices starting somewhere above zero. Typically the
764 * application is issuing multiple DrawRangeElements() to draw
765 * successive primitives layed out linearly in the vertex arrays.
766 * Unless the vertex arrays are all in a VBO (or locked as with
767 * CVA), the OpenGL semantics imply that we need to re-read or
768 * re-upload the vertex data on each draw call.
770 * In the case of hardware tnl, we want to avoid starting the
771 * upload at zero, as it will mean every draw call uploads an
772 * increasing amount of not-used vertex data. Worse - in the
773 * software tnl module, all those vertices might be transformed and
774 * lit but never rendered.
776 * If we just upload or transform the vertices in start..end,
777 * however, the indices will be incorrect.
779 * At this level, we don't know exactly what the requirements of
780 * the backend are going to be, though it will likely boil down to
783 * 1) Do nothing, everything is in a VBO and is processed once
786 * 2) Adjust the indices and vertex arrays so that start becomes
789 * Rather than doing anything here, I'll provide a helper function
790 * for the latter case elsewhere.
793 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
794 index_bounds_valid
, start
, end
);
799 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
801 static void GLAPIENTRY
802 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode
,
803 GLuint start
, GLuint end
,
804 GLsizei count
, GLenum type
,
805 const GLvoid
*indices
,
808 static GLuint warnCount
= 0;
809 GET_CURRENT_CONTEXT(ctx
);
811 if (MESA_VERBOSE
& VERBOSE_DRAW
)
813 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
814 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
815 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
817 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
,
818 type
, indices
, basevertex
))
821 /* NOTE: It's important that 'end' is a reasonable value.
822 * in _tnl_draw_prims(), we use end to determine how many vertices
823 * to transform. If it's too large, we can unnecessarily split prims
824 * or we can read/write out of memory in several different places!
827 /* Catch/fix some potential user errors */
828 if (type
== GL_UNSIGNED_BYTE
) {
829 start
= MIN2(start
, 0xff);
830 end
= MIN2(end
, 0xff);
832 else if (type
== GL_UNSIGNED_SHORT
) {
833 start
= MIN2(start
, 0xffff);
834 end
= MIN2(end
, 0xffff);
837 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
838 /* the max element is out of bounds of one or more enabled arrays */
841 if (warnCount
< 10) {
842 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
843 "type 0x%x, indices=%p)\n"
844 "\tend is out of bounds (max=%u) "
845 "Element Buffer %u (size %d)\n"
846 "\tThis should probably be fixed in the application.",
847 start
, end
, count
, type
, indices
,
848 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
849 ctx
->Array
.ElementArrayBufferObj
->Name
,
850 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
854 dump_element_buffer(ctx
, type
);
857 _mesa_print_arrays(ctx
);
860 /* 'end' was out of bounds, but now let's check the actual array
861 * indexes to see if any of them are out of bounds.
864 GLuint max
= _mesa_max_buffer_index(ctx
, count
, type
, indices
,
865 ctx
->Array
.ElementArrayBufferObj
);
866 if (max
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
867 if (warnCount
< 10) {
868 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, "
869 "count %d, type 0x%x, indices=%p)\n"
870 "\tindex=%u is out of bounds (max=%u) "
871 "Element Buffer %u (size %d)\n"
872 "\tSkipping the glDrawRangeElements() call",
873 start
, end
, count
, type
, indices
, max
,
874 ctx
->Array
.ArrayObj
->_MaxElement
- 1,
875 ctx
->Array
.ElementArrayBufferObj
->Name
,
876 (int) ctx
->Array
.ElementArrayBufferObj
->Size
);
879 /* XXX we could also find the min index and compare to 'start'
880 * to see if start is correct. But it's more likely to get the
886 /* Set 'end' to the max possible legal value */
887 assert(ctx
->Array
.ArrayObj
->_MaxElement
>= 1);
888 end
= ctx
->Array
.ArrayObj
->_MaxElement
- 1;
891 printf("glDraw[Range]Elements{,BaseVertex}"
892 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
894 start
, end
, type
, count
,
895 ctx
->Array
.ElementArrayBufferObj
->Name
,
900 check_draw_elements_data(ctx
, count
, type
, indices
);
902 (void) check_draw_elements_data
;
905 vbo_validated_drawrangeelements(ctx
, mode
, GL_TRUE
, start
, end
,
906 count
, type
, indices
, basevertex
, 1);
911 * Called by glDrawRangeElements() in immediate mode.
913 static void GLAPIENTRY
914 vbo_exec_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
,
915 GLsizei count
, GLenum type
, const GLvoid
*indices
)
917 GET_CURRENT_CONTEXT(ctx
);
919 if (MESA_VERBOSE
& VERBOSE_DRAW
)
921 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
922 _mesa_lookup_enum_by_nr(mode
), start
, end
, count
,
923 _mesa_lookup_enum_by_nr(type
), indices
);
925 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
931 * Called by glDrawElements() in immediate mode.
933 static void GLAPIENTRY
934 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
935 const GLvoid
*indices
)
937 GET_CURRENT_CONTEXT(ctx
);
939 if (MESA_VERBOSE
& VERBOSE_DRAW
)
940 _mesa_debug(ctx
, "glDrawElements(%s, %u, %s, %p)\n",
941 _mesa_lookup_enum_by_nr(mode
), count
,
942 _mesa_lookup_enum_by_nr(type
), indices
);
944 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
, 0 ))
947 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
948 count
, type
, indices
, 0, 1);
953 * Called by glDrawElementsBaseVertex() in immediate mode.
955 static void GLAPIENTRY
956 vbo_exec_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
957 const GLvoid
*indices
, GLint basevertex
)
959 GET_CURRENT_CONTEXT(ctx
);
961 if (MESA_VERBOSE
& VERBOSE_DRAW
)
962 _mesa_debug(ctx
, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
963 _mesa_lookup_enum_by_nr(mode
), count
,
964 _mesa_lookup_enum_by_nr(type
), indices
, basevertex
);
966 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
,
970 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
971 count
, type
, indices
, basevertex
, 1);
976 * Called by glDrawElementsInstanced() in immediate mode.
978 static void GLAPIENTRY
979 vbo_exec_DrawElementsInstanced(GLenum mode
, GLsizei count
, GLenum type
,
980 const GLvoid
*indices
, GLsizei numInstances
)
982 GET_CURRENT_CONTEXT(ctx
);
984 if (MESA_VERBOSE
& VERBOSE_DRAW
)
985 _mesa_debug(ctx
, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
986 _mesa_lookup_enum_by_nr(mode
), count
,
987 _mesa_lookup_enum_by_nr(type
), indices
, numInstances
);
989 if (!_mesa_validate_DrawElementsInstanced(ctx
, mode
, count
, type
, indices
,
993 vbo_validated_drawrangeelements(ctx
, mode
, GL_FALSE
, ~0, ~0,
994 count
, type
, indices
, 0, numInstances
);
999 * Inner support for both _mesa_MultiDrawElements() and
1000 * _mesa_MultiDrawRangeElements().
1001 * This does the actual rendering after we've checked array indexes, etc.
1004 vbo_validated_multidrawelements(struct gl_context
*ctx
, GLenum mode
,
1005 const GLsizei
*count
, GLenum type
,
1006 const GLvoid
**indices
, GLsizei primcount
,
1007 const GLint
*basevertex
)
1009 struct vbo_context
*vbo
= vbo_context(ctx
);
1010 struct vbo_exec_context
*exec
= &vbo
->exec
;
1011 struct _mesa_index_buffer ib
;
1012 struct _mesa_prim
*prim
;
1013 unsigned int index_type_size
= 0;
1014 uintptr_t min_index_ptr
, max_index_ptr
;
1015 GLboolean fallback
= GL_FALSE
;
1021 FLUSH_CURRENT( ctx
, 0 );
1023 if (!_mesa_valid_to_render(ctx
, "glMultiDrawElements")) {
1027 prim
= calloc(1, primcount
* sizeof(*prim
));
1029 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glMultiDrawElements");
1033 /* Decide if we can do this all as one set of primitives sharing the
1034 * same index buffer, or if we have to reset the index pointer per
1039 /* check for dirty state again */
1041 _mesa_update_state( ctx
);
1044 case GL_UNSIGNED_INT
:
1045 index_type_size
= 4;
1047 case GL_UNSIGNED_SHORT
:
1048 index_type_size
= 2;
1050 case GL_UNSIGNED_BYTE
:
1051 index_type_size
= 1;
1057 min_index_ptr
= (uintptr_t)indices
[0];
1059 for (i
= 0; i
< primcount
; i
++) {
1060 min_index_ptr
= MIN2(min_index_ptr
, (uintptr_t)indices
[i
]);
1061 max_index_ptr
= MAX2(max_index_ptr
, (uintptr_t)indices
[i
] +
1062 index_type_size
* count
[i
]);
1065 /* Check if we can handle this thing as a bunch of index offsets from the
1066 * same index pointer. If we can't, then we have to fall back to doing
1067 * a draw_prims per primitive.
1068 * Check that the difference between each prim's indexes is a multiple of
1069 * the index/element size.
1071 if (index_type_size
!= 1) {
1072 for (i
= 0; i
< primcount
; i
++) {
1073 if ((((uintptr_t)indices
[i
] - min_index_ptr
) % index_type_size
) != 0) {
1080 /* If the index buffer isn't in a VBO, then treating the application's
1081 * subranges of the index buffer as one large index buffer may lead to
1082 * us reading unmapped memory.
1084 if (!_mesa_is_bufferobj(ctx
->Array
.ElementArrayBufferObj
))
1088 ib
.count
= (max_index_ptr
- min_index_ptr
) / index_type_size
;
1090 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1091 ib
.ptr
= (void *)min_index_ptr
;
1093 for (i
= 0; i
< primcount
; i
++) {
1094 prim
[i
].begin
= (i
== 0);
1095 prim
[i
].end
= (i
== primcount
- 1);
1098 prim
[i
].mode
= mode
;
1099 prim
[i
].start
= ((uintptr_t)indices
[i
] - min_index_ptr
) / index_type_size
;
1100 prim
[i
].count
= count
[i
];
1101 prim
[i
].indexed
= 1;
1102 prim
[i
].num_instances
= 1;
1103 if (basevertex
!= NULL
)
1104 prim
[i
].basevertex
= basevertex
[i
];
1106 prim
[i
].basevertex
= 0;
1109 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, primcount
, &ib
,
1112 /* render one prim at a time */
1113 for (i
= 0; i
< primcount
; i
++) {
1114 ib
.count
= count
[i
];
1116 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
1117 ib
.ptr
= indices
[i
];
1123 prim
[0].mode
= mode
;
1125 prim
[0].count
= count
[i
];
1126 prim
[0].indexed
= 1;
1127 prim
[0].num_instances
= 1;
1128 if (basevertex
!= NULL
)
1129 prim
[0].basevertex
= basevertex
[i
];
1131 prim
[0].basevertex
= 0;
1133 vbo
->draw_prims(ctx
, exec
->array
.inputs
, prim
, 1, &ib
,
1142 static void GLAPIENTRY
1143 vbo_exec_MultiDrawElements(GLenum mode
,
1144 const GLsizei
*count
, GLenum type
,
1145 const GLvoid
**indices
,
1148 GET_CURRENT_CONTEXT(ctx
);
1151 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1153 for (i
= 0; i
< primcount
; i
++) {
1154 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1159 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1164 static void GLAPIENTRY
1165 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode
,
1166 const GLsizei
*count
, GLenum type
,
1167 const GLvoid
**indices
,
1169 const GLsizei
*basevertex
)
1171 GET_CURRENT_CONTEXT(ctx
);
1174 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
1176 for (i
= 0; i
< primcount
; i
++) {
1177 if (!_mesa_validate_DrawElements(ctx
, mode
, count
[i
], type
, indices
[i
],
1182 vbo_validated_multidrawelements(ctx
, mode
, count
, type
, indices
, primcount
,
1188 * Plug in the immediate-mode vertex array drawing commands into the
1189 * givven vbo_exec_context object.
1192 vbo_exec_array_init( struct vbo_exec_context
*exec
)
1194 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
1195 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
1196 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
1197 exec
->vtxfmt
.MultiDrawElementsEXT
= vbo_exec_MultiDrawElements
;
1198 exec
->vtxfmt
.DrawElementsBaseVertex
= vbo_exec_DrawElementsBaseVertex
;
1199 exec
->vtxfmt
.DrawRangeElementsBaseVertex
= vbo_exec_DrawRangeElementsBaseVertex
;
1200 exec
->vtxfmt
.MultiDrawElementsBaseVertex
= vbo_exec_MultiDrawElementsBaseVertex
;
1201 exec
->vtxfmt
.DrawArraysInstanced
= vbo_exec_DrawArraysInstanced
;
1202 exec
->vtxfmt
.DrawElementsInstanced
= vbo_exec_DrawElementsInstanced
;
1207 vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
1215 * The following functions are only used for OpenGL ES 1/2 support.
1216 * And some aren't even supported (yet) in ES 1/2.
1221 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
1223 vbo_exec_DrawArrays(mode
, first
, count
);
1228 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
1229 const GLvoid
*indices
)
1231 vbo_exec_DrawElements(mode
, count
, type
, indices
);
1236 _mesa_DrawElementsBaseVertex(GLenum mode
, GLsizei count
, GLenum type
,
1237 const GLvoid
*indices
, GLint basevertex
)
1239 vbo_exec_DrawElementsBaseVertex(mode
, count
, type
, indices
, basevertex
);
1244 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
1245 GLenum type
, const GLvoid
*indices
)
1247 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);
1252 _mesa_DrawRangeElementsBaseVertex(GLenum mode
, GLuint start
, GLuint end
,
1253 GLsizei count
, GLenum type
,
1254 const GLvoid
*indices
, GLint basevertex
)
1256 vbo_exec_DrawRangeElementsBaseVertex(mode
, start
, end
, count
, type
,
1257 indices
, basevertex
);
1262 _mesa_MultiDrawElementsEXT(GLenum mode
, const GLsizei
*count
, GLenum type
,
1263 const GLvoid
**indices
, GLsizei primcount
)
1265 vbo_exec_MultiDrawElements(mode
, count
, type
, indices
, primcount
);
1270 _mesa_MultiDrawElementsBaseVertex(GLenum mode
,
1271 const GLsizei
*count
, GLenum type
,
1272 const GLvoid
**indices
, GLsizei primcount
,
1273 const GLint
*basevertex
)
1275 vbo_exec_MultiDrawElementsBaseVertex(mode
, count
, type
, indices
,
1276 primcount
, basevertex
);