Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_cull.c
4565 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* \brief Drawing stage for polygon culling29*/3031/* Authors: Keith Whitwell <[email protected]>32*/333435#include "util/u_math.h"36#include "util/u_memory.h"37#include "pipe/p_defines.h"38#include "draw_pipe.h"394041struct cull_stage {42struct draw_stage stage;43unsigned cull_face; /**< which face(s) to cull (one of PIPE_FACE_x) */44unsigned front_ccw;45};464748static inline struct cull_stage *cull_stage( struct draw_stage *stage )49{50return (struct cull_stage *)stage;51}5253/*54* Triangles can be culled using regular face cull.55*/56static void cull_tri( struct draw_stage *stage,57struct prim_header *header )58{59/* Do the regular face culling */60{61const unsigned pos = draw_current_shader_position_output(stage->draw);62/* Window coords: */63const float *v0 = header->v[0]->data[pos];64const float *v1 = header->v[1]->data[pos];65const float *v2 = header->v[2]->data[pos];6667/* edge vectors: e = v0 - v2, f = v1 - v2 */68const float ex = v0[0] - v2[0];69const float ey = v0[1] - v2[1];70const float fx = v1[0] - v2[0];71const float fy = v1[1] - v2[1];727374/* det = cross(e,f).z */75header->det = ex * fy - ey * fx;7677if (header->det != 0) {78/* if det < 0 then Z points toward the camera and the triangle is79* counter-clockwise winding.80*/81unsigned ccw = (header->det < 0);82unsigned face = ((ccw == cull_stage(stage)->front_ccw) ?83PIPE_FACE_FRONT :84PIPE_FACE_BACK);8586if ((face & cull_stage(stage)->cull_face) == 0) {87/* triangle is not culled, pass to next stage */88stage->next->tri( stage->next, header );89}90} else {91/*92* With zero area, this is back facing (because the spec says93* it's front facing if sign is positive?).94* Some apis apparently do not allow us to cull zero area tris95* here, in case of fill mode line (which is rather lame).96*/97if ((PIPE_FACE_BACK & cull_stage(stage)->cull_face) == 0) {98stage->next->tri( stage->next, header );99}100}101}102}103104static void cull_first_tri( struct draw_stage *stage,105struct prim_header *header )106{107struct cull_stage *cull = cull_stage(stage);108109cull->cull_face = stage->draw->rasterizer->cull_face;110cull->front_ccw = stage->draw->rasterizer->front_ccw;111112stage->tri = cull_tri;113stage->tri( stage, header );114}115116117static void cull_flush( struct draw_stage *stage, unsigned flags )118{119stage->tri = cull_first_tri;120stage->next->flush( stage->next, flags );121}122123124static void cull_reset_stipple_counter( struct draw_stage *stage )125{126stage->next->reset_stipple_counter( stage->next );127}128129130static void cull_destroy( struct draw_stage *stage )131{132draw_free_temp_verts( stage );133FREE( stage );134}135136137/**138* Create a new polygon culling stage.139*/140struct draw_stage *draw_cull_stage( struct draw_context *draw )141{142struct cull_stage *cull = CALLOC_STRUCT(cull_stage);143if (!cull)144goto fail;145146cull->stage.draw = draw;147cull->stage.name = "cull";148cull->stage.next = NULL;149cull->stage.point = draw_pipe_passthrough_point;150cull->stage.line = draw_pipe_passthrough_line;151cull->stage.tri = cull_first_tri;152cull->stage.flush = cull_flush;153cull->stage.reset_stipple_counter = cull_reset_stipple_counter;154cull->stage.destroy = cull_destroy;155156if (!draw_alloc_temp_verts( &cull->stage, 0 ))157goto fail;158159return &cull->stage;160161fail:162if (cull)163cull->stage.destroy( &cull->stage );164165return NULL;166}167168169