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