Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / src / gallium / drivers / softpipe / sp_prim_setup.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 * \brief A draw stage that drives our triangle setup routines from
30 * within the draw pipeline. One of two ways to drive setup, the
31 * other being in sp_prim_vbuf.c.
32 *
33 * \author Keith Whitwell <keith@tungstengraphics.com>
34 * \author Brian Paul
35 */
36
37
38 #include "sp_context.h"
39 #include "sp_setup.h"
40 #include "sp_state.h"
41 #include "sp_prim_setup.h"
42 #include "draw/draw_pipe.h"
43 #include "draw/draw_vertex.h"
44 #include "pipe/p_util.h"
45
46 /**
47 * Triangle setup info (derived from draw_stage).
48 * Also used for line drawing (taking some liberties).
49 */
50 struct setup_stage {
51 struct draw_stage stage; /**< This must be first (base class) */
52
53 struct setup_context *setup;
54 };
55
56
57
58 /**
59 * Basically a cast wrapper.
60 */
61 static INLINE struct setup_stage *setup_stage( struct draw_stage *stage )
62 {
63 return (struct setup_stage *)stage;
64 }
65
66
67
68 static void
69 do_tri(struct draw_stage *stage, struct prim_header *prim)
70 {
71 struct setup_stage *setup = setup_stage( stage );
72
73 setup_tri( setup->setup,
74 prim->v[0]->data,
75 prim->v[1]->data,
76 prim->v[2]->data );
77 }
78
79 static void
80 do_line(struct draw_stage *stage, struct prim_header *prim)
81 {
82 struct setup_stage *setup = setup_stage( stage );
83
84 setup_line( setup->setup,
85 prim->v[0]->data,
86 prim->v[1]->data );
87 }
88
89 static void
90 do_point(struct draw_stage *stage, struct prim_header *prim)
91 {
92 struct setup_stage *setup = setup_stage( stage );
93
94 setup_point( setup->setup,
95 prim->v[0]->data );
96 }
97
98
99
100
101 static void setup_begin( struct draw_stage *stage )
102 {
103 struct setup_stage *setup = setup_stage(stage);
104
105 setup_prepare( setup->setup );
106
107 stage->point = do_point;
108 stage->line = do_line;
109 stage->tri = do_tri;
110 }
111
112
113 static void setup_first_point( struct draw_stage *stage,
114 struct prim_header *header )
115 {
116 setup_begin(stage);
117 stage->point( stage, header );
118 }
119
120 static void setup_first_line( struct draw_stage *stage,
121 struct prim_header *header )
122 {
123 setup_begin(stage);
124 stage->line( stage, header );
125 }
126
127
128 static void setup_first_tri( struct draw_stage *stage,
129 struct prim_header *header )
130 {
131 setup_begin(stage);
132 stage->tri( stage, header );
133 }
134
135
136
137 static void setup_flush( struct draw_stage *stage,
138 unsigned flags )
139 {
140 stage->point = setup_first_point;
141 stage->line = setup_first_line;
142 stage->tri = setup_first_tri;
143 }
144
145
146 static void reset_stipple_counter( struct draw_stage *stage )
147 {
148 }
149
150
151 static void render_destroy( struct draw_stage *stage )
152 {
153 struct setup_stage *ssetup = setup_stage(stage);
154 setup_destroy_context(ssetup->setup);
155 FREE( stage );
156 }
157
158
159 /**
160 * Create a new primitive setup/render stage.
161 */
162 struct draw_stage *sp_draw_render_stage( struct softpipe_context *softpipe )
163 {
164 struct setup_stage *sstage = CALLOC_STRUCT(setup_stage);
165
166 sstage->setup = setup_create_context(softpipe);
167 sstage->stage.draw = softpipe->draw;
168 sstage->stage.point = setup_first_point;
169 sstage->stage.line = setup_first_line;
170 sstage->stage.tri = setup_first_tri;
171 sstage->stage.flush = setup_flush;
172 sstage->stage.reset_stipple_counter = reset_stipple_counter;
173 sstage->stage.destroy = render_destroy;
174
175 return (struct draw_stage *)sstage;
176 }
177
178 struct setup_context *
179 sp_draw_setup_context( struct draw_stage *stage )
180 {
181 struct setup_stage *ssetup = setup_stage(stage);
182 return ssetup->setup;
183 }
184
185 void
186 sp_draw_flush( struct draw_stage *stage )
187 {
188 stage->flush( stage, 0 );
189 }