draw: fix line stippling with unfilled prims
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_unfilled.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 Drawing stage for handling glPolygonMode(line/point).
30 * Convert triangles to points or lines as needed.
31 */
32
33 /* Authors: Keith Whitwell <keithw@vmware.com>
34 */
35
36 #include "util/u_memory.h"
37 #include "pipe/p_defines.h"
38 #include "draw_private.h"
39 #include "draw_pipe.h"
40 #include "draw_fs.h"
41
42
43 struct unfilled_stage {
44 struct draw_stage stage;
45
46 /** [0] = front face, [1] = back face.
47 * legal values: PIPE_POLYGON_MODE_FILL, PIPE_POLYGON_MODE_LINE,
48 * and PIPE_POLYGON_MODE_POINT,
49 */
50 unsigned mode[2];
51
52 int face_slot;
53 };
54
55
56 static inline struct unfilled_stage *unfilled_stage( struct draw_stage *stage )
57 {
58 return (struct unfilled_stage *)stage;
59 }
60
61 static void
62 inject_front_face_info(struct draw_stage *stage,
63 struct prim_header *header)
64 {
65 struct unfilled_stage *unfilled = unfilled_stage(stage);
66 unsigned ccw = header->det < 0.0;
67 boolean is_front_face = (
68 (stage->draw->rasterizer->front_ccw && ccw) ||
69 (!stage->draw->rasterizer->front_ccw && !ccw));
70 int slot = unfilled->face_slot;
71 unsigned i;
72
73 /* In case the backend doesn't care about it */
74 if (slot < 0) {
75 return;
76 }
77
78 for (i = 0; i < 3; ++i) {
79 struct vertex_header *v = header->v[i];
80 v->data[slot][0] = is_front_face;
81 v->data[slot][1] = is_front_face;
82 v->data[slot][2] = is_front_face;
83 v->data[slot][3] = is_front_face;
84 v->vertex_id = UNDEFINED_VERTEX_ID;
85 }
86 }
87
88
89 static void point(struct draw_stage *stage,
90 struct prim_header *header,
91 struct vertex_header *v0)
92 {
93 struct prim_header tmp;
94 tmp.det = header->det;
95 tmp.flags = 0;
96 tmp.v[0] = v0;
97 stage->next->point(stage->next, &tmp);
98 }
99
100 static void line(struct draw_stage *stage,
101 struct prim_header *header,
102 struct vertex_header *v0,
103 struct vertex_header *v1)
104 {
105 struct prim_header tmp;
106 tmp.det = header->det;
107 tmp.flags = 0;
108 tmp.v[0] = v0;
109 tmp.v[1] = v1;
110 stage->next->line(stage->next, &tmp);
111 }
112
113
114 static void points(struct draw_stage *stage,
115 struct prim_header *header)
116 {
117 struct vertex_header *v0 = header->v[0];
118 struct vertex_header *v1 = header->v[1];
119 struct vertex_header *v2 = header->v[2];
120
121 inject_front_face_info(stage, header);
122
123 if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag)
124 point(stage, header, v0);
125 if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag)
126 point(stage, header, v1);
127 if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag)
128 point(stage, header, v2);
129 }
130
131
132 static void lines(struct draw_stage *stage,
133 struct prim_header *header)
134 {
135 struct vertex_header *v0 = header->v[0];
136 struct vertex_header *v1 = header->v[1];
137 struct vertex_header *v2 = header->v[2];
138
139 if (header->flags & DRAW_PIPE_RESET_STIPPLE)
140 /*
141 * XXX could revisit this. The only stage which cares is the line
142 * stipple stage. Could just emit correct reset flags here and not
143 * bother about all the calling through reset_stipple_counter
144 * stages. Though technically it is necessary if line stipple is
145 * handled by the driver, but this is not actually hooked up when
146 * using vbuf (vbuf stage reset_stipple_counter does nothing).
147 */
148 stage->next->reset_stipple_counter(stage->next);
149
150 inject_front_face_info(stage, header);
151
152 if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag)
153 line(stage, header, v2, v0);
154 if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag)
155 line(stage, header, v0, v1);
156 if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag)
157 line(stage, header, v1, v2);
158 }
159
160
161 /** For debugging */
162 static void
163 print_header_flags(unsigned flags)
164 {
165 debug_printf("header->flags = ");
166 if (flags & DRAW_PIPE_RESET_STIPPLE)
167 debug_printf("RESET_STIPPLE ");
168 if (flags & DRAW_PIPE_EDGE_FLAG_0)
169 debug_printf("EDGE_FLAG_0 ");
170 if (flags & DRAW_PIPE_EDGE_FLAG_1)
171 debug_printf("EDGE_FLAG_1 ");
172 if (flags & DRAW_PIPE_EDGE_FLAG_2)
173 debug_printf("EDGE_FLAG_2 ");
174 debug_printf("\n");
175 }
176
177
178 /* Unfilled tri:
179 *
180 * Note edgeflags in the vertex struct is not sufficient as we will
181 * need to manipulate them when decomposing primitives.
182 *
183 * We currently keep the vertex edgeflag and primitive edgeflag mask
184 * separate until the last possible moment.
185 */
186 static void unfilled_tri( struct draw_stage *stage,
187 struct prim_header *header )
188 {
189 struct unfilled_stage *unfilled = unfilled_stage(stage);
190 unsigned cw = header->det >= 0.0;
191 unsigned mode = unfilled->mode[cw];
192
193 if (0)
194 print_header_flags(header->flags);
195
196 switch (mode) {
197 case PIPE_POLYGON_MODE_FILL:
198 stage->next->tri( stage->next, header );
199 break;
200 case PIPE_POLYGON_MODE_LINE:
201 lines( stage, header );
202 break;
203 case PIPE_POLYGON_MODE_POINT:
204 points( stage, header );
205 break;
206 default:
207 assert(0);
208 }
209 }
210
211
212 static void unfilled_first_tri( struct draw_stage *stage,
213 struct prim_header *header )
214 {
215 struct unfilled_stage *unfilled = unfilled_stage(stage);
216 const struct pipe_rasterizer_state *rast = stage->draw->rasterizer;
217
218 unfilled->mode[0] = rast->front_ccw ? rast->fill_front : rast->fill_back;
219 unfilled->mode[1] = rast->front_ccw ? rast->fill_back : rast->fill_front;
220
221 stage->tri = unfilled_tri;
222 stage->tri( stage, header );
223 }
224
225
226
227 static void unfilled_flush( struct draw_stage *stage,
228 unsigned flags )
229 {
230 stage->next->flush( stage->next, flags );
231
232 stage->tri = unfilled_first_tri;
233 }
234
235
236 static void unfilled_reset_stipple_counter( struct draw_stage *stage )
237 {
238 stage->next->reset_stipple_counter( stage->next );
239 }
240
241
242 static void unfilled_destroy( struct draw_stage *stage )
243 {
244 draw_free_temp_verts( stage );
245 FREE( stage );
246 }
247
248 /*
249 * Try to allocate an output slot which we can use
250 * to preserve the front face information.
251 */
252 void
253 draw_unfilled_prepare_outputs( struct draw_context *draw,
254 struct draw_stage *stage )
255 {
256 struct unfilled_stage *unfilled = unfilled_stage(stage);
257 const struct pipe_rasterizer_state *rast = draw ? draw->rasterizer : 0;
258 boolean is_unfilled = (rast &&
259 (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
260 rast->fill_back != PIPE_POLYGON_MODE_FILL));
261 const struct draw_fragment_shader *fs = draw ? draw->fs.fragment_shader : 0;
262
263 if (is_unfilled && fs && fs->info.uses_frontface) {
264 unfilled->face_slot = draw_alloc_extra_vertex_attrib(
265 stage->draw, TGSI_SEMANTIC_FACE, 0);
266 } else {
267 unfilled->face_slot = -1;
268 }
269 }
270
271
272 /**
273 * Create unfilled triangle stage.
274 */
275 struct draw_stage *draw_unfilled_stage( struct draw_context *draw )
276 {
277 struct unfilled_stage *unfilled = CALLOC_STRUCT(unfilled_stage);
278 if (!unfilled)
279 goto fail;
280
281 unfilled->stage.draw = draw;
282 unfilled->stage.name = "unfilled";
283 unfilled->stage.next = NULL;
284 unfilled->stage.tmp = NULL;
285 unfilled->stage.point = draw_pipe_passthrough_point;
286 unfilled->stage.line = draw_pipe_passthrough_line;
287 unfilled->stage.tri = unfilled_first_tri;
288 unfilled->stage.flush = unfilled_flush;
289 unfilled->stage.reset_stipple_counter = unfilled_reset_stipple_counter;
290 unfilled->stage.destroy = unfilled_destroy;
291
292 unfilled->face_slot = -1;
293
294 if (!draw_alloc_temp_verts( &unfilled->stage, 0 ))
295 goto fail;
296
297 return &unfilled->stage;
298
299 fail:
300 if (unfilled)
301 unfilled->stage.destroy( &unfilled->stage );
302
303 return NULL;
304 }