2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keithw@vmware.com>
32 #include "main/macros.h"
33 #include "main/enums.h"
34 #include "program/program.h"
36 #include "intel_batchbuffer.h"
38 #include "brw_defines.h"
39 #include "brw_context.h"
45 /* This is performed against the original triangles, so no indirection
49 static void compute_tri_direction( struct brw_clip_compile
*c
)
51 struct brw_codegen
*p
= &c
->func
;
52 struct brw_reg e
= c
->reg
.tmp0
;
53 struct brw_reg f
= c
->reg
.tmp1
;
54 GLuint hpos_offset
= brw_varying_to_offset(&c
->vue_map
, VARYING_SLOT_POS
);
55 struct brw_reg v0
= byte_offset(c
->reg
.vertex
[0], hpos_offset
);
56 struct brw_reg v1
= byte_offset(c
->reg
.vertex
[1], hpos_offset
);
57 struct brw_reg v2
= byte_offset(c
->reg
.vertex
[2], hpos_offset
);
60 struct brw_reg v0n
= get_tmp(c
);
61 struct brw_reg v1n
= get_tmp(c
);
62 struct brw_reg v2n
= get_tmp(c
);
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.
70 * Try to optimize unnecessary MOV's.
76 brw_clip_project_position(c
, v0n
);
77 brw_clip_project_position(c
, v1n
);
78 brw_clip_project_position(c
, v2n
);
80 /* Calculate the vectors of two edges of the triangle:
82 brw_ADD(p
, e
, v0n
, negate(v2n
));
83 brw_ADD(p
, f
, v1n
, negate(v2n
));
85 /* Take their crossproduct:
87 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
88 brw_MUL(p
, vec4(brw_null_reg()), brw_swizzle(e
, BRW_SWIZZLE_YZXW
),
89 brw_swizzle(f
, BRW_SWIZZLE_ZXYW
));
90 brw_MAC(p
, vec4(e
), negate(brw_swizzle(e
, BRW_SWIZZLE_ZXYW
)),
91 brw_swizzle(f
, BRW_SWIZZLE_YZXW
));
92 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
94 brw_MUL(p
, c
->reg
.dir
, c
->reg
.dir
, vec4(e
));
98 static void cull_direction( struct brw_clip_compile
*c
)
100 struct brw_codegen
*p
= &c
->func
;
103 assert (!(c
->key
.fill_ccw
== CLIP_CULL
&&
104 c
->key
.fill_cw
== CLIP_CULL
));
106 if (c
->key
.fill_ccw
== CLIP_CULL
)
107 conditional
= BRW_CONDITIONAL_GE
;
109 conditional
= BRW_CONDITIONAL_L
;
112 vec1(brw_null_reg()),
114 get_element(c
->reg
.dir
, 2),
117 brw_IF(p
, BRW_EXECUTE_1
);
119 brw_clip_kill_thread(c
);
126 static void copy_bfc( struct brw_clip_compile
*c
)
128 struct brw_codegen
*p
= &c
->func
;
131 /* Do we have any colors to copy?
133 if (!(brw_clip_have_varying(c
, VARYING_SLOT_COL0
) &&
134 brw_clip_have_varying(c
, VARYING_SLOT_BFC0
)) &&
135 !(brw_clip_have_varying(c
, VARYING_SLOT_COL1
) &&
136 brw_clip_have_varying(c
, VARYING_SLOT_BFC1
)))
139 /* In some weird degenerate cases we can end up testing the
140 * direction twice, once for culling and once for bfc copying. Oh
141 * well, that's what you get for setting weird GL state.
143 if (c
->key
.copy_bfc_ccw
)
144 conditional
= BRW_CONDITIONAL_GE
;
146 conditional
= BRW_CONDITIONAL_L
;
149 vec1(brw_null_reg()),
151 get_element(c
->reg
.dir
, 2),
154 brw_IF(p
, BRW_EXECUTE_1
);
158 for (i
= 0; i
< 3; i
++) {
159 if (brw_clip_have_varying(c
, VARYING_SLOT_COL0
) &&
160 brw_clip_have_varying(c
, VARYING_SLOT_BFC0
))
162 byte_offset(c
->reg
.vertex
[i
],
163 brw_varying_to_offset(&c
->vue_map
,
165 byte_offset(c
->reg
.vertex
[i
],
166 brw_varying_to_offset(&c
->vue_map
,
167 VARYING_SLOT_BFC0
)));
169 if (brw_clip_have_varying(c
, VARYING_SLOT_COL1
) &&
170 brw_clip_have_varying(c
, VARYING_SLOT_BFC1
))
172 byte_offset(c
->reg
.vertex
[i
],
173 brw_varying_to_offset(&c
->vue_map
,
175 byte_offset(c
->reg
.vertex
[i
],
176 brw_varying_to_offset(&c
->vue_map
,
177 VARYING_SLOT_BFC1
)));
187 GLfloat iz = 1.0 / dir.z;
188 GLfloat ac = dir.x * iz;
189 GLfloat bc = dir.y * iz;
190 offset = ctx->Polygon.OffsetUnits * DEPTH_SCALE;
191 offset += MAX2( abs(ac), abs(bc) ) * ctx->Polygon.OffsetFactor;
192 if (ctx->Polygon.OffsetClamp && isfinite(ctx->Polygon.OffsetClamp)) {
193 if (ctx->Polygon.OffsetClamp < 0)
194 offset = MAX2( offset, ctx->Polygon.OffsetClamp );
196 offset = MIN2( offset, ctx->Polygon.OffsetClamp );
200 static void compute_offset( struct brw_clip_compile
*c
)
202 struct brw_codegen
*p
= &c
->func
;
203 struct brw_reg off
= c
->reg
.offset
;
204 struct brw_reg dir
= c
->reg
.dir
;
206 brw_math_invert(p
, get_element(off
, 2), get_element(dir
, 2));
207 brw_MUL(p
, vec2(off
), vec2(dir
), get_element(off
, 2));
210 vec1(brw_null_reg()),
212 brw_abs(get_element(off
, 0)),
213 brw_abs(get_element(off
, 1)));
215 brw_SEL(p
, vec1(off
),
216 brw_abs(get_element(off
, 0)), brw_abs(get_element(off
, 1)));
217 brw_inst_set_pred_control(p
->devinfo
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
219 brw_MUL(p
, vec1(off
), vec1(off
), brw_imm_f(c
->key
.offset_factor
));
220 brw_ADD(p
, vec1(off
), vec1(off
), brw_imm_f(c
->key
.offset_units
));
221 if (c
->key
.offset_clamp
&& isfinite(c
->key
.offset_clamp
)) {
223 vec1(brw_null_reg()),
224 c
->key
.offset_clamp
< 0 ? BRW_CONDITIONAL_GE
: BRW_CONDITIONAL_L
,
226 brw_imm_f(c
->key
.offset_clamp
));
227 brw_SEL(p
, vec1(off
), vec1(off
), brw_imm_f(c
->key
.offset_clamp
));
232 static void merge_edgeflags( struct brw_clip_compile
*c
)
234 struct brw_codegen
*p
= &c
->func
;
235 struct brw_reg tmp0
= get_element_ud(c
->reg
.tmp0
, 0);
237 brw_AND(p
, tmp0
, get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(PRIM_MASK
));
239 vec1(brw_null_reg()),
242 brw_imm_ud(_3DPRIM_POLYGON
));
244 /* Get away with using reg.vertex because we know that this is not
245 * a _3DPRIM_TRISTRIP_REVERSE:
247 brw_IF(p
, BRW_EXECUTE_1
);
249 brw_AND(p
, vec1(brw_null_reg()), get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(1<<8));
250 brw_inst_set_cond_modifier(p
->devinfo
, brw_last_inst
, BRW_CONDITIONAL_EQ
);
251 brw_MOV(p
, byte_offset(c
->reg
.vertex
[0],
252 brw_varying_to_offset(&c
->vue_map
,
255 brw_inst_set_pred_control(p
->devinfo
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
257 brw_AND(p
, vec1(brw_null_reg()), get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(1<<9));
258 brw_inst_set_cond_modifier(p
->devinfo
, brw_last_inst
, BRW_CONDITIONAL_EQ
);
259 brw_MOV(p
, byte_offset(c
->reg
.vertex
[2],
260 brw_varying_to_offset(&c
->vue_map
,
263 brw_inst_set_pred_control(p
->devinfo
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
270 static void apply_one_offset( struct brw_clip_compile
*c
,
271 struct brw_indirect vert
)
273 struct brw_codegen
*p
= &c
->func
;
274 GLuint ndc_offset
= brw_varying_to_offset(&c
->vue_map
,
275 BRW_VARYING_SLOT_NDC
);
276 struct brw_reg z
= deref_1f(vert
, ndc_offset
+
277 2 * type_sz(BRW_REGISTER_TYPE_F
));
279 brw_ADD(p
, z
, z
, vec1(c
->reg
.offset
));
284 /***********************************************************************
285 * Output clipped polygon as an unfilled primitive:
287 static void emit_lines(struct brw_clip_compile
*c
,
290 struct brw_codegen
*p
= &c
->func
;
291 struct brw_indirect v0
= brw_indirect(0, 0);
292 struct brw_indirect v1
= brw_indirect(1, 0);
293 struct brw_indirect v0ptr
= brw_indirect(2, 0);
294 struct brw_indirect v1ptr
= brw_indirect(3, 0);
296 /* Need a separate loop for offset:
299 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
300 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
302 brw_DO(p
, BRW_EXECUTE_1
);
304 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
305 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
307 apply_one_offset(c
, v0
);
309 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
310 brw_inst_set_cond_modifier(p
->devinfo
, brw_last_inst
, BRW_CONDITIONAL_G
);
313 brw_inst_set_pred_control(p
->devinfo
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
316 /* v1ptr = &inlist[nr_verts]
319 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
320 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
321 brw_ADD(p
, get_addr_reg(v1ptr
), get_addr_reg(v0ptr
), retype(c
->reg
.nr_verts
, BRW_REGISTER_TYPE_UW
));
322 brw_ADD(p
, get_addr_reg(v1ptr
), get_addr_reg(v1ptr
), retype(c
->reg
.nr_verts
, BRW_REGISTER_TYPE_UW
));
323 brw_MOV(p
, deref_1uw(v1ptr
, 0), deref_1uw(v0ptr
, 0));
325 brw_DO(p
, BRW_EXECUTE_1
);
327 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
328 brw_MOV(p
, get_addr_reg(v1
), deref_1uw(v0ptr
, 2));
329 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
331 /* draw edge if edgeflag != 0 */
333 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
,
334 deref_1f(v0
, brw_varying_to_offset(&c
->vue_map
,
337 brw_IF(p
, BRW_EXECUTE_1
);
339 brw_clip_emit_vue(c
, v0
, BRW_URB_WRITE_ALLOCATE_COMPLETE
,
340 (_3DPRIM_LINESTRIP
<< URB_WRITE_PRIM_TYPE_SHIFT
)
341 | URB_WRITE_PRIM_START
);
342 brw_clip_emit_vue(c
, v1
, BRW_URB_WRITE_ALLOCATE_COMPLETE
,
343 (_3DPRIM_LINESTRIP
<< URB_WRITE_PRIM_TYPE_SHIFT
)
344 | URB_WRITE_PRIM_END
);
348 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
349 brw_inst_set_cond_modifier(p
->devinfo
, brw_last_inst
, BRW_CONDITIONAL_NZ
);
352 brw_inst_set_pred_control(p
->devinfo
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
357 static void emit_points(struct brw_clip_compile
*c
,
360 struct brw_codegen
*p
= &c
->func
;
362 struct brw_indirect v0
= brw_indirect(0, 0);
363 struct brw_indirect v0ptr
= brw_indirect(2, 0);
365 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
366 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
368 brw_DO(p
, BRW_EXECUTE_1
);
370 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
371 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
373 /* draw if edgeflag != 0
376 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
,
377 deref_1f(v0
, brw_varying_to_offset(&c
->vue_map
,
380 brw_IF(p
, BRW_EXECUTE_1
);
383 apply_one_offset(c
, v0
);
385 brw_clip_emit_vue(c
, v0
, BRW_URB_WRITE_ALLOCATE_COMPLETE
,
386 (_3DPRIM_POINTLIST
<< URB_WRITE_PRIM_TYPE_SHIFT
)
387 | URB_WRITE_PRIM_START
| URB_WRITE_PRIM_END
);
391 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
392 brw_inst_set_cond_modifier(p
->devinfo
, brw_last_inst
, BRW_CONDITIONAL_NZ
);
395 brw_inst_set_pred_control(p
->devinfo
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
404 static void emit_primitives( struct brw_clip_compile
*c
,
410 brw_clip_tri_emit_polygon(c
);
414 emit_lines(c
, do_offset
);
418 emit_points(c
, do_offset
);
422 unreachable("not reached");
428 static void emit_unfilled_primitives( struct brw_clip_compile
*c
)
430 struct brw_codegen
*p
= &c
->func
;
432 /* Direction culling has already been done.
434 if (c
->key
.fill_ccw
!= c
->key
.fill_cw
&&
435 c
->key
.fill_ccw
!= CLIP_CULL
&&
436 c
->key
.fill_cw
!= CLIP_CULL
)
439 vec1(brw_null_reg()),
441 get_element(c
->reg
.dir
, 2),
444 brw_IF(p
, BRW_EXECUTE_1
);
446 emit_primitives(c
, c
->key
.fill_ccw
, c
->key
.offset_ccw
);
450 emit_primitives(c
, c
->key
.fill_cw
, c
->key
.offset_cw
);
454 else if (c
->key
.fill_cw
!= CLIP_CULL
) {
455 emit_primitives(c
, c
->key
.fill_cw
, c
->key
.offset_cw
);
457 else if (c
->key
.fill_ccw
!= CLIP_CULL
) {
458 emit_primitives(c
, c
->key
.fill_ccw
, c
->key
.offset_ccw
);
465 static void check_nr_verts( struct brw_clip_compile
*c
)
467 struct brw_codegen
*p
= &c
->func
;
469 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_L
, c
->reg
.nr_verts
, brw_imm_d(3));
470 brw_IF(p
, BRW_EXECUTE_1
);
472 brw_clip_kill_thread(c
);
478 void brw_emit_unfilled_clip( struct brw_clip_compile
*c
)
480 struct brw_codegen
*p
= &c
->func
;
482 c
->need_direction
= ((c
->key
.offset_ccw
|| c
->key
.offset_cw
) ||
483 (c
->key
.fill_ccw
!= c
->key
.fill_cw
) ||
484 c
->key
.fill_ccw
== CLIP_CULL
||
485 c
->key
.fill_cw
== CLIP_CULL
||
486 c
->key
.copy_bfc_cw
||
487 c
->key
.copy_bfc_ccw
);
489 brw_clip_tri_alloc_regs(c
, 3 + c
->key
.nr_userclip
+ 6);
490 brw_clip_tri_init_vertices(c
);
491 brw_clip_init_ff_sync(c
);
493 assert(brw_clip_have_varying(c
, VARYING_SLOT_EDGE
));
495 if (c
->key
.fill_ccw
== CLIP_CULL
&&
496 c
->key
.fill_cw
== CLIP_CULL
) {
497 brw_clip_kill_thread(c
);
503 /* Need to use the inlist indirection here:
505 if (c
->need_direction
)
506 compute_tri_direction(c
);
508 if (c
->key
.fill_ccw
== CLIP_CULL
||
509 c
->key
.fill_cw
== CLIP_CULL
)
512 if (c
->key
.offset_ccw
||
516 if (c
->key
.copy_bfc_ccw
||
520 /* Need to do this whether we clip or not:
522 if (c
->has_flat_shading
)
523 brw_clip_tri_flat_shade(c
);
525 brw_clip_init_clipmask(c
);
526 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
, c
->reg
.planemask
, brw_imm_ud(0));
527 brw_IF(p
, BRW_EXECUTE_1
);
529 brw_clip_init_planes(c
);
535 emit_unfilled_primitives(c
);
536 brw_clip_kill_thread(c
);