Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / i965 / brw_clip_util.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
33 #include "brw_defines.h"
34 #include "brw_context.h"
35 #include "brw_eu.h"
36 #include "brw_clip.h"
37
38
39
40
41 struct brw_reg get_tmp( struct brw_clip_compile *c )
42 {
43    struct brw_reg tmp = brw_vec4_grf(c->last_tmp, 0);
44
45    if (++c->last_tmp > c->prog_data.total_grf)
46       c->prog_data.total_grf = c->last_tmp;
47
48    return tmp;
49 }
50
51 static void release_tmp( struct brw_clip_compile *c, struct brw_reg tmp )
52 {
53    if (tmp.nr == c->last_tmp-1)
54       c->last_tmp--;
55 }
56
57
58 static struct brw_reg make_plane_ud(GLuint x, GLuint y, GLuint z, GLuint w)
59 {
60    return brw_imm_ud((w<<24) | (z<<16) | (y<<8) | x);
61 }
62
63
64 void brw_clip_init_planes( struct brw_clip_compile *c )
65 {
66    struct brw_compile *p = &c->func;
67
68    if (!c->key.nr_userclip) {
69       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 0), make_plane_ud( 0,    0, 0xff, 1));
70       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 1), make_plane_ud( 0,    0,    1, 1));
71       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 2), make_plane_ud( 0, 0xff,    0, 1));
72       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 3), make_plane_ud( 0,    1,    0, 1));
73       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 4), make_plane_ud(0xff,  0,    0, 1));
74       brw_MOV(p, get_element_ud(c->reg.fixed_planes, 5), make_plane_ud( 1,    0,    0, 1));
75    }
76 }
77
78
79
80 #define W 3
81
82 /* Project 'pos' to screen space (or back again), overwrite with results:
83  */
84 void brw_clip_project_position(struct brw_clip_compile *c, struct brw_reg pos )
85 {
86    struct brw_compile *p = &c->func;
87
88    /* calc rhw 
89     */
90    brw_math_invert(p, get_element(pos, W), get_element(pos, W));
91
92    /* value.xyz *= value.rhw
93     */
94    brw_set_access_mode(p, BRW_ALIGN_16);
95    brw_MUL(p, brw_writemask(pos, BRW_WRITEMASK_XYZ), pos, brw_swizzle1(pos, W));
96    brw_set_access_mode(p, BRW_ALIGN_1);
97 }
98
99
100 static void brw_clip_project_vertex( struct brw_clip_compile *c, 
101                                      struct brw_indirect vert_addr )
102 {
103    struct brw_compile *p = &c->func;
104    struct brw_reg tmp = get_tmp(c);
105
106    /* Fixup position.  Extract from the original vertex and re-project
107     * to screen space:
108     */
109    brw_MOV(p, tmp, deref_4f(vert_addr, c->offset_hpos));
110    brw_clip_project_position(c, tmp);
111    brw_MOV(p, deref_4f(vert_addr, c->header_position_offset), tmp);
112          
113    release_tmp(c, tmp);
114 }
115
116
117
118
119 /* Interpolate between two vertices and put the result into a0.0.  
120  * Increment a0.0 accordingly.
121  */
122 void brw_clip_interp_vertex( struct brw_clip_compile *c,
123                              struct brw_indirect dest_ptr,
124                              struct brw_indirect v0_ptr, /* from */
125                              struct brw_indirect v1_ptr, /* to */
126                              struct brw_reg t0,
127                              GLboolean force_edgeflag)
128 {
129    struct brw_compile *p = &c->func;
130    struct brw_context *brw = p->brw;
131    struct brw_reg tmp = get_tmp(c);
132    GLuint i;
133
134    /* Just copy the vertex header:
135     */
136    /*
137     * After CLIP stage, only first 256 bits of the VUE are read
138     * back on IGDNG, so needn't change it
139     */
140    brw_copy_indirect_to_indirect(p, dest_ptr, v0_ptr, 1);
141       
142    /* Iterate over each attribute (could be done in pairs?)
143     */
144    for (i = 0; i < c->key.nr_attrs; i++) {
145       GLuint delta = i*16 + 32;
146
147       if (brw->gen == 5)
148           delta = i * 16 + 32 * 3;
149
150       if (delta == c->offset_edgeflag) {
151          if (force_edgeflag) 
152             brw_MOV(p, deref_4f(dest_ptr, delta), brw_imm_f(1));
153          else
154             brw_MOV(p, deref_4f(dest_ptr, delta), deref_4f(v0_ptr, delta));
155       }
156       else {
157          /* Interpolate: 
158           *
159           *        New = attr0 + t*attr1 - t*attr0
160           */
161          brw_MUL(p, 
162                  vec4(brw_null_reg()),
163                  deref_4f(v1_ptr, delta),
164                  t0);
165
166          brw_MAC(p, 
167                  tmp,         
168                  negate(deref_4f(v0_ptr, delta)),
169                  t0); 
170               
171          brw_ADD(p,
172                  deref_4f(dest_ptr, delta), 
173                  deref_4f(v0_ptr, delta),
174                  tmp);
175       }
176    }
177
178    if (i & 1) {
179       GLuint delta = i*16 + 32;
180
181       if (brw->gen == 5)
182           delta = i * 16 + 32 * 3;
183
184       brw_MOV(p, deref_4f(dest_ptr, delta), brw_imm_f(0));
185    }
186
187    release_tmp(c, tmp);
188
189    /* Recreate the projected (NDC) coordinate in the new vertex
190     * header:
191     */
192    brw_clip_project_vertex(c, dest_ptr );
193 }
194
195
196
197
198 #define MAX_MRF 16
199
200 void brw_clip_emit_vue(struct brw_clip_compile *c, 
201                        struct brw_indirect vert,
202                        GLboolean allocate,
203                        GLboolean eot,
204                        GLuint header)
205 {
206    struct brw_compile *p = &c->func;
207    GLuint start = c->last_mrf;
208
209    brw_clip_ff_sync(c);
210
211    assert(!(allocate && eot));
212    
213    /* Cycle through mrf regs - probably futile as we have to wait for
214     * the allocation response anyway.  Also, the order this function
215     * is invoked doesn't correspond to the order the instructions will
216     * be executed, so it won't have any effect in many cases.
217     */
218 #if 0
219    if (start + c->nr_regs + 1 >= MAX_MRF)
220       start = 0;
221
222    c->last_mrf = start + c->nr_regs + 1;
223 #endif
224         
225    /* Copy the vertex from vertn into m1..mN+1:
226     */
227    brw_copy_from_indirect(p, brw_message_reg(start+1), vert, c->nr_regs);
228
229    /* Overwrite PrimType and PrimStart in the message header, for
230     * each vertex in turn:
231     */
232    brw_MOV(p, get_element_ud(c->reg.R0, 2), brw_imm_ud(header));
233
234
235    /* Send each vertex as a seperate write to the urb.  This
236     * is different to the concept in brw_sf_emit.c, where
237     * subsequent writes are used to build up a single urb
238     * entry.  Each of these writes instantiates a seperate
239     * urb entry - (I think... what about 'allocate'?)
240     */
241    brw_urb_WRITE(p, 
242                  allocate ? c->reg.R0 : retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
243                  start,
244                  c->reg.R0,
245                  allocate,
246                  1,             /* used */
247                  c->nr_regs + 1, /* msg length */
248                  allocate ? 1 : 0, /* response_length */ 
249                  eot,           /* eot */
250                  1,             /* writes_complete */
251                  0,             /* urb offset */
252                  BRW_URB_SWIZZLE_NONE);
253 }
254
255
256
257 void brw_clip_kill_thread(struct brw_clip_compile *c)
258 {
259    struct brw_compile *p = &c->func;
260
261    brw_clip_ff_sync(c);
262    /* Send an empty message to kill the thread and release any
263     * allocated urb entry:
264     */
265    brw_urb_WRITE(p, 
266                  retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
267                  0,
268                  c->reg.R0,
269                  0,             /* allocate */
270                  0,             /* used */
271                  1,             /* msg len */
272                  0,             /* response len */
273                  1,             /* eot */
274                  1,             /* writes complete */
275                  0,
276                  BRW_URB_SWIZZLE_NONE);
277 }
278
279
280
281
282 struct brw_reg brw_clip_plane0_address( struct brw_clip_compile *c )
283 {
284    return brw_address(c->reg.fixed_planes);
285 }
286
287
288 struct brw_reg brw_clip_plane_stride( struct brw_clip_compile *c )
289 {
290    if (c->key.nr_userclip) {
291       return brw_imm_uw(16);
292    }
293    else {
294       return brw_imm_uw(4);
295    }
296 }
297
298
299 /* If flatshading, distribute color from provoking vertex prior to
300  * clipping.
301  */
302 void brw_clip_copy_colors( struct brw_clip_compile *c,
303                            GLuint to, GLuint from )
304 {
305    struct brw_compile *p = &c->func;
306
307    if (c->offset_color0)
308       brw_MOV(p, 
309               byte_offset(c->reg.vertex[to], c->offset_color0),
310               byte_offset(c->reg.vertex[from], c->offset_color0));
311
312    if (c->offset_color1)
313       brw_MOV(p, 
314               byte_offset(c->reg.vertex[to], c->offset_color1),
315               byte_offset(c->reg.vertex[from], c->offset_color1));
316
317    if (c->offset_bfc0)
318       brw_MOV(p, 
319               byte_offset(c->reg.vertex[to], c->offset_bfc0),
320               byte_offset(c->reg.vertex[from], c->offset_bfc0));
321
322    if (c->offset_bfc1)
323       brw_MOV(p, 
324               byte_offset(c->reg.vertex[to], c->offset_bfc1),
325               byte_offset(c->reg.vertex[from], c->offset_bfc1));
326 }
327
328
329
330 void brw_clip_init_clipmask( struct brw_clip_compile *c )
331 {
332    struct brw_compile *p = &c->func;
333    struct brw_reg incoming = get_element_ud(c->reg.R0, 2);
334    
335    /* Shift so that lowest outcode bit is rightmost: 
336     */
337    brw_SHR(p, c->reg.planemask, incoming, brw_imm_ud(26));
338
339    if (c->key.nr_userclip) {
340       struct brw_reg tmp = retype(vec1(get_tmp(c)), BRW_REGISTER_TYPE_UD);
341
342       /* Rearrange userclip outcodes so that they come directly after
343        * the fixed plane bits.
344        */
345       brw_AND(p, tmp, incoming, brw_imm_ud(0x3f<<14));
346       brw_SHR(p, tmp, tmp, brw_imm_ud(8));
347       brw_OR(p, c->reg.planemask, c->reg.planemask, tmp);
348       
349       release_tmp(c, tmp);
350    }
351 }
352
353 void brw_clip_ff_sync(struct brw_clip_compile *c)
354 {
355     struct brw_context *brw = c->func.brw;
356     if (brw->needs_ff_sync) {
357         struct brw_compile *p = &c->func;
358         struct brw_instruction *need_ff_sync;
359
360         brw_set_conditionalmod(p, BRW_CONDITIONAL_Z);
361         brw_AND(p, brw_null_reg(), c->reg.ff_sync, brw_imm_ud(0x1));
362         need_ff_sync = brw_IF(p, BRW_EXECUTE_1);
363         {
364             brw_OR(p, c->reg.ff_sync, c->reg.ff_sync, brw_imm_ud(0x1));
365             brw_ff_sync(p, 
366                     c->reg.R0,
367                     0,
368                     c->reg.R0,
369                     1,  
370                     1,          /* used */
371                     1,          /* msg length */
372                     1,          /* response length */
373                     0,          /* eot */
374                     1,          /* write compelete */
375                     0,          /* urb offset */
376                     BRW_URB_SWIZZLE_NONE);
377         }
378         brw_ENDIF(p, need_ff_sync);
379         brw_set_predicate_control(p, BRW_PREDICATE_NONE);
380     }
381 }
382
383 void brw_clip_init_ff_sync(struct brw_clip_compile *c)
384 {
385     struct brw_context *brw = c->func.brw;
386     if (brw->needs_ff_sync) {
387         struct brw_compile *p = &c->func;
388         
389         brw_MOV(p, c->reg.ff_sync, brw_imm_ud(0));
390     }
391 }