draw: Add vsplit frontend.
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_vsplit_tmp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.9
4 *
5 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Copyright (C) 2010 LunarG Inc.
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 OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #define CONCAT2(name, elt_type) name ## elt_type
28 #define CONCAT(name, elt_type) CONCAT2(name, elt_type)
29
30 #ifdef ELT_TYPE
31
32 /**
33 * Fetch all elements in [min_index, max_index] with bias, and use the
34 * (rebased) index buffer as the draw elements.
35 */
36 static boolean
37 CONCAT(vsplit_segment_fast_, ELT_TYPE)(struct vsplit_frontend *vsplit,
38 unsigned flags,
39 unsigned istart, unsigned icount)
40 {
41 struct draw_context *draw = vsplit->draw;
42 const ELT_TYPE *ib = (const ELT_TYPE *) draw->pt.user.elts;
43 const unsigned min_index = draw->pt.user.min_index;
44 const unsigned max_index = draw->pt.user.max_index;
45 const int elt_bias = draw->pt.user.eltBias;
46 unsigned fetch_start, fetch_count;
47 const ushort *draw_elts;
48 unsigned i;
49
50 assert(icount <= vsplit->segment_size);
51
52 /* this is faster only when we fetch less elements than the normal path */
53 if (max_index - min_index > icount - 1)
54 return FALSE;
55
56 if (elt_bias < 0 && min_index < -elt_bias)
57 return FALSE;
58
59 /* why this check? */
60 for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
61 if (draw->pt.vertex_element[i].instance_divisor)
62 return FALSE;
63 }
64
65 fetch_start = min_index + elt_bias;
66 fetch_count = max_index - min_index + 1;
67
68 if (min_index == 0 && sizeof(ib[0]) == sizeof(draw_elts[0])) {
69 for (i = 0; i < icount; i++) {
70 ELT_TYPE idx = ib[istart + i];
71 assert(idx >= min_index && idx <= max_index);
72 }
73 draw_elts = (const ushort *) ib;
74 }
75 else {
76 if (min_index == 0) {
77 for (i = 0; i < icount; i++) {
78 ELT_TYPE idx = ib[istart + i];
79
80 assert(idx >= min_index && idx <= max_index);
81 vsplit->draw_elts[i] = (ushort) idx;
82 }
83 }
84 else {
85 for (i = 0; i < icount; i++) {
86 ELT_TYPE idx = ib[istart + i];
87
88 assert(idx >= min_index && idx <= max_index);
89 vsplit->draw_elts[i] = (ushort) (idx - min_index);
90 }
91 }
92
93 draw_elts = vsplit->draw_elts;
94 }
95
96 return vsplit->middle->run_linear_elts(vsplit->middle,
97 fetch_start, fetch_count,
98 draw_elts, icount, flags);
99 }
100
101 /**
102 * Use the cache to prepare the fetch and draw elements, and flush.
103 *
104 * When spoken is TRUE, ispoken replaces istart; When close is TRUE, iclose is
105 * appended.
106 */
107 static INLINE void
108 CONCAT(vsplit_segment_cache_, ELT_TYPE)(struct vsplit_frontend *vsplit,
109 unsigned flags,
110 unsigned istart, unsigned icount,
111 boolean spoken, unsigned ispoken,
112 boolean close, unsigned iclose)
113 {
114 struct draw_context *draw = vsplit->draw;
115 const ELT_TYPE *ib = (const ELT_TYPE *) draw->pt.user.elts;
116 const int ibias = draw->pt.user.eltBias;
117 unsigned i;
118
119 assert(icount + !!close <= vsplit->segment_size);
120
121 vsplit_clear_cache(vsplit);
122
123 spoken = !!spoken;
124 if (ibias == 0) {
125 if (spoken)
126 ADD_CACHE(vsplit, ib[ispoken]);
127
128 for (i = spoken; i < icount; i++)
129 ADD_CACHE(vsplit, ib[istart + i]);
130
131 if (close)
132 ADD_CACHE(vsplit, ib[iclose]);
133 }
134 else if (ibias > 0) {
135 if (spoken)
136 ADD_CACHE(vsplit, (uint) ib[ispoken] + ibias);
137
138 for (i = spoken; i < icount; i++)
139 ADD_CACHE(vsplit, (uint) ib[istart + i] + ibias);
140
141 if (close)
142 ADD_CACHE(vsplit, (uint) ib[iclose] + ibias);
143 }
144 else {
145 if (spoken) {
146 if (ib[ispoken] < -ibias)
147 return;
148 ADD_CACHE(vsplit, ib[ispoken] + ibias);
149 }
150
151 for (i = spoken; i < icount; i++) {
152 if (ib[istart + i] < -ibias)
153 return;
154 ADD_CACHE(vsplit, ib[istart + i] + ibias);
155 }
156
157 if (close) {
158 if (ib[iclose] < -ibias)
159 return;
160 ADD_CACHE(vsplit, ib[iclose] + ibias);
161 }
162 }
163
164 vsplit_flush_cache(vsplit, flags);
165 }
166
167 static void
168 CONCAT(vsplit_segment_simple_, ELT_TYPE)(struct vsplit_frontend *vsplit,
169 unsigned flags,
170 unsigned istart,
171 unsigned icount)
172 {
173 /* the primitive is not splitted */
174 if (!(flags)) {
175 if (CONCAT(vsplit_segment_fast_, ELT_TYPE)(vsplit,
176 flags, istart, icount))
177 return;
178 }
179 CONCAT(vsplit_segment_cache_, ELT_TYPE)(vsplit,
180 flags, istart, icount, FALSE, 0, FALSE, 0);
181 }
182
183 static void
184 CONCAT(vsplit_segment_loop_, ELT_TYPE)(struct vsplit_frontend *vsplit,
185 unsigned flags,
186 unsigned istart,
187 unsigned icount,
188 unsigned i0)
189 {
190 const boolean close_loop = ((flags) == DRAW_SPLIT_BEFORE);
191
192 CONCAT(vsplit_segment_cache_, ELT_TYPE)(vsplit,
193 flags, istart, icount, FALSE, 0, close_loop, i0);
194 }
195
196 static void
197 CONCAT(vsplit_segment_fan_, ELT_TYPE)(struct vsplit_frontend *vsplit,
198 unsigned flags,
199 unsigned istart,
200 unsigned icount,
201 unsigned i0)
202 {
203 const boolean use_spoken = (((flags) & DRAW_SPLIT_BEFORE) != 0);
204
205 CONCAT(vsplit_segment_cache_, ELT_TYPE)(vsplit,
206 flags, istart, icount, use_spoken, i0, FALSE, 0);
207 }
208
209 #define LOCAL_VARS \
210 struct vsplit_frontend *vsplit = (struct vsplit_frontend *) frontend; \
211 const unsigned prim = vsplit->prim; \
212 const unsigned max_count_simple = vsplit->segment_size; \
213 const unsigned max_count_loop = vsplit->segment_size - 1; \
214 const unsigned max_count_fan = vsplit->segment_size;
215
216 #else /* ELT_TYPE */
217
218 static void
219 vsplit_segment_simple_linear(struct vsplit_frontend *vsplit, unsigned flags,
220 unsigned istart, unsigned icount)
221 {
222 assert(icount <= vsplit->max_vertices);
223 vsplit->middle->run_linear(vsplit->middle, istart, icount, flags);
224 }
225
226 static void
227 vsplit_segment_loop_linear(struct vsplit_frontend *vsplit, unsigned flags,
228 unsigned istart, unsigned icount, unsigned i0)
229 {
230 boolean close_loop = (flags == DRAW_SPLIT_BEFORE);
231 unsigned nr;
232
233 assert(icount + !!close_loop <= vsplit->segment_size);
234
235 if (close_loop) {
236 for (nr = 0; nr < icount; nr++)
237 vsplit->fetch_elts[nr] = istart + nr;
238 vsplit->fetch_elts[nr++] = i0;
239
240 vsplit->middle->run(vsplit->middle, vsplit->fetch_elts, nr,
241 vsplit->identity_draw_elts, nr, flags);
242 }
243 else {
244 vsplit->middle->run_linear(vsplit->middle, istart, icount, flags);
245 }
246 }
247
248 static void
249 vsplit_segment_fan_linear(struct vsplit_frontend *vsplit, unsigned flags,
250 unsigned istart, unsigned icount, unsigned i0)
251 {
252 boolean use_spoken = ((flags & DRAW_SPLIT_BEFORE) != 0);
253 unsigned nr = 0, i;
254
255 assert(icount + !!use_spoken <= vsplit->segment_size);
256
257 if (use_spoken) {
258 vsplit->fetch_elts[nr++] = i0;
259 for (i = 1 ; i < icount; i++)
260 vsplit->fetch_elts[nr++] = istart + i;
261
262 vsplit->middle->run(vsplit->middle, vsplit->fetch_elts, nr,
263 vsplit->identity_draw_elts, nr, flags);
264 }
265 else {
266 vsplit->middle->run_linear(vsplit->middle, istart, icount, flags);
267 }
268 }
269
270 #define LOCAL_VARS \
271 struct vsplit_frontend *vsplit = (struct vsplit_frontend *) frontend; \
272 const unsigned prim = vsplit->prim; \
273 const unsigned max_count_simple = vsplit->max_vertices; \
274 const unsigned max_count_loop = vsplit->segment_size - 1; \
275 const unsigned max_count_fan = vsplit->segment_size;
276
277 #define ELT_TYPE linear
278
279 #endif /* ELT_TYPE */
280
281 #define FUNC_VARS \
282 struct draw_pt_front_end *frontend, \
283 unsigned start, \
284 unsigned count
285
286 #define SEGMENT_SIMPLE(flags, istart, icount) \
287 CONCAT(vsplit_segment_simple_, ELT_TYPE)(vsplit, flags, istart, icount)
288
289 #define SEGMENT_LOOP(flags, istart, icount, i0) \
290 CONCAT(vsplit_segment_loop_, ELT_TYPE)(vsplit, flags, istart, icount, i0)
291
292 #define SEGMENT_FAN(flags, istart, icount, i0) \
293 CONCAT(vsplit_segment_fan_, ELT_TYPE)(vsplit, flags, istart, icount, i0)
294
295 #include "draw_split_tmp.h"
296
297 #undef CONCAT2
298 #undef CONCAT
299
300 #undef ELT_TYPE
301 #undef ADD_CACHE