meson: fix missing dependencies
[mesa.git] / src / mesa / vbo / vbo_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_split.h"
34
35
36 #define MAX_PRIM 32
37
38 /* Used for splitting without copying. No attempt is made to handle
39 * too large indexed vertex buffers: In general you need to copy to do
40 * that.
41 */
42 struct split_context {
43 struct gl_context *ctx;
44 const struct gl_vertex_array **array;
45 const struct _mesa_prim *prim;
46 GLuint nr_prims;
47 const struct _mesa_index_buffer *ib;
48 GLuint min_index;
49 GLuint max_index;
50 vbo_draw_func draw;
51
52 const struct split_limits *limits;
53 GLuint limit;
54
55 struct _mesa_prim dstprim[MAX_PRIM];
56 GLuint dstprim_nr;
57 };
58
59
60
61
62 static void
63 flush_vertex( struct split_context *split)
64 {
65 struct gl_context *ctx = split->ctx;
66 const struct gl_vertex_array **saved_arrays = ctx->Array._DrawArrays;
67 struct _mesa_index_buffer ib;
68 GLuint i;
69
70 if (!split->dstprim_nr)
71 return;
72
73 if (split->ib) {
74 ib = *split->ib;
75
76 ib.count = split->max_index - split->min_index + 1;
77 ib.ptr = (const void *)((const char *)ib.ptr +
78 split->min_index * ib.index_size);
79
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;
83 }
84
85 assert(split->max_index >= split->min_index);
86
87 ctx->Array._DrawArrays = split->array;
88 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
89
90 split->draw(ctx,
91 split->dstprim,
92 split->dstprim_nr,
93 split->ib ? &ib : NULL,
94 !split->ib,
95 split->min_index,
96 split->max_index,
97 NULL, 0, NULL);
98
99 ctx->Array._DrawArrays = saved_arrays;
100 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
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 = split_prim_inplace(prim->mode, &first, &incr);
163 GLuint available = get_max_vertices(split, prim);
164 GLuint count = prim->count - (prim->count - first) % incr;
165
166 if (prim->count < first)
167 continue;
168
169 if ((available < count && !split_inplace) ||
170 (available < first && split_inplace)) {
171 flush_vertex(split);
172 available = get_max_vertices(split, prim);
173 }
174
175 if (available >= count) {
176 struct _mesa_prim *outprim = next_outprim(split);
177
178 *outprim = *prim;
179 update_index_bounds(split, outprim);
180 }
181 else if (split_inplace) {
182 GLuint j, nr;
183
184 for (j = 0 ; j < count ;) {
185 GLuint remaining = count - j;
186 struct _mesa_prim *outprim = next_outprim(split);
187
188 nr = MIN2(available, remaining);
189 nr -= (nr - first) % incr;
190
191 outprim->mode = prim->mode;
192 outprim->begin = (j == 0 && prim->begin);
193 outprim->end = (nr == remaining && prim->end);
194 outprim->start = prim->start + j;
195 outprim->count = nr;
196 outprim->num_instances = prim->num_instances;
197 outprim->base_instance = prim->base_instance;
198
199 update_index_bounds(split, outprim);
200
201 if (nr == remaining) {
202 /* Finished */
203 j += nr;
204 }
205 else {
206 /* Wrapped the primitive */
207 j += nr - (first - incr);
208 flush_vertex(split);
209 available = get_max_vertices(split, prim);
210 }
211 }
212 }
213 else if (split->ib == NULL) {
214 /* XXX: could at least send the first max_verts off from the
215 * inplace buffers.
216 */
217
218 /* else convert to indexed primitive and pass to split_elts,
219 * which will do the necessary copying and turn it back into a
220 * vertex primitive for rendering...
221 */
222 struct _mesa_index_buffer ib;
223 struct _mesa_prim tmpprim;
224 GLuint *elts = malloc(count * sizeof(GLuint));
225 GLuint j;
226
227 for (j = 0; j < count; j++)
228 elts[j] = prim->start + j;
229
230 ib.count = count;
231 ib.index_size = 4;
232 ib.obj = split->ctx->Shared->NullBufferObj;
233 ib.ptr = elts;
234
235 tmpprim = *prim;
236 tmpprim.indexed = 1;
237 tmpprim.start = 0;
238 tmpprim.count = count;
239 tmpprim.num_instances = 1;
240 tmpprim.base_instance = 0;
241
242 flush_vertex(split);
243
244 vbo_split_copy(split->ctx,
245 split->array,
246 &tmpprim, 1,
247 &ib,
248 split->draw,
249 split->limits);
250
251 free(elts);
252 }
253 else {
254 flush_vertex(split);
255
256 vbo_split_copy(split->ctx,
257 split->array,
258 prim, 1,
259 split->ib,
260 split->draw,
261 split->limits);
262 }
263 }
264
265 flush_vertex(split);
266 }
267
268
269 void
270 vbo_split_inplace(struct gl_context *ctx,
271 const struct gl_vertex_array *arrays[],
272 const struct _mesa_prim *prim,
273 GLuint nr_prims,
274 const struct _mesa_index_buffer *ib,
275 GLuint min_index,
276 GLuint max_index,
277 vbo_draw_func draw,
278 const struct split_limits *limits)
279 {
280 struct split_context split;
281
282 memset(&split, 0, sizeof(split));
283
284 split.ctx = ctx;
285 split.array = arrays;
286 split.prim = prim;
287 split.nr_prims = nr_prims;
288 split.ib = ib;
289
290 /* Empty interval, makes calculations simpler. */
291 split.min_index = ~0;
292 split.max_index = 0;
293
294 split.draw = draw;
295 split.limits = limits;
296 split.limit = ib ? limits->max_indices : limits->max_verts;
297
298 split_prims(&split);
299 }
300
301