1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
26 **************************************************************************/
28 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
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"
36 #include "draw_pipe.h"
38 struct twoside_stage
{
39 struct draw_stage stage
;
40 float sign
; /**< +1 or -1 */
41 uint attrib_front0
, attrib_back0
;
42 uint attrib_front1
, attrib_back1
;
46 static INLINE
struct twoside_stage
*twoside_stage( struct draw_stage
*stage
)
48 return (struct twoside_stage
*)stage
;
55 * Copy back color(s) to front color(s).
57 static INLINE
struct vertex_header
*
58 copy_bfc( struct twoside_stage
*twoside
,
59 const struct vertex_header
*v
,
62 struct vertex_header
*tmp
= dup_vert( &twoside
->stage
, v
, idx
);
64 if (twoside
->attrib_back0
) {
65 COPY_4FV(tmp
->data
[twoside
->attrib_front0
],
66 tmp
->data
[twoside
->attrib_back0
]);
68 if (twoside
->attrib_back1
) {
69 COPY_4FV(tmp
->data
[twoside
->attrib_front1
],
70 tmp
->data
[twoside
->attrib_back1
]);
79 static void twoside_tri( struct draw_stage
*stage
,
80 struct prim_header
*header
)
82 struct twoside_stage
*twoside
= twoside_stage(stage
);
84 if (header
->det
* twoside
->sign
< 0.0) {
85 /* this is a back-facing triangle */
86 struct prim_header tmp
;
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);
96 stage
->next
->tri( stage
->next
, &tmp
);
99 stage
->next
->tri( stage
->next
, header
);
105 static void twoside_first_tri( struct draw_stage
*stage
,
106 struct prim_header
*header
)
108 struct twoside_stage
*twoside
= twoside_stage(stage
);
109 const struct draw_vertex_shader
*vs
= stage
->draw
->vs
.vertex_shader
;
112 twoside
->attrib_front0
= 0;
113 twoside
->attrib_front1
= 0;
114 twoside
->attrib_back0
= 0;
115 twoside
->attrib_back1
= 0;
117 /* Find which vertex shader outputs are front/back colors */
118 for (i
= 0; i
< vs
->info
.num_outputs
; i
++) {
119 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
) {
120 if (vs
->info
.output_semantic_index
[i
] == 0)
121 twoside
->attrib_front0
= i
;
123 twoside
->attrib_front1
= i
;
125 if (vs
->info
.output_semantic_name
[i
] == TGSI_SEMANTIC_BCOLOR
) {
126 if (vs
->info
.output_semantic_index
[i
] == 0)
127 twoside
->attrib_back0
= i
;
129 twoside
->attrib_back1
= i
;
133 if (!twoside
->attrib_back0
)
134 twoside
->attrib_front0
= 0;
136 if (!twoside
->attrib_back1
)
137 twoside
->attrib_front1
= 0;
140 * We'll multiply the primitive's determinant by this sign to determine
141 * if the triangle is back-facing (negative).
142 * sign = -1 for CCW, +1 for CW
144 twoside
->sign
= stage
->draw
->rasterizer
->front_ccw
? -1.0f
: 1.0f
;
146 stage
->tri
= twoside_tri
;
147 stage
->tri( stage
, header
);
151 static void twoside_flush( struct draw_stage
*stage
, unsigned flags
)
153 stage
->tri
= twoside_first_tri
;
154 stage
->next
->flush( stage
->next
, flags
);
158 static void twoside_reset_stipple_counter( struct draw_stage
*stage
)
160 stage
->next
->reset_stipple_counter( stage
->next
);
164 static void twoside_destroy( struct draw_stage
*stage
)
166 draw_free_temp_verts( stage
);
172 * Create twoside pipeline stage.
174 struct draw_stage
*draw_twoside_stage( struct draw_context
*draw
)
176 struct twoside_stage
*twoside
= CALLOC_STRUCT(twoside_stage
);
180 twoside
->stage
.draw
= draw
;
181 twoside
->stage
.name
= "twoside";
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
;
190 if (!draw_alloc_temp_verts( &twoside
->stage
, 3 ))
193 return &twoside
->stage
;
197 twoside
->stage
.destroy( &twoside
->stage
);