3 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keithw@vmware.com>
29 /* Split indexed primitives with per-vertex copying.
34 #include "main/glheader.h"
35 #include "main/bufferobj.h"
36 #include "main/imports.h"
37 #include "main/glformats.h"
38 #include "main/macros.h"
39 #include "main/mtypes.h"
41 #include "vbo_split.h"
45 #define ELT_TABLE_SIZE 16
48 * Used for vertex-level splitting of indexed buffers. Note that
49 * non-indexed primitives may be converted to indexed in some cases
50 * (eg loops, fans) in order to use this splitting path.
54 struct gl_context
*ctx
;
55 const struct gl_client_array
**array
;
56 const struct _mesa_prim
*prim
;
58 const struct _mesa_index_buffer
*ib
;
61 const struct split_limits
*limits
;
66 const struct gl_client_array
*array
;
67 const GLubyte
*src_ptr
;
69 struct gl_client_array dstarray
;
71 } varying
[VERT_ATTRIB_MAX
];
74 const struct gl_client_array
*dstarray_ptr
[VERT_ATTRIB_MAX
];
75 struct _mesa_index_buffer dstib
;
77 GLuint
*translated_elt_buf
;
80 /** A baby hash table to avoid re-emitting (some) duplicate
81 * vertices when splitting indexed primitives.
86 } vert_cache
[ELT_TABLE_SIZE
];
90 GLubyte
*dstptr
; /**< dstptr == dstbuf + dstelt_max * vertsize */
91 GLuint dstbuf_size
; /**< in vertices */
92 GLuint dstbuf_nr
; /**< count of emitted vertices, also the largest value
93 * in dstelt. Our MaxIndex.
101 struct _mesa_prim dstprim
[MAX_PRIM
];
107 static GLuint
attr_size( const struct gl_client_array
*array
)
109 return array
->Size
* _mesa_sizeof_type(array
->Type
);
114 * Starts returning true slightly before the buffer fills, to ensure
115 * that there is sufficient room for any remaining vertices to finish
119 check_flush( struct copy_context
*copy
)
121 GLenum mode
= copy
->dstprim
[copy
->dstprim_nr
].mode
;
123 if (GL_TRIANGLE_STRIP
== mode
&&
124 copy
->dstelt_nr
& 1) { /* see bug9962 */
128 if (copy
->dstbuf_nr
+ 4 > copy
->dstbuf_size
)
131 if (copy
->dstelt_nr
+ 4 > copy
->dstelt_size
)
139 * Dump the parameters/info for a vbo->draw() call.
142 dump_draw_info(struct gl_context
*ctx
,
143 const struct gl_client_array
**arrays
,
144 const struct _mesa_prim
*prims
,
146 const struct _mesa_index_buffer
*ib
,
152 printf("VBO Draw:\n");
153 for (i
= 0; i
< nr_prims
; i
++) {
154 printf("Prim %u of %u\n", i
, nr_prims
);
155 printf(" Prim mode 0x%x\n", prims
[i
].mode
);
156 printf(" IB: %p\n", (void*) ib
);
157 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
158 printf(" array %d at %p:\n", j
, (void*) arrays
[j
]);
159 printf(" enabled %d, ptr %p, size %d, type 0x%x, stride %d\n",
160 arrays
[j
]->Enabled
, arrays
[j
]->Ptr
,
161 arrays
[j
]->Size
, arrays
[j
]->Type
, arrays
[j
]->StrideB
);
163 GLint k
= prims
[i
].start
+ prims
[i
].count
- 1;
164 GLfloat
*last
= (GLfloat
*) (arrays
[j
]->Ptr
+ arrays
[j
]->Stride
* k
);
165 printf(" last: %f %f %f\n",
166 last
[0], last
[1], last
[2]);
174 flush( struct copy_context
*copy
)
176 struct gl_context
*ctx
= copy
->ctx
;
177 const struct gl_client_array
**saved_arrays
= ctx
->Array
._DrawArrays
;
180 /* Set some counters:
182 copy
->dstib
.count
= copy
->dstelt_nr
;
185 dump_draw_info(copy
->ctx
,
193 (void) dump_draw_info
;
196 ctx
->Array
._DrawArrays
= copy
->dstarray_ptr
;
197 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
208 ctx
->Array
._DrawArrays
= saved_arrays
;
209 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
211 /* Reset all pointers:
213 copy
->dstprim_nr
= 0;
216 copy
->dstptr
= copy
->dstbuf
;
218 /* Clear the vertex cache:
220 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
221 copy
->vert_cache
[i
].in
= ~0;
226 * Called at begin of each primitive during replay.
229 begin( struct copy_context
*copy
, GLenum mode
, GLboolean begin_flag
)
231 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
234 prim
->begin
= begin_flag
;
235 prim
->num_instances
= 1;
240 * Use a hashtable to attempt to identify recently-emitted vertices
241 * and avoid re-emitting them.
244 elt(struct copy_context
*copy
, GLuint elt_idx
)
246 GLuint elt
= copy
->srcelt
[elt_idx
];
247 GLuint slot
= elt
& (ELT_TABLE_SIZE
-1);
249 /* printf("elt %d\n", elt); */
251 /* Look up the incoming element in the vertex cache. Re-emit if
254 if (copy
->vert_cache
[slot
].in
!= elt
) {
255 GLubyte
*csr
= copy
->dstptr
;
258 /* printf(" --> emit to dstelt %d\n", copy->dstbuf_nr); */
260 for (i
= 0; i
< copy
->nr_varying
; i
++) {
261 const struct gl_client_array
*srcarray
= copy
->varying
[i
].array
;
262 const GLubyte
*srcptr
= copy
->varying
[i
].src_ptr
+ elt
* srcarray
->StrideB
;
264 memcpy(csr
, srcptr
, copy
->varying
[i
].size
);
265 csr
+= copy
->varying
[i
].size
;
268 if (srcarray
->Type
== GL_FLOAT
) {
270 GLfloat
*f
= (GLfloat
*) srcptr
;
271 for (k
= 0; k
< srcarray
->Size
; k
++) {
272 assert(!IS_INF_OR_NAN(f
[k
]));
273 assert(f
[k
] <= 1.0e20
&& f
[k
] >= -1.0e20
);
280 const GLuint
*f
= (const GLuint
*)srcptr
;
282 printf(" varying %d: ", i
);
283 for(j
= 0; j
< copy
->varying
[i
].size
/ 4; j
++)
289 copy
->vert_cache
[slot
].in
= elt
;
290 copy
->vert_cache
[slot
].out
= copy
->dstbuf_nr
++;
291 copy
->dstptr
+= copy
->vertex_size
;
293 assert(csr
== copy
->dstptr
);
294 assert(copy
->dstptr
== (copy
->dstbuf
+
295 copy
->dstbuf_nr
* copy
->vertex_size
));
298 /* printf(" --> reuse vertex\n"); */
300 /* printf(" --> emit %d\n", copy->vert_cache[slot].out); */
301 copy
->dstelt
[copy
->dstelt_nr
++] = copy
->vert_cache
[slot
].out
;
302 return check_flush(copy
);
307 * Called at end of each primitive during replay.
310 end( struct copy_context
*copy
, GLboolean end_flag
)
312 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
314 /* printf("end (%d)\n", end_flag); */
316 prim
->end
= end_flag
;
317 prim
->count
= copy
->dstelt_nr
- prim
->start
;
319 if (++copy
->dstprim_nr
== MAX_PRIM
||
326 replay_elts( struct copy_context
*copy
)
331 for (i
= 0; i
< copy
->nr_prims
; i
++) {
332 const struct _mesa_prim
*prim
= ©
->prim
[i
];
333 const GLuint start
= prim
->start
;
336 switch (prim
->mode
) {
339 /* Convert to linestrip and emit the final vertex explicitly,
340 * but only in the resultant strip that requires it.
343 while (j
!= prim
->count
) {
344 begin(copy
, GL_LINE_STRIP
, prim
->begin
&& j
== 0);
346 for (split
= GL_FALSE
; j
!= prim
->count
&& !split
; j
++)
347 split
= elt(copy
, start
+ j
);
349 if (j
== prim
->count
) {
350 /* Done, emit final line. Split doesn't matter as
351 * it is always raised a bit early so we can emit
352 * the last verts if necessary!
355 (void)elt(copy
, start
+ 0);
357 end(copy
, prim
->end
);
369 case GL_TRIANGLE_FAN
:
372 while (j
!= prim
->count
) {
373 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
375 split
= elt(copy
, start
+0);
378 split
= elt(copy
, start
+j
-1);
381 for (; j
!= prim
->count
&& !split
; j
++)
382 split
= elt(copy
, start
+j
);
384 end(copy
, prim
->end
&& j
== prim
->count
);
386 if (j
!= prim
->count
) {
387 /* Wrapped the primitive, need to repeat some vertices:
395 (void)split_prim_inplace(prim
->mode
, &first
, &incr
);
398 while (j
!= prim
->count
) {
400 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
403 for (k
= 0; k
< first
; k
++, j
++)
404 split
|= elt(copy
, start
+j
);
408 for (; j
!= prim
->count
&& !split
; )
409 for (k
= 0; k
< incr
; k
++, j
++)
410 split
|= elt(copy
, start
+j
);
412 end(copy
, prim
->end
&& j
== prim
->count
);
414 if (j
!= prim
->count
) {
415 /* Wrapped the primitive, need to repeat some vertices:
417 assert(j
> first
- incr
);
425 if (copy
->dstprim_nr
)
431 replay_init( struct copy_context
*copy
)
433 struct gl_context
*ctx
= copy
->ctx
;
436 const GLvoid
*srcptr
;
438 /* Make a list of varying attributes and their vbo's. Also
439 * calculate vertex size.
441 copy
->vertex_size
= 0;
442 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
443 struct gl_buffer_object
*vbo
= copy
->array
[i
]->BufferObj
;
445 if (copy
->array
[i
]->StrideB
== 0) {
446 copy
->dstarray_ptr
[i
] = copy
->array
[i
];
449 GLuint j
= copy
->nr_varying
++;
451 copy
->varying
[j
].attr
= i
;
452 copy
->varying
[j
].array
= copy
->array
[i
];
453 copy
->varying
[j
].size
= attr_size(copy
->array
[i
]);
454 copy
->vertex_size
+= attr_size(copy
->array
[i
]);
456 if (_mesa_is_bufferobj(vbo
) &&
457 !_mesa_bufferobj_mapped(vbo
, MAP_INTERNAL
))
458 ctx
->Driver
.MapBufferRange(ctx
, 0, vbo
->Size
, GL_MAP_READ_BIT
, vbo
,
461 copy
->varying
[j
].src_ptr
=
462 ADD_POINTERS(vbo
->Mappings
[MAP_INTERNAL
].Pointer
,
463 copy
->array
[i
]->Ptr
);
465 copy
->dstarray_ptr
[i
] = ©
->varying
[j
].dstarray
;
469 /* There must always be an index buffer. Currently require the
470 * caller convert non-indexed prims to indexed. Could alternately
473 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
474 !_mesa_bufferobj_mapped(copy
->ib
->obj
, MAP_INTERNAL
))
475 ctx
->Driver
.MapBufferRange(ctx
, 0, copy
->ib
->obj
->Size
, GL_MAP_READ_BIT
,
476 copy
->ib
->obj
, MAP_INTERNAL
);
478 srcptr
= (const GLubyte
*)
479 ADD_POINTERS(copy
->ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
,
482 switch (copy
->ib
->type
) {
483 case GL_UNSIGNED_BYTE
:
484 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
485 copy
->srcelt
= copy
->translated_elt_buf
;
487 for (i
= 0; i
< copy
->ib
->count
; i
++)
488 copy
->translated_elt_buf
[i
] = ((const GLubyte
*)srcptr
)[i
];
491 case GL_UNSIGNED_SHORT
:
492 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
493 copy
->srcelt
= copy
->translated_elt_buf
;
495 for (i
= 0; i
< copy
->ib
->count
; i
++)
496 copy
->translated_elt_buf
[i
] = ((const GLushort
*)srcptr
)[i
];
499 case GL_UNSIGNED_INT
:
500 copy
->translated_elt_buf
= NULL
;
501 copy
->srcelt
= (const GLuint
*)srcptr
;
505 /* Figure out the maximum allowed vertex buffer size:
507 if (copy
->vertex_size
* copy
->limits
->max_verts
<= copy
->limits
->max_vb_size
) {
508 copy
->dstbuf_size
= copy
->limits
->max_verts
;
511 copy
->dstbuf_size
= copy
->limits
->max_vb_size
/ copy
->vertex_size
;
514 /* Allocate an output vertex buffer:
516 * XXX: This should be a VBO!
518 copy
->dstbuf
= malloc(copy
->dstbuf_size
* copy
->vertex_size
);
519 copy
->dstptr
= copy
->dstbuf
;
521 /* Setup new vertex arrays to point into the output buffer:
523 for (offset
= 0, i
= 0; i
< copy
->nr_varying
; i
++) {
524 const struct gl_client_array
*src
= copy
->varying
[i
].array
;
525 struct gl_client_array
*dst
= ©
->varying
[i
].dstarray
;
527 dst
->Size
= src
->Size
;
528 dst
->Type
= src
->Type
;
529 dst
->Format
= GL_RGBA
;
530 dst
->Stride
= copy
->vertex_size
;
531 dst
->StrideB
= copy
->vertex_size
;
532 dst
->Ptr
= copy
->dstbuf
+ offset
;
533 dst
->Enabled
= GL_TRUE
;
534 dst
->Normalized
= src
->Normalized
;
535 dst
->Integer
= src
->Integer
;
536 dst
->BufferObj
= ctx
->Shared
->NullBufferObj
;
537 dst
->_ElementSize
= src
->_ElementSize
;
539 offset
+= copy
->varying
[i
].size
;
542 /* Allocate an output element list:
544 copy
->dstelt_size
= MIN2(65536,
545 copy
->ib
->count
* 2 + 3);
546 copy
->dstelt_size
= MIN2(copy
->dstelt_size
,
547 copy
->limits
->max_indices
);
548 copy
->dstelt
= malloc(sizeof(GLuint
) * copy
->dstelt_size
);
551 /* Setup the new index buffer to point to the allocated element
554 copy
->dstib
.count
= 0; /* duplicates dstelt_nr */
555 copy
->dstib
.type
= GL_UNSIGNED_INT
;
556 copy
->dstib
.obj
= ctx
->Shared
->NullBufferObj
;
557 copy
->dstib
.ptr
= copy
->dstelt
;
562 * Free up everything allocated during split/replay.
565 replay_finish( struct copy_context
*copy
)
567 struct gl_context
*ctx
= copy
->ctx
;
570 /* Free our vertex and index buffers:
572 free(copy
->translated_elt_buf
);
578 for (i
= 0; i
< copy
->nr_varying
; i
++) {
579 struct gl_buffer_object
*vbo
= copy
->varying
[i
].array
->BufferObj
;
580 if (_mesa_is_bufferobj(vbo
) && _mesa_bufferobj_mapped(vbo
, MAP_INTERNAL
))
581 ctx
->Driver
.UnmapBuffer(ctx
, vbo
, MAP_INTERNAL
);
584 /* Unmap index buffer:
586 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
587 _mesa_bufferobj_mapped(copy
->ib
->obj
, MAP_INTERNAL
)) {
588 ctx
->Driver
.UnmapBuffer(ctx
, copy
->ib
->obj
, MAP_INTERNAL
);
594 * Split VBO into smaller pieces, draw the pieces.
596 void vbo_split_copy( struct gl_context
*ctx
,
597 const struct gl_client_array
*arrays
[],
598 const struct _mesa_prim
*prim
,
600 const struct _mesa_index_buffer
*ib
,
602 const struct split_limits
*limits
)
604 struct copy_context copy
;
605 GLuint i
, this_nr_prims
;
607 for (i
= 0; i
< nr_prims
;) {
608 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
609 * will rebase the elements to the basevertex, and we'll only
610 * emit strings of prims with the same basevertex in one draw call.
612 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
614 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
618 memset(©
, 0, sizeof(copy
));
620 /* Require indexed primitives:
626 copy
.prim
= &prim
[i
];
627 copy
.nr_prims
= this_nr_prims
;
630 copy
.limits
= limits
;
632 /* Clear the vertex cache:
634 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
635 copy
.vert_cache
[i
].in
= ~0;
639 replay_finish(©
);