ee229b6a97fbcfb403f8736a845e6a8f7456f686
[mesa.git] / src / mesa / tnl / t_split_inplace.c
1
2 /*
3 * Mesa 3-D graphics library
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 *
25 * Authors:
26 * Keith Whitwell <keithw@vmware.com>
27 */
28
29
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/enums.h"
33 #include "vbo/vbo.h"
34
35 #include "t_split.h"
36
37
38 #define MAX_PRIM 32
39
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
42 * that.
43 */
44 struct split_context {
45 struct gl_context *ctx;
46 const struct tnl_vertex_array *array;
47 const struct _mesa_prim *prim;
48 GLuint nr_prims;
49 const struct _mesa_index_buffer *ib;
50 GLuint min_index;
51 GLuint max_index;
52 GLuint num_instances;
53 GLuint base_instance;
54 tnl_draw_func draw;
55
56 const struct split_limits *limits;
57 GLuint limit;
58
59 struct _mesa_prim dstprim[MAX_PRIM];
60 GLuint dstprim_nr;
61 };
62
63
64
65
66 static void
67 flush_vertex( struct split_context *split)
68 {
69 struct gl_context *ctx = split->ctx;
70 struct _mesa_index_buffer ib;
71 GLuint i;
72
73 if (!split->dstprim_nr)
74 return;
75
76 if (split->ib) {
77 ib = *split->ib;
78
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);
82
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;
86 }
87
88 assert(split->max_index >= split->min_index);
89
90 split->draw(ctx,
91 split->array,
92 split->dstprim,
93 split->dstprim_nr,
94 split->ib ? &ib : NULL,
95 !split->ib,
96 split->min_index,
97 split->max_index,
98 split->num_instances,
99 split->base_instance,
100 NULL, 0);
101
102 split->dstprim_nr = 0;
103 split->min_index = ~0;
104 split->max_index = 0;
105 }
106
107
108 static struct _mesa_prim *
109 next_outprim(struct split_context *split)
110 {
111 if (split->dstprim_nr == MAX_PRIM-1) {
112 flush_vertex(split);
113 }
114
115 {
116 struct _mesa_prim *prim = &split->dstprim[split->dstprim_nr++];
117 memset(prim, 0, sizeof(*prim));
118 return prim;
119 }
120 }
121
122
123 static void
124 update_index_bounds(struct split_context *split,
125 const struct _mesa_prim *prim)
126 {
127 split->min_index = MIN2(split->min_index, prim->start);
128 split->max_index = MAX2(split->max_index, prim->start + prim->count - 1);
129 }
130
131
132 /* Return the maximum amount of vertices that can be emitted for a
133 * primitive starting at 'prim->start', depending on the previous
134 * index bounds.
135 */
136 static GLuint
137 get_max_vertices(struct split_context *split,
138 const struct _mesa_prim *prim)
139 {
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. */
145 return 0;
146
147 return MIN2(split->min_index, prim->start) + split->limit - prim->start;
148 }
149
150
151 /* Break large primitives into smaller ones. If not possible, convert
152 * the primitive to indexed and pass to split_elts().
153 */
154 static void
155 split_prims(struct split_context *split)
156 {
157 GLuint i;
158
159 for (i = 0; i < split->nr_prims; i++) {
160 const struct _mesa_prim *prim = &split->prim[i];
161 GLuint first, incr;
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;
166
167 if (prim->count < first)
168 continue;
169
170 if ((available < count && !split_inplace) ||
171 (available < first && split_inplace)) {
172 flush_vertex(split);
173 available = get_max_vertices(split, prim);
174 }
175
176 if (available >= count) {
177 struct _mesa_prim *outprim = next_outprim(split);
178
179 *outprim = *prim;
180 update_index_bounds(split, outprim);
181 }
182 else if (split_inplace) {
183 GLuint j, nr;
184
185 for (j = 0 ; j < count ;) {
186 GLuint remaining = count - j;
187 struct _mesa_prim *outprim = next_outprim(split);
188
189 nr = MIN2(available, remaining);
190 nr -= (nr - first) % incr;
191
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;
196 outprim->count = nr;
197
198 update_index_bounds(split, outprim);
199
200 if (nr == remaining) {
201 /* Finished */
202 j += nr;
203 }
204 else {
205 /* Wrapped the primitive */
206 j += nr - (first - incr);
207 flush_vertex(split);
208 available = get_max_vertices(split, prim);
209 }
210 }
211 }
212 else if (split->ib == NULL) {
213 /* XXX: could at least send the first max_verts off from the
214 * inplace buffers.
215 */
216
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...
220 */
221 struct _mesa_index_buffer ib;
222 struct _mesa_prim tmpprim;
223 GLuint *elts = malloc(count * sizeof(GLuint));
224 GLuint j;
225
226 for (j = 0; j < count; j++)
227 elts[j] = prim->start + j;
228
229 ib.count = count;
230 ib.index_size = 4;
231 ib.index_size_shift = 2;
232 ib.obj = split->ctx->Shared->NullBufferObj;
233 ib.ptr = elts;
234
235 tmpprim = *prim;
236 tmpprim.start = 0;
237 tmpprim.count = count;
238
239 flush_vertex(split);
240
241 _tnl_split_copy(split->ctx,
242 split->array,
243 &tmpprim, 1,
244 &ib,
245 split->draw,
246 split->limits);
247
248 free(elts);
249 }
250 else {
251 flush_vertex(split);
252
253 _tnl_split_copy(split->ctx,
254 split->array,
255 prim, 1,
256 split->ib,
257 split->draw,
258 split->limits);
259 }
260 }
261
262 flush_vertex(split);
263 }
264
265
266 void
267 _tnl_split_inplace(struct gl_context *ctx,
268 const struct tnl_vertex_array *arrays,
269 const struct _mesa_prim *prim,
270 GLuint nr_prims,
271 const struct _mesa_index_buffer *ib,
272 GLuint min_index,
273 GLuint max_index,
274 GLuint num_instances,
275 GLuint base_instance,
276 tnl_draw_func draw,
277 const struct split_limits *limits)
278 {
279 struct split_context split;
280
281 memset(&split, 0, sizeof(split));
282
283 split.ctx = ctx;
284 split.array = arrays;
285 split.prim = prim;
286 split.nr_prims = nr_prims;
287 split.ib = ib;
288
289 /* Empty interval, makes calculations simpler. */
290 split.min_index = ~0;
291 split.max_index = 0;
292 split.num_instances = num_instances;
293 split.base_instance = base_instance;
294
295 split.draw = draw;
296 split.limits = limits;
297 split.limit = ib ? limits->max_indices : limits->max_verts;
298
299 split_prims(&split);
300 }
301
302