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 **************************************************************************/
29 * \brief Drawing stage for polygon culling
32 /* Authors: Keith Whitwell <keith@tungstengraphics.com>
36 #include "util/u_memory.h"
37 #include "pipe/p_defines.h"
38 #include "draw_pipe.h"
42 struct draw_stage stage
;
43 unsigned winding
; /**< which winding(s) to cull (one of PIPE_WINDING_x) */
47 static INLINE
struct cull_stage
*cull_stage( struct draw_stage
*stage
)
49 return (struct cull_stage
*)stage
;
55 static void cull_tri( struct draw_stage
*stage
,
56 struct prim_header
*header
)
58 const unsigned pos
= stage
->draw
->vs
.position_output
;
61 const float *v0
= header
->v
[0]->data
[pos
];
62 const float *v1
= header
->v
[1]->data
[pos
];
63 const float *v2
= header
->v
[2]->data
[pos
];
65 /* edge vectors e = v0 - v2, f = v1 - v2 */
66 const float ex
= v0
[0] - v2
[0];
67 const float ey
= v0
[1] - v2
[1];
68 const float fx
= v1
[0] - v2
[0];
69 const float fy
= v1
[1] - v2
[1];
71 /* det = cross(e,f).z */
72 header
->det
= ex
* fy
- ey
* fx
;
74 if (header
->det
!= 0) {
75 /* if (det < 0 then Z points toward camera and triangle is
76 * counter-clockwise winding.
78 unsigned winding
= (header
->det
< 0) ? PIPE_WINDING_CCW
: PIPE_WINDING_CW
;
80 if ((winding
& cull_stage(stage
)->winding
) == 0) {
81 /* triangle is not culled, pass to next stage */
82 stage
->next
->tri( stage
->next
, header
);
87 static void cull_first_tri( struct draw_stage
*stage
,
88 struct prim_header
*header
)
90 struct cull_stage
*cull
= cull_stage(stage
);
92 cull
->winding
= stage
->draw
->rasterizer
->cull_mode
;
94 stage
->tri
= cull_tri
;
95 stage
->tri( stage
, header
);
100 static void cull_flush( struct draw_stage
*stage
, unsigned flags
)
102 stage
->tri
= cull_first_tri
;
103 stage
->next
->flush( stage
->next
, flags
);
106 static void cull_reset_stipple_counter( struct draw_stage
*stage
)
108 stage
->next
->reset_stipple_counter( stage
->next
);
112 static void cull_destroy( struct draw_stage
*stage
)
114 draw_free_temp_verts( stage
);
120 * Create a new polygon culling stage.
122 struct draw_stage
*draw_cull_stage( struct draw_context
*draw
)
124 struct cull_stage
*cull
= CALLOC_STRUCT(cull_stage
);
128 if (!draw_alloc_temp_verts( &cull
->stage
, 0 ))
131 cull
->stage
.draw
= draw
;
132 cull
->stage
.name
= "cull";
133 cull
->stage
.next
= NULL
;
134 cull
->stage
.point
= draw_pipe_passthrough_point
;
135 cull
->stage
.line
= draw_pipe_passthrough_line
;
136 cull
->stage
.tri
= cull_first_tri
;
137 cull
->stage
.flush
= cull_flush
;
138 cull
->stage
.reset_stipple_counter
= cull_reset_stipple_counter
;
139 cull
->stage
.destroy
= cull_destroy
;
145 cull
->stage
.destroy( &cull
->stage
);