s/Tungsten Graphics/VMware/
[mesa.git] / src / gallium / auxiliary / draw / draw_pipe_cull.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.com>
33 */
34
35
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38 #include "pipe/p_defines.h"
39 #include "draw_pipe.h"
40
41
42 struct cull_stage {
43 struct draw_stage stage;
44 unsigned cull_face; /**< which face(s) to cull (one of PIPE_FACE_x) */
45 unsigned front_ccw;
46 };
47
48
49 static INLINE struct cull_stage *cull_stage( struct draw_stage *stage )
50 {
51 return (struct cull_stage *)stage;
52 }
53
54 static INLINE boolean
55 cull_distance_is_out(float dist)
56 {
57 return (dist < 0.0f) || util_is_inf_or_nan(dist);
58 }
59
60 /*
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)
65 */
66 static void cull_point( struct draw_stage *stage,
67 struct prim_header *header )
68 {
69 const unsigned num_written_culldistances =
70 draw_current_shader_num_written_culldistances(stage->draw);
71 unsigned i;
72
73 debug_assert(num_written_culldistances);
74
75 for (i = 0; i < num_written_culldistances; ++i) {
76 unsigned cull_idx = i / 4;
77 unsigned out_idx =
78 draw_current_shader_culldistance_output(stage->draw, cull_idx);
79 unsigned idx = i % 4;
80 float cull1 = header->v[0]->data[out_idx][idx];
81 boolean vert1_out = cull_distance_is_out(cull1);
82 if (vert1_out)
83 return;
84 }
85 stage->next->point( stage->next, header );
86 }
87
88 /*
89 * If the shader writes the culldistance then we can
90 * perform distance based culling. Distance based
91 * culling doesn't require a face and can be performed
92 * on primitives without faces (e.g. points and lines)
93 */
94 static void cull_line( struct draw_stage *stage,
95 struct prim_header *header )
96 {
97 const unsigned num_written_culldistances =
98 draw_current_shader_num_written_culldistances(stage->draw);
99 unsigned i;
100
101 debug_assert(num_written_culldistances);
102
103 for (i = 0; i < num_written_culldistances; ++i) {
104 unsigned cull_idx = i / 4;
105 unsigned out_idx =
106 draw_current_shader_culldistance_output(stage->draw, cull_idx);
107 unsigned idx = i % 4;
108 float cull1 = header->v[0]->data[out_idx][idx];
109 float cull2 = header->v[1]->data[out_idx][idx];
110 boolean vert1_out = cull_distance_is_out(cull1);
111 boolean vert2_out = cull_distance_is_out(cull2);
112 if (vert1_out && vert2_out)
113 return;
114 }
115 stage->next->line( stage->next, header );
116 }
117
118 /*
119 * Triangles can be culled either using the cull distance
120 * shader outputs or the regular face culling. If required
121 * this function performs both, starting with distance culling.
122 */
123 static void cull_tri( struct draw_stage *stage,
124 struct prim_header *header )
125 {
126 const unsigned num_written_culldistances =
127 draw_current_shader_num_written_culldistances(stage->draw);
128
129 /* Do the distance culling */
130 if (num_written_culldistances) {
131 unsigned i;
132 for (i = 0; i < num_written_culldistances; ++i) {
133 unsigned cull_idx = i / 4;
134 unsigned out_idx =
135 draw_current_shader_culldistance_output(stage->draw, cull_idx);
136 unsigned idx = i % 4;
137 float cull1 = header->v[0]->data[out_idx][idx];
138 float cull2 = header->v[1]->data[out_idx][idx];
139 float cull3 = header->v[2]->data[out_idx][idx];
140 boolean vert1_out = cull_distance_is_out(cull1);
141 boolean vert2_out = cull_distance_is_out(cull2);
142 boolean vert3_out = cull_distance_is_out(cull3);
143 if (vert1_out && vert2_out && vert3_out)
144 return;
145 }
146 }
147
148 /* Do the regular face culling */
149 {
150 const unsigned pos = draw_current_shader_position_output(stage->draw);
151 /* Window coords: */
152 const float *v0 = header->v[0]->data[pos];
153 const float *v1 = header->v[1]->data[pos];
154 const float *v2 = header->v[2]->data[pos];
155
156 /* edge vectors: e = v0 - v2, f = v1 - v2 */
157 const float ex = v0[0] - v2[0];
158 const float ey = v0[1] - v2[1];
159 const float fx = v1[0] - v2[0];
160 const float fy = v1[1] - v2[1];
161
162
163 /* det = cross(e,f).z */
164 header->det = ex * fy - ey * fx;
165
166 if (header->det != 0) {
167 /* if det < 0 then Z points toward the camera and the triangle is
168 * counter-clockwise winding.
169 */
170 unsigned ccw = (header->det < 0);
171 unsigned face = ((ccw == cull_stage(stage)->front_ccw) ?
172 PIPE_FACE_FRONT :
173 PIPE_FACE_BACK);
174
175 if ((face & cull_stage(stage)->cull_face) == 0) {
176 /* triangle is not culled, pass to next stage */
177 stage->next->tri( stage->next, header );
178 }
179 }
180 }
181 }
182
183 static void cull_first_point( struct draw_stage *stage,
184 struct prim_header *header )
185 {
186 const unsigned num_written_culldistances =
187 draw_current_shader_num_written_culldistances(stage->draw);
188
189 if (num_written_culldistances) {
190 stage->point = cull_point;
191 stage->point( stage, header );
192 } else {
193 stage->point = draw_pipe_passthrough_point;
194 stage->point( stage, header );
195 }
196 }
197
198 static void cull_first_line( struct draw_stage *stage,
199 struct prim_header *header )
200 {
201 const unsigned num_written_culldistances =
202 draw_current_shader_num_written_culldistances(stage->draw);
203
204 if (num_written_culldistances) {
205 stage->line = cull_line;
206 stage->line( stage, header );
207 } else {
208 stage->line = draw_pipe_passthrough_line;
209 stage->line( stage, header );
210 }
211 }
212
213 static void cull_first_tri( struct draw_stage *stage,
214 struct prim_header *header )
215 {
216 struct cull_stage *cull = cull_stage(stage);
217
218 cull->cull_face = stage->draw->rasterizer->cull_face;
219 cull->front_ccw = stage->draw->rasterizer->front_ccw;
220
221 stage->tri = cull_tri;
222 stage->tri( stage, header );
223 }
224
225
226 static void cull_flush( struct draw_stage *stage, unsigned flags )
227 {
228 stage->point = cull_first_point;
229 stage->line = cull_first_line;
230 stage->tri = cull_first_tri;
231 stage->next->flush( stage->next, flags );
232 }
233
234
235 static void cull_reset_stipple_counter( struct draw_stage *stage )
236 {
237 stage->next->reset_stipple_counter( stage->next );
238 }
239
240
241 static void cull_destroy( struct draw_stage *stage )
242 {
243 draw_free_temp_verts( stage );
244 FREE( stage );
245 }
246
247
248 /**
249 * Create a new polygon culling stage.
250 */
251 struct draw_stage *draw_cull_stage( struct draw_context *draw )
252 {
253 struct cull_stage *cull = CALLOC_STRUCT(cull_stage);
254 if (cull == NULL)
255 goto fail;
256
257 cull->stage.draw = draw;
258 cull->stage.name = "cull";
259 cull->stage.next = NULL;
260 cull->stage.point = cull_first_point;
261 cull->stage.line = cull_first_line;
262 cull->stage.tri = cull_first_tri;
263 cull->stage.flush = cull_flush;
264 cull->stage.reset_stipple_counter = cull_reset_stipple_counter;
265 cull->stage.destroy = cull_destroy;
266
267 if (!draw_alloc_temp_verts( &cull->stage, 0 ))
268 goto fail;
269
270 return &cull->stage;
271
272 fail:
273 if (cull)
274 cull->stage.destroy( &cull->stage );
275
276 return NULL;
277 }