• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2010, 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 #include "util/u_bitcast.h"
29 
TAG(do_cliptest)30 static boolean TAG(do_cliptest)( struct pt_post_vs *pvs,
31                                  struct draw_vertex_info *info,
32                                  const struct draw_prim_info *prim_info )
33 {
34    struct vertex_header *out = info->verts;
35    /* const */ float (*plane)[4] = pvs->draw->plane;
36    const unsigned pos = draw_current_shader_position_output(pvs->draw);
37    const unsigned cv = draw_current_shader_clipvertex_output(pvs->draw);
38    unsigned cd[2];
39    const unsigned ef = pvs->draw->vs.edgeflag_output;
40    unsigned ucp_enable = pvs->draw->rasterizer->clip_plane_enable;
41    unsigned flags = (FLAGS);
42    unsigned need_pipeline = 0;
43    unsigned j;
44    unsigned i;
45    bool have_cd = false;
46    bool uses_vp_idx = draw_current_shader_uses_viewport_index(pvs->draw);
47    unsigned viewport_index_output =
48       draw_current_shader_viewport_index_output(pvs->draw);
49    int viewport_index = 0;
50    int num_written_clipdistance =
51       draw_current_shader_num_written_clipdistances(pvs->draw);
52 
53    if (uses_vp_idx) {
54       viewport_index = u_bitcast_f2u(out->data[viewport_index_output][0]);
55       viewport_index = draw_clamp_viewport_idx(viewport_index);
56    }
57 
58    cd[0] = draw_current_shader_ccdistance_output(pvs->draw, 0);
59    cd[1] = draw_current_shader_ccdistance_output(pvs->draw, 1);
60 
61    if (cd[0] != pos || cd[1] != pos)
62       have_cd = true;
63 
64    /* If clipdistance semantic has been written by the shader
65     * that means we're expected to do 'user plane clipping' */
66    if (num_written_clipdistance && !(flags & DO_CLIP_USER)) {
67       flags |= DO_CLIP_USER;
68       ucp_enable = (1 << num_written_clipdistance) - 1;
69    }
70 
71    assert(pos != -1);
72    unsigned prim_idx = 0, prim_vert_idx = 0;
73    for (j = 0; j < info->count; j++) {
74       float *position = out->data[pos];
75       unsigned mask = 0x0;
76 
77       if (uses_vp_idx) {
78          /* only change the viewport_index for the leading vertex */
79          if (prim_vert_idx == (prim_info->primitive_lengths[prim_idx])) {
80             prim_idx++;
81             prim_vert_idx = 0;
82             viewport_index = u_bitcast_f2u(out->data[viewport_index_output][0]);
83             viewport_index = draw_clamp_viewport_idx(viewport_index);
84          }
85          prim_vert_idx++;
86       }
87       float *scale = pvs->draw->viewports[viewport_index].scale;
88       float *trans = pvs->draw->viewports[viewport_index].translate;
89       initialize_vertex_header(out);
90 
91       if (flags & (DO_CLIP_XY | DO_CLIP_XY_GUARD_BAND |
92                    DO_CLIP_FULL_Z | DO_CLIP_HALF_Z | DO_CLIP_USER)) {
93          float *clipvertex = position;
94 
95          if ((flags & DO_CLIP_USER) && cv != pos) {
96             assert(cv != -1);
97             clipvertex = out->data[cv];
98          }
99 
100          for (i = 0; i < 4; i++) {
101             out->clip_pos[i] = position[i];
102          }
103 
104          /* Be careful with NaNs. Comparisons must be true for them. */
105          /* Do the hardwired planes first:
106           */
107          if (flags & DO_CLIP_XY_GUARD_BAND) {
108             if (!(-0.50 * position[0] + position[3] >= 0)) mask |= (1<<0);
109             if (!( 0.50 * position[0] + position[3] >= 0)) mask |= (1<<1);
110             if (!(-0.50 * position[1] + position[3] >= 0)) mask |= (1<<2);
111             if (!( 0.50 * position[1] + position[3] >= 0)) mask |= (1<<3);
112          }
113          else if (flags & DO_CLIP_XY) {
114             if (!(-position[0] + position[3] >= 0)) mask |= (1<<0);
115             if (!( position[0] + position[3] >= 0)) mask |= (1<<1);
116             if (!(-position[1] + position[3] >= 0)) mask |= (1<<2);
117             if (!( position[1] + position[3] >= 0)) mask |= (1<<3);
118          }
119 
120          /* Clip Z planes according to full cube, half cube or none.
121           */
122          if (flags & DO_CLIP_FULL_Z) {
123             if (!( position[2] + position[3] >= 0)) mask |= (1<<4);
124             if (!(-position[2] + position[3] >= 0)) mask |= (1<<5);
125          }
126          else if (flags & DO_CLIP_HALF_Z) {
127             if (!( position[2]               >= 0)) mask |= (1<<4);
128             if (!(-position[2] + position[3] >= 0)) mask |= (1<<5);
129          }
130 
131          if (flags & DO_CLIP_USER) {
132             unsigned ucp_mask = ucp_enable;
133 
134             while (ucp_mask) {
135                unsigned plane_idx = ffs(ucp_mask)-1;
136                ucp_mask &= ~(1 << plane_idx);
137                plane_idx += 6;
138 
139                /*
140                 * for user clipping check if we have a clip distance output
141                 * and the shader has written to it, otherwise use clipvertex
142                 * to decide when the plane is clipping.
143                 */
144                if (have_cd && num_written_clipdistance) {
145                   float clipdist;
146                   i = plane_idx - 6;
147                   /* first four clip distance in first vector etc. */
148                   if (i < 4)
149                      clipdist = out->data[cd[0]][i];
150                   else
151                      clipdist = out->data[cd[1]][i-4];
152                   if (clipdist < 0 || util_is_inf_or_nan(clipdist))
153                      mask |= 1 << plane_idx;
154                } else {
155                   if (!(dot4(clipvertex, plane[plane_idx]) >= 0))
156                      mask |= 1 << plane_idx;
157                }
158             }
159          }
160 
161          out->clipmask = mask;
162          need_pipeline |= out->clipmask;
163       }
164 
165       /*
166        * Transform the vertex position from clip coords to window coords,
167        * if the vertex is unclipped.
168        */
169       if ((flags & DO_VIEWPORT) && mask == 0)
170       {
171 	 /* divide by w */
172 	 float w = 1.0f / position[3];
173 
174 	 /* Viewport mapping */
175 	 position[0] = position[0] * w * scale[0] + trans[0];
176 	 position[1] = position[1] * w * scale[1] + trans[1];
177 	 position[2] = position[2] * w * scale[2] + trans[2];
178 	 position[3] = w;
179       }
180 #ifdef DEBUG
181       /* For debug builds, set the clipped vertex's window coordinate
182        * to NaN to help catch potential errors later.
183        */
184       else {
185          float zero = 0.0f;
186          position[0] =
187          position[1] =
188          position[2] =
189          position[3] = zero / zero; /* MSVC doesn't accept 0.0 / 0.0 */
190       }
191 #endif
192 
193       if ((flags & DO_EDGEFLAG) && ef) {
194          const float *edgeflag = out->data[ef];
195          out->edgeflag = !(edgeflag[0] != 1.0f);
196          need_pipeline |= !out->edgeflag;
197       }
198 
199       out = (struct vertex_header *)( (char *)out + info->stride );
200    }
201    return need_pipeline != 0;
202 }
203 
204 
205 #undef FLAGS
206 #undef TAG
207