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