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 "main/glformats.h"
34 #include "vbo_split.h"
39 /* Used for splitting without copying. No attempt is made to handle
40 * too large indexed vertex buffers: In general you need to copy to do
43 struct split_context
{
44 struct gl_context
*ctx
;
45 const struct gl_client_array
**array
;
46 const struct _mesa_prim
*prim
;
48 const struct _mesa_index_buffer
*ib
;
53 const struct split_limits
*limits
;
56 struct _mesa_prim dstprim
[MAX_PRIM
];
63 static void flush_vertex( struct split_context
*split
)
65 struct gl_context
*ctx
= split
->ctx
;
66 const struct gl_client_array
**saved_arrays
= ctx
->Array
._DrawArrays
;
67 struct _mesa_index_buffer ib
;
70 if (!split
->dstprim_nr
)
76 ib
.count
= split
->max_index
- split
->min_index
+ 1;
77 ib
.ptr
= (const void *)((const char *)ib
.ptr
+
78 split
->min_index
* _mesa_sizeof_type(ib
.type
));
80 /* Rebase the primitives to save index buffer entries. */
81 for (i
= 0; i
< split
->dstprim_nr
; i
++)
82 split
->dstprim
[i
].start
-= split
->min_index
;
85 assert(split
->max_index
>= split
->min_index
);
87 ctx
->Array
._DrawArrays
= split
->array
;
88 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
93 split
->ib
? &ib
: NULL
,
99 ctx
->Array
._DrawArrays
= saved_arrays
;
100 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
102 split
->dstprim_nr
= 0;
103 split
->min_index
= ~0;
104 split
->max_index
= 0;
108 static struct _mesa_prim
*next_outprim( struct split_context
*split
)
110 if (split
->dstprim_nr
== MAX_PRIM
-1) {
115 struct _mesa_prim
*prim
= &split
->dstprim
[split
->dstprim_nr
++];
116 memset(prim
, 0, sizeof(*prim
));
121 static void update_index_bounds(struct split_context
*split
,
122 const struct _mesa_prim
*prim
)
124 split
->min_index
= MIN2(split
->min_index
, prim
->start
);
125 split
->max_index
= MAX2(split
->max_index
, prim
->start
+ prim
->count
- 1);
128 /* Return the maximum amount of vertices that can be emitted for a
129 * primitive starting at 'prim->start', depending on the previous
132 static GLuint
get_max_vertices(struct split_context
*split
,
133 const struct _mesa_prim
*prim
)
135 if ((prim
->start
> split
->min_index
&&
136 prim
->start
- split
->min_index
>= split
->limit
) ||
137 (prim
->start
< split
->max_index
&&
138 split
->max_index
- prim
->start
>= split
->limit
))
139 /* "prim" starts too far away from the old range. */
142 return MIN2(split
->min_index
, prim
->start
) + split
->limit
- prim
->start
;
145 /* Break large primitives into smaller ones. If not possible, convert
146 * the primitive to indexed and pass to split_elts().
148 static void split_prims( struct split_context
*split
)
152 for (i
= 0; i
< split
->nr_prims
; i
++) {
153 const struct _mesa_prim
*prim
= &split
->prim
[i
];
155 GLboolean split_inplace
= split_prim_inplace(prim
->mode
, &first
, &incr
);
156 GLuint available
= get_max_vertices(split
, prim
);
157 GLuint count
= prim
->count
- (prim
->count
- first
) % incr
;
159 if (prim
->count
< first
)
162 if ((available
< count
&& !split_inplace
) ||
163 (available
< first
&& split_inplace
)) {
165 available
= get_max_vertices(split
, prim
);
168 if (available
>= count
) {
169 struct _mesa_prim
*outprim
= next_outprim(split
);
172 update_index_bounds(split
, outprim
);
174 else if (split_inplace
) {
177 for (j
= 0 ; j
< count
; ) {
178 GLuint remaining
= count
- j
;
179 struct _mesa_prim
*outprim
= next_outprim(split
);
181 nr
= MIN2( available
, remaining
);
182 nr
-= (nr
- first
) % incr
;
184 outprim
->mode
= prim
->mode
;
185 outprim
->begin
= (j
== 0 && prim
->begin
);
186 outprim
->end
= (nr
== remaining
&& prim
->end
);
187 outprim
->start
= prim
->start
+ j
;
189 outprim
->num_instances
= prim
->num_instances
;
190 outprim
->base_instance
= prim
->base_instance
;
192 update_index_bounds(split
, outprim
);
194 if (nr
== remaining
) {
200 /* Wrapped the primitive:
202 j
+= nr
- (first
- incr
);
204 available
= get_max_vertices(split
, prim
);
208 else if (split
->ib
== NULL
) {
209 /* XXX: could at least send the first max_verts off from the
213 /* else convert to indexed primitive and pass to split_elts,
214 * which will do the necessary copying and turn it back into a
215 * vertex primitive for rendering...
217 struct _mesa_index_buffer ib
;
218 struct _mesa_prim tmpprim
;
219 GLuint
*elts
= malloc(count
* sizeof(GLuint
));
222 for (j
= 0; j
< count
; j
++)
223 elts
[j
] = prim
->start
+ j
;
226 ib
.type
= GL_UNSIGNED_INT
;
227 ib
.obj
= split
->ctx
->Shared
->NullBufferObj
;
233 tmpprim
.count
= count
;
234 tmpprim
.num_instances
= 1;
235 tmpprim
.base_instance
= 0;
239 vbo_split_copy(split
->ctx
,
251 vbo_split_copy(split
->ctx
,
264 void vbo_split_inplace( struct gl_context
*ctx
,
265 const struct gl_client_array
*arrays
[],
266 const struct _mesa_prim
*prim
,
268 const struct _mesa_index_buffer
*ib
,
272 const struct split_limits
*limits
)
274 struct split_context split
;
276 memset(&split
, 0, sizeof(split
));
279 split
.array
= arrays
;
281 split
.nr_prims
= nr_prims
;
284 /* Empty interval, makes calculations simpler. */
285 split
.min_index
= ~0;
289 split
.limits
= limits
;
290 split
.limit
= ib
? limits
->max_indices
: limits
->max_verts
;
292 split_prims( &split
);