1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 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.
26 **************************************************************************/
29 * \brief Drawing stage for user culling
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "pipe/p_defines.h"
35 #include "draw_pipe.h"
37 struct user_cull_stage
{
38 struct draw_stage stage
;
41 static inline struct user_cull_stage
*user_cull_stage( struct draw_stage
*stage
)
43 return (struct user_cull_stage
*)stage
;
47 cull_distance_is_out(float dist
)
49 return (dist
< 0.0f
) || util_is_inf_or_nan(dist
);
53 * If the shader writes the culldistance then we can
54 * perform distance based culling. Distance based
55 * culling doesn't require a face and can be performed
56 * on primitives without faces (e.g. points and lines)
58 static void user_cull_point( struct draw_stage
*stage
,
59 struct prim_header
*header
)
61 const unsigned num_written_culldistances
=
62 draw_current_shader_num_written_culldistances(stage
->draw
);
63 const unsigned num_written_clipdistances
=
64 draw_current_shader_num_written_clipdistances(stage
->draw
);
67 debug_assert(num_written_culldistances
);
69 for (i
= 0; i
< num_written_culldistances
; ++i
) {
70 unsigned cull_idx
= (num_written_clipdistances
+ i
) / 4;
72 draw_current_shader_ccdistance_output(stage
->draw
, cull_idx
);
73 unsigned idx
= (num_written_clipdistances
+ i
) % 4;
74 float cull1
= header
->v
[0]->data
[out_idx
][idx
];
75 boolean vert1_out
= cull_distance_is_out(cull1
);
79 stage
->next
->point( stage
->next
, header
);
83 * If the shader writes the culldistance then we can
84 * perform distance based culling. Distance based
85 * culling doesn't require a face and can be performed
86 * on primitives without faces (e.g. points and lines)
88 static void user_cull_line( struct draw_stage
*stage
,
89 struct prim_header
*header
)
91 const unsigned num_written_culldistances
=
92 draw_current_shader_num_written_culldistances(stage
->draw
);
93 const unsigned num_written_clipdistances
=
94 draw_current_shader_num_written_clipdistances(stage
->draw
);
97 debug_assert(num_written_culldistances
);
99 for (i
= 0; i
< num_written_culldistances
; ++i
) {
100 unsigned cull_idx
= (num_written_clipdistances
+ i
) / 4;
102 draw_current_shader_ccdistance_output(stage
->draw
, cull_idx
);
103 unsigned idx
= (num_written_clipdistances
+ i
) % 4;
104 float cull1
= header
->v
[0]->data
[out_idx
][idx
];
105 float cull2
= header
->v
[1]->data
[out_idx
][idx
];
106 boolean vert1_out
= cull_distance_is_out(cull1
);
107 boolean vert2_out
= cull_distance_is_out(cull2
);
108 if (vert1_out
&& vert2_out
)
111 stage
->next
->line( stage
->next
, header
);
115 * Triangles can be culled either using the cull distance
116 * shader outputs or the regular face culling. If required
117 * this function performs both, starting with distance culling.
119 static void user_cull_tri( struct draw_stage
*stage
,
120 struct prim_header
*header
)
122 const unsigned num_written_culldistances
=
123 draw_current_shader_num_written_culldistances(stage
->draw
);
124 const unsigned num_written_clipdistances
=
125 draw_current_shader_num_written_clipdistances(stage
->draw
);
128 debug_assert(num_written_culldistances
);
130 /* Do the distance culling */
131 for (i
= 0; i
< num_written_culldistances
; ++i
) {
132 unsigned cull_idx
= (num_written_clipdistances
+ i
) / 4;
134 draw_current_shader_ccdistance_output(stage
->draw
, cull_idx
);
135 unsigned idx
= (num_written_clipdistances
+ i
) % 4;
136 float cull1
= header
->v
[0]->data
[out_idx
][idx
];
137 float cull2
= header
->v
[1]->data
[out_idx
][idx
];
138 float cull3
= header
->v
[2]->data
[out_idx
][idx
];
139 boolean vert1_out
= cull_distance_is_out(cull1
);
140 boolean vert2_out
= cull_distance_is_out(cull2
);
141 boolean vert3_out
= cull_distance_is_out(cull3
);
142 if (vert1_out
&& vert2_out
&& vert3_out
) {
146 stage
->next
->tri( stage
->next
, header
);
149 static void user_cull_flush( struct draw_stage
*stage
, unsigned flags
)
151 stage
->point
= user_cull_point
;
152 stage
->line
= user_cull_line
;
153 stage
->tri
= user_cull_tri
;
154 stage
->next
->flush( stage
->next
, flags
);
157 static void user_cull_reset_stipple_counter( struct draw_stage
*stage
)
159 stage
->next
->reset_stipple_counter( stage
->next
);
162 static void user_cull_destroy( struct draw_stage
*stage
)
164 draw_free_temp_verts( stage
);
169 * Create a new polygon culling stage.
171 struct draw_stage
*draw_user_cull_stage( struct draw_context
*draw
)
173 struct user_cull_stage
*user_cull
= CALLOC_STRUCT(user_cull_stage
);
177 user_cull
->stage
.draw
= draw
;
178 user_cull
->stage
.name
= "user_cull";
179 user_cull
->stage
.next
= NULL
;
180 user_cull
->stage
.point
= user_cull_point
;
181 user_cull
->stage
.line
= user_cull_line
;
182 user_cull
->stage
.tri
= user_cull_tri
;
183 user_cull
->stage
.flush
= user_cull_flush
;
184 user_cull
->stage
.reset_stipple_counter
= user_cull_reset_stipple_counter
;
185 user_cull
->stage
.destroy
= user_cull_destroy
;
187 if (!draw_alloc_temp_verts( &user_cull
->stage
, 0 ))
190 return &user_cull
->stage
;
194 user_cull
->stage
.destroy( &user_cull
->stage
);