1/**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 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.
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 <keithw@vmware.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
43struct 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
56static inline struct unfilled_stage *unfilled_stage( struct draw_stage *stage )
57{
58   return (struct unfilled_stage *)stage;
59}
60
61static void
62inject_front_face_info(struct draw_stage *stage,
63                       struct prim_header *header)
64{
65   struct unfilled_stage *unfilled = unfilled_stage(stage);
66   boolean is_front_face = (
67      (stage->draw->rasterizer->front_ccw && header->det < 0.0f) ||
68      (!stage->draw->rasterizer->front_ccw && header->det > 0.0f));
69   int slot = unfilled->face_slot;
70   unsigned i;
71
72   /* In case the backend doesn't care about it */
73   if (slot < 0) {
74      return;
75   }
76
77   for (i = 0; i < 3; ++i) {
78      struct vertex_header *v = header->v[i];
79      v->data[slot][0] = is_front_face;
80      v->data[slot][1] = is_front_face;
81      v->data[slot][2] = is_front_face;
82      v->data[slot][3] = is_front_face;
83      v->vertex_id = UNDEFINED_VERTEX_ID;
84   }
85}
86
87
88static void point(struct draw_stage *stage,
89                  struct prim_header *header,
90                  struct vertex_header *v0)
91{
92   struct prim_header tmp;
93   tmp.det = header->det;
94   tmp.flags = 0;
95   tmp.v[0] = v0;
96   stage->next->point(stage->next, &tmp);
97}
98
99static void line(struct draw_stage *stage,
100                 struct prim_header *header,
101                 struct vertex_header *v0,
102                 struct vertex_header *v1)
103{
104   struct prim_header tmp;
105   tmp.det = header->det;
106   tmp.flags = 0;
107   tmp.v[0] = v0;
108   tmp.v[1] = v1;
109   stage->next->line(stage->next, &tmp);
110}
111
112
113static void points(struct draw_stage *stage,
114                   struct prim_header *header)
115{
116   struct vertex_header *v0 = header->v[0];
117   struct vertex_header *v1 = header->v[1];
118   struct vertex_header *v2 = header->v[2];
119
120   inject_front_face_info(stage, header);
121
122   if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag)
123      point(stage, header, v0);
124   if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag)
125      point(stage, header, v1);
126   if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag)
127      point(stage, header, v2);
128}
129
130
131static void lines(struct draw_stage *stage,
132                  struct prim_header *header)
133{
134   struct vertex_header *v0 = header->v[0];
135   struct vertex_header *v1 = header->v[1];
136   struct vertex_header *v2 = header->v[2];
137
138   if (header->flags & DRAW_PIPE_RESET_STIPPLE)
139      /*
140       * XXX could revisit this. The only stage which cares is the line
141       * stipple stage. Could just emit correct reset flags here and not
142       * bother about all the calling through reset_stipple_counter
143       * stages. Though technically it is necessary if line stipple is
144       * handled by the driver, but this is not actually hooked up when
145       * using vbuf (vbuf stage reset_stipple_counter does nothing).
146       */
147      stage->next->reset_stipple_counter(stage->next);
148
149   inject_front_face_info(stage, header);
150
151   if ((header->flags & DRAW_PIPE_EDGE_FLAG_2) && v2->edgeflag)
152      line(stage, header, v2, v0);
153   if ((header->flags & DRAW_PIPE_EDGE_FLAG_0) && v0->edgeflag)
154      line(stage, header, v0, v1);
155   if ((header->flags & DRAW_PIPE_EDGE_FLAG_1) && v1->edgeflag)
156      line(stage, header, v1, v2);
157}
158
159
160/** For debugging */
161static void
162print_header_flags(unsigned flags)
163{
164   debug_printf("header->flags = ");
165   if (flags & DRAW_PIPE_RESET_STIPPLE)
166      debug_printf("RESET_STIPPLE ");
167   if (flags & DRAW_PIPE_EDGE_FLAG_0)
168      debug_printf("EDGE_FLAG_0 ");
169   if (flags & DRAW_PIPE_EDGE_FLAG_1)
170      debug_printf("EDGE_FLAG_1 ");
171   if (flags & DRAW_PIPE_EDGE_FLAG_2)
172      debug_printf("EDGE_FLAG_2 ");
173   debug_printf("\n");
174}
175
176
177/* Unfilled tri:
178 *
179 * Note edgeflags in the vertex struct is not sufficient as we will
180 * need to manipulate them when decomposing primitives.
181 *
182 * We currently keep the vertex edgeflag and primitive edgeflag mask
183 * separate until the last possible moment.
184 */
185static void unfilled_tri( struct draw_stage *stage,
186			  struct prim_header *header )
187{
188   struct unfilled_stage *unfilled = unfilled_stage(stage);
189   unsigned cw = header->det >= 0.0;
190   unsigned mode = unfilled->mode[cw];
191
192   if (0)
193      print_header_flags(header->flags);
194
195   switch (mode) {
196   case PIPE_POLYGON_MODE_FILL:
197      stage->next->tri( stage->next, header );
198      break;
199   case PIPE_POLYGON_MODE_LINE:
200      lines( stage, header );
201      break;
202   case PIPE_POLYGON_MODE_POINT:
203      points( stage, header );
204      break;
205   default:
206      assert(0);
207   }
208}
209
210
211static void unfilled_first_tri( struct draw_stage *stage,
212				struct prim_header *header )
213{
214   struct unfilled_stage *unfilled = unfilled_stage(stage);
215   const struct pipe_rasterizer_state *rast = stage->draw->rasterizer;
216
217   unfilled->mode[0] = rast->front_ccw ? rast->fill_front : rast->fill_back;
218   unfilled->mode[1] = rast->front_ccw ? rast->fill_back : rast->fill_front;
219
220   stage->tri = unfilled_tri;
221   stage->tri( stage, header );
222}
223
224
225
226static void unfilled_flush( struct draw_stage *stage,
227			    unsigned flags )
228{
229   stage->next->flush( stage->next, flags );
230
231   stage->tri = unfilled_first_tri;
232}
233
234
235static void unfilled_reset_stipple_counter( struct draw_stage *stage )
236{
237   stage->next->reset_stipple_counter( stage->next );
238}
239
240
241static void unfilled_destroy( struct draw_stage *stage )
242{
243   draw_free_temp_verts( stage );
244   FREE( stage );
245}
246
247/*
248 * Try to allocate an output slot which we can use
249 * to preserve the front face information.
250 */
251void
252draw_unfilled_prepare_outputs( struct draw_context *draw,
253                               struct draw_stage *stage )
254{
255   struct unfilled_stage *unfilled = unfilled_stage(stage);
256   const struct pipe_rasterizer_state *rast = draw ? draw->rasterizer : NULL;
257   boolean is_unfilled = (rast &&
258                          (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
259                           rast->fill_back != PIPE_POLYGON_MODE_FILL));
260   const struct draw_fragment_shader *fs = draw ? draw->fs.fragment_shader : NULL;
261
262   if (is_unfilled && fs && fs->info.uses_frontface)  {
263      unfilled->face_slot = draw_alloc_extra_vertex_attrib(
264         stage->draw, TGSI_SEMANTIC_FACE, 0);
265   } else {
266      unfilled->face_slot = -1;
267   }
268}
269
270
271/**
272 * Create unfilled triangle stage.
273 */
274struct draw_stage *draw_unfilled_stage( struct draw_context *draw )
275{
276   struct unfilled_stage *unfilled = CALLOC_STRUCT(unfilled_stage);
277   if (!unfilled)
278      goto fail;
279
280   unfilled->stage.draw = draw;
281   unfilled->stage.name = "unfilled";
282   unfilled->stage.next = NULL;
283   unfilled->stage.tmp = NULL;
284   unfilled->stage.point = draw_pipe_passthrough_point;
285   unfilled->stage.line = draw_pipe_passthrough_line;
286   unfilled->stage.tri = unfilled_first_tri;
287   unfilled->stage.flush = unfilled_flush;
288   unfilled->stage.reset_stipple_counter = unfilled_reset_stipple_counter;
289   unfilled->stage.destroy = unfilled_destroy;
290
291   unfilled->face_slot = -1;
292
293   if (!draw_alloc_temp_verts( &unfilled->stage, 0 ))
294      goto fail;
295
296   return &unfilled->stage;
297
298 fail:
299   if (unfilled)
300      unfilled->stage.destroy( &unfilled->stage );
301
302   return NULL;
303}
304