4f0326bf3998becbebc97c806cdfbc4fe9add791
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_unfilled.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 Drawing stage for handling glPolygonMode(line/point).
30 * Convert triangles to points or lines as needed.
31 */
32
33 /* Authors: Keith Whitwell <keith@tungstengraphics.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 vertex_header *v0 )
91 {
92 struct prim_header tmp;
93 tmp.v[0] = v0;
94 stage->next->point( stage->next, &tmp );
95 }
96
97 static void line( struct draw_stage *stage,
98 struct vertex_header *v0,
99 struct vertex_header *v1 )
100 {
101 struct prim_header tmp;
102 tmp.v[0] = v0;
103 tmp.v[1] = v1;
104 stage->next->line( stage->next, &tmp );
105 }
106
107
108 static void points( struct draw_stage *stage,
109 struct prim_header *header )
110 {
111 struct vertex_header *v0 = header->v[0];
112 struct vertex_header *v1 = header->v[1];
113 struct vertex_header *v2 = header->v[2];
114
115 inject_front_face_info(stage, header);
116
117 if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag) point( stage, v0 );
118 if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag) point( stage, v1 );
119 if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag) point( stage, v2 );
120 }
121
122
123 static void lines( struct draw_stage *stage,
124 struct prim_header *header )
125 {
126 struct vertex_header *v0 = header->v[0];
127 struct vertex_header *v1 = header->v[1];
128 struct vertex_header *v2 = header->v[2];
129
130 if (header->flags & DRAW_PIPE_RESET_STIPPLE)
131 stage->next->reset_stipple_counter( stage->next );
132
133 inject_front_face_info(stage, header);
134
135 if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag) line( stage, v2, v0 );
136 if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag) line( stage, v0, v1 );
137 if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag) line( stage, v1, v2 );
138 }
139
140
141 /** For debugging */
142 static void
143 print_header_flags(unsigned flags)
144 {
145 debug_printf("header->flags = ");
146 if (flags & DRAW_PIPE_RESET_STIPPLE)
147 debug_printf("RESET_STIPPLE ");
148 if (flags & DRAW_PIPE_EDGE_FLAG_0)
149 debug_printf("EDGE_FLAG_0 ");
150 if (flags & DRAW_PIPE_EDGE_FLAG_1)
151 debug_printf("EDGE_FLAG_1 ");
152 if (flags & DRAW_PIPE_EDGE_FLAG_2)
153 debug_printf("EDGE_FLAG_2 ");
154 debug_printf("\n");
155 }
156
157
158 /* Unfilled tri:
159 *
160 * Note edgeflags in the vertex struct is not sufficient as we will
161 * need to manipulate them when decomposing primitives.
162 *
163 * We currently keep the vertex edgeflag and primitive edgeflag mask
164 * separate until the last possible moment.
165 */
166 static void unfilled_tri( struct draw_stage *stage,
167 struct prim_header *header )
168 {
169 struct unfilled_stage *unfilled = unfilled_stage(stage);
170 unsigned cw = header->det >= 0.0;
171 unsigned mode = unfilled->mode[cw];
172
173 if (0)
174 print_header_flags(header->flags);
175
176 switch (mode) {
177 case PIPE_POLYGON_MODE_FILL:
178 stage->next->tri( stage->next, header );
179 break;
180 case PIPE_POLYGON_MODE_LINE:
181 lines( stage, header );
182 break;
183 case PIPE_POLYGON_MODE_POINT:
184 points( stage, header );
185 break;
186 default:
187 assert(0);
188 }
189 }
190
191
192 static void unfilled_first_tri( struct draw_stage *stage,
193 struct prim_header *header )
194 {
195 struct unfilled_stage *unfilled = unfilled_stage(stage);
196 const struct pipe_rasterizer_state *rast = stage->draw->rasterizer;
197
198 unfilled->mode[0] = rast->front_ccw ? rast->fill_front : rast->fill_back;
199 unfilled->mode[1] = rast->front_ccw ? rast->fill_back : rast->fill_front;
200
201 stage->tri = unfilled_tri;
202 stage->tri( stage, header );
203 }
204
205
206
207 static void unfilled_flush( struct draw_stage *stage,
208 unsigned flags )
209 {
210 stage->next->flush( stage->next, flags );
211
212 stage->tri = unfilled_first_tri;
213 }
214
215
216 static void unfilled_reset_stipple_counter( struct draw_stage *stage )
217 {
218 stage->next->reset_stipple_counter( stage->next );
219 }
220
221
222 static void unfilled_destroy( struct draw_stage *stage )
223 {
224 draw_free_temp_verts( stage );
225 FREE( stage );
226 }
227
228 /*
229 * Try to allocate an output slot which we can use
230 * to preserve the front face information.
231 */
232 void
233 draw_unfilled_prepare_outputs( struct draw_context *draw,
234 struct draw_stage *stage )
235 {
236 struct unfilled_stage *unfilled = unfilled_stage(stage);
237 const struct pipe_rasterizer_state *rast = draw ? draw->rasterizer : 0;
238 boolean is_unfilled = (rast &&
239 (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
240 rast->fill_back != PIPE_POLYGON_MODE_FILL));
241 const struct draw_fragment_shader *fs = draw ? draw->fs.fragment_shader : 0;
242
243 if (is_unfilled && fs && fs->info.uses_frontface) {
244 unfilled->face_slot = draw_alloc_extra_vertex_attrib(
245 stage->draw, TGSI_SEMANTIC_FACE, 0);
246 } else {
247 unfilled->face_slot = -1;
248 }
249 }
250
251
252 /**
253 * Create unfilled triangle stage.
254 */
255 struct draw_stage *draw_unfilled_stage( struct draw_context *draw )
256 {
257 struct unfilled_stage *unfilled = CALLOC_STRUCT(unfilled_stage);
258 if (unfilled == NULL)
259 goto fail;
260
261 unfilled->stage.draw = draw;
262 unfilled->stage.name = "unfilled";
263 unfilled->stage.next = NULL;
264 unfilled->stage.tmp = NULL;
265 unfilled->stage.point = draw_pipe_passthrough_point;
266 unfilled->stage.line = draw_pipe_passthrough_line;
267 unfilled->stage.tri = unfilled_first_tri;
268 unfilled->stage.flush = unfilled_flush;
269 unfilled->stage.reset_stipple_counter = unfilled_reset_stipple_counter;
270 unfilled->stage.destroy = unfilled_destroy;
271
272 unfilled->face_slot = -1;
273
274 if (!draw_alloc_temp_verts( &unfilled->stage, 0 ))
275 goto fail;
276
277 return &unfilled->stage;
278
279 fail:
280 if (unfilled)
281 unfilled->stage.destroy( &unfilled->stage );
282
283 return NULL;
284 }