16c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell/**************************************************************************
26c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell *
36c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * Copyright 2010, VMware, inc.
46c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * All Rights Reserved.
56c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell *
66c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
76c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * copy of this software and associated documentation files (the
86c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * "Software"), to deal in the Software without restriction, including
96c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
106c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
116c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * permit persons to whom the Software is furnished to do so, subject to
126c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * the following conditions:
136c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell *
146c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * The above copyright notice and this permission notice (including the
156c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * next paragraph) shall be included in all copies or substantial portions
166c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * of the Software.
176c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell *
186c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
196c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
206c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
216c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
226c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
236c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
246c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
256c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell *
266c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell **************************************************************************/
276c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
286c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
296c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
306c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwellstatic boolean TAG(do_cliptest)( struct pt_post_vs *pvs,
316c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell                                 struct draw_vertex_info *info )
326c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell{
336c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   struct vertex_header *out = info->verts;
346c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   const float *scale = pvs->draw->viewport.scale;
356c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   const float *trans = pvs->draw->viewport.translate;
366c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   /* const */ float (*plane)[4] = pvs->draw->plane;
376c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   const unsigned pos = draw_current_shader_position_output(pvs->draw);
3840c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie   const unsigned cv = draw_current_shader_clipvertex_output(pvs->draw);
391865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie   unsigned cd[2];
406c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   const unsigned ef = pvs->draw->vs.edgeflag_output;
41dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   const unsigned ucp_enable = pvs->draw->rasterizer->clip_plane_enable;
426c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   const unsigned flags = (FLAGS);
436c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   unsigned need_pipeline = 0;
446c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   unsigned j;
4540c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie   unsigned i;
461865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie   bool have_cd = false;
471865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie   cd[0] = draw_current_shader_clipdistance_output(pvs->draw, 0);
481865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie   cd[1] = draw_current_shader_clipdistance_output(pvs->draw, 1);
491865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie
501865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie   if (cd[0] != pos || cd[1] != pos)
511865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie     have_cd = true;
526c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
536c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   for (j = 0; j < info->count; j++) {
546c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      float *position = out->data[pos];
556c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      unsigned mask = 0x0;
566c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
576c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      initialize_vertex_header(out);
586c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
594465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul      if (flags & (DO_CLIP_XY | DO_CLIP_XY_GUARD_BAND |
604465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul                   DO_CLIP_FULL_Z | DO_CLIP_HALF_Z | DO_CLIP_USER)) {
6140c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie         float *clipvertex = position;
6240c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie
6340c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie         if ((flags & DO_CLIP_USER) && cv != pos)
6440c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie            clipvertex = out->data[cv];
6540c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie
6640c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie         for (i = 0; i < 4; i++) {
6740c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie            out->clip[i] = clipvertex[i];
6840c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie            out->pre_clip_pos[i] = position[i];
6940c5987ed84f9f0b8bb1f707bb13c1aafc39330aDave Airlie         }
706c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
716c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         /* Do the hardwired planes first:
726c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell          */
734465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul         if (flags & DO_CLIP_XY_GUARD_BAND) {
744465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul            if (-0.50 * position[0] + position[3] < 0) mask |= (1<<0);
754465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul            if ( 0.50 * position[0] + position[3] < 0) mask |= (1<<1);
764465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul            if (-0.50 * position[1] + position[3] < 0) mask |= (1<<2);
774465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul            if ( 0.50 * position[1] + position[3] < 0) mask |= (1<<3);
784465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul         }
794465efc3bf8d755a9afb7a4bb5382e2f5bf113e1Brian Paul         else if (flags & DO_CLIP_XY) {
806c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if (-position[0] + position[3] < 0) mask |= (1<<0);
816c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if ( position[0] + position[3] < 0) mask |= (1<<1);
826c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if (-position[1] + position[3] < 0) mask |= (1<<2);
836c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if ( position[1] + position[3] < 0) mask |= (1<<3);
846c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         }
856c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
866c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         /* Clip Z planes according to full cube, half cube or none.
876c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell          */
886c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         if (flags & DO_CLIP_FULL_Z) {
896c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if ( position[2] + position[3] < 0) mask |= (1<<4);
906c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if (-position[2] + position[3] < 0) mask |= (1<<5);
916c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         }
926c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         else if (flags & DO_CLIP_HALF_Z) {
936c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if ( position[2]               < 0) mask |= (1<<4);
946c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            if (-position[2] + position[3] < 0) mask |= (1<<5);
956c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         }
966c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
976c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         if (flags & DO_CLIP_USER) {
98dc4c821f0817a3db716f965692fb701079f66340Marek Olšák            unsigned ucp_mask = ucp_enable;
991865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie            int num_written_clipdistance = pvs->draw->vs.vertex_shader->info.num_written_clipdistance;
100dc4c821f0817a3db716f965692fb701079f66340Marek Olšák            while (ucp_mask) {
101dc4c821f0817a3db716f965692fb701079f66340Marek Olšák               unsigned plane_idx = ffs(ucp_mask)-1;
102dc4c821f0817a3db716f965692fb701079f66340Marek Olšák               ucp_mask &= ~(1 << plane_idx);
103dc4c821f0817a3db716f965692fb701079f66340Marek Olšák               plane_idx += 6;
104dc4c821f0817a3db716f965692fb701079f66340Marek Olšák
1051865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie               /*
1061865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                * for user clipping check if we have a clip distance output
1071865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                * and the shader has written to it, otherwise use clipvertex
1081865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                * to decide when the plane is clipping.
1091865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                */
1101865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie               if (have_cd && num_written_clipdistance) {
1111865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  float clipdist;
1121865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  i = plane_idx - 6;
1131865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  out->have_clipdist = 1;
1141865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  /* first four clip distance in first vector etc. */
1151865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  if (i < 4)
1161865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                     clipdist = out->data[cd[0]][i];
1171865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  else
1181865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                     clipdist = out->data[cd[1]][i-4];
1191865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  if (clipdist < 0)
1201865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                     mask |= 1 << plane_idx;
1211865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie               } else {
1221865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                  if (dot4(clipvertex, plane[plane_idx]) < 0)
1231865f341d8f45b389061fc08d2da90b7aa8a6099Dave Airlie                     mask |= 1 << plane_idx;
124dc4c821f0817a3db716f965692fb701079f66340Marek Olšák               }
1256c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell            }
1266c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         }
1276c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1286c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         out->clipmask = mask;
1296c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         need_pipeline |= out->clipmask;
1306c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      }
1316c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1326c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      if ((flags & DO_VIEWPORT) && mask == 0)
1336c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      {
1346c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 /* divide by w */
1356c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 float w = 1.0f / position[3];
1366c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1376c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 /* Viewport mapping */
1386c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 position[0] = position[0] * w * scale[0] + trans[0];
1396c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 position[1] = position[1] * w * scale[1] + trans[1];
1406c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 position[2] = position[2] * w * scale[2] + trans[2];
1416c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell	 position[3] = w;
1426c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      }
1436c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1446c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      if ((flags & DO_EDGEFLAG) && ef) {
1456c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         const float *edgeflag = out->data[ef];
1466c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         out->edgeflag = !(edgeflag[0] != 1.0f);
1476c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell         need_pipeline |= !out->edgeflag;
1486c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      }
1496c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1506c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell      out = (struct vertex_header *)( (char *)out + info->stride );
1516c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   }
1526c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1536c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell   return need_pipeline != 0;
1546c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell}
1556c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1566c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell
1576c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell#undef FLAGS
1586c0dc4bafbdbdc0cb4b6e5934fe064226dbd47ecKeith Whitwell#undef TAG
159