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>
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/enums.h"
33 #include "vbo_split.h"
38 /* Used for splitting without copying.
40 struct split_context
{
42 const struct gl_client_array
**array
;
43 const struct _mesa_prim
*prim
;
45 const struct _mesa_index_buffer
*ib
;
50 const struct split_limits
*limits
;
52 struct _mesa_prim dstprim
[MAX_PRIM
];
59 static void flush_vertex( struct split_context
*split
)
61 GLuint min_index
, max_index
;
64 if (!split
->dstprim_nr
)
67 min_index
= split
->dstprim
[0].start
;
68 max_index
= min_index
+ split
->dstprim
[0].count
- 1;
70 for (i
= 1; i
< split
->dstprim_nr
; i
++) {
71 GLuint tmp_min
= split
->dstprim
[i
].start
;
72 GLuint tmp_max
= tmp_min
+ split
->dstprim
[i
].count
- 1;
74 if (tmp_min
< min_index
)
77 if (tmp_max
> max_index
)
81 assert(max_index
>= min_index
);
83 split
->draw( split
->ctx
,
92 split
->dstprim_nr
= 0;
96 static struct _mesa_prim
*next_outprim( struct split_context
*split
)
98 if (split
->dstprim_nr
== MAX_PRIM
-1) {
103 struct _mesa_prim
*prim
= &split
->dstprim
[split
->dstprim_nr
++];
104 memset(prim
, 0, sizeof(*prim
));
109 static int align(int value
, int alignment
)
111 return (value
+ alignment
- 1) & ~(alignment
- 1);
116 /* Break large primitives into smaller ones. If not possible, convert
117 * the primitive to indexed and pass to split_elts().
119 static void split_prims( struct split_context
*split
)
124 for (i
= 0; i
< split
->nr_prims
; i
++) {
125 const struct _mesa_prim
*prim
= &split
->prim
[i
];
127 GLboolean split_inplace
= split_prim_inplace(prim
->mode
, &first
, &incr
);
130 /* Always wrap on an even numbered vertex to avoid problems with
133 GLuint available
= align(split
->limits
->max_verts
- csr
- 1, 2);
134 assert(split
->limits
->max_verts
>= csr
);
136 if (prim
->count
< first
)
139 count
= prim
->count
- (prim
->count
- first
) % incr
;
142 if ((available
< count
&& !split_inplace
) ||
143 (available
< first
&& split_inplace
)) {
146 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
149 if (available
>= count
) {
150 struct _mesa_prim
*outprim
= next_outprim(split
);
153 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
155 else if (split_inplace
) {
159 for (j
= 0 ; j
< count
; ) {
160 GLuint remaining
= count
- j
;
161 struct _mesa_prim
*outprim
= next_outprim(split
);
163 nr
= MIN2( available
, remaining
);
165 nr
-= (nr
- first
) % incr
;
167 outprim
->mode
= prim
->mode
;
168 outprim
->begin
= (j
== 0 && prim
->begin
);
169 outprim
->end
= (nr
== remaining
&& prim
->end
);
170 outprim
->start
= prim
->start
+ j
;
173 if (nr
== remaining
) {
178 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
181 /* Wrapped the primitive:
183 j
+= nr
- (first
- incr
);
186 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
190 else if (split
->ib
== NULL
) {
191 /* XXX: could at least send the first max_verts off from the
195 /* else convert to indexed primitive and pass to split_elts,
196 * which will do the necessary copying and turn it back into a
197 * vertex primitive for rendering...
199 struct _mesa_index_buffer ib
;
200 struct _mesa_prim tmpprim
;
201 GLuint
*elts
= malloc(count
* sizeof(GLuint
));
204 for (j
= 0; j
< count
; j
++)
205 elts
[j
] = prim
->start
+ j
;
208 ib
.type
= GL_UNSIGNED_INT
;
209 ib
.obj
= split
->ctx
->Shared
->NullBufferObj
;
215 tmpprim
.count
= count
;
219 vbo_split_copy(split
->ctx
,
231 vbo_split_copy(split
->ctx
,
244 void vbo_split_inplace( GLcontext
*ctx
,
245 const struct gl_client_array
*arrays
[],
246 const struct _mesa_prim
*prim
,
248 const struct _mesa_index_buffer
*ib
,
252 const struct split_limits
*limits
)
254 struct split_context split
;
256 memset(&split
, 0, sizeof(split
));
259 split
.array
= arrays
;
261 split
.nr_prims
= nr_prims
;
263 split
.min_index
= min_index
;
264 split
.max_index
= max_index
;
266 split
.limits
= limits
;
268 split_prims( &split
);