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