c9fd1a793aea800169c978c06f98b87c08ffe2f8
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "draw/draw_private.h"
34 #include "draw/draw_pipe.h"
35
36
37
38 boolean draw_pipeline_init( struct draw_context *draw )
39 {
40 /* create pipeline stages */
41 draw->pipeline.wide_line = draw_wide_line_stage( draw );
42 draw->pipeline.wide_point = draw_wide_point_stage( draw );
43 draw->pipeline.stipple = draw_stipple_stage( draw );
44 draw->pipeline.unfilled = draw_unfilled_stage( draw );
45 draw->pipeline.twoside = draw_twoside_stage( draw );
46 draw->pipeline.offset = draw_offset_stage( draw );
47 draw->pipeline.clip = draw_clip_stage( draw );
48 draw->pipeline.flatshade = draw_flatshade_stage( draw );
49 draw->pipeline.cull = draw_cull_stage( draw );
50 draw->pipeline.validate = draw_validate_stage( draw );
51 draw->pipeline.first = draw->pipeline.validate;
52
53 if (!draw->pipeline.wide_line ||
54 !draw->pipeline.wide_point ||
55 !draw->pipeline.stipple ||
56 !draw->pipeline.unfilled ||
57 !draw->pipeline.twoside ||
58 !draw->pipeline.offset ||
59 !draw->pipeline.clip ||
60 !draw->pipeline.flatshade ||
61 !draw->pipeline.cull ||
62 !draw->pipeline.validate)
63 return FALSE;
64
65 /* these defaults are oriented toward the needs of softpipe */
66 draw->pipeline.wide_point_threshold = 1000000.0; /* infinity */
67 draw->pipeline.wide_line_threshold = 1.0;
68 draw->pipeline.line_stipple = TRUE;
69 draw->pipeline.point_sprite = TRUE;
70
71 return TRUE;
72 }
73
74
75 void draw_pipeline_destroy( struct draw_context *draw )
76 {
77 if (draw->pipeline.wide_line)
78 draw->pipeline.wide_line->destroy( draw->pipeline.wide_line );
79 if (draw->pipeline.wide_point)
80 draw->pipeline.wide_point->destroy( draw->pipeline.wide_point );
81 if (draw->pipeline.stipple)
82 draw->pipeline.stipple->destroy( draw->pipeline.stipple );
83 if (draw->pipeline.unfilled)
84 draw->pipeline.unfilled->destroy( draw->pipeline.unfilled );
85 if (draw->pipeline.twoside)
86 draw->pipeline.twoside->destroy( draw->pipeline.twoside );
87 if (draw->pipeline.offset)
88 draw->pipeline.offset->destroy( draw->pipeline.offset );
89 if (draw->pipeline.clip)
90 draw->pipeline.clip->destroy( draw->pipeline.clip );
91 if (draw->pipeline.flatshade)
92 draw->pipeline.flatshade->destroy( draw->pipeline.flatshade );
93 if (draw->pipeline.cull)
94 draw->pipeline.cull->destroy( draw->pipeline.cull );
95 if (draw->pipeline.validate)
96 draw->pipeline.validate->destroy( draw->pipeline.validate );
97 if (draw->pipeline.aaline)
98 draw->pipeline.aaline->destroy( draw->pipeline.aaline );
99 if (draw->pipeline.aapoint)
100 draw->pipeline.aapoint->destroy( draw->pipeline.aapoint );
101 if (draw->pipeline.pstipple)
102 draw->pipeline.pstipple->destroy( draw->pipeline.pstipple );
103 if (draw->pipeline.rasterize)
104 draw->pipeline.rasterize->destroy( draw->pipeline.rasterize );
105 }
106
107
108
109 /**
110 * Build primitive to render a point with vertex at v0.
111 */
112 static void do_point( struct draw_context *draw,
113 const char *v0 )
114 {
115 struct prim_header prim;
116
117 prim.flags = 0;
118 prim.pad = 0;
119 prim.v[0] = (struct vertex_header *)v0;
120
121 draw->pipeline.first->point( draw->pipeline.first, &prim );
122 }
123
124
125 /**
126 * Build primitive to render a line with vertices at v0, v1.
127 * \param flags bitmask of DRAW_PIPE_EDGE_x, DRAW_PIPE_RESET_STIPPLE
128 */
129 static void do_line( struct draw_context *draw,
130 ushort flags,
131 const char *v0,
132 const char *v1 )
133 {
134 struct prim_header prim;
135
136 prim.flags = flags;
137 prim.pad = 0;
138 prim.v[0] = (struct vertex_header *)v0;
139 prim.v[1] = (struct vertex_header *)v1;
140
141 draw->pipeline.first->line( draw->pipeline.first, &prim );
142 }
143
144
145 /**
146 * Build primitive to render a triangle with vertices at v0, v1, v2.
147 * \param flags bitmask of DRAW_PIPE_EDGE_x, DRAW_PIPE_RESET_STIPPLE
148 */
149 static void do_triangle( struct draw_context *draw,
150 ushort flags,
151 char *v0,
152 char *v1,
153 char *v2 )
154 {
155 struct prim_header prim;
156
157 prim.v[0] = (struct vertex_header *)v0;
158 prim.v[1] = (struct vertex_header *)v1;
159 prim.v[2] = (struct vertex_header *)v2;
160 prim.flags = flags;
161 prim.pad = 0;
162
163 draw->pipeline.first->tri( draw->pipeline.first, &prim );
164 }
165
166
167 /*
168 * Set up macros for draw_pt_decompose.h template code.
169 * This code uses vertex indexes / elements.
170 */
171 #define QUAD(i0,i1,i2,i3) \
172 do_triangle( draw, \
173 ( DRAW_PIPE_RESET_STIPPLE | \
174 DRAW_PIPE_EDGE_FLAG_0 | \
175 DRAW_PIPE_EDGE_FLAG_2 ), \
176 verts + stride * elts[i0], \
177 verts + stride * elts[i1], \
178 verts + stride * elts[i3]); \
179 do_triangle( draw, \
180 ( DRAW_PIPE_EDGE_FLAG_0 | \
181 DRAW_PIPE_EDGE_FLAG_1 ), \
182 verts + stride * elts[i1], \
183 verts + stride * elts[i2], \
184 verts + stride * elts[i3])
185
186 #define TRIANGLE(flags,i0,i1,i2) \
187 do_triangle( draw, \
188 elts[i0], /* flags */ \
189 verts + stride * (elts[i0] & ~DRAW_PIPE_FLAG_MASK), \
190 verts + stride * elts[i1], \
191 verts + stride * elts[i2])
192
193 #define LINE(flags,i0,i1) \
194 do_line( draw, \
195 elts[i0], \
196 verts + stride * (elts[i0] & ~DRAW_PIPE_FLAG_MASK), \
197 verts + stride * elts[i1])
198
199 #define POINT(i0) \
200 do_point( draw, \
201 verts + stride * elts[i0] )
202
203 #define FUNC pipe_run
204 #define ARGS \
205 struct draw_context *draw, \
206 unsigned prim, \
207 struct vertex_header *vertices, \
208 unsigned stride, \
209 const ushort *elts
210
211 #define LOCAL_VARS \
212 char *verts = (char *)vertices; \
213 boolean flatfirst = (draw->rasterizer->flatshade && \
214 draw->rasterizer->flatshade_first); \
215 unsigned i; \
216 ushort flags
217
218 #define FLUSH
219
220 #include "draw_pt_decompose.h"
221 #undef ARGS
222 #undef LOCAL_VARS
223
224
225
226 /**
227 * Code to run the pipeline on a fairly arbitary collection of vertices.
228 * For drawing indexed primitives.
229 *
230 * Vertex headers must be pre-initialized with the
231 * UNDEFINED_VERTEX_ID, this code will cause that id to become
232 * overwritten, so it may have to be reset if there is the intention
233 * to reuse the vertices.
234 *
235 * This code provides a callback to reset the vertex id's which the
236 * draw_vbuf.c code uses when it has to perform a flush.
237 */
238 void draw_pipeline_run( struct draw_context *draw,
239 unsigned prim,
240 struct vertex_header *vertices,
241 unsigned vertex_count,
242 unsigned stride,
243 const ushort *elts,
244 unsigned count )
245 {
246 char *verts = (char *)vertices;
247
248 draw->pipeline.verts = verts;
249 draw->pipeline.vertex_stride = stride;
250 draw->pipeline.vertex_count = vertex_count;
251
252 pipe_run(draw, prim, vertices, stride, elts, count);
253
254 draw->pipeline.verts = NULL;
255 draw->pipeline.vertex_count = 0;
256 }
257
258
259
260 /*
261 * Set up macros for draw_pt_decompose.h template code.
262 * This code is for non-indexed rendering (no elts).
263 */
264 #define QUAD(i0,i1,i2,i3) \
265 do_triangle( draw, \
266 ( DRAW_PIPE_RESET_STIPPLE | \
267 DRAW_PIPE_EDGE_FLAG_0 | \
268 DRAW_PIPE_EDGE_FLAG_2 ), \
269 verts + stride * ((i0) & ~DRAW_PIPE_FLAG_MASK), \
270 verts + stride * (i1), \
271 verts + stride * (i3)); \
272 do_triangle( draw, \
273 ( DRAW_PIPE_EDGE_FLAG_0 | \
274 DRAW_PIPE_EDGE_FLAG_1 ), \
275 verts + stride * ((i1) & ~DRAW_PIPE_FLAG_MASK), \
276 verts + stride * (i2), \
277 verts + stride * (i3))
278
279 #define TRIANGLE(flags,i0,i1,i2) \
280 do_triangle( draw, \
281 flags, /* flags */ \
282 verts + stride * ((i0) & ~DRAW_PIPE_FLAG_MASK), \
283 verts + stride * (i1), \
284 verts + stride * (i2))
285
286 #define LINE(flags,i0,i1) \
287 do_line( draw, \
288 flags, \
289 verts + stride * ((i0) & ~DRAW_PIPE_FLAG_MASK), \
290 verts + stride * (i1))
291
292 #define POINT(i0) \
293 do_point( draw, \
294 verts + stride * i0 )
295
296 #define FUNC pipe_run_linear
297 #define ARGS \
298 struct draw_context *draw, \
299 unsigned prim, \
300 struct vertex_header *vertices, \
301 unsigned stride
302
303 #define LOCAL_VARS \
304 char *verts = (char *)vertices; \
305 boolean flatfirst = (draw->rasterizer->flatshade && \
306 draw->rasterizer->flatshade_first); \
307 unsigned i; \
308 ushort flags
309
310 #define FLUSH
311
312 #include "draw_pt_decompose.h"
313
314
315 /*
316 * For drawing non-indexed primitives.
317 */
318 void draw_pipeline_run_linear( struct draw_context *draw,
319 unsigned prim,
320 struct vertex_header *vertices,
321 unsigned count,
322 unsigned stride )
323 {
324 char *verts = (char *)vertices;
325 draw->pipeline.verts = verts;
326 draw->pipeline.vertex_stride = stride;
327 draw->pipeline.vertex_count = count;
328
329 pipe_run_linear(draw, prim, vertices, stride, count);
330
331 draw->pipeline.verts = NULL;
332 draw->pipeline.vertex_count = 0;
333 }
334
335
336 void draw_pipeline_flush( struct draw_context *draw,
337 unsigned flags )
338 {
339 draw->pipeline.first->flush( draw->pipeline.first, flags );
340 draw->pipeline.first = draw->pipeline.validate;
341 }