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