Merge branch 'mesa_7_5_branch' into dlist-statechange-shortcircuit
[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
111
112
113 static void do_point( struct draw_context *draw,
114 const char *v0 )
115 {
116 struct prim_header prim;
117
118 prim.flags = 0;
119 prim.pad = 0;
120 prim.v[0] = (struct vertex_header *)v0;
121
122 draw->pipeline.first->point( draw->pipeline.first, &prim );
123 }
124
125
126 static void do_line( struct draw_context *draw,
127 ushort flags,
128 const char *v0,
129 const char *v1 )
130 {
131 struct prim_header prim;
132
133 prim.flags = flags;
134 prim.pad = 0;
135 prim.v[0] = (struct vertex_header *)v0;
136 prim.v[1] = (struct vertex_header *)v1;
137
138 draw->pipeline.first->line( draw->pipeline.first, &prim );
139 }
140
141
142 static void do_triangle( struct draw_context *draw,
143 ushort flags,
144 char *v0,
145 char *v1,
146 char *v2 )
147 {
148 struct prim_header prim;
149
150 prim.v[0] = (struct vertex_header *)v0;
151 prim.v[1] = (struct vertex_header *)v1;
152 prim.v[2] = (struct vertex_header *)v2;
153 prim.flags = flags;
154 prim.pad = 0;
155
156 draw->pipeline.first->tri( draw->pipeline.first, &prim );
157 }
158
159
160
161
162 /* Code to run the pipeline on a fairly arbitary collection of vertices.
163 *
164 * Vertex headers must be pre-initialized with the
165 * UNDEFINED_VERTEX_ID, this code will cause that id to become
166 * overwritten, so it may have to be reset if there is the intention
167 * to reuse the vertices.
168 *
169 * This code provides a callback to reset the vertex id's which the
170 * draw_vbuf.c code uses when it has to perform a flush.
171 */
172 void draw_pipeline_run( struct draw_context *draw,
173 unsigned prim,
174 struct vertex_header *vertices,
175 unsigned vertex_count,
176 unsigned stride,
177 const ushort *elts,
178 unsigned count )
179 {
180 char *verts = (char *)vertices;
181 unsigned i;
182
183 draw->pipeline.verts = verts;
184 draw->pipeline.vertex_stride = stride;
185 draw->pipeline.vertex_count = vertex_count;
186
187 switch (prim) {
188 case PIPE_PRIM_POINTS:
189 for (i = 0; i < count; i++)
190 do_point( draw,
191 verts + stride * elts[i] );
192 break;
193 case PIPE_PRIM_LINES:
194 for (i = 0; i+1 < count; i += 2)
195 do_line( draw,
196 elts[i+0], /* flags */
197 verts + stride * (elts[i+0] & ~DRAW_PIPE_FLAG_MASK),
198 verts + stride * elts[i+1]);
199 break;
200 case PIPE_PRIM_TRIANGLES:
201 for (i = 0; i+2 < count; i += 3)
202 do_triangle( draw,
203 elts[i+0], /* flags */
204 verts + stride * (elts[i+0] & ~DRAW_PIPE_FLAG_MASK),
205 verts + stride * elts[i+1],
206 verts + stride * elts[i+2]);
207 break;
208 }
209
210 draw->pipeline.verts = NULL;
211 draw->pipeline.vertex_count = 0;
212 }
213
214 #define QUAD(i0,i1,i2,i3) \
215 do_triangle( draw, \
216 ( DRAW_PIPE_RESET_STIPPLE | \
217 DRAW_PIPE_EDGE_FLAG_0 | \
218 DRAW_PIPE_EDGE_FLAG_2 ), \
219 verts + stride * ((i0) & ~DRAW_PIPE_FLAG_MASK), \
220 verts + stride * (i1), \
221 verts + stride * (i3)); \
222 do_triangle( draw, \
223 ( DRAW_PIPE_EDGE_FLAG_0 | \
224 DRAW_PIPE_EDGE_FLAG_1 ), \
225 verts + stride * ((i1) & ~DRAW_PIPE_FLAG_MASK), \
226 verts + stride * (i2), \
227 verts + stride * (i3))
228
229 #define TRIANGLE(flags,i0,i1,i2) \
230 do_triangle( draw, \
231 flags, /* flags */ \
232 verts + stride * ((i0) & ~DRAW_PIPE_FLAG_MASK), \
233 verts + stride * (i1), \
234 verts + stride * (i2))
235
236 #define LINE(flags,i0,i1) \
237 do_line( draw, \
238 flags, \
239 verts + stride * ((i0) & ~DRAW_PIPE_FLAG_MASK), \
240 verts + stride * (i1))
241
242 #define POINT(i0) \
243 do_point( draw, \
244 verts + stride * i0 )
245
246 #define FUNC pipe_run_linear
247 #define ARGS \
248 struct draw_context *draw, \
249 unsigned prim, \
250 struct vertex_header *vertices, \
251 unsigned stride
252
253 #define LOCAL_VARS \
254 char *verts = (char *)vertices; \
255 boolean flatfirst = (draw->rasterizer->flatshade && \
256 draw->rasterizer->flatshade_first); \
257 unsigned i; \
258 ushort flags
259
260 #define FLUSH
261
262 #include "draw_pt_decompose.h"
263
264 void draw_pipeline_run_linear( struct draw_context *draw,
265 unsigned prim,
266 struct vertex_header *vertices,
267 unsigned count,
268 unsigned stride )
269 {
270 char *verts = (char *)vertices;
271 draw->pipeline.verts = verts;
272 draw->pipeline.vertex_stride = stride;
273 draw->pipeline.vertex_count = count;
274
275 pipe_run_linear(draw, prim, vertices, stride, count);
276
277 draw->pipeline.verts = NULL;
278 draw->pipeline.vertex_count = 0;
279 }
280
281
282 void draw_pipeline_flush( struct draw_context *draw,
283 unsigned flags )
284 {
285 draw->pipeline.first->flush( draw->pipeline.first, flags );
286 draw->pipeline.first = draw->pipeline.validate;
287 }