*/
+#include "util/u_math.h"
#include "util/u_memory.h"
#include "pipe/p_defines.h"
#include "draw_pipe.h"
return (struct cull_stage *)stage;
}
+static INLINE
+boolean cull_distance_is_out(float dist)
+{
+ return (dist < 0) || util_is_inf_or_nan(dist);
+}
+
+/*
+ * If the shader writes the culldistance then we can
+ * perform distance based culling. Distance based
+ * culling doesn't require a face and can be performed
+ * on primitives without faces (e.g. points and lines)
+ */
+static void cull_point( struct draw_stage *stage,
+ struct prim_header *header )
+{
+ const unsigned num_written_culldistances =
+ draw_current_shader_num_written_culldistances(stage->draw);
+
+ if (num_written_culldistances) {
+ unsigned i;
+ boolean culled = FALSE;
+ for (i = 0; i < num_written_culldistances; ++i) {
+ unsigned cull_idx = i / 4;
+ unsigned out_idx =
+ draw_current_shader_culldistance_output(stage->draw, cull_idx);
+ unsigned idx = i % 4;
+ float cull1 = header->v[0]->data[out_idx][idx];
+ boolean vert1_out = cull_distance_is_out(cull1);
+ if (vert1_out)
+ culled = TRUE;
+ }
+ if (!culled)
+ stage->next->point( stage->next, header );
+ }
+}
+/*
+ * If the shader writes the culldistance then we can
+ * perform distance based culling. Distance based
+ * culling doesn't require a face and can be performed
+ * on primitives without faces (e.g. points and lines)
+ */
+static void cull_line( struct draw_stage *stage,
+ struct prim_header *header )
+{
+ const unsigned num_written_culldistances =
+ draw_current_shader_num_written_culldistances(stage->draw);
+
+ if (num_written_culldistances) {
+ unsigned i;
+ boolean culled = FALSE;
+ for (i = 0; i < num_written_culldistances; ++i) {
+ unsigned cull_idx = i / 4;
+ unsigned out_idx =
+ draw_current_shader_culldistance_output(stage->draw, cull_idx);
+ unsigned idx = i % 4;
+ float cull1 = header->v[0]->data[out_idx][idx];
+ float cull2 = header->v[1]->data[out_idx][idx];
+ boolean vert1_out = cull_distance_is_out(cull1);
+ boolean vert2_out = cull_distance_is_out(cull2);
+ if (vert1_out && vert2_out)
+ culled = TRUE;
+ }
+ if (!culled)
+ stage->next->line( stage->next, header );
+ }
+}
+
+/*
+ * Triangles can be culled either using the cull distance
+ * shader outputs or the regular face culling. If required
+ * this function performs both, starting with distance culling.
+ */
static void cull_tri( struct draw_stage *stage,
struct prim_header *header )
{
- const unsigned pos = draw_current_shader_position_output(stage->draw);
-
- /* Window coords: */
- const float *v0 = header->v[0]->data[pos];
- const float *v1 = header->v[1]->data[pos];
- const float *v2 = header->v[2]->data[pos];
-
- /* edge vectors: e = v0 - v2, f = v1 - v2 */
- const float ex = v0[0] - v2[0];
- const float ey = v0[1] - v2[1];
- const float fx = v1[0] - v2[0];
- const float fy = v1[1] - v2[1];
+ const unsigned num_written_culldistances =
+ draw_current_shader_num_written_culldistances(stage->draw);
+
+ /* Do the distance culling */
+ if (num_written_culldistances) {
+ unsigned i;
+ boolean culled = FALSE;
+ for (i = 0; i < num_written_culldistances; ++i) {
+ unsigned cull_idx = i / 4;
+ unsigned out_idx =
+ draw_current_shader_culldistance_output(stage->draw, cull_idx);
+ unsigned idx = i % 4;
+ float cull1 = header->v[0]->data[out_idx][idx];
+ float cull2 = header->v[1]->data[out_idx][idx];
+ float cull3 = header->v[2]->data[out_idx][idx];
+ boolean vert1_out = cull_distance_is_out(cull1);
+ boolean vert2_out = cull_distance_is_out(cull2);
+ boolean vert3_out = cull_distance_is_out(cull3);
+ if (vert1_out && vert2_out && vert3_out)
+ culled = TRUE;
+ }
+ if (!culled)
+ stage->next->tri( stage->next, header );
+ }
+
+ /* Do the regular face culling */
+ {
+ const unsigned pos = draw_current_shader_position_output(stage->draw);
+ /* Window coords: */
+ const float *v0 = header->v[0]->data[pos];
+ const float *v1 = header->v[1]->data[pos];
+ const float *v2 = header->v[2]->data[pos];
+
+ /* edge vectors: e = v0 - v2, f = v1 - v2 */
+ const float ex = v0[0] - v2[0];
+ const float ey = v0[1] - v2[1];
+ const float fx = v1[0] - v2[0];
+ const float fy = v1[1] - v2[1];
+
- /* det = cross(e,f).z */
- header->det = ex * fy - ey * fx;
-
- if (header->det != 0) {
- /* if det < 0 then Z points toward the camera and the triangle is
- * counter-clockwise winding.
- */
- unsigned ccw = (header->det < 0);
- unsigned face = ((ccw == cull_stage(stage)->front_ccw) ?
- PIPE_FACE_FRONT :
- PIPE_FACE_BACK);
-
- if ((face & cull_stage(stage)->cull_face) == 0) {
- /* triangle is not culled, pass to next stage */
- stage->next->tri( stage->next, header );
+ /* det = cross(e,f).z */
+ header->det = ex * fy - ey * fx;
+
+ if (header->det != 0) {
+ /* if det < 0 then Z points toward the camera and the triangle is
+ * counter-clockwise winding.
+ */
+ unsigned ccw = (header->det < 0);
+ unsigned face = ((ccw == cull_stage(stage)->front_ccw) ?
+ PIPE_FACE_FRONT :
+ PIPE_FACE_BACK);
+
+ if ((face & cull_stage(stage)->cull_face) == 0) {
+ /* triangle is not culled, pass to next stage */
+ stage->next->tri( stage->next, header );
+ }
}
}
}
+static void cull_first_point( struct draw_stage *stage,
+ struct prim_header *header )
+{
+ const unsigned num_written_culldistances =
+ draw_current_shader_num_written_culldistances(stage->draw);
+
+ if (num_written_culldistances) {
+ stage->point = cull_point;
+ stage->point( stage, header );
+ } else {
+ stage->point = draw_pipe_passthrough_point;
+ stage->point( stage, header );
+ }
+}
+
+static void cull_first_line( struct draw_stage *stage,
+ struct prim_header *header )
+{
+ const unsigned num_written_culldistances =
+ draw_current_shader_num_written_culldistances(stage->draw);
+
+ if (num_written_culldistances) {
+ stage->line = cull_line;
+ stage->line( stage, header );
+ } else {
+ stage->line = draw_pipe_passthrough_line;
+ stage->line( stage, header );
+ }
+}
static void cull_first_tri( struct draw_stage *stage,
struct prim_header *header )
static void cull_flush( struct draw_stage *stage, unsigned flags )
{
+ stage->point = cull_first_point;
+ stage->line = cull_first_line;
stage->tri = cull_first_tri;
stage->next->flush( stage->next, flags );
}
cull->stage.draw = draw;
cull->stage.name = "cull";
cull->stage.next = NULL;
- cull->stage.point = draw_pipe_passthrough_point;
- cull->stage.line = draw_pipe_passthrough_line;
+ cull->stage.point = cull_first_point;
+ cull->stage.line = cull_first_line;
cull->stage.tri = cull_first_tri;
cull->stage.flush = cull_flush;
cull->stage.reset_stipple_counter = cull_reset_stipple_counter;