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/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "main/enums.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.
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 flush( struct copy_context
*copy
)
141 /* Set some counters:
143 copy
->dstib
.count
= copy
->dstelt_nr
;
145 copy
->draw( copy
->ctx
,
153 /* Reset all pointers:
155 copy
->dstprim_nr
= 0;
158 copy
->dstptr
= copy
->dstbuf
;
160 /* Clear the vertex cache:
162 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
163 copy
->vert_cache
[i
].in
= ~0;
168 * Called at begin of each primitive during replay.
171 begin( struct copy_context
*copy
, GLenum mode
, GLboolean begin_flag
)
173 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
175 /* _mesa_printf("begin %s (%d)\n", _mesa_lookup_enum_by_nr(mode), begin_flag); */
178 prim
->begin
= begin_flag
;
183 * Use a hashtable to attempt to identify recently-emitted vertices
184 * and avoid re-emitting them.
187 elt(struct copy_context
*copy
, GLuint elt_idx
)
189 GLuint elt
= copy
->srcelt
[elt_idx
];
190 GLuint slot
= elt
& (ELT_TABLE_SIZE
-1);
192 /* _mesa_printf("elt %d\n", elt); */
194 /* Look up the incoming element in the vertex cache. Re-emit if
197 if (copy
->vert_cache
[slot
].in
!= elt
) {
198 GLubyte
*csr
= copy
->dstptr
;
201 /* _mesa_printf(" --> emit to dstelt %d\n", copy->dstbuf_nr); */
203 for (i
= 0; i
< copy
->nr_varying
; i
++) {
204 const struct gl_client_array
*srcarray
= copy
->varying
[i
].array
;
205 const GLubyte
*srcptr
= copy
->varying
[i
].src_ptr
+ elt
* srcarray
->StrideB
;
207 memcpy(csr
, srcptr
, copy
->varying
[i
].size
);
208 csr
+= copy
->varying
[i
].size
;
212 const GLuint
*f
= (const GLuint
*)srcptr
;
214 _mesa_printf(" varying %d: ", i
);
215 for(j
= 0; j
< copy
->varying
[i
].size
/ 4; j
++)
216 _mesa_printf("%x ", f
[j
]);
221 copy
->vert_cache
[slot
].in
= elt
;
222 copy
->vert_cache
[slot
].out
= copy
->dstbuf_nr
++;
223 copy
->dstptr
+= copy
->vertex_size
;
225 assert(csr
== copy
->dstptr
);
226 assert(copy
->dstptr
== (copy
->dstbuf
+
227 copy
->dstbuf_nr
* copy
->vertex_size
));
230 /* _mesa_printf(" --> reuse vertex\n"); */
232 /* _mesa_printf(" --> emit %d\n", copy->vert_cache[slot].out); */
233 copy
->dstelt
[copy
->dstelt_nr
++] = copy
->vert_cache
[slot
].out
;
234 return check_flush(copy
);
239 * Called at end of each primitive during replay.
242 end( struct copy_context
*copy
, GLboolean end_flag
)
244 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
246 /* _mesa_printf("end (%d)\n", end_flag); */
248 prim
->end
= end_flag
;
249 prim
->count
= copy
->dstelt_nr
- prim
->start
;
251 if (++copy
->dstprim_nr
== MAX_PRIM
||
258 replay_elts( struct copy_context
*copy
)
263 for (i
= 0; i
< copy
->nr_prims
; i
++) {
264 const struct _mesa_prim
*prim
= ©
->prim
[i
];
265 const GLuint start
= prim
->start
;
268 switch (prim
->mode
) {
271 /* Convert to linestrip and emit the final vertex explicitly,
272 * but only in the resultant strip that requires it.
275 while (j
!= prim
->count
) {
276 begin(copy
, GL_LINE_STRIP
, prim
->begin
&& j
== 0);
278 for (split
= GL_FALSE
; j
!= prim
->count
&& !split
; j
++)
279 split
= elt(copy
, start
+ j
);
281 if (j
== prim
->count
) {
282 /* Done, emit final line. Split doesn't matter as
283 * it is always raised a bit early so we can emit
284 * the last verts if necessary!
287 (void)elt(copy
, start
+ 0);
289 end(copy
, prim
->end
);
301 case GL_TRIANGLE_FAN
:
304 while (j
!= prim
->count
) {
305 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
307 split
= elt(copy
, start
+0);
310 split
= elt(copy
, start
+j
-1);
313 for (; j
!= prim
->count
&& !split
; j
++)
314 split
= elt(copy
, start
+j
);
316 end(copy
, prim
->end
&& j
== prim
->count
);
318 if (j
!= prim
->count
) {
319 /* Wrapped the primitive, need to repeat some vertices:
327 (void)split_prim_inplace(prim
->mode
, &first
, &incr
);
330 while (j
!= prim
->count
) {
332 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
335 for (k
= 0; k
< first
; k
++, j
++)
336 split
|= elt(copy
, start
+j
);
340 for (; j
!= prim
->count
&& !split
; )
341 for (k
= 0; k
< incr
; k
++, j
++)
342 split
|= elt(copy
, start
+j
);
344 end(copy
, prim
->end
&& j
== prim
->count
);
346 if (j
!= prim
->count
) {
347 /* Wrapped the primitive, need to repeat some vertices:
349 assert(j
> first
- incr
);
357 if (copy
->dstprim_nr
)
363 replay_init( struct copy_context
*copy
)
365 GLcontext
*ctx
= copy
->ctx
;
368 const GLvoid
*srcptr
;
370 /* Make a list of varying attributes and their vbo's. Also
371 * calculate vertex size.
373 copy
->vertex_size
= 0;
374 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
375 struct gl_buffer_object
*vbo
= copy
->array
[i
]->BufferObj
;
377 if (copy
->array
[i
]->StrideB
== 0) {
378 copy
->dstarray_ptr
[i
] = copy
->array
[i
];
381 GLuint j
= copy
->nr_varying
++;
383 copy
->varying
[j
].attr
= i
;
384 copy
->varying
[j
].array
= copy
->array
[i
];
385 copy
->varying
[j
].size
= attr_size(copy
->array
[i
]);
386 copy
->vertex_size
+= attr_size(copy
->array
[i
]);
388 if (vbo
->Name
&& !vbo
->Pointer
)
389 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY
, vbo
);
391 copy
->varying
[j
].src_ptr
= ADD_POINTERS(vbo
->Pointer
,
392 copy
->array
[i
]->Ptr
);
394 copy
->dstarray_ptr
[i
] = ©
->varying
[j
].dstarray
;
398 /* There must always be an index buffer. Currently require the
399 * caller convert non-indexed prims to indexed. Could alternately
402 if (copy
->ib
->obj
->Name
&& !copy
->ib
->obj
->Pointer
)
403 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, GL_READ_ONLY
,
406 srcptr
= (const GLubyte
*) ADD_POINTERS(copy
->ib
->obj
->Pointer
,
409 switch (copy
->ib
->type
) {
410 case GL_UNSIGNED_BYTE
:
411 copy
->translated_elt_buf
= _mesa_malloc(sizeof(GLuint
) * copy
->ib
->count
);
412 copy
->srcelt
= copy
->translated_elt_buf
;
414 for (i
= 0; i
< copy
->ib
->count
; i
++)
415 copy
->translated_elt_buf
[i
] = ((const GLubyte
*)srcptr
)[i
];
418 case GL_UNSIGNED_SHORT
:
419 copy
->translated_elt_buf
= _mesa_malloc(sizeof(GLuint
) * copy
->ib
->count
);
420 copy
->srcelt
= copy
->translated_elt_buf
;
422 for (i
= 0; i
< copy
->ib
->count
; i
++)
423 copy
->translated_elt_buf
[i
] = ((const GLushort
*)srcptr
)[i
];
426 case GL_UNSIGNED_INT
:
427 copy
->translated_elt_buf
= NULL
;
428 copy
->srcelt
= (const GLuint
*)srcptr
;
432 /* Figure out the maximum allowed vertex buffer size:
434 if (copy
->vertex_size
* copy
->limits
->max_verts
<= copy
->limits
->max_vb_size
) {
435 copy
->dstbuf_size
= copy
->limits
->max_verts
;
438 copy
->dstbuf_size
= copy
->limits
->max_vb_size
/ copy
->vertex_size
;
441 /* Allocate an output vertex buffer:
443 * XXX: This should be a VBO!
445 copy
->dstbuf
= _mesa_malloc(copy
->dstbuf_size
* copy
->vertex_size
);
446 copy
->dstptr
= copy
->dstbuf
;
448 /* Setup new vertex arrays to point into the output buffer:
450 for (offset
= 0, i
= 0; i
< copy
->nr_varying
; i
++) {
451 const struct gl_client_array
*src
= copy
->varying
[i
].array
;
452 struct gl_client_array
*dst
= ©
->varying
[i
].dstarray
;
454 dst
->Size
= src
->Size
;
455 dst
->Type
= src
->Type
;
456 dst
->Format
= GL_RGBA
;
457 dst
->Stride
= copy
->vertex_size
;
458 dst
->StrideB
= copy
->vertex_size
;
459 dst
->Ptr
= copy
->dstbuf
+ offset
;
460 dst
->Enabled
= GL_TRUE
;
461 dst
->Normalized
= src
->Normalized
;
462 dst
->BufferObj
= ctx
->Shared
->NullBufferObj
;
463 dst
->_MaxElement
= copy
->dstbuf_size
; /* may be less! */
465 offset
+= copy
->varying
[i
].size
;
468 /* Allocate an output element list:
470 copy
->dstelt_size
= MIN2(65536,
471 copy
->ib
->count
* 2 + 3);
472 copy
->dstelt_size
= MIN2(copy
->dstelt_size
,
473 copy
->limits
->max_indices
);
474 copy
->dstelt
= _mesa_malloc(sizeof(GLuint
) * copy
->dstelt_size
);
477 /* Setup the new index buffer to point to the allocated element
480 copy
->dstib
.count
= 0; /* duplicates dstelt_nr */
481 copy
->dstib
.type
= GL_UNSIGNED_INT
;
482 copy
->dstib
.obj
= ctx
->Shared
->NullBufferObj
;
483 copy
->dstib
.ptr
= copy
->dstelt
;
488 * Free up everything allocated during split/replay.
491 replay_finish( struct copy_context
*copy
)
493 GLcontext
*ctx
= copy
->ctx
;
496 /* Free our vertex and index buffers:
498 _mesa_free(copy
->translated_elt_buf
);
499 _mesa_free(copy
->dstbuf
);
500 _mesa_free(copy
->dstelt
);
504 for (i
= 0; i
< copy
->nr_varying
; i
++) {
505 struct gl_buffer_object
*vbo
= copy
->varying
[i
].array
->BufferObj
;
506 if (vbo
->Name
&& vbo
->Pointer
)
507 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, vbo
);
510 /* Unmap index buffer:
512 if (copy
->ib
->obj
->Name
&& copy
->ib
->obj
->Pointer
) {
513 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, copy
->ib
->obj
);
519 * Split VBO into smaller pieces, draw the pieces.
521 void vbo_split_copy( GLcontext
*ctx
,
522 const struct gl_client_array
*arrays
[],
523 const struct _mesa_prim
*prim
,
525 const struct _mesa_index_buffer
*ib
,
527 const struct split_limits
*limits
)
529 struct copy_context copy
;
532 memset(©
, 0, sizeof(copy
));
534 /* Require indexed primitives:
541 copy
.nr_prims
= nr_prims
;
544 copy
.limits
= limits
;
546 /* Clear the vertex cache:
548 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
549 copy
.vert_cache
[i
].in
= ~0;
553 replay_finish(©
);