1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * \brief Drawing stage for handling glPolygonMode(line/point).
30 * Convert triangles to points or lines as needed.
33 /* Authors: Keith Whitwell <keithw@vmware.com>
36 #include "util/u_memory.h"
37 #include "pipe/p_defines.h"
38 #include "draw_private.h"
39 #include "draw_pipe.h"
43 struct unfilled_stage
{
44 struct draw_stage stage
;
46 /** [0] = front face, [1] = back face.
47 * legal values: PIPE_POLYGON_MODE_FILL, PIPE_POLYGON_MODE_LINE,
48 * and PIPE_POLYGON_MODE_POINT,
56 static inline struct unfilled_stage
*unfilled_stage( struct draw_stage
*stage
)
58 return (struct unfilled_stage
*)stage
;
62 inject_front_face_info(struct draw_stage
*stage
,
63 struct prim_header
*header
)
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
;
73 /* In case the backend doesn't care about it */
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
;
89 static void point(struct draw_stage
*stage
,
90 struct prim_header
*header
,
91 struct vertex_header
*v0
)
93 struct prim_header tmp
;
94 tmp
.det
= header
->det
;
97 stage
->next
->point(stage
->next
, &tmp
);
100 static void line(struct draw_stage
*stage
,
101 struct prim_header
*header
,
102 struct vertex_header
*v0
,
103 struct vertex_header
*v1
)
105 struct prim_header tmp
;
106 tmp
.det
= header
->det
;
110 stage
->next
->line(stage
->next
, &tmp
);
114 static void points(struct draw_stage
*stage
,
115 struct prim_header
*header
)
117 struct vertex_header
*v0
= header
->v
[0];
118 struct vertex_header
*v1
= header
->v
[1];
119 struct vertex_header
*v2
= header
->v
[2];
121 inject_front_face_info(stage
, header
);
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
);
132 static void lines(struct draw_stage
*stage
,
133 struct prim_header
*header
)
135 struct vertex_header
*v0
= header
->v
[0];
136 struct vertex_header
*v1
= header
->v
[1];
137 struct vertex_header
*v2
= header
->v
[2];
139 if (header
->flags
& DRAW_PIPE_RESET_STIPPLE
)
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).
148 stage
->next
->reset_stipple_counter(stage
->next
);
150 inject_front_face_info(stage
, header
);
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
);
163 print_header_flags(unsigned flags
)
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 ");
180 * Note edgeflags in the vertex struct is not sufficient as we will
181 * need to manipulate them when decomposing primitives.
183 * We currently keep the vertex edgeflag and primitive edgeflag mask
184 * separate until the last possible moment.
186 static void unfilled_tri( struct draw_stage
*stage
,
187 struct prim_header
*header
)
189 struct unfilled_stage
*unfilled
= unfilled_stage(stage
);
190 unsigned cw
= header
->det
>= 0.0;
191 unsigned mode
= unfilled
->mode
[cw
];
194 print_header_flags(header
->flags
);
197 case PIPE_POLYGON_MODE_FILL
:
198 stage
->next
->tri( stage
->next
, header
);
200 case PIPE_POLYGON_MODE_LINE
:
201 lines( stage
, header
);
203 case PIPE_POLYGON_MODE_POINT
:
204 points( stage
, header
);
212 static void unfilled_first_tri( struct draw_stage
*stage
,
213 struct prim_header
*header
)
215 struct unfilled_stage
*unfilled
= unfilled_stage(stage
);
216 const struct pipe_rasterizer_state
*rast
= stage
->draw
->rasterizer
;
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
;
221 stage
->tri
= unfilled_tri
;
222 stage
->tri( stage
, header
);
227 static void unfilled_flush( struct draw_stage
*stage
,
230 stage
->next
->flush( stage
->next
, flags
);
232 stage
->tri
= unfilled_first_tri
;
236 static void unfilled_reset_stipple_counter( struct draw_stage
*stage
)
238 stage
->next
->reset_stipple_counter( stage
->next
);
242 static void unfilled_destroy( struct draw_stage
*stage
)
244 draw_free_temp_verts( stage
);
249 * Try to allocate an output slot which we can use
250 * to preserve the front face information.
253 draw_unfilled_prepare_outputs( struct draw_context
*draw
,
254 struct draw_stage
*stage
)
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;
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);
267 unfilled
->face_slot
= -1;
273 * Create unfilled triangle stage.
275 struct draw_stage
*draw_unfilled_stage( struct draw_context
*draw
)
277 struct unfilled_stage
*unfilled
= CALLOC_STRUCT(unfilled_stage
);
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
;
292 unfilled
->face_slot
= -1;
294 if (!draw_alloc_temp_verts( &unfilled
->stage
, 0 ))
297 return &unfilled
->stage
;
301 unfilled
->stage
.destroy( &unfilled
->stage
);