Merge remote branch 'origin/master' into lp-setup-llvm
[mesa.git] / src / mesa / drivers / dri / i965 / brw_clip_unfilled.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 "main/glheader.h"
33 #include "main/macros.h"
34 #include "main/enums.h"
35 #include "program/program.h"
36
37 #include "intel_batchbuffer.h"
38
39 #include "brw_defines.h"
40 #include "brw_context.h"
41 #include "brw_eu.h"
42 #include "brw_clip.h"
43
44
45
46 /* This is performed against the original triangles, so no indirection
47 * required:
48 BZZZT!
49 */
50 static void compute_tri_direction( struct brw_clip_compile *c )
51 {
52 struct brw_compile *p = &c->func;
53 struct brw_reg e = c->reg.tmp0;
54 struct brw_reg f = c->reg.tmp1;
55 struct brw_reg v0 = byte_offset(c->reg.vertex[0], c->offset[VERT_RESULT_HPOS]);
56 struct brw_reg v1 = byte_offset(c->reg.vertex[1], c->offset[VERT_RESULT_HPOS]);
57 struct brw_reg v2 = byte_offset(c->reg.vertex[2], c->offset[VERT_RESULT_HPOS]);
58
59
60 struct brw_reg v0n = get_tmp(c);
61 struct brw_reg v1n = get_tmp(c);
62 struct brw_reg v2n = get_tmp(c);
63
64 /* Convert to NDC.
65 * NOTE: We can't modify the original vertex coordinates,
66 * as it may impact further operations.
67 * So, we have to keep normalized coordinates in temp registers.
68 *
69 * TBD-KC
70 * Try to optimize unnecessary MOV's.
71 */
72 brw_MOV(p, v0n, v0);
73 brw_MOV(p, v1n, v1);
74 brw_MOV(p, v2n, v2);
75
76 brw_clip_project_position(c, v0n);
77 brw_clip_project_position(c, v1n);
78 brw_clip_project_position(c, v2n);
79
80 /* Calculate the vectors of two edges of the triangle:
81 */
82 brw_ADD(p, e, v0n, negate(v2n));
83 brw_ADD(p, f, v1n, negate(v2n));
84
85 /* Take their crossproduct:
86 */
87 brw_set_access_mode(p, BRW_ALIGN_16);
88 brw_MUL(p, vec4(brw_null_reg()), brw_swizzle(e, 1,2,0,3), brw_swizzle(f,2,0,1,3));
89 brw_MAC(p, vec4(e), negate(brw_swizzle(e, 2,0,1,3)), brw_swizzle(f,1,2,0,3));
90 brw_set_access_mode(p, BRW_ALIGN_1);
91
92 brw_MUL(p, c->reg.dir, c->reg.dir, vec4(e));
93 }
94
95
96 static void cull_direction( struct brw_clip_compile *c )
97 {
98 struct brw_compile *p = &c->func;
99 struct brw_instruction *ccw;
100 GLuint conditional;
101
102 assert (!(c->key.fill_ccw == CLIP_CULL &&
103 c->key.fill_cw == CLIP_CULL));
104
105 if (c->key.fill_ccw == CLIP_CULL)
106 conditional = BRW_CONDITIONAL_GE;
107 else
108 conditional = BRW_CONDITIONAL_L;
109
110 brw_CMP(p,
111 vec1(brw_null_reg()),
112 conditional,
113 get_element(c->reg.dir, 2),
114 brw_imm_f(0));
115
116 ccw = brw_IF(p, BRW_EXECUTE_1);
117 {
118 brw_clip_kill_thread(c);
119 }
120 brw_ENDIF(p, ccw);
121 }
122
123
124
125 static void copy_bfc( struct brw_clip_compile *c )
126 {
127 struct brw_compile *p = &c->func;
128 struct brw_instruction *ccw;
129 GLuint conditional;
130
131 /* Do we have any colors to copy?
132 */
133 if (!(c->offset[VERT_RESULT_COL0] && c->offset[VERT_RESULT_BFC0]) &&
134 !(c->offset[VERT_RESULT_COL1] && c->offset[VERT_RESULT_BFC1]))
135 return;
136
137 /* In some wierd degnerate cases we can end up testing the
138 * direction twice, once for culling and once for bfc copying. Oh
139 * well, that's what you get for setting wierd GL state.
140 */
141 if (c->key.copy_bfc_ccw)
142 conditional = BRW_CONDITIONAL_GE;
143 else
144 conditional = BRW_CONDITIONAL_L;
145
146 brw_CMP(p,
147 vec1(brw_null_reg()),
148 conditional,
149 get_element(c->reg.dir, 2),
150 brw_imm_f(0));
151
152 ccw = brw_IF(p, BRW_EXECUTE_1);
153 {
154 GLuint i;
155
156 for (i = 0; i < 3; i++) {
157 if (c->offset[VERT_RESULT_COL0] && c->offset[VERT_RESULT_BFC0])
158 brw_MOV(p,
159 byte_offset(c->reg.vertex[i], c->offset[VERT_RESULT_COL0]),
160 byte_offset(c->reg.vertex[i], c->offset[VERT_RESULT_BFC0]));
161
162 if (c->offset[VERT_RESULT_COL1] && c->offset[VERT_RESULT_BFC1])
163 brw_MOV(p,
164 byte_offset(c->reg.vertex[i], c->offset[VERT_RESULT_COL1]),
165 byte_offset(c->reg.vertex[i], c->offset[VERT_RESULT_BFC1]));
166 }
167 }
168 brw_ENDIF(p, ccw);
169 }
170
171
172
173
174 /*
175 GLfloat iz = 1.0 / dir.z;
176 GLfloat ac = dir.x * iz;
177 GLfloat bc = dir.y * iz;
178 offset = ctx->Polygon.OffsetUnits * DEPTH_SCALE;
179 offset += MAX2( abs(ac), abs(bc) ) * ctx->Polygon.OffsetFactor;
180 offset *= MRD;
181 */
182 static void compute_offset( struct brw_clip_compile *c )
183 {
184 struct brw_compile *p = &c->func;
185 struct brw_reg off = c->reg.offset;
186 struct brw_reg dir = c->reg.dir;
187
188 brw_math_invert(p, get_element(off, 2), get_element(dir, 2));
189 brw_MUL(p, vec2(off), dir, get_element(off, 2));
190
191 brw_CMP(p,
192 vec1(brw_null_reg()),
193 BRW_CONDITIONAL_GE,
194 brw_abs(get_element(off, 0)),
195 brw_abs(get_element(off, 1)));
196
197 brw_SEL(p, vec1(off), brw_abs(get_element(off, 0)), brw_abs(get_element(off, 1)));
198 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
199
200 brw_MUL(p, vec1(off), off, brw_imm_f(c->key.offset_factor));
201 brw_ADD(p, vec1(off), off, brw_imm_f(c->key.offset_units));
202 }
203
204
205 static void merge_edgeflags( struct brw_clip_compile *c )
206 {
207 struct brw_compile *p = &c->func;
208 struct brw_instruction *is_poly;
209 struct brw_reg tmp0 = get_element_ud(c->reg.tmp0, 0);
210
211 brw_AND(p, tmp0, get_element_ud(c->reg.R0, 2), brw_imm_ud(PRIM_MASK));
212 brw_CMP(p,
213 vec1(brw_null_reg()),
214 BRW_CONDITIONAL_EQ,
215 tmp0,
216 brw_imm_ud(_3DPRIM_POLYGON));
217
218 /* Get away with using reg.vertex because we know that this is not
219 * a _3DPRIM_TRISTRIP_REVERSE:
220 */
221 is_poly = brw_IF(p, BRW_EXECUTE_1);
222 {
223 brw_set_conditionalmod(p, BRW_CONDITIONAL_EQ);
224 brw_AND(p, vec1(brw_null_reg()), get_element_ud(c->reg.R0, 2), brw_imm_ud(1<<8));
225 brw_MOV(p, byte_offset(c->reg.vertex[0], c->offset[VERT_RESULT_EDGE]), brw_imm_f(0));
226 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
227
228 brw_set_conditionalmod(p, BRW_CONDITIONAL_EQ);
229 brw_AND(p, vec1(brw_null_reg()), get_element_ud(c->reg.R0, 2), brw_imm_ud(1<<9));
230 brw_MOV(p, byte_offset(c->reg.vertex[2], c->offset[VERT_RESULT_EDGE]), brw_imm_f(0));
231 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
232 }
233 brw_ENDIF(p, is_poly);
234 }
235
236
237
238 static void apply_one_offset( struct brw_clip_compile *c,
239 struct brw_indirect vert )
240 {
241 struct brw_compile *p = &c->func;
242 struct brw_reg z = deref_1f(vert, c->header_position_offset +
243 2 * type_sz(BRW_REGISTER_TYPE_F));
244
245 brw_ADD(p, z, z, vec1(c->reg.offset));
246 }
247
248
249
250 /***********************************************************************
251 * Output clipped polygon as an unfilled primitive:
252 */
253 static void emit_lines(struct brw_clip_compile *c,
254 GLboolean do_offset)
255 {
256 struct brw_compile *p = &c->func;
257 struct brw_instruction *loop;
258 struct brw_instruction *draw_edge;
259 struct brw_indirect v0 = brw_indirect(0, 0);
260 struct brw_indirect v1 = brw_indirect(1, 0);
261 struct brw_indirect v0ptr = brw_indirect(2, 0);
262 struct brw_indirect v1ptr = brw_indirect(3, 0);
263
264 /* Need a seperate loop for offset:
265 */
266 if (do_offset) {
267 brw_MOV(p, c->reg.loopcount, c->reg.nr_verts);
268 brw_MOV(p, get_addr_reg(v0ptr), brw_address(c->reg.inlist));
269
270 loop = brw_DO(p, BRW_EXECUTE_1);
271 {
272 brw_MOV(p, get_addr_reg(v0), deref_1uw(v0ptr, 0));
273 brw_ADD(p, get_addr_reg(v0ptr), get_addr_reg(v0ptr), brw_imm_uw(2));
274
275 apply_one_offset(c, v0);
276
277 brw_set_conditionalmod(p, BRW_CONDITIONAL_G);
278 brw_ADD(p, c->reg.loopcount, c->reg.loopcount, brw_imm_d(-1));
279 }
280 brw_WHILE(p, loop);
281 }
282
283 /* v1ptr = &inlist[nr_verts]
284 * *v1ptr = v0
285 */
286 brw_MOV(p, c->reg.loopcount, c->reg.nr_verts);
287 brw_MOV(p, get_addr_reg(v0ptr), brw_address(c->reg.inlist));
288 brw_ADD(p, get_addr_reg(v1ptr), get_addr_reg(v0ptr), retype(c->reg.nr_verts, BRW_REGISTER_TYPE_UW));
289 brw_ADD(p, get_addr_reg(v1ptr), get_addr_reg(v1ptr), retype(c->reg.nr_verts, BRW_REGISTER_TYPE_UW));
290 brw_MOV(p, deref_1uw(v1ptr, 0), deref_1uw(v0ptr, 0));
291
292 loop = brw_DO(p, BRW_EXECUTE_1);
293 {
294 brw_MOV(p, get_addr_reg(v0), deref_1uw(v0ptr, 0));
295 brw_MOV(p, get_addr_reg(v1), deref_1uw(v0ptr, 2));
296 brw_ADD(p, get_addr_reg(v0ptr), get_addr_reg(v0ptr), brw_imm_uw(2));
297
298 /* draw edge if edgeflag != 0 */
299 brw_CMP(p,
300 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ,
301 deref_1f(v0, c->offset[VERT_RESULT_EDGE]),
302 brw_imm_f(0));
303 draw_edge = brw_IF(p, BRW_EXECUTE_1);
304 {
305 brw_clip_emit_vue(c, v0, 1, 0, (_3DPRIM_LINESTRIP << 2) | R02_PRIM_START);
306 brw_clip_emit_vue(c, v1, 1, 0, (_3DPRIM_LINESTRIP << 2) | R02_PRIM_END);
307 }
308 brw_ENDIF(p, draw_edge);
309
310 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
311 brw_ADD(p, c->reg.loopcount, c->reg.loopcount, brw_imm_d(-1));
312 }
313 brw_WHILE(p, loop);
314 }
315
316
317
318 static void emit_points(struct brw_clip_compile *c,
319 GLboolean do_offset )
320 {
321 struct brw_compile *p = &c->func;
322 struct brw_instruction *loop;
323 struct brw_instruction *draw_point;
324
325 struct brw_indirect v0 = brw_indirect(0, 0);
326 struct brw_indirect v0ptr = brw_indirect(2, 0);
327
328 brw_MOV(p, c->reg.loopcount, c->reg.nr_verts);
329 brw_MOV(p, get_addr_reg(v0ptr), brw_address(c->reg.inlist));
330
331 loop = brw_DO(p, BRW_EXECUTE_1);
332 {
333 brw_MOV(p, get_addr_reg(v0), deref_1uw(v0ptr, 0));
334 brw_ADD(p, get_addr_reg(v0ptr), get_addr_reg(v0ptr), brw_imm_uw(2));
335
336 /* draw if edgeflag != 0
337 */
338 brw_CMP(p,
339 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ,
340 deref_1f(v0, c->offset[VERT_RESULT_EDGE]),
341 brw_imm_f(0));
342 draw_point = brw_IF(p, BRW_EXECUTE_1);
343 {
344 if (do_offset)
345 apply_one_offset(c, v0);
346
347 brw_clip_emit_vue(c, v0, 1, 0, (_3DPRIM_POINTLIST << 2) | R02_PRIM_START | R02_PRIM_END);
348 }
349 brw_ENDIF(p, draw_point);
350
351 brw_set_conditionalmod(p, BRW_CONDITIONAL_NZ);
352 brw_ADD(p, c->reg.loopcount, c->reg.loopcount, brw_imm_d(-1));
353 }
354 brw_WHILE(p, loop);
355 }
356
357
358
359
360
361
362
363 static void emit_primitives( struct brw_clip_compile *c,
364 GLuint mode,
365 GLboolean do_offset )
366 {
367 switch (mode) {
368 case CLIP_FILL:
369 brw_clip_tri_emit_polygon(c);
370 break;
371
372 case CLIP_LINE:
373 emit_lines(c, do_offset);
374 break;
375
376 case CLIP_POINT:
377 emit_points(c, do_offset);
378 break;
379
380 case CLIP_CULL:
381 assert(0);
382 break;
383 }
384 }
385
386
387
388 static void emit_unfilled_primitives( struct brw_clip_compile *c )
389 {
390 struct brw_compile *p = &c->func;
391 struct brw_instruction *ccw;
392
393 /* Direction culling has already been done.
394 */
395 if (c->key.fill_ccw != c->key.fill_cw &&
396 c->key.fill_ccw != CLIP_CULL &&
397 c->key.fill_cw != CLIP_CULL)
398 {
399 brw_CMP(p,
400 vec1(brw_null_reg()),
401 BRW_CONDITIONAL_GE,
402 get_element(c->reg.dir, 2),
403 brw_imm_f(0));
404
405 ccw = brw_IF(p, BRW_EXECUTE_1);
406 {
407 emit_primitives(c, c->key.fill_ccw, c->key.offset_ccw);
408 }
409 ccw = brw_ELSE(p, ccw);
410 {
411 emit_primitives(c, c->key.fill_cw, c->key.offset_cw);
412 }
413 brw_ENDIF(p, ccw);
414 }
415 else if (c->key.fill_cw != CLIP_CULL) {
416 emit_primitives(c, c->key.fill_cw, c->key.offset_cw);
417 }
418 else if (c->key.fill_ccw != CLIP_CULL) {
419 emit_primitives(c, c->key.fill_ccw, c->key.offset_ccw);
420 }
421 }
422
423
424
425
426 static void check_nr_verts( struct brw_clip_compile *c )
427 {
428 struct brw_compile *p = &c->func;
429 struct brw_instruction *if_insn;
430
431 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_L, c->reg.nr_verts, brw_imm_d(3));
432 if_insn = brw_IF(p, BRW_EXECUTE_1);
433 {
434 brw_clip_kill_thread(c);
435 }
436 brw_ENDIF(p, if_insn);
437 }
438
439
440 void brw_emit_unfilled_clip( struct brw_clip_compile *c )
441 {
442 struct brw_compile *p = &c->func;
443 struct brw_instruction *do_clip;
444
445
446 c->need_direction = ((c->key.offset_ccw || c->key.offset_cw) ||
447 (c->key.fill_ccw != c->key.fill_cw) ||
448 c->key.fill_ccw == CLIP_CULL ||
449 c->key.fill_cw == CLIP_CULL ||
450 c->key.copy_bfc_cw ||
451 c->key.copy_bfc_ccw);
452
453 brw_clip_tri_alloc_regs(c, 3 + c->key.nr_userclip + 6);
454 brw_clip_tri_init_vertices(c);
455 brw_clip_init_ff_sync(c);
456
457 assert(c->offset[VERT_RESULT_EDGE]);
458
459 if (c->key.fill_ccw == CLIP_CULL &&
460 c->key.fill_cw == CLIP_CULL) {
461 brw_clip_kill_thread(c);
462 return;
463 }
464
465 merge_edgeflags(c);
466
467 /* Need to use the inlist indirection here:
468 */
469 if (c->need_direction)
470 compute_tri_direction(c);
471
472 if (c->key.fill_ccw == CLIP_CULL ||
473 c->key.fill_cw == CLIP_CULL)
474 cull_direction(c);
475
476 if (c->key.offset_ccw ||
477 c->key.offset_cw)
478 compute_offset(c);
479
480 if (c->key.copy_bfc_ccw ||
481 c->key.copy_bfc_cw)
482 copy_bfc(c);
483
484 /* Need to do this whether we clip or not:
485 */
486 if (c->key.do_flat_shading)
487 brw_clip_tri_flat_shade(c);
488
489 brw_clip_init_clipmask(c);
490 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_NZ, c->reg.planemask, brw_imm_ud(0));
491 do_clip = brw_IF(p, BRW_EXECUTE_1);
492 {
493 brw_clip_init_planes(c);
494 brw_clip_tri(c);
495 check_nr_verts(c);
496 }
497 brw_ENDIF(p, do_clip);
498
499 emit_unfilled_primitives(c);
500 brw_clip_kill_thread(c);
501 }
502
503
504