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