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