xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_pipe_twoside.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 /* Authors:  Keith Whitwell <[email protected]>
29  */
30 
31 #include "util/u_math.h"
32 #include "util/u_memory.h"
33 #include "pipe/p_defines.h"
34 #include "pipe/p_shader_tokens.h"
35 #include "draw_vs.h"
36 #include "draw_pipe.h"
37 
38 struct twoside_stage {
39    struct draw_stage stage;
40    float sign;         /**< +1 or -1 */
41    int attrib_front0, attrib_back0;
42    int attrib_front1, attrib_back1;
43 };
44 
45 
46 static inline struct twoside_stage *
twoside_stage(struct draw_stage * stage)47 twoside_stage(struct draw_stage *stage)
48 {
49    return (struct twoside_stage *) stage;
50 }
51 
52 
53 /**
54  * Copy back color(s) to front color(s).
55  */
56 static inline struct vertex_header *
copy_bfc(struct twoside_stage * twoside,const struct vertex_header * v,unsigned idx)57 copy_bfc(struct twoside_stage *twoside,
58          const struct vertex_header *v,
59          unsigned idx)
60 {
61    struct vertex_header *tmp = dup_vert(&twoside->stage, v, idx);
62 
63    if (twoside->attrib_back0 >= 0 && twoside->attrib_front0 >= 0) {
64       COPY_4FV(tmp->data[twoside->attrib_front0],
65                tmp->data[twoside->attrib_back0]);
66    }
67    if (twoside->attrib_back1 >= 0 && twoside->attrib_front1 >= 0) {
68       COPY_4FV(tmp->data[twoside->attrib_front1],
69                tmp->data[twoside->attrib_back1]);
70    }
71 
72    return tmp;
73 }
74 
75 
76 /* Twoside tri:
77  */
78 static void
twoside_tri(struct draw_stage * stage,struct prim_header * header)79 twoside_tri(struct draw_stage *stage,
80             struct prim_header *header)
81 {
82    struct twoside_stage *twoside = twoside_stage(stage);
83 
84    if (header->det * twoside->sign < 0.0) {
85       /* this is a back-facing triangle */
86       struct prim_header tmp;
87 
88       tmp.det = header->det;
89       tmp.flags = header->flags;
90       tmp.pad = header->pad;
91       /* copy back attribs to front attribs */
92       tmp.v[0] = copy_bfc(twoside, header->v[0], 0);
93       tmp.v[1] = copy_bfc(twoside, header->v[1], 1);
94       tmp.v[2] = copy_bfc(twoside, header->v[2], 2);
95 
96       stage->next->tri(stage->next, &tmp);
97    } else {
98       stage->next->tri(stage->next, header);
99    }
100 }
101 
102 
103 static void
twoside_first_tri(struct draw_stage * stage,struct prim_header * header)104 twoside_first_tri(struct draw_stage *stage,
105                   struct prim_header *header)
106 {
107    struct twoside_stage *twoside = twoside_stage(stage);
108    const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;
109 
110    twoside->attrib_front0 = -1;
111    twoside->attrib_front1 = -1;
112    twoside->attrib_back0 = -1;
113    twoside->attrib_back1 = -1;
114 
115    /*
116     * Find which vertex shader outputs are front/back colors
117     * (only first two can be front or back).
118     */
119    for (unsigned i = 0; i < vs->info.num_outputs; i++) {
120       if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR) {
121          if (vs->info.output_semantic_index[i] == 0)
122             twoside->attrib_front0 = i;
123          else if (vs->info.output_semantic_index[i] == 1)
124             twoside->attrib_front1 = i;
125       }
126       if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
127          if (vs->info.output_semantic_index[i] == 0)
128             twoside->attrib_back0 = i;
129          else if (vs->info.output_semantic_index[i] == 1)
130             twoside->attrib_back1 = i;
131       }
132    }
133 
134    /*
135     * We'll multiply the primitive's determinant by this sign to determine
136     * if the triangle is back-facing (negative).
137     * sign = -1 for CCW, +1 for CW
138     */
139    twoside->sign = stage->draw->rasterizer->front_ccw ? -1.0f : 1.0f;
140 
141    stage->tri = twoside_tri;
142    stage->tri(stage, header);
143 }
144 
145 
146 static void
twoside_flush(struct draw_stage * stage,unsigned flags)147 twoside_flush(struct draw_stage *stage, unsigned flags)
148 {
149    stage->tri = twoside_first_tri;
150    stage->next->flush(stage->next, flags);
151 }
152 
153 
154 static void
twoside_reset_stipple_counter(struct draw_stage * stage)155 twoside_reset_stipple_counter(struct draw_stage *stage)
156 {
157    stage->next->reset_stipple_counter(stage->next);
158 }
159 
160 
161 static void
twoside_destroy(struct draw_stage * stage)162 twoside_destroy(struct draw_stage *stage)
163 {
164    draw_free_temp_verts(stage);
165    FREE(stage);
166 }
167 
168 
169 /**
170  * Create twoside pipeline stage.
171  */
172 struct draw_stage *
draw_twoside_stage(struct draw_context * draw)173 draw_twoside_stage(struct draw_context *draw)
174 {
175    struct twoside_stage *twoside = CALLOC_STRUCT(twoside_stage);
176    if (!twoside)
177       goto fail;
178 
179    twoside->stage.draw = draw;
180    twoside->stage.name = "twoside";
181    twoside->stage.next = NULL;
182    twoside->stage.point = draw_pipe_passthrough_point;
183    twoside->stage.line = draw_pipe_passthrough_line;
184    twoside->stage.tri = twoside_first_tri;
185    twoside->stage.flush = twoside_flush;
186    twoside->stage.reset_stipple_counter = twoside_reset_stipple_counter;
187    twoside->stage.destroy = twoside_destroy;
188 
189    if (!draw_alloc_temp_verts(&twoside->stage, 3))
190       goto fail;
191 
192    return &twoside->stage;
193 
194 fail:
195    if (twoside)
196       twoside->stage.destroy(&twoside->stage);
197 
198    return NULL;
199 }
200