draw/softpipe: add clip vertex support. (v2)
[mesa.git] / src / gallium / auxiliary / draw / draw_cliptest_tmp.h
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
29
30 static boolean TAG(do_cliptest)( struct pt_post_vs *pvs,
31 struct draw_vertex_info *info )
32 {
33 struct vertex_header *out = info->verts;
34 const float *scale = pvs->draw->viewport.scale;
35 const float *trans = pvs->draw->viewport.translate;
36 /* const */ float (*plane)[4] = pvs->draw->plane;
37 const unsigned pos = draw_current_shader_position_output(pvs->draw);
38 const unsigned cv = draw_current_shader_clipvertex_output(pvs->draw);
39 const unsigned ef = pvs->draw->vs.edgeflag_output;
40 const unsigned ucp_enable = pvs->draw->rasterizer->clip_plane_enable;
41 const unsigned flags = (FLAGS);
42 unsigned need_pipeline = 0;
43 unsigned j;
44 unsigned i;
45
46 for (j = 0; j < info->count; j++) {
47 float *position = out->data[pos];
48 unsigned mask = 0x0;
49
50 initialize_vertex_header(out);
51
52 if (flags & (DO_CLIP_XY | DO_CLIP_XY_GUARD_BAND |
53 DO_CLIP_FULL_Z | DO_CLIP_HALF_Z | DO_CLIP_USER)) {
54 float *clipvertex = position;
55
56 if ((flags & DO_CLIP_USER) && cv != pos)
57 clipvertex = out->data[cv];
58
59 for (i = 0; i < 4; i++) {
60 out->clip[i] = clipvertex[i];
61 out->pre_clip_pos[i] = position[i];
62 }
63
64 /* Do the hardwired planes first:
65 */
66 if (flags & DO_CLIP_XY_GUARD_BAND) {
67 if (-0.50 * position[0] + position[3] < 0) mask |= (1<<0);
68 if ( 0.50 * position[0] + position[3] < 0) mask |= (1<<1);
69 if (-0.50 * position[1] + position[3] < 0) mask |= (1<<2);
70 if ( 0.50 * position[1] + position[3] < 0) mask |= (1<<3);
71 }
72 else if (flags & DO_CLIP_XY) {
73 if (-position[0] + position[3] < 0) mask |= (1<<0);
74 if ( position[0] + position[3] < 0) mask |= (1<<1);
75 if (-position[1] + position[3] < 0) mask |= (1<<2);
76 if ( position[1] + position[3] < 0) mask |= (1<<3);
77 }
78
79 /* Clip Z planes according to full cube, half cube or none.
80 */
81 if (flags & DO_CLIP_FULL_Z) {
82 if ( position[2] + position[3] < 0) mask |= (1<<4);
83 if (-position[2] + position[3] < 0) mask |= (1<<5);
84 }
85 else if (flags & DO_CLIP_HALF_Z) {
86 if ( position[2] < 0) mask |= (1<<4);
87 if (-position[2] + position[3] < 0) mask |= (1<<5);
88 }
89
90 if (flags & DO_CLIP_USER) {
91 unsigned ucp_mask = ucp_enable;
92
93 while (ucp_mask) {
94 unsigned plane_idx = ffs(ucp_mask)-1;
95 ucp_mask &= ~(1 << plane_idx);
96 plane_idx += 6;
97
98 if (dot4(clipvertex, plane[plane_idx]) < 0) {
99 mask |= 1 << plane_idx;
100 }
101 }
102 }
103
104 out->clipmask = mask;
105 need_pipeline |= out->clipmask;
106 }
107
108 if ((flags & DO_VIEWPORT) && mask == 0)
109 {
110 /* divide by w */
111 float w = 1.0f / position[3];
112
113 /* Viewport mapping */
114 position[0] = position[0] * w * scale[0] + trans[0];
115 position[1] = position[1] * w * scale[1] + trans[1];
116 position[2] = position[2] * w * scale[2] + trans[2];
117 position[3] = w;
118 }
119
120 if ((flags & DO_EDGEFLAG) && ef) {
121 const float *edgeflag = out->data[ef];
122 out->edgeflag = !(edgeflag[0] != 1.0f);
123 need_pipeline |= !out->edgeflag;
124 }
125
126 out = (struct vertex_header *)( (char *)out + info->stride );
127 }
128
129 return need_pipeline != 0;
130 }
131
132
133 #undef FLAGS
134 #undef TAG