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"
40 #include "main/varray.h"
47 #define ELT_TABLE_SIZE 16
50 * Used for vertex-level splitting of indexed buffers. Note that
51 * non-indexed primitives may be converted to indexed in some cases
52 * (eg loops, fans) in order to use this splitting path.
55 struct gl_context
*ctx
;
56 const struct tnl_vertex_array
*array
;
57 const struct _mesa_prim
*prim
;
59 const struct _mesa_index_buffer
*ib
;
62 const struct split_limits
*limits
;
67 const struct tnl_vertex_array
*array
;
68 const GLubyte
*src_ptr
;
70 struct gl_vertex_buffer_binding dstbinding
;
71 struct gl_array_attributes dstattribs
;
73 } varying
[VERT_ATTRIB_MAX
];
76 struct tnl_vertex_array dstarray
[VERT_ATTRIB_MAX
];
77 struct _mesa_index_buffer dstib
;
79 GLuint
*translated_elt_buf
;
82 /** A baby hash table to avoid re-emitting (some) duplicate
83 * vertices when splitting indexed primitives.
88 } vert_cache
[ELT_TABLE_SIZE
];
92 GLubyte
*dstptr
; /**< dstptr == dstbuf + dstelt_max * vertsize */
93 GLuint dstbuf_size
; /**< in vertices */
94 GLuint dstbuf_nr
; /**< count of emitted vertices, also the largest value
95 * in dstelt. Our MaxIndex.
103 struct _mesa_prim dstprim
[MAX_PRIM
];
109 attr_size(const struct gl_array_attributes
*attrib
)
111 return attrib
->Size
* _mesa_sizeof_type(attrib
->Type
);
116 * Shallow copy one vertex array to another.
119 copy_vertex_array(struct tnl_vertex_array
*dst
,
120 const struct tnl_vertex_array
*src
)
122 dst
->VertexAttrib
= src
->VertexAttrib
;
123 dst
->BufferBinding
= src
->BufferBinding
;
128 * Starts returning true slightly before the buffer fills, to ensure
129 * that there is sufficient room for any remaining vertices to finish
133 check_flush(struct copy_context
*copy
)
135 GLenum mode
= copy
->dstprim
[copy
->dstprim_nr
].mode
;
137 if (GL_TRIANGLE_STRIP
== mode
&&
138 copy
->dstelt_nr
& 1) { /* see bug9962 */
142 if (copy
->dstbuf_nr
+ 4 > copy
->dstbuf_size
)
145 if (copy
->dstelt_nr
+ 4 > copy
->dstelt_size
)
153 * Dump the parameters/info for a vbo->draw() call.
156 dump_draw_info(struct gl_context
*ctx
,
157 const struct tnl_vertex_array
*arrays
,
158 const struct _mesa_prim
*prims
,
160 const struct _mesa_index_buffer
*ib
,
166 printf("VBO Draw:\n");
167 for (i
= 0; i
< nr_prims
; i
++) {
168 printf("Prim %u of %u\n", i
, nr_prims
);
169 printf(" Prim mode 0x%x\n", prims
[i
].mode
);
170 printf(" IB: %p\n", (void*) ib
);
171 for (j
= 0; j
< VERT_ATTRIB_MAX
; j
++) {
172 const struct tnl_vertex_array
*array
= &arrays
[j
];
173 const struct gl_vertex_buffer_binding
*binding
174 = array
->BufferBinding
;
175 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
176 const GLubyte
*ptr
= _mesa_vertex_attrib_address(attrib
, binding
);
177 printf(" array %d at %p:\n", j
, (void*) &arrays
[j
]);
178 printf(" ptr %p, size %d, type 0x%x, stride %d\n",
179 ptr
, attrib
->Size
, attrib
->Type
, binding
->Stride
);
181 GLint k
= prims
[i
].start
+ prims
[i
].count
- 1;
182 GLfloat
*last
= (GLfloat
*) (ptr
+ binding
->Stride
* k
);
183 printf(" last: %f %f %f\n",
184 last
[0], last
[1], last
[2]);
192 flush(struct copy_context
*copy
)
194 struct gl_context
*ctx
= copy
->ctx
;
197 /* Set some counters:
199 copy
->dstib
.count
= copy
->dstelt_nr
;
202 dump_draw_info(copy
->ctx
,
210 (void) dump_draw_info
;
223 /* Reset all pointers:
225 copy
->dstprim_nr
= 0;
228 copy
->dstptr
= copy
->dstbuf
;
230 /* Clear the vertex cache:
232 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
233 copy
->vert_cache
[i
].in
= ~0;
238 * Called at begin of each primitive during replay.
241 begin(struct copy_context
*copy
, GLenum mode
, GLboolean begin_flag
)
243 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
246 prim
->begin
= begin_flag
;
247 prim
->num_instances
= 1;
252 * Use a hashtable to attempt to identify recently-emitted vertices
253 * and avoid re-emitting them.
256 elt(struct copy_context
*copy
, GLuint elt_idx
)
258 GLuint elt
= copy
->srcelt
[elt_idx
] + copy
->prim
->basevertex
;
259 GLuint slot
= elt
& (ELT_TABLE_SIZE
-1);
261 /* Look up the incoming element in the vertex cache. Re-emit if
264 if (copy
->vert_cache
[slot
].in
!= elt
) {
265 GLubyte
*csr
= copy
->dstptr
;
268 for (i
= 0; i
< copy
->nr_varying
; i
++) {
269 const struct tnl_vertex_array
*srcarray
= copy
->varying
[i
].array
;
270 const struct gl_vertex_buffer_binding
* srcbinding
271 = srcarray
->BufferBinding
;
272 const GLubyte
*srcptr
273 = copy
->varying
[i
].src_ptr
+ elt
* srcbinding
->Stride
;
275 memcpy(csr
, srcptr
, copy
->varying
[i
].size
);
276 csr
+= copy
->varying
[i
].size
;
279 if (srcarray
->Type
== GL_FLOAT
) {
281 GLfloat
*f
= (GLfloat
*) srcptr
;
282 for (k
= 0; k
< srcarray
->Size
; k
++) {
283 assert(!IS_INF_OR_NAN(f
[k
]));
284 assert(f
[k
] <= 1.0e20
&& f
[k
] >= -1.0e20
);
290 const GLuint
*f
= (const GLuint
*)srcptr
;
292 printf(" varying %d: ", i
);
293 for (j
= 0; j
< copy
->varying
[i
].size
/ 4; j
++)
299 copy
->vert_cache
[slot
].in
= elt
;
300 copy
->vert_cache
[slot
].out
= copy
->dstbuf_nr
++;
301 copy
->dstptr
+= copy
->vertex_size
;
303 assert(csr
== copy
->dstptr
);
304 assert(copy
->dstptr
== (copy
->dstbuf
+
305 copy
->dstbuf_nr
* copy
->vertex_size
));
308 copy
->dstelt
[copy
->dstelt_nr
++] = copy
->vert_cache
[slot
].out
;
309 return check_flush(copy
);
314 * Called at end of each primitive during replay.
317 end(struct copy_context
*copy
, GLboolean end_flag
)
319 struct _mesa_prim
*prim
= ©
->dstprim
[copy
->dstprim_nr
];
321 prim
->end
= end_flag
;
322 prim
->count
= copy
->dstelt_nr
- prim
->start
;
324 if (++copy
->dstprim_nr
== MAX_PRIM
|| check_flush(copy
)) {
331 replay_elts(struct copy_context
*copy
)
336 for (i
= 0; i
< copy
->nr_prims
; i
++) {
337 const struct _mesa_prim
*prim
= ©
->prim
[i
];
338 const GLuint start
= prim
->start
;
341 switch (prim
->mode
) {
343 /* Convert to linestrip and emit the final vertex explicitly,
344 * but only in the resultant strip that requires it.
347 while (j
!= prim
->count
) {
348 begin(copy
, GL_LINE_STRIP
, prim
->begin
&& j
== 0);
350 for (split
= GL_FALSE
; j
!= prim
->count
&& !split
; j
++)
351 split
= elt(copy
, start
+ j
);
353 if (j
== prim
->count
) {
354 /* Done, emit final line. Split doesn't matter as
355 * it is always raised a bit early so we can emit
356 * the last verts if necessary!
359 (void)elt(copy
, start
+ 0);
361 end(copy
, prim
->end
);
373 case GL_TRIANGLE_FAN
:
376 while (j
!= prim
->count
) {
377 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
379 split
= elt(copy
, start
+0);
382 split
= elt(copy
, start
+j
-1);
385 for (; j
!= prim
->count
&& !split
; j
++)
386 split
= elt(copy
, start
+j
);
388 end(copy
, prim
->end
&& j
== prim
->count
);
390 if (j
!= prim
->count
) {
391 /* Wrapped the primitive, need to repeat some vertices:
399 (void)_tnl_split_prim_inplace(prim
->mode
, &first
, &incr
);
402 while (j
!= prim
->count
) {
404 begin(copy
, prim
->mode
, prim
->begin
&& j
== 0);
407 for (k
= 0; k
< first
; k
++, j
++)
408 split
|= elt(copy
, start
+j
);
412 for (; j
!= prim
->count
&& !split
;)
413 for (k
= 0; k
< incr
; k
++, j
++)
414 split
|= elt(copy
, start
+j
);
416 end(copy
, prim
->end
&& j
== prim
->count
);
418 if (j
!= prim
->count
) {
419 /* Wrapped the primitive, need to repeat some vertices:
421 assert(j
> first
- incr
);
429 if (copy
->dstprim_nr
)
435 replay_init(struct copy_context
*copy
)
437 struct gl_context
*ctx
= copy
->ctx
;
440 const GLvoid
*srcptr
;
442 /* Make a list of varying attributes and their vbo's. Also
443 * calculate vertex size.
445 copy
->vertex_size
= 0;
446 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
447 const struct tnl_vertex_array
*array
= ©
->array
[i
];
448 const struct gl_vertex_buffer_binding
*binding
= array
->BufferBinding
;
450 if (binding
->Stride
== 0) {
451 copy_vertex_array(©
->dstarray
[i
], array
);
454 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
455 struct gl_buffer_object
*vbo
= binding
->BufferObj
;
456 const GLubyte
*ptr
= _mesa_vertex_attrib_address(attrib
, binding
);
457 GLuint j
= copy
->nr_varying
++;
459 copy
->varying
[j
].attr
= i
;
460 copy
->varying
[j
].array
= ©
->array
[i
];
461 copy
->varying
[j
].size
= attr_size(attrib
);
462 copy
->vertex_size
+= attr_size(attrib
);
464 if (_mesa_is_bufferobj(vbo
) &&
465 !_mesa_bufferobj_mapped(vbo
, MAP_INTERNAL
))
466 ctx
->Driver
.MapBufferRange(ctx
, 0, vbo
->Size
, GL_MAP_READ_BIT
, vbo
,
469 copy
->varying
[j
].src_ptr
=
470 ADD_POINTERS(vbo
->Mappings
[MAP_INTERNAL
].Pointer
, ptr
);
472 copy
->dstarray
[i
].VertexAttrib
= ©
->varying
[j
].dstattribs
;
473 copy
->dstarray
[i
].BufferBinding
= ©
->varying
[j
].dstbinding
;
477 /* There must always be an index buffer. Currently require the
478 * caller convert non-indexed prims to indexed. Could alternately
481 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
482 !_mesa_bufferobj_mapped(copy
->ib
->obj
, MAP_INTERNAL
))
483 ctx
->Driver
.MapBufferRange(ctx
, 0, copy
->ib
->obj
->Size
, GL_MAP_READ_BIT
,
484 copy
->ib
->obj
, MAP_INTERNAL
);
486 srcptr
= (const GLubyte
*)
487 ADD_POINTERS(copy
->ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
,
490 switch (copy
->ib
->index_size
) {
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 GLubyte
*)srcptr
)[i
];
500 copy
->translated_elt_buf
= malloc(sizeof(GLuint
) * copy
->ib
->count
);
501 copy
->srcelt
= copy
->translated_elt_buf
;
503 for (i
= 0; i
< copy
->ib
->count
; i
++)
504 copy
->translated_elt_buf
[i
] = ((const GLushort
*)srcptr
)[i
];
508 copy
->translated_elt_buf
= NULL
;
509 copy
->srcelt
= (const GLuint
*)srcptr
;
513 /* Figure out the maximum allowed vertex buffer size:
515 if (copy
->vertex_size
* copy
->limits
->max_verts
<= copy
->limits
->max_vb_size
) {
516 copy
->dstbuf_size
= copy
->limits
->max_verts
;
519 copy
->dstbuf_size
= copy
->limits
->max_vb_size
/ copy
->vertex_size
;
522 /* Allocate an output vertex buffer:
524 * XXX: This should be a VBO!
526 copy
->dstbuf
= malloc(copy
->dstbuf_size
* copy
->vertex_size
);
527 copy
->dstptr
= copy
->dstbuf
;
529 /* Setup new vertex arrays to point into the output buffer:
531 for (offset
= 0, i
= 0; i
< copy
->nr_varying
; i
++) {
532 const struct tnl_vertex_array
*src
= copy
->varying
[i
].array
;
533 const struct gl_array_attributes
*srcattr
= src
->VertexAttrib
;
534 struct tnl_vertex_array
*dst
= ©
->dstarray
[i
];
535 struct gl_vertex_buffer_binding
*dstbind
= ©
->varying
[i
].dstbinding
;
536 struct gl_array_attributes
*dstattr
= ©
->varying
[i
].dstattribs
;
538 dstattr
->Size
= srcattr
->Size
;
539 dstattr
->Type
= srcattr
->Type
;
540 dstattr
->Format
= GL_RGBA
;
541 dstbind
->Stride
= copy
->vertex_size
;
542 dstattr
->Ptr
= copy
->dstbuf
+ offset
;
543 dstattr
->Normalized
= srcattr
->Normalized
;
544 dstattr
->Integer
= srcattr
->Integer
;
545 dstattr
->Doubles
= srcattr
->Doubles
;
546 dstbind
->BufferObj
= ctx
->Shared
->NullBufferObj
;
547 dstattr
->_ElementSize
= srcattr
->_ElementSize
;
548 dst
->BufferBinding
= dstbind
;
549 dst
->VertexAttrib
= dstattr
;
551 offset
+= copy
->varying
[i
].size
;
554 /* Allocate an output element list:
556 copy
->dstelt_size
= MIN2(65536, copy
->ib
->count
* 2 + 3);
557 copy
->dstelt_size
= MIN2(copy
->dstelt_size
, copy
->limits
->max_indices
);
558 copy
->dstelt
= malloc(sizeof(GLuint
) * copy
->dstelt_size
);
561 /* Setup the new index buffer to point to the allocated element
564 copy
->dstib
.count
= 0; /* duplicates dstelt_nr */
565 copy
->dstib
.index_size
= 4;
566 copy
->dstib
.obj
= ctx
->Shared
->NullBufferObj
;
567 copy
->dstib
.ptr
= copy
->dstelt
;
572 * Free up everything allocated during split/replay.
575 replay_finish(struct copy_context
*copy
)
577 struct gl_context
*ctx
= copy
->ctx
;
580 /* Free our vertex and index buffers */
581 free(copy
->translated_elt_buf
);
586 for (i
= 0; i
< copy
->nr_varying
; i
++) {
587 struct gl_buffer_object
*vbo
=
588 copy
->varying
[i
].array
->BufferBinding
->BufferObj
;
589 if (_mesa_is_bufferobj(vbo
) && _mesa_bufferobj_mapped(vbo
, MAP_INTERNAL
))
590 ctx
->Driver
.UnmapBuffer(ctx
, vbo
, MAP_INTERNAL
);
593 /* Unmap index buffer */
594 if (_mesa_is_bufferobj(copy
->ib
->obj
) &&
595 _mesa_bufferobj_mapped(copy
->ib
->obj
, MAP_INTERNAL
)) {
596 ctx
->Driver
.UnmapBuffer(ctx
, copy
->ib
->obj
, MAP_INTERNAL
);
602 * Split VBO into smaller pieces, draw the pieces.
605 _tnl_split_copy(struct gl_context
*ctx
,
606 const struct tnl_vertex_array
*arrays
,
607 const struct _mesa_prim
*prim
,
609 const struct _mesa_index_buffer
*ib
,
611 const struct split_limits
*limits
)
613 struct copy_context copy
;
614 GLuint i
, this_nr_prims
;
616 for (i
= 0; i
< nr_prims
;) {
617 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
618 * will rebase the elements to the basevertex, and we'll only
619 * emit strings of prims with the same basevertex in one draw call.
621 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
623 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
627 memset(©
, 0, sizeof(copy
));
629 /* Require indexed primitives:
635 copy
.prim
= &prim
[i
];
636 copy
.nr_prims
= this_nr_prims
;
639 copy
.limits
= limits
;
641 /* Clear the vertex cache:
643 for (i
= 0; i
< ELT_TABLE_SIZE
; i
++)
644 copy
.vert_cache
[i
].in
= ~0;
648 replay_finish(©
);