Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / auxiliary / draw / draw_pipe_cull.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 /**
29  * \brief  Drawing stage for polygon culling
30  */
31
32 /* Authors:  Keith Whitwell <keith@tungstengraphics.com>
33  */
34
35
36 #include "util/u_memory.h"
37 #include "pipe/p_defines.h"
38 #include "draw_pipe.h"
39
40
41 struct cull_stage {
42    struct draw_stage stage;
43    unsigned cull_face;  /**< which face(s) to cull (one of PIPE_FACE_x) */
44    unsigned front_ccw;
45 };
46
47
48 static INLINE struct cull_stage *cull_stage( struct draw_stage *stage )
49 {
50    return (struct cull_stage *)stage;
51 }
52
53
54 static void cull_tri( struct draw_stage *stage,
55                       struct prim_header *header )
56 {
57    const unsigned pos = draw_current_shader_position_output(stage->draw);
58
59    /* Window coords: */
60    const float *v0 = header->v[0]->data[pos];
61    const float *v1 = header->v[1]->data[pos];
62    const float *v2 = header->v[2]->data[pos];
63
64    /* edge vectors: e = v0 - v2, f = v1 - v2 */
65    const float ex = v0[0] - v2[0];
66    const float ey = v0[1] - v2[1];
67    const float fx = v1[0] - v2[0];
68    const float fy = v1[1] - v2[1];
69    
70    /* det = cross(e,f).z */
71    header->det = ex * fy - ey * fx;
72
73    if (header->det != 0) {
74       /* if det < 0 then Z points toward the camera and the triangle is 
75        * counter-clockwise winding.
76        */
77       unsigned ccw = (header->det < 0);
78       unsigned face = ((ccw == cull_stage(stage)->front_ccw) ?
79                        PIPE_FACE_FRONT :
80                        PIPE_FACE_BACK);
81
82       if ((face & cull_stage(stage)->cull_face) == 0) {
83          /* triangle is not culled, pass to next stage */
84          stage->next->tri( stage->next, header );
85       }
86    }
87 }
88
89
90 static void cull_first_tri( struct draw_stage *stage, 
91                             struct prim_header *header )
92 {
93    struct cull_stage *cull = cull_stage(stage);
94
95    cull->cull_face = stage->draw->rasterizer->cull_face;
96    cull->front_ccw = stage->draw->rasterizer->front_ccw;
97
98    stage->tri = cull_tri;
99    stage->tri( stage, header );
100 }
101
102
103 static void cull_flush( struct draw_stage *stage, unsigned flags )
104 {
105    stage->tri = cull_first_tri;
106    stage->next->flush( stage->next, flags );
107 }
108
109
110 static void cull_reset_stipple_counter( struct draw_stage *stage )
111 {
112    stage->next->reset_stipple_counter( stage->next );
113 }
114
115
116 static void cull_destroy( struct draw_stage *stage )
117 {
118    draw_free_temp_verts( stage );
119    FREE( stage );
120 }
121
122
123 /**
124  * Create a new polygon culling stage.
125  */
126 struct draw_stage *draw_cull_stage( struct draw_context *draw )
127 {
128    struct cull_stage *cull = CALLOC_STRUCT(cull_stage);
129    if (cull == NULL)
130       goto fail;
131
132    cull->stage.draw = draw;
133    cull->stage.name = "cull";
134    cull->stage.next = NULL;
135    cull->stage.point = draw_pipe_passthrough_point;
136    cull->stage.line = draw_pipe_passthrough_line;
137    cull->stage.tri = cull_first_tri;
138    cull->stage.flush = cull_flush;
139    cull->stage.reset_stipple_counter = cull_reset_stipple_counter;
140    cull->stage.destroy = cull_destroy;
141
142    if (!draw_alloc_temp_verts( &cull->stage, 0 ))
143       goto fail;
144
145    return &cull->stage;
146
147 fail:
148    if (cull)
149       cull->stage.destroy( &cull->stage );
150
151    return NULL;
152 }