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