• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3  Intel funded Tungsten Graphics 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 <keithw@vmware.com>
30   */
31 
32 #include "main/macros.h"
33 #include "main/enums.h"
34 #include "program/program.h"
35 
36 #include "brw_clip.h"
37 #include "brw_prim.h"
38 
brw_clip_line_alloc_regs(struct brw_clip_compile * c)39 static void brw_clip_line_alloc_regs( struct brw_clip_compile *c )
40 {
41    const struct intel_device_info *devinfo = c->func.devinfo;
42    GLuint 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    c->reg.vertex_src_mask = retype(brw_vec1_grf(i, 0), BRW_REGISTER_TYPE_UD);
82    c->reg.clipdistance_offset = retype(brw_vec1_grf(i, 1), BRW_REGISTER_TYPE_W);
83    i++;
84 
85    if (devinfo->ver == 5) {
86       c->reg.ff_sync = retype(brw_vec1_grf(i, 0), BRW_REGISTER_TYPE_UD);
87       i++;
88    }
89 
90    c->first_tmp = i;
91    c->last_tmp = i;
92 
93    c->prog_data.urb_read_length = c->nr_regs; /* ? */
94    c->prog_data.total_grf = i;
95 }
96 
97 
98 /* Line clipping, more or less following the following algorithm:
99  *
100  *  for (p=0;p<MAX_PLANES;p++) {
101  *     if (clipmask & (1 << p)) {
102  *        GLfloat dp0 = DOTPROD( vtx0, plane[p] );
103  *        GLfloat dp1 = DOTPROD( vtx1, plane[p] );
104  *
105  *        if (dp1 < 0.0f) {
106  *           GLfloat t = dp1 / (dp1 - dp0);
107  *           if (t > t1) t1 = t;
108  *        } else {
109  *           GLfloat t = dp0 / (dp0 - dp1);
110  *           if (t > t0) t0 = t;
111  *        }
112  *
113  *        if (t0 + t1 >= 1.0)
114  *           return;
115  *     }
116  *  }
117  *
118  *  interp( ctx, newvtx0, vtx0, vtx1, t0 );
119  *  interp( ctx, newvtx1, vtx1, vtx0, t1 );
120  *
121  */
clip_and_emit_line(struct brw_clip_compile * c)122 static void clip_and_emit_line( struct brw_clip_compile *c )
123 {
124    struct brw_codegen *p = &c->func;
125    struct brw_indirect vtx0     = brw_indirect(0, 0);
126    struct brw_indirect vtx1      = brw_indirect(1, 0);
127    struct brw_indirect newvtx0   = brw_indirect(2, 0);
128    struct brw_indirect newvtx1   = brw_indirect(3, 0);
129    struct brw_indirect plane_ptr = brw_indirect(4, 0);
130    struct brw_reg v1_null_ud = retype(vec1(brw_null_reg()), BRW_REGISTER_TYPE_UD);
131    GLuint hpos_offset = brw_varying_to_offset(&c->vue_map, VARYING_SLOT_POS);
132    GLint clipdist0_offset = c->key.nr_userclip
133       ? brw_varying_to_offset(&c->vue_map, VARYING_SLOT_CLIP_DIST0)
134       : 0;
135 
136    brw_MOV(p, get_addr_reg(vtx0),      brw_address(c->reg.vertex[0]));
137    brw_MOV(p, get_addr_reg(vtx1),      brw_address(c->reg.vertex[1]));
138    brw_MOV(p, get_addr_reg(newvtx0),   brw_address(c->reg.vertex[2]));
139    brw_MOV(p, get_addr_reg(newvtx1),   brw_address(c->reg.vertex[3]));
140    brw_MOV(p, get_addr_reg(plane_ptr), brw_clip_plane0_address(c));
141 
142    /* Note: init t0, t1 together:
143     */
144    brw_MOV(p, vec2(c->reg.t0), brw_imm_f(0));
145 
146    brw_clip_init_planes(c);
147    brw_clip_init_clipmask(c);
148 
149    /* -ve rhw workaround */
150    if (p->devinfo->has_negative_rhw_bug) {
151       brw_AND(p, brw_null_reg(), get_element_ud(c->reg.R0, 2),
152               brw_imm_ud(1<<20));
153       brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);
154       brw_OR(p, c->reg.planemask, c->reg.planemask, brw_imm_ud(0x3f));
155       brw_inst_set_pred_control(p->devinfo, brw_last_inst, BRW_PREDICATE_NORMAL);
156    }
157 
158    /* Set the initial vertex source mask: The first 6 planes are the bounds
159     * of the view volume; the next 8 planes are the user clipping planes.
160     */
161    brw_MOV(p, c->reg.vertex_src_mask, brw_imm_ud(0x3fc0));
162 
163    /* Set the initial clipdistance offset to be 6 floats before gl_ClipDistance[0].
164     * We'll increment 6 times before we start hitting actual user clipping. */
165    brw_MOV(p, c->reg.clipdistance_offset, brw_imm_d(clipdist0_offset - 6*sizeof(float)));
166 
167    brw_DO(p, BRW_EXECUTE_1);
168    {
169       /* if (planemask & 1)
170        */
171       brw_AND(p, v1_null_ud, c->reg.planemask, brw_imm_ud(1));
172       brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);
173 
174       brw_IF(p, BRW_EXECUTE_1);
175       {
176          brw_AND(p, v1_null_ud, c->reg.vertex_src_mask, brw_imm_ud(1));
177          brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);
178          brw_IF(p, BRW_EXECUTE_1);
179          {
180             /* user clip distance: just fetch the correct float from each vertex */
181             struct brw_indirect temp_ptr = brw_indirect(7, 0);
182             brw_ADD(p, get_addr_reg(temp_ptr), get_addr_reg(vtx0), c->reg.clipdistance_offset);
183             brw_MOV(p, c->reg.dp0, deref_1f(temp_ptr, 0));
184             brw_ADD(p, get_addr_reg(temp_ptr), get_addr_reg(vtx1), c->reg.clipdistance_offset);
185             brw_MOV(p, c->reg.dp1, deref_1f(temp_ptr, 0));
186          }
187          brw_ELSE(p);
188          {
189             /* fixed plane: fetch the hpos, dp4 against the plane. */
190             if (c->key.nr_userclip)
191                brw_MOV(p, c->reg.plane_equation, deref_4f(plane_ptr, 0));
192             else
193                brw_MOV(p, c->reg.plane_equation, deref_4b(plane_ptr, 0));
194 
195             brw_DP4(p, vec4(c->reg.dp0), deref_4f(vtx0, hpos_offset), c->reg.plane_equation);
196             brw_DP4(p, vec4(c->reg.dp1), deref_4f(vtx1, hpos_offset), c->reg.plane_equation);
197          }
198          brw_ENDIF(p);
199 
200          brw_CMP(p, brw_null_reg(), BRW_CONDITIONAL_L, vec1(c->reg.dp1), brw_imm_f(0.0f));
201 
202          brw_IF(p, BRW_EXECUTE_1);
203          {
204              /*
205               * Both can be negative on GM965/G965 due to RHW workaround
206               * if so, this object should be rejected.
207               */
208              if (p->devinfo->has_negative_rhw_bug) {
209                  brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_LE, c->reg.dp0, brw_imm_f(0.0));
210                  brw_IF(p, BRW_EXECUTE_1);
211                  {
212                      brw_clip_kill_thread(c);
213                  }
214                  brw_ENDIF(p);
215              }
216 
217              brw_ADD(p, c->reg.t, c->reg.dp1, negate(c->reg.dp0));
218              brw_math_invert(p, c->reg.t, c->reg.t);
219              brw_MUL(p, c->reg.t, c->reg.t, c->reg.dp1);
220 
221              brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_G, c->reg.t, c->reg.t1 );
222              brw_MOV(p, c->reg.t1, c->reg.t);
223              brw_inst_set_pred_control(p->devinfo, brw_last_inst,
224                                        BRW_PREDICATE_NORMAL);
225 	 }
226 	 brw_ELSE(p);
227 	 {
228              /* Coming back in.  We know that both cannot be negative
229               * because the line would have been culled in that case.
230               */
231 
232              /* If both are positive, do nothing */
233              /* Only on GM965/G965 */
234              if (p->devinfo->has_negative_rhw_bug) {
235                  brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_L, c->reg.dp0, brw_imm_f(0.0));
236                  brw_IF(p, BRW_EXECUTE_1);
237              }
238 
239              {
240                  brw_ADD(p, c->reg.t, c->reg.dp0, negate(c->reg.dp1));
241                  brw_math_invert(p, c->reg.t, c->reg.t);
242                  brw_MUL(p, c->reg.t, c->reg.t, c->reg.dp0);
243 
244                  brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_G, c->reg.t, c->reg.t0 );
245                  brw_MOV(p, c->reg.t0, c->reg.t);
246                  brw_inst_set_pred_control(p->devinfo, brw_last_inst,
247                                            BRW_PREDICATE_NORMAL);
248              }
249 
250              if (p->devinfo->has_negative_rhw_bug) {
251                  brw_ENDIF(p);
252              }
253          }
254 	 brw_ENDIF(p);
255       }
256       brw_ENDIF(p);
257 
258       /* plane_ptr++;
259        */
260       brw_ADD(p, get_addr_reg(plane_ptr), get_addr_reg(plane_ptr), brw_clip_plane_stride(c));
261 
262       /* while (planemask>>=1) != 0
263        */
264       brw_SHR(p, c->reg.planemask, c->reg.planemask, brw_imm_ud(1));
265       brw_inst_set_cond_modifier(p->devinfo, brw_last_inst, BRW_CONDITIONAL_NZ);
266       brw_SHR(p, c->reg.vertex_src_mask, c->reg.vertex_src_mask, brw_imm_ud(1));
267       brw_inst_set_pred_control(p->devinfo, brw_last_inst, BRW_PREDICATE_NORMAL);
268       brw_ADD(p, c->reg.clipdistance_offset, c->reg.clipdistance_offset, brw_imm_w(sizeof(float)));
269       brw_inst_set_pred_control(p->devinfo, brw_last_inst, BRW_PREDICATE_NORMAL);
270    }
271    brw_WHILE(p);
272    brw_inst_set_pred_control(p->devinfo, brw_last_inst, BRW_PREDICATE_NORMAL);
273 
274    brw_ADD(p, c->reg.t, c->reg.t0, c->reg.t1);
275    brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_L, c->reg.t, brw_imm_f(1.0));
276    brw_IF(p, BRW_EXECUTE_1);
277    {
278       brw_clip_interp_vertex(c, newvtx0, vtx0, vtx1, c->reg.t0, false);
279       brw_clip_interp_vertex(c, newvtx1, vtx1, vtx0, c->reg.t1, false);
280 
281       brw_clip_emit_vue(c, newvtx0, BRW_URB_WRITE_ALLOCATE_COMPLETE,
282                         (_3DPRIM_LINESTRIP << URB_WRITE_PRIM_TYPE_SHIFT)
283                         | URB_WRITE_PRIM_START);
284       brw_clip_emit_vue(c, newvtx1, BRW_URB_WRITE_EOT_COMPLETE,
285                         (_3DPRIM_LINESTRIP << URB_WRITE_PRIM_TYPE_SHIFT)
286                         | URB_WRITE_PRIM_END);
287    }
288    brw_ENDIF(p);
289    brw_clip_kill_thread(c);
290 }
291 
292 
293 
brw_emit_line_clip(struct brw_clip_compile * c)294 void brw_emit_line_clip( struct brw_clip_compile *c )
295 {
296    brw_clip_line_alloc_regs(c);
297    brw_clip_init_ff_sync(c);
298 
299    if (c->key.contains_flat_varying) {
300       if (c->key.pv_first)
301          brw_clip_copy_flatshaded_attributes(c, 1, 0);
302       else
303          brw_clip_copy_flatshaded_attributes(c, 0, 1);
304    }
305 
306    clip_and_emit_line(c);
307 }
308