1 /**************************************************************************
3 * Copyright 2010, 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 **************************************************************************/
28 static boolean TAG(do_cliptest)( struct pt_post_vs *pvs,
29 struct draw_vertex_info *info,
30 const struct draw_prim_info *prim_info )
32 struct vertex_header *out = info->verts;
33 /* const */ float (*plane)[4] = pvs->draw->plane;
34 const unsigned pos = draw_current_shader_position_output(pvs->draw);
35 const unsigned cv = draw_current_shader_clipvertex_output(pvs->draw);
37 const unsigned ef = pvs->draw->vs.edgeflag_output;
38 const unsigned ucp_enable = pvs->draw->rasterizer->clip_plane_enable;
39 const unsigned flags = (FLAGS);
40 unsigned need_pipeline = 0;
44 unsigned viewport_index_output =
45 draw_current_shader_viewport_index_output(pvs->draw);
47 draw_current_shader_uses_viewport_index(pvs->draw) ?
48 *((unsigned*)out->data[viewport_index_output]): 0;
50 cd[0] = draw_current_shader_clipdistance_output(pvs->draw, 0);
51 cd[1] = draw_current_shader_clipdistance_output(pvs->draw, 1);
53 if (cd[0] != pos || cd[1] != pos)
56 for (j = 0; j < info->count; j++) {
57 float *position = out->data[pos];
59 float *scale = pvs->draw->viewports[0].scale;
60 float *trans = pvs->draw->viewports[0].translate;
61 if (draw_current_shader_uses_viewport_index(pvs->draw)) {
62 unsigned verts_per_prim = u_vertices_per_prim(prim_info->prim);
63 /* only change the viewport_index for the leading vertex */
64 if (!(j % verts_per_prim)) {
65 viewport_index = *((unsigned*)out->data[viewport_index_output]);
66 viewport_index = draw_clamp_viewport_idx(viewport_index);
68 scale = pvs->draw->viewports[viewport_index].scale;
69 trans = pvs->draw->viewports[viewport_index].translate;
72 initialize_vertex_header(out);
74 if (flags & (DO_CLIP_XY | DO_CLIP_XY_GUARD_BAND |
75 DO_CLIP_FULL_Z | DO_CLIP_HALF_Z | DO_CLIP_USER)) {
76 float *clipvertex = position;
78 if ((flags & DO_CLIP_USER) && cv != pos)
79 clipvertex = out->data[cv];
81 for (i = 0; i < 4; i++) {
82 out->clip[i] = clipvertex[i];
83 out->pre_clip_pos[i] = position[i];
86 /* Do the hardwired planes first:
88 if (flags & DO_CLIP_XY_GUARD_BAND) {
89 if (-0.50 * position[0] + position[3] < 0) mask |= (1<<0);
90 if ( 0.50 * position[0] + position[3] < 0) mask |= (1<<1);
91 if (-0.50 * position[1] + position[3] < 0) mask |= (1<<2);
92 if ( 0.50 * position[1] + position[3] < 0) mask |= (1<<3);
94 else if (flags & DO_CLIP_XY) {
95 if (-position[0] + position[3] < 0) mask |= (1<<0);
96 if ( position[0] + position[3] < 0) mask |= (1<<1);
97 if (-position[1] + position[3] < 0) mask |= (1<<2);
98 if ( position[1] + position[3] < 0) mask |= (1<<3);
101 /* Clip Z planes according to full cube, half cube or none.
103 if (flags & DO_CLIP_FULL_Z) {
104 if ( position[2] + position[3] < 0) mask |= (1<<4);
105 if (-position[2] + position[3] < 0) mask |= (1<<5);
107 else if (flags & DO_CLIP_HALF_Z) {
108 if ( position[2] < 0) mask |= (1<<4);
109 if (-position[2] + position[3] < 0) mask |= (1<<5);
112 if (flags & DO_CLIP_USER) {
113 unsigned ucp_mask = ucp_enable;
114 int num_written_clipdistance = pvs->draw->vs.vertex_shader->info.num_written_clipdistance;
116 unsigned plane_idx = ffs(ucp_mask)-1;
117 ucp_mask &= ~(1 << plane_idx);
121 * for user clipping check if we have a clip distance output
122 * and the shader has written to it, otherwise use clipvertex
123 * to decide when the plane is clipping.
125 if (have_cd && num_written_clipdistance) {
128 out->have_clipdist = 1;
129 /* first four clip distance in first vector etc. */
131 clipdist = out->data[cd[0]][i];
133 clipdist = out->data[cd[1]][i-4];
135 mask |= 1 << plane_idx;
137 if (dot4(clipvertex, plane[plane_idx]) < 0)
138 mask |= 1 << plane_idx;
143 out->clipmask = mask;
144 need_pipeline |= out->clipmask;
148 * Transform the vertex position from clip coords to window coords,
149 * if the vertex is unclipped.
151 if ((flags & DO_VIEWPORT) && mask == 0)
154 float w = 1.0f / position[3];
156 /* Viewport mapping */
157 position[0] = position[0] * w * scale[0] + trans[0];
158 position[1] = position[1] * w * scale[1] + trans[1];
159 position[2] = position[2] * w * scale[2] + trans[2];
163 /* For debug builds, set the clipped vertex's window coordinate
164 * to NaN to help catch potential errors later.
171 position[3] = zero / zero; /* MSVC doesn't accept 0.0 / 0.0 */
175 if ((flags & DO_EDGEFLAG) && ef) {
176 const float *edgeflag = out->data[ef];
177 out->edgeflag = !(edgeflag[0] != 1.0f);
178 need_pipeline |= !out->edgeflag;
181 out = (struct vertex_header *)( (char *)out + info->stride );
184 return need_pipeline != 0;