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