5910dccc43cc216eef8e5f2996487c5bd2b702a2
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_twoside.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 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
29 */
30
31 #include "pipe/p_util.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_shader_tokens.h"
34 #include "draw_vs.h"
35 #include "draw_pipe.h"
36
37 struct twoside_stage {
38 struct draw_stage stage;
39 float sign; /**< +1 or -1 */
40 uint attrib_front0, attrib_back0;
41 uint attrib_front1, attrib_back1;
42 };
43
44
45 static INLINE struct twoside_stage *twoside_stage( struct draw_stage *stage )
46 {
47 return (struct twoside_stage *)stage;
48 }
49
50
51
52
53 /**
54 * Copy back color(s) to front color(s).
55 */
56 static INLINE struct vertex_header *
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) {
64 COPY_4FV(tmp->data[twoside->attrib_front0],
65 tmp->data[twoside->attrib_back0]);
66 }
67 if (twoside->attrib_back1) {
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,
79 struct prim_header *header )
80 {
81 struct twoside_stage *twoside = twoside_stage(stage);
82
83 if (header->det * twoside->sign < 0.0) {
84 /* this is a back-facing triangle */
85 struct prim_header tmp;
86
87 tmp.det = header->det;
88 tmp.edgeflags = header->edgeflags;
89 /* copy back attribs to front attribs */
90 tmp.v[0] = copy_bfc(twoside, header->v[0], 0);
91 tmp.v[1] = copy_bfc(twoside, header->v[1], 1);
92 tmp.v[2] = copy_bfc(twoside, header->v[2], 2);
93
94 stage->next->tri( stage->next, &tmp );
95 }
96 else {
97 stage->next->tri( stage->next, header );
98 }
99 }
100
101
102
103 static void twoside_first_tri( struct draw_stage *stage,
104 struct prim_header *header )
105 {
106 struct twoside_stage *twoside = twoside_stage(stage);
107 const struct draw_vertex_shader *vs = stage->draw->vertex_shader;
108 uint i;
109
110 twoside->attrib_front0 = 0;
111 twoside->attrib_front1 = 0;
112 twoside->attrib_back0 = 0;
113 twoside->attrib_back1 = 0;
114
115 /* Find which vertex shader outputs are front/back colors */
116 for (i = 0; i < vs->info.num_outputs; i++) {
117 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR) {
118 if (vs->info.output_semantic_index[i] == 0)
119 twoside->attrib_front0 = i;
120 else
121 twoside->attrib_front1 = i;
122 }
123 if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {
124 if (vs->info.output_semantic_index[i] == 0)
125 twoside->attrib_back0 = i;
126 else
127 twoside->attrib_back1 = i;
128 }
129 }
130
131 if (!twoside->attrib_back0)
132 twoside->attrib_front0 = 0;
133
134 if (!twoside->attrib_back1)
135 twoside->attrib_front1 = 0;
136
137 /*
138 * We'll multiply the primitive's determinant by this sign to determine
139 * if the triangle is back-facing (negative).
140 * sign = -1 for CCW, +1 for CW
141 */
142 twoside->sign = (stage->draw->rasterizer->front_winding == PIPE_WINDING_CCW) ? -1.0f : 1.0f;
143
144 stage->tri = twoside_tri;
145 stage->tri( stage, header );
146 }
147
148
149 static void twoside_flush( struct draw_stage *stage, unsigned flags )
150 {
151 stage->tri = twoside_first_tri;
152 stage->next->flush( stage->next, flags );
153 }
154
155
156 static void twoside_reset_stipple_counter( struct draw_stage *stage )
157 {
158 stage->next->reset_stipple_counter( stage->next );
159 }
160
161
162 static void 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 {
174 struct twoside_stage *twoside = CALLOC_STRUCT(twoside_stage);
175 if (twoside == NULL)
176 goto fail;
177
178 if (!draw_alloc_temp_verts( &twoside->stage, 3 ))
179 goto fail;
180
181 twoside->stage.draw = draw;
182 twoside->stage.next = NULL;
183 twoside->stage.point = draw_pipe_passthrough_point;
184 twoside->stage.line = draw_pipe_passthrough_line;
185 twoside->stage.tri = twoside_first_tri;
186 twoside->stage.flush = twoside_flush;
187 twoside->stage.reset_stipple_counter = twoside_reset_stipple_counter;
188 twoside->stage.destroy = twoside_destroy;
189
190 return &twoside->stage;
191
192 fail:
193 if (twoside)
194 twoside->stage.destroy( &twoside->stage );
195
196 return NULL;
197 }