gallium: convert rasterizer state to use gl-style front/back concepts
[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
41
42 struct unfilled_stage {
43 struct draw_stage stage;
44
45 /** [0] = front face, [1] = back face.
46 * legal values: PIPE_POLYGON_MODE_FILL, PIPE_POLYGON_MODE_LINE,
47 * and PIPE_POLYGON_MODE_POINT,
48 */
49 unsigned mode[2];
50 };
51
52
53 static INLINE struct unfilled_stage *unfilled_stage( struct draw_stage *stage )
54 {
55 return (struct unfilled_stage *)stage;
56 }
57
58
59
60 static void point( struct draw_stage *stage,
61 struct vertex_header *v0 )
62 {
63 struct prim_header tmp;
64 tmp.v[0] = v0;
65 stage->next->point( stage->next, &tmp );
66 }
67
68 static void line( struct draw_stage *stage,
69 struct vertex_header *v0,
70 struct vertex_header *v1 )
71 {
72 struct prim_header tmp;
73 tmp.v[0] = v0;
74 tmp.v[1] = v1;
75 stage->next->line( stage->next, &tmp );
76 }
77
78
79 static void points( struct draw_stage *stage,
80 struct prim_header *header )
81 {
82 struct vertex_header *v0 = header->v[0];
83 struct vertex_header *v1 = header->v[1];
84 struct vertex_header *v2 = header->v[2];
85
86 if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag) point( stage, v0 );
87 if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag) point( stage, v1 );
88 if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag) point( stage, v2 );
89 }
90
91
92 static void lines( struct draw_stage *stage,
93 struct prim_header *header )
94 {
95 struct vertex_header *v0 = header->v[0];
96 struct vertex_header *v1 = header->v[1];
97 struct vertex_header *v2 = header->v[2];
98
99 if (header->flags & DRAW_PIPE_RESET_STIPPLE)
100 stage->next->reset_stipple_counter( stage->next );
101
102 if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag) line( stage, v2, v0 );
103 if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag) line( stage, v0, v1 );
104 if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag) line( stage, v1, v2 );
105 }
106
107
108 /** For debugging */
109 static void
110 print_header_flags(unsigned flags)
111 {
112 debug_printf("header->flags = ");
113 if (flags & DRAW_PIPE_RESET_STIPPLE)
114 debug_printf("RESET_STIPPLE ");
115 if (flags & DRAW_PIPE_EDGE_FLAG_0)
116 debug_printf("EDGE_FLAG_0 ");
117 if (flags & DRAW_PIPE_EDGE_FLAG_1)
118 debug_printf("EDGE_FLAG_1 ");
119 if (flags & DRAW_PIPE_EDGE_FLAG_2)
120 debug_printf("EDGE_FLAG_2 ");
121 debug_printf("\n");
122 }
123
124
125 /* Unfilled tri:
126 *
127 * Note edgeflags in the vertex struct is not sufficient as we will
128 * need to manipulate them when decomposing primitives.
129 *
130 * We currently keep the vertex edgeflag and primitive edgeflag mask
131 * separate until the last possible moment.
132 */
133 static void unfilled_tri( struct draw_stage *stage,
134 struct prim_header *header )
135 {
136 struct unfilled_stage *unfilled = unfilled_stage(stage);
137 unsigned mode = unfilled->mode[header->det >= 0.0];
138
139 if (0)
140 print_header_flags(header->flags);
141
142 switch (mode) {
143 case PIPE_POLYGON_MODE_FILL:
144 stage->next->tri( stage->next, header );
145 break;
146 case PIPE_POLYGON_MODE_LINE:
147 lines( stage, header );
148 break;
149 case PIPE_POLYGON_MODE_POINT:
150 points( stage, header );
151 break;
152 default:
153 assert(0);
154 }
155 }
156
157
158 static void unfilled_first_tri( struct draw_stage *stage,
159 struct prim_header *header )
160 {
161 struct unfilled_stage *unfilled = unfilled_stage(stage);
162 const struct pipe_rasterizer_state *rast = stage->draw->rasterizer;
163
164 unfilled->mode[rast->front_ccw ? 0 : 1] = rast->fill_front;
165 unfilled->mode[rast->front_ccw ? 1 : 0] = rast->fill_back;
166
167 stage->tri = unfilled_tri;
168 stage->tri( stage, header );
169 }
170
171
172
173 static void unfilled_flush( struct draw_stage *stage,
174 unsigned flags )
175 {
176 stage->next->flush( stage->next, flags );
177
178 stage->tri = unfilled_first_tri;
179 }
180
181
182 static void unfilled_reset_stipple_counter( struct draw_stage *stage )
183 {
184 stage->next->reset_stipple_counter( stage->next );
185 }
186
187
188 static void unfilled_destroy( struct draw_stage *stage )
189 {
190 draw_free_temp_verts( stage );
191 FREE( stage );
192 }
193
194
195 /**
196 * Create unfilled triangle stage.
197 */
198 struct draw_stage *draw_unfilled_stage( struct draw_context *draw )
199 {
200 struct unfilled_stage *unfilled = CALLOC_STRUCT(unfilled_stage);
201 if (unfilled == NULL)
202 goto fail;
203
204 if (!draw_alloc_temp_verts( &unfilled->stage, 0 ))
205 goto fail;
206
207 unfilled->stage.draw = draw;
208 unfilled->stage.name = "unfilled";
209 unfilled->stage.next = NULL;
210 unfilled->stage.tmp = NULL;
211 unfilled->stage.point = draw_pipe_passthrough_point;
212 unfilled->stage.line = draw_pipe_passthrough_line;
213 unfilled->stage.tri = unfilled_first_tri;
214 unfilled->stage.flush = unfilled_flush;
215 unfilled->stage.reset_stipple_counter = unfilled_reset_stipple_counter;
216 unfilled->stage.destroy = unfilled_destroy;
217
218 return &unfilled->stage;
219
220 fail:
221 if (unfilled)
222 unfilled->stage.destroy( &unfilled->stage );
223
224 return NULL;
225 }