3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
29 /* Split indexed primitives with per-vertex copying.
32 #include "main/glheader.h"
33 #include "main/bufferobj.h"
34 #include "main/imports.h"
35 #include "main/image.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
39 #include "vbo_split.h"
43 #define ELT_TABLE_SIZE 16
46 * Used for vertex-level splitting of indexed buffers. Note that
47 * non-indexed primitives may be converted to indexed in some cases
48 * (eg loops, fans) in order to use this splitting path.
52 struct gl_context
*ctx
;
53 const struct gl_client_array
**array
;
54 const struct _mesa_prim
*prim
;
56 const struct _mesa_index_buffer
*ib
;
59 const struct split_limits
*limits
;
64 const struct gl_client_array
*array
;
65 const GLubyte
*src_ptr
;
67 struct gl_client_array dstarray
;
69 } varying
[VERT_ATTRIB_MAX
];
72 const struct gl_client_array
*dstarray_ptr
[VERT_ATTRIB_MAX
];
73 struct _mesa_index_buffer dstib
;
75 GLuint
*translated_elt_buf
;
78 /** A baby hash table to avoid re-emitting (some) duplicate
79 * vertices when splitting indexed primitives.
84 } vert_cache
[ELT_TABLE_SIZE
];
88 GLubyte
*dstptr
; /**< dstptr == dstbuf + dstelt_max * vertsize */
89 GLuint dstbuf_size
; /**< in vertices */
90 GLuint dstbuf_nr
; /**< count of emitted vertices, also the largest value
91 * in dstelt. Our MaxIndex.
99 struct _mesa_prim dstprim
[MAX_PRIM
];
105 static GLuint
attr_size( const struct gl_client_array
*array
)
107 return array
->Size
* _mesa_sizeof_type(array
->Type
);
112 * Starts returning true slightly before the buffer fills, to ensure
113 * that there is sufficient room for any remaining vertices to finish
117 check_flush( struct copy_context
*copy
)
119 GLenum mode
= copy
->dstprim
[copy
->dstprim_nr
].mode
;
121 if (GL_TRIANGLE_STRIP
== mode
&&
122 copy
->dstelt_nr
& 1) { /* see bug9962 */
126 if (copy
->dstbuf_nr
+ 4 > copy
->dstbuf_size
)
129 if (copy
->dstelt_nr
+ 4 > copy
->dstelt_size
)
137 * Dump the parameters/info for a vbo->draw() call.
140 dump_draw_info(struct gl_context
*ctx
,
141 const struct gl_client_array
**arrays
,
142 const struct _mesa_prim
*prims
,
144 const struct _mesa_index_buffer
*ib
,
150 printf("VBO Draw:\n");
151 for (i
= 0; i
< nr_prims
; i
++) {
152 printf("Prim %u of %u\n", i
, nr_prims
);
153 printf(" Prim mode 0x%x\n", prims
[i
].mode
);
154 printf(" IB: %p\n", (void*) ib
);
155 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
156 printf(" array %d at %p:\n", j
, (void*) arrays
[j
]);
157 printf(" enabled %d, ptr %p, size %d, type 0x%x, stride %d\n",
158 arrays
[j
]->Enabled
, arrays
[j
]->Ptr
,
159 arrays
[j
]->Size
, arrays
[j
]->Type
, arrays
[j
]->StrideB
);
161 GLint k
= prims
[i
].start
+ prims
[i
].count
- 1;
162 GLfloat
*last
= (GLfloat
*) (arrays
[j
]->Ptr
+ arrays
[j
]->Stride
* k
);
163 printf(" last: %f %f %f\n",
164 last
[0], last
[1], last
[2]);
172 flush( struct copy_context
*copy
)
176 /* Set some counters:
178 copy
->dstib
.count
= copy
->dstelt_nr
;
181 dump_draw_info(copy
->ctx
,
189 (void) dump_draw_info
;
192 copy
->draw( copy
->ctx
,
199 copy
->dstbuf_nr
- 1 );
201 /* Reset all pointers:
203 copy
->dstprim_nr
= 0;
206 copy
->dstptr
= copy
->dstbuf
;
208 /* Clear the vertex cache:
210 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
211 copy
->vert_cache
[i
].in
= ~0;
216 * Called at begin of each primitive during replay.
219 begin( struct copy_context
*copy
, GLenum mode
, GLboolean begin_flag
)
221 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
224 prim
->begin
= begin_flag
;
225 prim
->num_instances
= 1;
230 * Use a hashtable to attempt to identify recently-emitted vertices
231 * and avoid re-emitting them.
234 elt(struct copy_context
*copy
, GLuint elt_idx
)
236 GLuint elt
= copy
->srcelt
[elt_idx
];
237 GLuint slot
= elt
& (ELT_TABLE_SIZE
-1);
239 /* printf("elt %d\n", elt); */
241 /* Look up the incoming element in the vertex cache. Re-emit if
244 if (copy
->vert_cache
[slot
].in
!= elt
) {
245 GLubyte
*csr
= copy
->dstptr
;
248 /* printf(" --> emit to dstelt %d\n", copy->dstbuf_nr); */
250 for (i
= 0; i
< copy
->nr_varying
; i
++) {
251 const struct gl_client_array
*srcarray
= copy
->varying
[i
].array
;
252 const GLubyte
*srcptr
= copy
->varying
[i
].src_ptr
+ elt
* srcarray
->StrideB
;
254 memcpy(csr
, srcptr
, copy
->varying
[i
].size
);
255 csr
+= copy
->varying
[i
].size
;
258 if (srcarray
->Type
== GL_FLOAT
) {
260 GLfloat
*f
= (GLfloat
*) srcptr
;
261 for (k
= 0; k
< srcarray
->Size
; k
++) {
262 assert(!IS_INF_OR_NAN(f
[k
]));
263 assert(f
[k
] <= 1.0e20
&& f
[k
] >= -1.0e20
);
270 const GLuint
*f
= (const GLuint
*)srcptr
;
272 printf(" varying %d: ", i
);
273 for(j
= 0; j
< copy
->varying
[i
].size
/ 4; j
++)
279 copy
->vert_cache
[slot
].in
= elt
;
280 copy
->vert_cache
[slot
].out
= copy
->dstbuf_nr
++;
281 copy
->dstptr
+= copy
->vertex_size
;
283 assert(csr
== copy
->dstptr
);
284 assert(copy
->dstptr
== (copy
->dstbuf
+
285 copy
->dstbuf_nr
* copy
->vertex_size
));
288 /* printf(" --> reuse vertex\n"); */
290 /* printf(" --> emit %d\n", copy->vert_cache[slot].out); */
291 copy
->dstelt
[copy
->dstelt_nr
++] = copy
->vert_cache
[slot
].out
;
292 return check_flush(copy
);
297 * Called at end of each primitive during replay.
300 end( struct copy_context
*copy
, GLboolean end_flag
)
302 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
304 /* printf("end (%d)\n", end_flag); */
306 prim
->end
= end_flag
;
307 prim
->count
= copy
->dstelt_nr
- prim
->start
;
309 if (++copy
->dstprim_nr
== MAX_PRIM
||
316 replay_elts( struct copy_context
*copy
)
321 for (i
= 0; i
< copy
->nr_prims
; i
++) {
322 const struct _mesa_prim
*prim
= ©
->prim
[i
];
323 const GLuint start
= prim
->start
;
326 switch (prim
->mode
) {
329 /* Convert to linestrip and emit the final vertex explicitly,
330 * but only in the resultant strip that requires it.
333 while (j
!= prim
->count
) {
334 begin(copy
, GL_LINE_STRIP
, prim
->begin
&& j
== 0);
336 for (split
= GL_FALSE
; j
!= prim
->count
&& !split
; j
++)
337 split
= elt(copy
, start
+ j
);
339 if (j
== prim
->count
) {
340 /* Done, emit final line. Split doesn't matter as
341 * it is always raised a bit early so we can emit
342 * the last verts if necessary!
345 (void)elt(copy
, start
+ 0);
347 end(copy
, prim
->end
);
359 case GL_TRIANGLE_FAN
:
362 while (j
!= prim
->count
) {
363 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
365 split
= elt(copy
, start
+0);
368 split
= elt(copy
, start
+j
-1);
371 for (; j
!= prim
->count
&& !split
; j
++)
372 split
= elt(copy
, start
+j
);
374 end(copy
, prim
->end
&& j
== prim
->count
);
376 if (j
!= prim
->count
) {
377 /* Wrapped the primitive, need to repeat some vertices:
385 (void)split_prim_inplace(prim
->mode
, &first
, &incr
);
388 while (j
!= prim
->count
) {
390 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
393 for (k
= 0; k
< first
; k
++, j
++)
394 split
|= elt(copy
, start
+j
);
398 for (; j
!= prim
->count
&& !split
; )
399 for (k
= 0; k
< incr
; k
++, j
++)
400 split
|= elt(copy
, start
+j
);
402 end(copy
, prim
->end
&& j
== prim
->count
);
404 if (j
!= prim
->count
) {
405 /* Wrapped the primitive, need to repeat some vertices:
407 assert(j
> first
- incr
);
415 if (copy
->dstprim_nr
)
421 replay_init( struct copy_context
*copy
)
423 struct gl_context
*ctx
= copy
->ctx
;
426 const GLvoid
*srcptr
;
428 /* Make a list of varying attributes and their vbo's. Also
429 * calculate vertex size.
431 copy
->vertex_size
= 0;
432 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
433 struct gl_buffer_object
*vbo
= copy
->array
[i
]->BufferObj
;
435 if (copy
->array
[i
]->StrideB
== 0) {
436 copy
->dstarray_ptr
[i
] = copy
->array
[i
];
439 GLuint j
= copy
->nr_varying
++;
441 copy
->varying
[j
].attr
= i
;
442 copy
->varying
[j
].array
= copy
->array
[i
];
443 copy
->varying
[j
].size
= attr_size(copy
->array
[i
]);
444 copy
->vertex_size
+= attr_size(copy
->array
[i
]);
446 if (_mesa_is_bufferobj(vbo
) && !_mesa_bufferobj_mapped(vbo
))
447 ctx
->Driver
.MapBufferRange(ctx
, 0, vbo
->Size
, GL_MAP_READ_BIT
, vbo
);
449 copy
->varying
[j
].src_ptr
= ADD_POINTERS(vbo
->Pointer
,
450 copy
->array
[i
]->Ptr
);
452 copy
->dstarray_ptr
[i
] = ©
->varying
[j
].dstarray
;
456 /* There must always be an index buffer. Currently require the
457 * caller convert non-indexed prims to indexed. Could alternately
460 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
461 !_mesa_bufferobj_mapped(copy
->ib
->obj
))
462 ctx
->Driver
.MapBufferRange(ctx
, 0, copy
->ib
->obj
->Size
, GL_MAP_READ_BIT
,
465 srcptr
= (const GLubyte
*) ADD_POINTERS(copy
->ib
->obj
->Pointer
,
468 switch (copy
->ib
->type
) {
469 case GL_UNSIGNED_BYTE
:
470 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
471 copy
->srcelt
= copy
->translated_elt_buf
;
473 for (i
= 0; i
< copy
->ib
->count
; i
++)
474 copy
->translated_elt_buf
[i
] = ((const GLubyte
*)srcptr
)[i
];
477 case GL_UNSIGNED_SHORT
:
478 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
479 copy
->srcelt
= copy
->translated_elt_buf
;
481 for (i
= 0; i
< copy
->ib
->count
; i
++)
482 copy
->translated_elt_buf
[i
] = ((const GLushort
*)srcptr
)[i
];
485 case GL_UNSIGNED_INT
:
486 copy
->translated_elt_buf
= NULL
;
487 copy
->srcelt
= (const GLuint
*)srcptr
;
491 /* Figure out the maximum allowed vertex buffer size:
493 if (copy
->vertex_size
* copy
->limits
->max_verts
<= copy
->limits
->max_vb_size
) {
494 copy
->dstbuf_size
= copy
->limits
->max_verts
;
497 copy
->dstbuf_size
= copy
->limits
->max_vb_size
/ copy
->vertex_size
;
500 /* Allocate an output vertex buffer:
502 * XXX: This should be a VBO!
504 copy
->dstbuf
= malloc(copy
->dstbuf_size
* copy
->vertex_size
);
505 copy
->dstptr
= copy
->dstbuf
;
507 /* Setup new vertex arrays to point into the output buffer:
509 for (offset
= 0, i
= 0; i
< copy
->nr_varying
; i
++) {
510 const struct gl_client_array
*src
= copy
->varying
[i
].array
;
511 struct gl_client_array
*dst
= ©
->varying
[i
].dstarray
;
513 dst
->Size
= src
->Size
;
514 dst
->Type
= src
->Type
;
515 dst
->Format
= GL_RGBA
;
516 dst
->Stride
= copy
->vertex_size
;
517 dst
->StrideB
= copy
->vertex_size
;
518 dst
->Ptr
= copy
->dstbuf
+ offset
;
519 dst
->Enabled
= GL_TRUE
;
520 dst
->Normalized
= src
->Normalized
;
521 dst
->BufferObj
= ctx
->Shared
->NullBufferObj
;
522 dst
->_ElementSize
= src
->_ElementSize
;
523 dst
->_MaxElement
= copy
->dstbuf_size
; /* may be less! */
525 offset
+= copy
->varying
[i
].size
;
528 /* Allocate an output element list:
530 copy
->dstelt_size
= MIN2(65536,
531 copy
->ib
->count
* 2 + 3);
532 copy
->dstelt_size
= MIN2(copy
->dstelt_size
,
533 copy
->limits
->max_indices
);
534 copy
->dstelt
= malloc(sizeof(GLuint
) * copy
->dstelt_size
);
537 /* Setup the new index buffer to point to the allocated element
540 copy
->dstib
.count
= 0; /* duplicates dstelt_nr */
541 copy
->dstib
.type
= GL_UNSIGNED_INT
;
542 copy
->dstib
.obj
= ctx
->Shared
->NullBufferObj
;
543 copy
->dstib
.ptr
= copy
->dstelt
;
548 * Free up everything allocated during split/replay.
551 replay_finish( struct copy_context
*copy
)
553 struct gl_context
*ctx
= copy
->ctx
;
556 /* Free our vertex and index buffers:
558 free(copy
->translated_elt_buf
);
564 for (i
= 0; i
< copy
->nr_varying
; i
++) {
565 struct gl_buffer_object
*vbo
= copy
->varying
[i
].array
->BufferObj
;
566 if (_mesa_is_bufferobj(vbo
) && _mesa_bufferobj_mapped(vbo
))
567 ctx
->Driver
.UnmapBuffer(ctx
, vbo
);
570 /* Unmap index buffer:
572 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
573 _mesa_bufferobj_mapped(copy
->ib
->obj
)) {
574 ctx
->Driver
.UnmapBuffer(ctx
, copy
->ib
->obj
);
580 * Split VBO into smaller pieces, draw the pieces.
582 void vbo_split_copy( struct gl_context
*ctx
,
583 const struct gl_client_array
*arrays
[],
584 const struct _mesa_prim
*prim
,
586 const struct _mesa_index_buffer
*ib
,
588 const struct split_limits
*limits
)
590 struct copy_context copy
;
591 GLuint i
, this_nr_prims
;
593 for (i
= 0; i
< nr_prims
;) {
594 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
595 * will rebase the elements to the basevertex, and we'll only
596 * emit strings of prims with the same basevertex in one draw call.
598 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
600 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
604 memset(©
, 0, sizeof(copy
));
606 /* Require indexed primitives:
612 copy
.prim
= &prim
[i
];
613 copy
.nr_prims
= this_nr_prims
;
616 copy
.limits
= limits
;
618 /* Clear the vertex cache:
620 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
621 copy
.vert_cache
[i
].in
= ~0;
625 replay_finish(©
);