Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / drivers / i965simple / brw_clip_line.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "brw_defines.h"
33 #include "brw_context.h"
34 #include "brw_eu.h"
35 #include "brw_util.h"
36 #include "brw_clip.h"
37
38
39
40 static void brw_clip_line_alloc_regs( struct brw_clip_compile *c )
41 {
42 unsigned i = 0,j;
43
44 /* Register usage is static, precompute here:
45 */
46 c->reg.R0 = retype(brw_vec8_grf(i, 0), BRW_REGISTER_TYPE_UD); i++;
47
48 if (c->key.nr_userclip) {
49 c->reg.fixed_planes = brw_vec4_grf(i, 0);
50 i += (6 + c->key.nr_userclip + 1) / 2;
51
52 c->prog_data.curb_read_length = (6 + c->key.nr_userclip + 1) / 2;
53 }
54 else
55 c->prog_data.curb_read_length = 0;
56
57
58 /* Payload vertices plus space for more generated vertices:
59 */
60 for (j = 0; j < 4; j++) {
61 c->reg.vertex[j] = brw_vec4_grf(i, 0);
62 i += c->nr_regs;
63 }
64
65 c->reg.t = brw_vec1_grf(i, 0);
66 c->reg.t0 = brw_vec1_grf(i, 1);
67 c->reg.t1 = brw_vec1_grf(i, 2);
68 c->reg.planemask = retype(brw_vec1_grf(i, 3), BRW_REGISTER_TYPE_UD);
69 c->reg.plane_equation = brw_vec4_grf(i, 4);
70 i++;
71
72 c->reg.dp0 = brw_vec1_grf(i, 0); /* fixme - dp4 will clobber r.1,2,3 */
73 c->reg.dp1 = brw_vec1_grf(i, 4);
74 i++;
75
76 if (!c->key.nr_userclip) {
77 c->reg.fixed_planes = brw_vec8_grf(i, 0);
78 i++;
79 }
80
81
82 c->first_tmp = i;
83 c->last_tmp = i;
84
85 c->prog_data.urb_read_length = c->nr_regs; /* ? */
86 c->prog_data.total_grf = i;
87 }
88
89
90
91 /* Line clipping, more or less following the following algorithm:
92 *
93 * for (p=0;p<MAX_PLANES;p++) {
94 * if (clipmask & (1 << p)) {
95 * float dp0 = DOTPROD( vtx0, plane[p] );
96 * float dp1 = DOTPROD( vtx1, plane[p] );
97 *
98 * if (IS_NEGATIVE(dp1)) {
99 * float t = dp1 / (dp1 - dp0);
100 * if (t > t1) t1 = t;
101 * } else {
102 * float t = dp0 / (dp0 - dp1);
103 * if (t > t0) t0 = t;
104 * }
105 *
106 * if (t0 + t1 >= 1.0)
107 * return;
108 * }
109 * }
110 *
111 * interp( ctx, newvtx0, vtx0, vtx1, t0 );
112 * interp( ctx, newvtx1, vtx1, vtx0, t1 );
113 *
114 */
115 static void clip_and_emit_line( struct brw_clip_compile *c )
116 {
117 struct brw_compile *p = &c->func;
118 struct brw_indirect vtx0 = brw_indirect(0, 0);
119 struct brw_indirect vtx1 = brw_indirect(1, 0);
120 struct brw_indirect newvtx0 = brw_indirect(2, 0);
121 struct brw_indirect newvtx1 = brw_indirect(3, 0);
122 struct brw_indirect plane_ptr = brw_indirect(4, 0);
123 struct brw_instruction *plane_loop;
124 struct brw_instruction *plane_active;
125 struct brw_instruction *is_negative;
126 struct brw_instruction *is_neg2;
127 struct brw_instruction *not_culled;
128 struct brw_reg v1_null_ud = retype(vec1(brw_null_reg()), BRW_REGISTER_TYPE_UD);
129
130 brw_MOV(p, get_addr_reg(vtx0), brw_address(c->reg.vertex[0]));
131 brw_MOV(p, get_addr_reg(vtx1), brw_address(c->reg.vertex[1]));
132 brw_MOV(p, get_addr_reg(newvtx0), brw_address(c->reg.vertex[2]));
133 brw_MOV(p, get_addr_reg(newvtx1), brw_address(c->reg.vertex[3]));
134 brw_MOV(p, get_addr_reg(plane_ptr), brw_clip_plane0_address(c));
135
136 /* Note: init t0, t1 together:
137 */
138 brw_MOV(p, vec2(c->reg.t0), brw_imm_f(0));
139
140 brw_clip_init_planes(c);
141 brw_clip_init_clipmask(c);
142
143 /* -ve rhw workaround */
144 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
145 brw_AND(p, brw_null_reg(), get_element_ud(c->reg.R0, 2),
146 brw_imm_ud(1<<20));
147 brw_OR(p, c->reg.planemask, c->reg.planemask, brw_imm_ud(0x3f));
148 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
149
150 plane_loop = brw_DO(p, BRW_EXECUTE_1);
151 {
152 /* if (planemask & 1)
153 */
154 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
155 brw_AND(p, v1_null_ud, c->reg.planemask, brw_imm_ud(1));
156
157 plane_active = brw_IF(p, BRW_EXECUTE_1);
158 {
159 if (c->key.nr_userclip)
160 brw_MOV(p, c->reg.plane_equation, deref_4f(plane_ptr, 0));
161 else
162 brw_MOV(p, c->reg.plane_equation, deref_4b(plane_ptr, 0));
163
164 #if 0
165 /* dp = DP4(vtx->position, plane)
166 */
167 brw_DP4(p, vec4(c->reg.dp0), deref_4f(vtx0, c->offset[VERT_RESULT_HPOS]), c->reg.plane_equation);
168
169 /* if (IS_NEGATIVE(dp1))
170 */
171 brw_set_conditionalmod(p, BRW_CONDITIONAL_L);
172 brw_DP4(p, vec4(c->reg.dp1), deref_4f(vtx1, c->offset[VERT_RESULT_HPOS]), c->reg.plane_equation);
173 #else
174 #warning "disabled"
175 #endif
176 is_negative = brw_IF(p, BRW_EXECUTE_1);
177 {
178 brw_ADD(p, c->reg.t, c->reg.dp1, negate(c->reg.dp0));
179 brw_math_invert(p, c->reg.t, c->reg.t);
180 brw_MUL(p, c->reg.t, c->reg.t, c->reg.dp1);
181
182 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_G, c->reg.t, c->reg.t1 );
183 brw_MOV(p, c->reg.t1, c->reg.t);
184 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
185 }
186 is_negative = brw_ELSE(p, is_negative);
187 {
188 /* Coming back in. We know that both cannot be negative
189 * because the line would have been culled in that case.
190 */
191
192 /* If both are positive, do nothing */
193 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_L, c->reg.dp0, brw_imm_f(0.0));
194 is_neg2 = brw_IF(p, BRW_EXECUTE_1);
195 {
196 brw_ADD(p, c->reg.t, c->reg.dp0, negate(c->reg.dp1));
197 brw_math_invert(p, c->reg.t, c->reg.t);
198 brw_MUL(p, c->reg.t, c->reg.t, c->reg.dp0);
199
200 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_G, c->reg.t, c->reg.t0 );
201 brw_MOV(p, c->reg.t0, c->reg.t);
202 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
203 }
204 brw_ENDIF(p, is_neg2);
205 }
206 brw_ENDIF(p, is_negative);
207 }
208 brw_ENDIF(p, plane_active);
209
210 /* plane_ptr++;
211 */
212 brw_ADD(p, get_addr_reg(plane_ptr), get_addr_reg(plane_ptr), brw_clip_plane_stride(c));
213
214 /* while (planemask>>=1) != 0
215 */
216 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
217 brw_SHR(p, c->reg.planemask, c->reg.planemask, brw_imm_ud(1));
218 }
219 brw_WHILE(p, plane_loop);
220
221 brw_ADD(p, c->reg.t, c->reg.t0, c->reg.t1);
222 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_L, c->reg.t, brw_imm_f(1.0));
223 not_culled = brw_IF(p, BRW_EXECUTE_1);
224 {
225 brw_clip_interp_vertex(c, newvtx0, vtx0, vtx1, c->reg.t0, FALSE);
226 brw_clip_interp_vertex(c, newvtx1, vtx1, vtx0, c->reg.t1, FALSE);
227
228 brw_clip_emit_vue(c, newvtx0, 1, 0, (_3DPRIM_LINESTRIP << 2) | R02_PRIM_START);
229 brw_clip_emit_vue(c, newvtx1, 0, 1, (_3DPRIM_LINESTRIP << 2) | R02_PRIM_END);
230 }
231 brw_ENDIF(p, not_culled);
232 brw_clip_kill_thread(c);
233 }
234
235
236
237 void brw_emit_line_clip( struct brw_clip_compile *c )
238 {
239 brw_clip_line_alloc_regs(c);
240
241 if (c->key.do_flat_shading)
242 brw_clip_copy_colors(c, 0, 1);
243
244 clip_and_emit_line(c);
245 }