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_attrib.h"
40 #include "vbo_split.h"
44 #define ELT_TABLE_SIZE 16
47 * Used for vertex-level splitting of indexed buffers. Note that
48 * non-indexed primitives may be converted to indexed in some cases
49 * (eg loops, fans) in order to use this splitting path.
53 struct gl_context
*ctx
;
54 const struct gl_client_array
**array
;
55 const struct _mesa_prim
*prim
;
57 const struct _mesa_index_buffer
*ib
;
60 const struct split_limits
*limits
;
65 const struct gl_client_array
*array
;
66 const GLubyte
*src_ptr
;
68 struct gl_client_array dstarray
;
70 } varying
[VBO_ATTRIB_MAX
];
73 const struct gl_client_array
*dstarray_ptr
[VBO_ATTRIB_MAX
];
74 struct _mesa_index_buffer dstib
;
76 GLuint
*translated_elt_buf
;
79 /** A baby hash table to avoid re-emitting (some) duplicate
80 * vertices when splitting indexed primitives.
85 } vert_cache
[ELT_TABLE_SIZE
];
89 GLubyte
*dstptr
; /**< dstptr == dstbuf + dstelt_max * vertsize */
90 GLuint dstbuf_size
; /**< in vertices */
91 GLuint dstbuf_nr
; /**< count of emitted vertices, also the largest value
92 * in dstelt. Our MaxIndex.
100 struct _mesa_prim dstprim
[MAX_PRIM
];
106 static GLuint
attr_size( const struct gl_client_array
*array
)
108 return array
->Size
* _mesa_sizeof_type(array
->Type
);
113 * Starts returning true slightly before the buffer fills, to ensure
114 * that there is sufficient room for any remaining vertices to finish
118 check_flush( struct copy_context
*copy
)
120 GLenum mode
= copy
->dstprim
[copy
->dstprim_nr
].mode
;
122 if (GL_TRIANGLE_STRIP
== mode
&&
123 copy
->dstelt_nr
& 1) { /* see bug9962 */
127 if (copy
->dstbuf_nr
+ 4 > copy
->dstbuf_size
)
130 if (copy
->dstelt_nr
+ 4 > copy
->dstelt_size
)
138 * Dump the parameters/info for a vbo->draw() call.
141 dump_draw_info(struct gl_context
*ctx
,
142 const struct gl_client_array
**arrays
,
143 const struct _mesa_prim
*prims
,
145 const struct _mesa_index_buffer
*ib
,
151 printf("VBO Draw:\n");
152 for (i
= 0; i
< nr_prims
; i
++) {
153 printf("Prim %u of %u\n", i
, nr_prims
);
154 printf(" Prim mode 0x%x\n", prims
[i
].mode
);
155 printf(" IB: %p\n", (void*) ib
);
156 for (j
= 0; j
< VBO_ATTRIB_MAX
; j
++) {
157 printf(" array %d at %p:\n", j
, (void*) arrays
[j
]);
158 printf(" enabled %d, ptr %p, size %d, type 0x%x, stride %d\n",
159 arrays
[j
]->Enabled
, arrays
[j
]->Ptr
,
160 arrays
[j
]->Size
, arrays
[j
]->Type
, arrays
[j
]->StrideB
);
162 GLint k
= prims
[i
].start
+ prims
[i
].count
- 1;
163 GLfloat
*last
= (GLfloat
*) (arrays
[j
]->Ptr
+ arrays
[j
]->Stride
* k
);
164 printf(" last: %f %f %f\n",
165 last
[0], last
[1], last
[2]);
173 flush( struct copy_context
*copy
)
177 /* Set some counters:
179 copy
->dstib
.count
= copy
->dstelt_nr
;
182 dump_draw_info(copy
->ctx
,
190 (void) dump_draw_info
;
193 copy
->draw( copy
->ctx
,
200 copy
->dstbuf_nr
- 1);
202 /* Reset all pointers:
204 copy
->dstprim_nr
= 0;
207 copy
->dstptr
= copy
->dstbuf
;
209 /* Clear the vertex cache:
211 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
212 copy
->vert_cache
[i
].in
= ~0;
217 * Called at begin of each primitive during replay.
220 begin( struct copy_context
*copy
, GLenum mode
, GLboolean begin_flag
)
222 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
225 prim
->begin
= begin_flag
;
226 prim
->num_instances
= 1;
231 * Use a hashtable to attempt to identify recently-emitted vertices
232 * and avoid re-emitting them.
235 elt(struct copy_context
*copy
, GLuint elt_idx
)
237 GLuint elt
= copy
->srcelt
[elt_idx
];
238 GLuint slot
= elt
& (ELT_TABLE_SIZE
-1);
240 /* printf("elt %d\n", elt); */
242 /* Look up the incoming element in the vertex cache. Re-emit if
245 if (copy
->vert_cache
[slot
].in
!= elt
) {
246 GLubyte
*csr
= copy
->dstptr
;
249 /* printf(" --> emit to dstelt %d\n", copy->dstbuf_nr); */
251 for (i
= 0; i
< copy
->nr_varying
; i
++) {
252 const struct gl_client_array
*srcarray
= copy
->varying
[i
].array
;
253 const GLubyte
*srcptr
= copy
->varying
[i
].src_ptr
+ elt
* srcarray
->StrideB
;
255 memcpy(csr
, srcptr
, copy
->varying
[i
].size
);
256 csr
+= copy
->varying
[i
].size
;
259 if (srcarray
->Type
== GL_FLOAT
) {
261 GLfloat
*f
= (GLfloat
*) srcptr
;
262 for (k
= 0; k
< srcarray
->Size
; k
++) {
263 assert(!IS_INF_OR_NAN(f
[k
]));
264 assert(f
[k
] <= 1.0e20
&& f
[k
] >= -1.0e20
);
271 const GLuint
*f
= (const GLuint
*)srcptr
;
273 printf(" varying %d: ", i
);
274 for(j
= 0; j
< copy
->varying
[i
].size
/ 4; j
++)
280 copy
->vert_cache
[slot
].in
= elt
;
281 copy
->vert_cache
[slot
].out
= copy
->dstbuf_nr
++;
282 copy
->dstptr
+= copy
->vertex_size
;
284 assert(csr
== copy
->dstptr
);
285 assert(copy
->dstptr
== (copy
->dstbuf
+
286 copy
->dstbuf_nr
* copy
->vertex_size
));
289 /* printf(" --> reuse vertex\n"); */
291 /* printf(" --> emit %d\n", copy->vert_cache[slot].out); */
292 copy
->dstelt
[copy
->dstelt_nr
++] = copy
->vert_cache
[slot
].out
;
293 return check_flush(copy
);
298 * Called at end of each primitive during replay.
301 end( struct copy_context
*copy
, GLboolean end_flag
)
303 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
305 /* printf("end (%d)\n", end_flag); */
307 prim
->end
= end_flag
;
308 prim
->count
= copy
->dstelt_nr
- prim
->start
;
310 if (++copy
->dstprim_nr
== MAX_PRIM
||
317 replay_elts( struct copy_context
*copy
)
322 for (i
= 0; i
< copy
->nr_prims
; i
++) {
323 const struct _mesa_prim
*prim
= ©
->prim
[i
];
324 const GLuint start
= prim
->start
;
327 switch (prim
->mode
) {
330 /* Convert to linestrip and emit the final vertex explicitly,
331 * but only in the resultant strip that requires it.
334 while (j
!= prim
->count
) {
335 begin(copy
, GL_LINE_STRIP
, prim
->begin
&& j
== 0);
337 for (split
= GL_FALSE
; j
!= prim
->count
&& !split
; j
++)
338 split
= elt(copy
, start
+ j
);
340 if (j
== prim
->count
) {
341 /* Done, emit final line. Split doesn't matter as
342 * it is always raised a bit early so we can emit
343 * the last verts if necessary!
346 (void)elt(copy
, start
+ 0);
348 end(copy
, prim
->end
);
360 case GL_TRIANGLE_FAN
:
363 while (j
!= prim
->count
) {
364 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
366 split
= elt(copy
, start
+0);
369 split
= elt(copy
, start
+j
-1);
372 for (; j
!= prim
->count
&& !split
; j
++)
373 split
= elt(copy
, start
+j
);
375 end(copy
, prim
->end
&& j
== prim
->count
);
377 if (j
!= prim
->count
) {
378 /* Wrapped the primitive, need to repeat some vertices:
386 (void)split_prim_inplace(prim
->mode
, &first
, &incr
);
389 while (j
!= prim
->count
) {
391 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
394 for (k
= 0; k
< first
; k
++, j
++)
395 split
|= elt(copy
, start
+j
);
399 for (; j
!= prim
->count
&& !split
; )
400 for (k
= 0; k
< incr
; k
++, j
++)
401 split
|= elt(copy
, start
+j
);
403 end(copy
, prim
->end
&& j
== prim
->count
);
405 if (j
!= prim
->count
) {
406 /* Wrapped the primitive, need to repeat some vertices:
408 assert(j
> first
- incr
);
416 if (copy
->dstprim_nr
)
422 replay_init( struct copy_context
*copy
)
424 struct gl_context
*ctx
= copy
->ctx
;
427 const GLvoid
*srcptr
;
429 /* Make a list of varying attributes and their vbo's. Also
430 * calculate vertex size.
432 copy
->vertex_size
= 0;
433 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
434 struct gl_buffer_object
*vbo
= copy
->array
[i
]->BufferObj
;
436 if (copy
->array
[i
]->StrideB
== 0) {
437 copy
->dstarray_ptr
[i
] = copy
->array
[i
];
440 GLuint j
= copy
->nr_varying
++;
442 copy
->varying
[j
].attr
= i
;
443 copy
->varying
[j
].array
= copy
->array
[i
];
444 copy
->varying
[j
].size
= attr_size(copy
->array
[i
]);
445 copy
->vertex_size
+= attr_size(copy
->array
[i
]);
447 if (_mesa_is_bufferobj(vbo
) && !_mesa_bufferobj_mapped(vbo
))
448 ctx
->Driver
.MapBufferRange(ctx
, 0, vbo
->Size
, GL_MAP_READ_BIT
, vbo
);
450 copy
->varying
[j
].src_ptr
= ADD_POINTERS(vbo
->Pointer
,
451 copy
->array
[i
]->Ptr
);
453 copy
->dstarray_ptr
[i
] = ©
->varying
[j
].dstarray
;
457 /* There must always be an index buffer. Currently require the
458 * caller convert non-indexed prims to indexed. Could alternately
461 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
462 !_mesa_bufferobj_mapped(copy
->ib
->obj
))
463 ctx
->Driver
.MapBufferRange(ctx
, 0, copy
->ib
->obj
->Size
, GL_MAP_READ_BIT
,
466 srcptr
= (const GLubyte
*) ADD_POINTERS(copy
->ib
->obj
->Pointer
,
469 switch (copy
->ib
->type
) {
470 case GL_UNSIGNED_BYTE
:
471 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
472 copy
->srcelt
= copy
->translated_elt_buf
;
474 for (i
= 0; i
< copy
->ib
->count
; i
++)
475 copy
->translated_elt_buf
[i
] = ((const GLubyte
*)srcptr
)[i
];
478 case GL_UNSIGNED_SHORT
:
479 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
480 copy
->srcelt
= copy
->translated_elt_buf
;
482 for (i
= 0; i
< copy
->ib
->count
; i
++)
483 copy
->translated_elt_buf
[i
] = ((const GLushort
*)srcptr
)[i
];
486 case GL_UNSIGNED_INT
:
487 copy
->translated_elt_buf
= NULL
;
488 copy
->srcelt
= (const GLuint
*)srcptr
;
492 /* Figure out the maximum allowed vertex buffer size:
494 if (copy
->vertex_size
* copy
->limits
->max_verts
<= copy
->limits
->max_vb_size
) {
495 copy
->dstbuf_size
= copy
->limits
->max_verts
;
498 copy
->dstbuf_size
= copy
->limits
->max_vb_size
/ copy
->vertex_size
;
501 /* Allocate an output vertex buffer:
503 * XXX: This should be a VBO!
505 copy
->dstbuf
= malloc(copy
->dstbuf_size
* copy
->vertex_size
);
506 copy
->dstptr
= copy
->dstbuf
;
508 /* Setup new vertex arrays to point into the output buffer:
510 for (offset
= 0, i
= 0; i
< copy
->nr_varying
; i
++) {
511 const struct gl_client_array
*src
= copy
->varying
[i
].array
;
512 struct gl_client_array
*dst
= ©
->varying
[i
].dstarray
;
514 dst
->Size
= src
->Size
;
515 dst
->Type
= src
->Type
;
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
->Integer
= src
->Integer
;
522 dst
->BufferObj
= ctx
->Shared
->NullBufferObj
;
523 dst
->_ElementSize
= src
->_ElementSize
;
524 dst
->_MaxElement
= copy
->dstbuf_size
; /* may be less! */
526 offset
+= copy
->varying
[i
].size
;
529 /* Allocate an output element list:
531 copy
->dstelt_size
= MIN2(65536,
532 copy
->ib
->count
* 2 + 3);
533 copy
->dstelt_size
= MIN2(copy
->dstelt_size
,
534 copy
->limits
->max_indices
);
535 copy
->dstelt
= malloc(sizeof(GLuint
) * copy
->dstelt_size
);
538 /* Setup the new index buffer to point to the allocated element
541 copy
->dstib
.count
= 0; /* duplicates dstelt_nr */
542 copy
->dstib
.type
= GL_UNSIGNED_INT
;
543 copy
->dstib
.obj
= ctx
->Shared
->NullBufferObj
;
544 copy
->dstib
.ptr
= copy
->dstelt
;
549 * Free up everything allocated during split/replay.
552 replay_finish( struct copy_context
*copy
)
554 struct gl_context
*ctx
= copy
->ctx
;
557 /* Free our vertex and index buffers:
559 free(copy
->translated_elt_buf
);
565 for (i
= 0; i
< copy
->nr_varying
; i
++) {
566 struct gl_buffer_object
*vbo
= copy
->varying
[i
].array
->BufferObj
;
567 if (_mesa_is_bufferobj(vbo
) && _mesa_bufferobj_mapped(vbo
))
568 ctx
->Driver
.UnmapBuffer(ctx
, vbo
);
571 /* Unmap index buffer:
573 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
574 _mesa_bufferobj_mapped(copy
->ib
->obj
)) {
575 ctx
->Driver
.UnmapBuffer(ctx
, copy
->ib
->obj
);
581 * Split VBO into smaller pieces, draw the pieces.
583 void vbo_split_copy( struct gl_context
*ctx
,
584 const struct gl_client_array
*arrays
[],
585 const struct _mesa_prim
*prim
,
587 const struct _mesa_index_buffer
*ib
,
589 const struct split_limits
*limits
)
591 struct copy_context copy
;
594 memset(©
, 0, sizeof(copy
));
596 /* Require indexed primitives:
603 copy
.nr_prims
= nr_prims
;
606 copy
.limits
= limits
;
608 /* Clear the vertex cache:
610 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
611 copy
.vert_cache
[i
].in
= ~0;
615 replay_finish(©
);