ee229b6a97fbcfb403f8736a845e6a8f7456f686
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>
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/enums.h"
40 /* Used for splitting without copying. No attempt is made to handle
41 * too large indexed vertex buffers: In general you need to copy to do
44 struct split_context
{
45 struct gl_context
*ctx
;
46 const struct tnl_vertex_array
*array
;
47 const struct _mesa_prim
*prim
;
49 const struct _mesa_index_buffer
*ib
;
56 const struct split_limits
*limits
;
59 struct _mesa_prim dstprim
[MAX_PRIM
];
67 flush_vertex( struct split_context
*split
)
69 struct gl_context
*ctx
= split
->ctx
;
70 struct _mesa_index_buffer ib
;
73 if (!split
->dstprim_nr
)
79 ib
.count
= split
->max_index
- split
->min_index
+ 1;
80 ib
.ptr
= (const void *)((const char *)ib
.ptr
+
81 split
->min_index
* ib
.index_size
);
83 /* Rebase the primitives to save index buffer entries. */
84 for (i
= 0; i
< split
->dstprim_nr
; i
++)
85 split
->dstprim
[i
].start
-= split
->min_index
;
88 assert(split
->max_index
>= split
->min_index
);
94 split
->ib
? &ib
: NULL
,
102 split
->dstprim_nr
= 0;
103 split
->min_index
= ~0;
104 split
->max_index
= 0;
108 static struct _mesa_prim
*
109 next_outprim(struct split_context
*split
)
111 if (split
->dstprim_nr
== MAX_PRIM
-1) {
116 struct _mesa_prim
*prim
= &split
->dstprim
[split
->dstprim_nr
++];
117 memset(prim
, 0, sizeof(*prim
));
124 update_index_bounds(struct split_context
*split
,
125 const struct _mesa_prim
*prim
)
127 split
->min_index
= MIN2(split
->min_index
, prim
->start
);
128 split
->max_index
= MAX2(split
->max_index
, prim
->start
+ prim
->count
- 1);
132 /* Return the maximum amount of vertices that can be emitted for a
133 * primitive starting at 'prim->start', depending on the previous
137 get_max_vertices(struct split_context
*split
,
138 const struct _mesa_prim
*prim
)
140 if ((prim
->start
> split
->min_index
&&
141 prim
->start
- split
->min_index
>= split
->limit
) ||
142 (prim
->start
< split
->max_index
&&
143 split
->max_index
- prim
->start
>= split
->limit
))
144 /* "prim" starts too far away from the old range. */
147 return MIN2(split
->min_index
, prim
->start
) + split
->limit
- prim
->start
;
151 /* Break large primitives into smaller ones. If not possible, convert
152 * the primitive to indexed and pass to split_elts().
155 split_prims(struct split_context
*split
)
159 for (i
= 0; i
< split
->nr_prims
; i
++) {
160 const struct _mesa_prim
*prim
= &split
->prim
[i
];
162 GLboolean split_inplace
=
163 _tnl_split_prim_inplace(prim
->mode
, &first
, &incr
);
164 GLuint available
= get_max_vertices(split
, prim
);
165 GLuint count
= prim
->count
- (prim
->count
- first
) % incr
;
167 if (prim
->count
< first
)
170 if ((available
< count
&& !split_inplace
) ||
171 (available
< first
&& split_inplace
)) {
173 available
= get_max_vertices(split
, prim
);
176 if (available
>= count
) {
177 struct _mesa_prim
*outprim
= next_outprim(split
);
180 update_index_bounds(split
, outprim
);
182 else if (split_inplace
) {
185 for (j
= 0 ; j
< count
;) {
186 GLuint remaining
= count
- j
;
187 struct _mesa_prim
*outprim
= next_outprim(split
);
189 nr
= MIN2(available
, remaining
);
190 nr
-= (nr
- first
) % incr
;
192 outprim
->mode
= prim
->mode
;
193 outprim
->begin
= (j
== 0 && prim
->begin
);
194 outprim
->end
= (nr
== remaining
&& prim
->end
);
195 outprim
->start
= prim
->start
+ j
;
198 update_index_bounds(split
, outprim
);
200 if (nr
== remaining
) {
205 /* Wrapped the primitive */
206 j
+= nr
- (first
- incr
);
208 available
= get_max_vertices(split
, prim
);
212 else if (split
->ib
== NULL
) {
213 /* XXX: could at least send the first max_verts off from the
217 /* else convert to indexed primitive and pass to split_elts,
218 * which will do the necessary copying and turn it back into a
219 * vertex primitive for rendering...
221 struct _mesa_index_buffer ib
;
222 struct _mesa_prim tmpprim
;
223 GLuint
*elts
= malloc(count
* sizeof(GLuint
));
226 for (j
= 0; j
< count
; j
++)
227 elts
[j
] = prim
->start
+ j
;
231 ib
.index_size_shift
= 2;
232 ib
.obj
= split
->ctx
->Shared
->NullBufferObj
;
237 tmpprim
.count
= count
;
241 _tnl_split_copy(split
->ctx
,
253 _tnl_split_copy(split
->ctx
,
267 _tnl_split_inplace(struct gl_context
*ctx
,
268 const struct tnl_vertex_array
*arrays
,
269 const struct _mesa_prim
*prim
,
271 const struct _mesa_index_buffer
*ib
,
274 GLuint num_instances
,
275 GLuint base_instance
,
277 const struct split_limits
*limits
)
279 struct split_context split
;
281 memset(&split
, 0, sizeof(split
));
284 split
.array
= arrays
;
286 split
.nr_prims
= nr_prims
;
289 /* Empty interval, makes calculations simpler. */
290 split
.min_index
= ~0;
292 split
.num_instances
= num_instances
;
293 split
.base_instance
= base_instance
;
296 split
.limits
= limits
;
297 split
.limit
= ib
? limits
->max_indices
: limits
->max_verts
;