786826b33c1af7ffadb5430bd56affc20e72fad4
[mesa.git] / src / mesa / pipe / draw / draw_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 "pipe/p_util.h"
37 #include "pipe/p_defines.h"
38 #include "draw_private.h"
39
40
41 struct unfilled_stage {
42 struct draw_stage stage;
43
44 /** [0] = front face, [1] = back face.
45 * legal values: PIPE_POLYGON_MODE_FILL, PIPE_POLYGON_MODE_LINE,
46 * and PIPE_POLYGON_MODE_POINT,
47 */
48 unsigned mode[2];
49 };
50
51
52 static INLINE struct unfilled_stage *unfilled_stage( struct draw_stage *stage )
53 {
54 return (struct unfilled_stage *)stage;
55 }
56
57
58 static void unfilled_begin( struct draw_stage *stage )
59 {
60 struct unfilled_stage *unfilled = unfilled_stage(stage);
61
62 unfilled->mode[0] = stage->draw->rasterizer->fill_ccw; /* front */
63 unfilled->mode[1] = stage->draw->rasterizer->fill_cw; /* back */
64
65 stage->next->begin( stage->next );
66 }
67
68 static void point( struct draw_stage *stage,
69 struct vertex_header *v0 )
70 {
71 struct prim_header tmp;
72 tmp.v[0] = v0;
73 stage->next->point( stage->next, &tmp );
74 }
75
76 static void line( struct draw_stage *stage,
77 struct vertex_header *v0,
78 struct vertex_header *v1 )
79 {
80 struct prim_header tmp;
81 tmp.v[0] = v0;
82 tmp.v[1] = v1;
83 stage->next->line( stage->next, &tmp );
84 }
85
86
87 static void points( struct draw_stage *stage,
88 struct prim_header *header )
89 {
90 struct vertex_header *v0 = header->v[0];
91 struct vertex_header *v1 = header->v[1];
92 struct vertex_header *v2 = header->v[2];
93
94 if (header->edgeflags & 0x1) point( stage, v0 );
95 if (header->edgeflags & 0x2) point( stage, v1 );
96 if (header->edgeflags & 0x4) point( stage, v2 );
97 }
98
99
100 static void lines( struct draw_stage *stage,
101 struct prim_header *header )
102 {
103 struct vertex_header *v0 = header->v[0];
104 struct vertex_header *v1 = header->v[1];
105 struct vertex_header *v2 = header->v[2];
106
107 #if 0
108 assert(((header->edgeflags & 0x1) >> 0) == header->v[0]->edgeflag);
109 assert(((header->edgeflags & 0x2) >> 1) == header->v[1]->edgeflag);
110 assert(((header->edgeflags & 0x4) >> 2) == header->v[2]->edgeflag);
111 #endif
112
113 if (header->edgeflags & 0x1) line( stage, v0, v1 );
114 if (header->edgeflags & 0x2) line( stage, v1, v2 );
115 if (header->edgeflags & 0x4) line( stage, v2, v0 );
116 }
117
118
119 /* Unfilled tri:
120 *
121 * Note edgeflags in the vertex struct is not sufficient as we will
122 * need to manipulate them when decomposing primitives???
123 */
124 static void unfilled_tri( struct draw_stage *stage,
125 struct prim_header *header )
126 {
127 struct unfilled_stage *unfilled = unfilled_stage(stage);
128 unsigned mode = unfilled->mode[header->det >= 0.0];
129
130 switch (mode) {
131 case PIPE_POLYGON_MODE_FILL:
132 stage->next->tri( stage->next, header );
133 break;
134 case PIPE_POLYGON_MODE_LINE:
135 lines( stage, header );
136 break;
137 case PIPE_POLYGON_MODE_POINT:
138 points( stage, header );
139 break;
140 default:
141 abort();
142 }
143 }
144
145 static void unfilled_line( struct draw_stage *stage,
146 struct prim_header *header )
147 {
148 stage->next->line( stage->next, header );
149 }
150
151
152 static void unfilled_point( struct draw_stage *stage,
153 struct prim_header *header )
154 {
155 stage->next->point( stage->next, header );
156 }
157
158
159 static void unfilled_end( struct draw_stage *stage )
160 {
161 stage->next->end( stage->next );
162 }
163
164
165 static void unfilled_reset_stipple_counter( struct draw_stage *stage )
166 {
167 stage->next->reset_stipple_counter( stage->next );
168 }
169
170
171 static void unfilled_destroy( struct draw_stage *stage )
172 {
173 draw_free_tmps( stage );
174 FREE( stage );
175 }
176
177
178 /**
179 * Create unfilled triangle stage.
180 */
181 struct draw_stage *draw_unfilled_stage( struct draw_context *draw )
182 {
183 struct unfilled_stage *unfilled = CALLOC_STRUCT(unfilled_stage);
184
185 draw_alloc_tmps( &unfilled->stage, 0 );
186
187 unfilled->stage.draw = draw;
188 unfilled->stage.next = NULL;
189 unfilled->stage.tmp = NULL;
190 unfilled->stage.begin = unfilled_begin;
191 unfilled->stage.point = unfilled_point;
192 unfilled->stage.line = unfilled_line;
193 unfilled->stage.tri = unfilled_tri;
194 unfilled->stage.end = unfilled_end;
195 unfilled->stage.reset_stipple_counter = unfilled_reset_stipple_counter;
196 unfilled->stage.destroy = unfilled_destroy;
197
198 return &unfilled->stage;
199 }