Merge branch 'gallium-0.1' into gallium-vertex-linear
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_varray.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "pipe/p_util.h"
29 #include "draw/draw_context.h"
30 #include "draw/draw_private.h"
31 #include "draw/draw_pt.h"
32
33 #define FETCH_MAX 256
34 #define DRAW_MAX (16*FETCH_MAX)
35
36 struct varray_frontend {
37 struct draw_pt_front_end base;
38 struct draw_context *draw;
39
40 ushort draw_elts[DRAW_MAX];
41 unsigned fetch_elts[FETCH_MAX];
42
43 unsigned draw_count;
44 unsigned fetch_count;
45
46 unsigned fetch_start;
47
48 struct draw_pt_middle_end *middle;
49
50 unsigned input_prim;
51 unsigned output_prim;
52 };
53
54 static void varray_flush(struct varray_frontend *varray)
55 {
56 if (varray->draw_count) {
57 #if 0
58 debug_printf("FLUSH fc = %d, dc = %d\n",
59 varray->fetch_count,
60 varray->draw_count);
61 debug_printf("\telt0 = %d, eltx = %d, draw0 = %d, drawx = %d\n",
62 varray->fetch_elts[0],
63 varray->fetch_elts[varray->fetch_count-1],
64 varray->draw_elts[0],
65 varray->draw_elts[varray->draw_count-1]);
66 #endif
67 varray->middle->run(varray->middle,
68 varray->fetch_elts,
69 varray->fetch_count,
70 varray->draw_elts,
71 varray->draw_count);
72 }
73
74 varray->fetch_count = 0;
75 varray->draw_count = 0;
76 }
77
78 static void varray_flush_linear(struct varray_frontend *varray,
79 unsigned start, unsigned count)
80 {
81 if (count) {
82 #if 0
83 debug_printf("FLUSH LINEAR start = %d, count = %d\n",
84 start,
85 count);
86 #endif
87 assert(varray->middle->run_linear);
88 varray->middle->run_linear(varray->middle, start, count);
89 }
90 }
91
92 static INLINE void fetch_init(struct varray_frontend *varray,
93 unsigned count)
94 {
95 unsigned idx;
96 #if 0
97 debug_printf("FETCH INIT c = %d, fs = %d\n",
98 count,
99 varray->fetch_start);
100 #endif
101 for (idx = 0; idx < count; ++idx) {
102 varray->fetch_elts[idx] = varray->fetch_start + idx;
103 }
104 varray->fetch_start += idx;
105 varray->fetch_count = idx;
106 }
107
108
109 static boolean split_prim_inplace(unsigned prim, unsigned *first, unsigned *incr)
110 {
111 switch (prim) {
112 case PIPE_PRIM_POINTS:
113 *first = 1;
114 *incr = 1;
115 return TRUE;
116 case PIPE_PRIM_LINES:
117 *first = 2;
118 *incr = 2;
119 return TRUE;
120 case PIPE_PRIM_LINE_STRIP:
121 *first = 2;
122 *incr = 1;
123 return TRUE;
124 case PIPE_PRIM_TRIANGLES:
125 *first = 3;
126 *incr = 3;
127 return TRUE;
128 case PIPE_PRIM_TRIANGLE_STRIP:
129 *first = 3;
130 *incr = 1;
131 return TRUE;
132 case PIPE_PRIM_TRIANGLE_FAN:
133 *first = 3;
134 *incr = 1;
135 return TRUE;
136 case PIPE_PRIM_QUADS:
137 *first = 4;
138 *incr = 4;
139 return TRUE;
140 case PIPE_PRIM_QUAD_STRIP:
141 *first = 4;
142 *incr = 2;
143 return TRUE;
144 case PIPE_PRIM_POLYGON:
145 *first = 3;
146 *incr = 1;
147 return TRUE;
148 default:
149 *first = 0;
150 *incr = 1; /* set to one so that count % incr works */
151 return FALSE;
152 }
153 }
154
155
156 static INLINE void add_draw_el(struct varray_frontend *varray,
157 int idx, ushort flags)
158 {
159 varray->draw_elts[varray->draw_count++] = idx | flags;
160 }
161
162
163 static INLINE void varray_triangle( struct varray_frontend *varray,
164 unsigned i0,
165 unsigned i1,
166 unsigned i2 )
167 {
168 add_draw_el(varray, i0, 0);
169 add_draw_el(varray, i1, 0);
170 add_draw_el(varray, i2, 0);
171 }
172
173 static INLINE void varray_triangle_flags( struct varray_frontend *varray,
174 ushort flags,
175 unsigned i0,
176 unsigned i1,
177 unsigned i2 )
178 {
179 add_draw_el(varray, i0, flags);
180 add_draw_el(varray, i1, 0);
181 add_draw_el(varray, i2, 0);
182 }
183
184 static INLINE void varray_line( struct varray_frontend *varray,
185 unsigned i0,
186 unsigned i1 )
187 {
188 add_draw_el(varray, i0, 0);
189 add_draw_el(varray, i1, 0);
190 }
191
192
193 static INLINE void varray_line_flags( struct varray_frontend *varray,
194 ushort flags,
195 unsigned i0,
196 unsigned i1 )
197 {
198 add_draw_el(varray, i0, flags);
199 add_draw_el(varray, i1, 0);
200 }
201
202
203 static INLINE void varray_point( struct varray_frontend *varray,
204 unsigned i0 )
205 {
206 add_draw_el(varray, i0, 0);
207 }
208
209 static INLINE void varray_quad( struct varray_frontend *varray,
210 unsigned i0,
211 unsigned i1,
212 unsigned i2,
213 unsigned i3 )
214 {
215 varray_triangle( varray, i0, i1, i3 );
216 varray_triangle( varray, i1, i2, i3 );
217 }
218
219 static INLINE void varray_ef_quad( struct varray_frontend *varray,
220 unsigned i0,
221 unsigned i1,
222 unsigned i2,
223 unsigned i3 )
224 {
225 const unsigned omitEdge1 = DRAW_PIPE_EDGE_FLAG_0 | DRAW_PIPE_EDGE_FLAG_2;
226 const unsigned omitEdge2 = DRAW_PIPE_EDGE_FLAG_0 | DRAW_PIPE_EDGE_FLAG_1;
227
228 varray_triangle_flags( varray,
229 DRAW_PIPE_RESET_STIPPLE | omitEdge1,
230 i0, i1, i3 );
231
232 varray_triangle_flags( varray,
233 omitEdge2,
234 i1, i2, i3 );
235 }
236
237 /* At least for now, we're back to using a template include file for
238 * this. The two paths aren't too different though - it may be
239 * possible to reunify them.
240 */
241 #define TRIANGLE(vc,flags,i0,i1,i2) varray_triangle_flags(vc,flags,i0,i1,i2)
242 #define QUAD(vc,i0,i1,i2,i3) varray_ef_quad(vc,i0,i1,i2,i3)
243 #define LINE(vc,flags,i0,i1) varray_line_flags(vc,flags,i0,i1)
244 #define POINT(vc,i0) varray_point(vc,i0)
245 #define FUNC varray_run_extras
246 #include "draw_pt_varray_tmp.h"
247
248 #define TRIANGLE(vc,flags,i0,i1,i2) varray_triangle(vc,i0,i1,i2)
249 #define QUAD(vc,i0,i1,i2,i3) varray_quad(vc,i0,i1,i2,i3)
250 #define LINE(vc,flags,i0,i1) varray_line(vc,i0,i1)
251 #define POINT(vc,i0) varray_point(vc,i0)
252 #define FUNC varray_run
253 #include "draw_pt_varray_tmp_linear.h"
254
255
256
257 static unsigned reduced_prim[PIPE_PRIM_POLYGON + 1] = {
258 PIPE_PRIM_POINTS,
259 PIPE_PRIM_LINES,
260 PIPE_PRIM_LINES,
261 PIPE_PRIM_LINES,
262 PIPE_PRIM_TRIANGLES,
263 PIPE_PRIM_TRIANGLES,
264 PIPE_PRIM_TRIANGLES,
265 PIPE_PRIM_TRIANGLES,
266 PIPE_PRIM_TRIANGLES,
267 PIPE_PRIM_TRIANGLES
268 };
269
270
271
272 static void varray_prepare(struct draw_pt_front_end *frontend,
273 unsigned prim,
274 struct draw_pt_middle_end *middle,
275 unsigned opt)
276 {
277 struct varray_frontend *varray = (struct varray_frontend *)frontend;
278
279 if (opt & PT_PIPELINE)
280 {
281 varray->base.run = varray_run_extras;
282 }
283 else
284 {
285 varray->base.run = varray_run;
286 }
287
288 varray->input_prim = prim;
289 varray->output_prim = reduced_prim[prim];
290
291 varray->middle = middle;
292 middle->prepare(middle, varray->output_prim, opt);
293 }
294
295
296
297
298 static void varray_finish(struct draw_pt_front_end *frontend)
299 {
300 struct varray_frontend *varray = (struct varray_frontend *)frontend;
301 varray->middle->finish(varray->middle);
302 varray->middle = NULL;
303 }
304
305 static void varray_destroy(struct draw_pt_front_end *frontend)
306 {
307 FREE(frontend);
308 }
309
310
311 struct draw_pt_front_end *draw_pt_varray(struct draw_context *draw)
312 {
313 struct varray_frontend *varray = CALLOC_STRUCT(varray_frontend);
314 if (varray == NULL)
315 return NULL;
316
317 varray->base.prepare = varray_prepare;
318 varray->base.run = NULL;
319 varray->base.finish = varray_finish;
320 varray->base.destroy = varray_destroy;
321 varray->draw = draw;
322
323 return &varray->base;
324 }