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_math.h"
37 #include "util/u_memory.h"
38 #include "pipe/p_defines.h"
39 #include "draw_pipe.h"
43 struct draw_stage stage;
44 unsigned cull_face; /**< which face(s) to cull (one of PIPE_FACE_x) */
49 static INLINE struct cull_stage *cull_stage( struct draw_stage *stage )
51 return (struct cull_stage *)stage;
55 boolean cull_distance_is_out(float dist)
57 return (dist < 0) || util_is_inf_or_nan(dist);
61 * If the shader writes the culldistance then we can
62 * perform distance based culling. Distance based
63 * culling doesn't require a face and can be performed
64 * on primitives without faces (e.g. points and lines)
66 static void cull_point( struct draw_stage *stage,
67 struct prim_header *header )
69 const unsigned num_written_culldistances =
70 draw_current_shader_num_written_culldistances(stage->draw);
72 if (num_written_culldistances) {
74 boolean culled = FALSE;
75 for (i = 0; i < num_written_culldistances; ++i) {
76 unsigned cull_idx = i / 4;
78 draw_current_shader_culldistance_output(stage->draw, cull_idx);
80 float cull1 = header->v[0]->data[out_idx][idx];
81 boolean vert1_out = cull_distance_is_out(cull1);
86 stage->next->point( stage->next, header );
91 * If the shader writes the culldistance then we can
92 * perform distance based culling. Distance based
93 * culling doesn't require a face and can be performed
94 * on primitives without faces (e.g. points and lines)
96 static void cull_line( struct draw_stage *stage,
97 struct prim_header *header )
99 const unsigned num_written_culldistances =
100 draw_current_shader_num_written_culldistances(stage->draw);
102 if (num_written_culldistances) {
104 boolean culled = FALSE;
105 for (i = 0; i < num_written_culldistances; ++i) {
106 unsigned cull_idx = i / 4;
108 draw_current_shader_culldistance_output(stage->draw, cull_idx);
109 unsigned idx = i % 4;
110 float cull1 = header->v[0]->data[out_idx][idx];
111 float cull2 = header->v[1]->data[out_idx][idx];
112 boolean vert1_out = cull_distance_is_out(cull1);
113 boolean vert2_out = cull_distance_is_out(cull2);
114 if (vert1_out && vert2_out)
118 stage->next->line( stage->next, header );
123 * Triangles can be culled either using the cull distance
124 * shader outputs or the regular face culling. If required
125 * this function performs both, starting with distance culling.
127 static void cull_tri( struct draw_stage *stage,
128 struct prim_header *header )
130 const unsigned num_written_culldistances =
131 draw_current_shader_num_written_culldistances(stage->draw);
133 /* Do the distance culling */
134 if (num_written_culldistances) {
136 boolean culled = FALSE;
137 for (i = 0; i < num_written_culldistances; ++i) {
138 unsigned cull_idx = i / 4;
140 draw_current_shader_culldistance_output(stage->draw, cull_idx);
141 unsigned idx = i % 4;
142 float cull1 = header->v[0]->data[out_idx][idx];
143 float cull2 = header->v[1]->data[out_idx][idx];
144 float cull3 = header->v[2]->data[out_idx][idx];
145 boolean vert1_out = cull_distance_is_out(cull1);
146 boolean vert2_out = cull_distance_is_out(cull2);
147 boolean vert3_out = cull_distance_is_out(cull3);
148 if (vert1_out && vert2_out && vert3_out)
152 stage->next->tri( stage->next, header );
155 /* Do the regular face culling */
157 const unsigned pos = draw_current_shader_position_output(stage->draw);
159 const float *v0 = header->v[0]->data[pos];
160 const float *v1 = header->v[1]->data[pos];
161 const float *v2 = header->v[2]->data[pos];
163 /* edge vectors: e = v0 - v2, f = v1 - v2 */
164 const float ex = v0[0] - v2[0];
165 const float ey = v0[1] - v2[1];
166 const float fx = v1[0] - v2[0];
167 const float fy = v1[1] - v2[1];
170 /* det = cross(e,f).z */
171 header->det = ex * fy - ey * fx;
173 if (header->det != 0) {
174 /* if det < 0 then Z points toward the camera and the triangle is
175 * counter-clockwise winding.
177 unsigned ccw = (header->det < 0);
178 unsigned face = ((ccw == cull_stage(stage)->front_ccw) ?
182 if ((face & cull_stage(stage)->cull_face) == 0) {
183 /* triangle is not culled, pass to next stage */
184 stage->next->tri( stage->next, header );
190 static void cull_first_point( struct draw_stage *stage,
191 struct prim_header *header )
193 const unsigned num_written_culldistances =
194 draw_current_shader_num_written_culldistances(stage->draw);
196 if (num_written_culldistances) {
197 stage->point = cull_point;
198 stage->point( stage, header );
200 stage->point = draw_pipe_passthrough_point;
201 stage->point( stage, header );
205 static void cull_first_line( struct draw_stage *stage,
206 struct prim_header *header )
208 const unsigned num_written_culldistances =
209 draw_current_shader_num_written_culldistances(stage->draw);
211 if (num_written_culldistances) {
212 stage->line = cull_line;
213 stage->line( stage, header );
215 stage->line = draw_pipe_passthrough_line;
216 stage->line( stage, header );
220 static void cull_first_tri( struct draw_stage *stage,
221 struct prim_header *header )
223 struct cull_stage *cull = cull_stage(stage);
225 cull->cull_face = stage->draw->rasterizer->cull_face;
226 cull->front_ccw = stage->draw->rasterizer->front_ccw;
228 stage->tri = cull_tri;
229 stage->tri( stage, header );
233 static void cull_flush( struct draw_stage *stage, unsigned flags )
235 stage->point = cull_first_point;
236 stage->line = cull_first_line;
237 stage->tri = cull_first_tri;
238 stage->next->flush( stage->next, flags );
242 static void cull_reset_stipple_counter( struct draw_stage *stage )
244 stage->next->reset_stipple_counter( stage->next );
248 static void cull_destroy( struct draw_stage *stage )
250 draw_free_temp_verts( stage );
256 * Create a new polygon culling stage.
258 struct draw_stage *draw_cull_stage( struct draw_context *draw )
260 struct cull_stage *cull = CALLOC_STRUCT(cull_stage);
264 cull->stage.draw = draw;
265 cull->stage.name = "cull";
266 cull->stage.next = NULL;
267 cull->stage.point = cull_first_point;
268 cull->stage.line = cull_first_line;
269 cull->stage.tri = cull_first_tri;
270 cull->stage.flush = cull_flush;
271 cull->stage.reset_stipple_counter = cull_reset_stipple_counter;
272 cull->stage.destroy = cull_destroy;
274 if (!draw_alloc_temp_verts( &cull->stage, 0 ))
281 cull->stage.destroy( &cull->stage );