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
;
63 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
,
91 split
->dstprim_nr
= 0;
95 static struct _mesa_prim
*next_outprim( struct split_context
*split
)
97 if (split
->dstprim_nr
== MAX_PRIM
-1) {
102 struct _mesa_prim
*prim
= &split
->dstprim
[split
->dstprim_nr
++];
103 memset(prim
, 0, sizeof(*prim
));
108 static int align(int value
, int alignment
)
110 return (value
+ alignment
- 1) & ~(alignment
- 1);
115 /* Break large primitives into smaller ones. If not possible, convert
116 * the primitive to indexed and pass to split_elts().
118 static void split_prims( struct split_context
*split
)
123 for (i
= 0; i
< split
->nr_prims
; i
++) {
124 const struct _mesa_prim
*prim
= &split
->prim
[i
];
126 GLboolean split_inplace
= split_prim_inplace(prim
->mode
, &first
, &incr
);
129 /* Always wrap on an even numbered vertex to avoid problems with
132 GLuint available
= align(split
->limits
->max_verts
- csr
- 1, 2);
133 assert(split
->limits
->max_verts
>= csr
);
135 if (prim
->count
< first
)
138 count
= prim
->count
- (prim
->count
- first
) % incr
;
141 if ((available
< count
&& !split_inplace
) ||
142 (available
< first
&& split_inplace
)) {
145 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
148 if (available
>= count
) {
149 struct _mesa_prim
*outprim
= next_outprim(split
);
152 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
154 else if (split_inplace
) {
158 for (j
= 0 ; j
< count
; ) {
159 GLuint remaining
= count
- j
;
160 struct _mesa_prim
*outprim
= next_outprim(split
);
162 nr
= MIN2( available
, remaining
);
164 nr
-= (nr
- first
) % incr
;
166 outprim
->mode
= prim
->mode
;
167 outprim
->begin
= (j
== 0 && prim
->begin
);
168 outprim
->end
= (nr
== remaining
&& prim
->end
);
169 outprim
->start
= prim
->start
+ j
;
172 if (nr
== remaining
) {
177 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
180 /* Wrapped the primitive:
182 j
+= nr
- (first
- incr
);
185 available
= align(split
->limits
->max_verts
- csr
- 1, 2);
189 else if (split
->ib
== NULL
) {
190 /* XXX: could at least send the first max_verts off from the
194 /* else convert to indexed primitive and pass to split_elts,
195 * which will do the necessary copying and turn it back into a
196 * vertex primitive for rendering...
198 struct _mesa_index_buffer ib
;
199 struct _mesa_prim tmpprim
;
200 GLuint
*elts
= malloc(count
* sizeof(GLuint
));
203 for (j
= 0; j
< count
; j
++)
204 elts
[j
] = prim
->start
+ j
;
207 ib
.type
= GL_UNSIGNED_INT
;
208 ib
.obj
= split
->ctx
->Shared
->NullBufferObj
;
214 tmpprim
.count
= count
;
218 vbo_split_copy(split
->ctx
,
230 vbo_split_copy(split
->ctx
,
243 void vbo_split_inplace( GLcontext
*ctx
,
244 const struct gl_client_array
*arrays
[],
245 const struct _mesa_prim
*prim
,
247 const struct _mesa_index_buffer
*ib
,
251 const struct split_limits
*limits
)
253 struct split_context split
;
255 memset(&split
, 0, sizeof(split
));
258 split
.array
= arrays
;
260 split
.nr_prims
= nr_prims
;
262 split
.min_index
= min_index
;
263 split
.max_index
= max_index
;
265 split
.limits
= limits
;
267 split_prims( &split
);