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/glheader.h"
33 #include "main/macros.h"
34 #include "main/enums.h"
35 #include "program/program.h"
37 #include "intel_batchbuffer.h"
39 #include "brw_defines.h"
40 #include "brw_context.h"
46 /* This is performed against the original triangles, so no indirection
50 static void compute_tri_direction( struct brw_clip_compile
*c
)
52 struct brw_compile
*p
= &c
->func
;
53 struct brw_reg e
= c
->reg
.tmp0
;
54 struct brw_reg f
= c
->reg
.tmp1
;
55 GLuint hpos_offset
= brw_varying_to_offset(&c
->vue_map
, VARYING_SLOT_POS
);
56 struct brw_reg v0
= byte_offset(c
->reg
.vertex
[0], hpos_offset
);
57 struct brw_reg v1
= byte_offset(c
->reg
.vertex
[1], hpos_offset
);
58 struct brw_reg v2
= byte_offset(c
->reg
.vertex
[2], hpos_offset
);
61 struct brw_reg v0n
= get_tmp(c
);
62 struct brw_reg v1n
= get_tmp(c
);
63 struct brw_reg v2n
= get_tmp(c
);
66 * NOTE: We can't modify the original vertex coordinates,
67 * as it may impact further operations.
68 * So, we have to keep normalized coordinates in temp registers.
71 * Try to optimize unnecessary MOV's.
77 brw_clip_project_position(c
, v0n
);
78 brw_clip_project_position(c
, v1n
);
79 brw_clip_project_position(c
, v2n
);
81 /* Calculate the vectors of two edges of the triangle:
83 brw_ADD(p
, e
, v0n
, negate(v2n
));
84 brw_ADD(p
, f
, v1n
, negate(v2n
));
86 /* Take their crossproduct:
88 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
89 brw_MUL(p
, vec4(brw_null_reg()), brw_swizzle(e
, 1,2,0,3), brw_swizzle(f
,2,0,1,3));
90 brw_MAC(p
, vec4(e
), negate(brw_swizzle(e
, 2,0,1,3)), brw_swizzle(f
,1,2,0,3));
91 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
93 brw_MUL(p
, c
->reg
.dir
, c
->reg
.dir
, vec4(e
));
97 static void cull_direction( struct brw_clip_compile
*c
)
99 struct brw_compile
*p
= &c
->func
;
102 assert (!(c
->key
.fill_ccw
== CLIP_CULL
&&
103 c
->key
.fill_cw
== CLIP_CULL
));
105 if (c
->key
.fill_ccw
== CLIP_CULL
)
106 conditional
= BRW_CONDITIONAL_GE
;
108 conditional
= BRW_CONDITIONAL_L
;
111 vec1(brw_null_reg()),
113 get_element(c
->reg
.dir
, 2),
116 brw_IF(p
, BRW_EXECUTE_1
);
118 brw_clip_kill_thread(c
);
125 static void copy_bfc( struct brw_clip_compile
*c
)
127 struct brw_compile
*p
= &c
->func
;
130 /* Do we have any colors to copy?
132 if (!(brw_clip_have_varying(c
, VARYING_SLOT_COL0
) &&
133 brw_clip_have_varying(c
, VARYING_SLOT_BFC0
)) &&
134 !(brw_clip_have_varying(c
, VARYING_SLOT_COL1
) &&
135 brw_clip_have_varying(c
, VARYING_SLOT_BFC1
)))
138 /* In some wierd degnerate cases we can end up testing the
139 * direction twice, once for culling and once for bfc copying. Oh
140 * well, that's what you get for setting wierd GL state.
142 if (c
->key
.copy_bfc_ccw
)
143 conditional
= BRW_CONDITIONAL_GE
;
145 conditional
= BRW_CONDITIONAL_L
;
148 vec1(brw_null_reg()),
150 get_element(c
->reg
.dir
, 2),
153 brw_IF(p
, BRW_EXECUTE_1
);
157 for (i
= 0; i
< 3; i
++) {
158 if (brw_clip_have_varying(c
, VARYING_SLOT_COL0
) &&
159 brw_clip_have_varying(c
, VARYING_SLOT_BFC0
))
161 byte_offset(c
->reg
.vertex
[i
],
162 brw_varying_to_offset(&c
->vue_map
,
164 byte_offset(c
->reg
.vertex
[i
],
165 brw_varying_to_offset(&c
->vue_map
,
166 VARYING_SLOT_BFC0
)));
168 if (brw_clip_have_varying(c
, VARYING_SLOT_COL1
) &&
169 brw_clip_have_varying(c
, VARYING_SLOT_BFC1
))
171 byte_offset(c
->reg
.vertex
[i
],
172 brw_varying_to_offset(&c
->vue_map
,
174 byte_offset(c
->reg
.vertex
[i
],
175 brw_varying_to_offset(&c
->vue_map
,
176 VARYING_SLOT_BFC1
)));
186 GLfloat iz = 1.0 / dir.z;
187 GLfloat ac = dir.x * iz;
188 GLfloat bc = dir.y * iz;
189 offset = ctx->Polygon.OffsetUnits * DEPTH_SCALE;
190 offset += MAX2( abs(ac), abs(bc) ) * ctx->Polygon.OffsetFactor;
193 static void compute_offset( struct brw_clip_compile
*c
)
195 struct brw_compile
*p
= &c
->func
;
196 const struct brw_context
*brw
= p
->brw
;
197 struct brw_reg off
= c
->reg
.offset
;
198 struct brw_reg dir
= c
->reg
.dir
;
200 brw_math_invert(p
, get_element(off
, 2), get_element(dir
, 2));
201 brw_MUL(p
, vec2(off
), vec2(dir
), get_element(off
, 2));
204 vec1(brw_null_reg()),
206 brw_abs(get_element(off
, 0)),
207 brw_abs(get_element(off
, 1)));
209 brw_SEL(p
, vec1(off
),
210 brw_abs(get_element(off
, 0)), brw_abs(get_element(off
, 1)));
211 brw_inst_set_pred_control(brw
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
213 brw_MUL(p
, vec1(off
), vec1(off
), brw_imm_f(c
->key
.offset_factor
));
214 brw_ADD(p
, vec1(off
), vec1(off
), brw_imm_f(c
->key
.offset_units
));
218 static void merge_edgeflags( struct brw_clip_compile
*c
)
220 struct brw_compile
*p
= &c
->func
;
221 const struct brw_context
*brw
= p
->brw
;
222 struct brw_reg tmp0
= get_element_ud(c
->reg
.tmp0
, 0);
224 brw_AND(p
, tmp0
, get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(PRIM_MASK
));
226 vec1(brw_null_reg()),
229 brw_imm_ud(_3DPRIM_POLYGON
));
231 /* Get away with using reg.vertex because we know that this is not
232 * a _3DPRIM_TRISTRIP_REVERSE:
234 brw_IF(p
, BRW_EXECUTE_1
);
236 brw_AND(p
, vec1(brw_null_reg()), get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(1<<8));
237 brw_inst_set_cond_modifier(brw
, brw_last_inst
, BRW_CONDITIONAL_EQ
);
238 brw_MOV(p
, byte_offset(c
->reg
.vertex
[0],
239 brw_varying_to_offset(&c
->vue_map
,
242 brw_inst_set_pred_control(brw
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
244 brw_AND(p
, vec1(brw_null_reg()), get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(1<<9));
245 brw_inst_set_cond_modifier(brw
, brw_last_inst
, BRW_CONDITIONAL_EQ
);
246 brw_MOV(p
, byte_offset(c
->reg
.vertex
[2],
247 brw_varying_to_offset(&c
->vue_map
,
250 brw_inst_set_pred_control(brw
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
257 static void apply_one_offset( struct brw_clip_compile
*c
,
258 struct brw_indirect vert
)
260 struct brw_compile
*p
= &c
->func
;
261 GLuint ndc_offset
= brw_varying_to_offset(&c
->vue_map
,
262 BRW_VARYING_SLOT_NDC
);
263 struct brw_reg z
= deref_1f(vert
, ndc_offset
+
264 2 * type_sz(BRW_REGISTER_TYPE_F
));
266 brw_ADD(p
, z
, z
, vec1(c
->reg
.offset
));
271 /***********************************************************************
272 * Output clipped polygon as an unfilled primitive:
274 static void emit_lines(struct brw_clip_compile
*c
,
277 struct brw_compile
*p
= &c
->func
;
278 const struct brw_context
*brw
= p
->brw
;
279 struct brw_indirect v0
= brw_indirect(0, 0);
280 struct brw_indirect v1
= brw_indirect(1, 0);
281 struct brw_indirect v0ptr
= brw_indirect(2, 0);
282 struct brw_indirect v1ptr
= brw_indirect(3, 0);
284 /* Need a seperate loop for offset:
287 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
288 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
290 brw_DO(p
, BRW_EXECUTE_1
);
292 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
293 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
295 apply_one_offset(c
, v0
);
297 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
298 brw_inst_set_cond_modifier(brw
, brw_last_inst
, BRW_CONDITIONAL_G
);
301 brw_inst_set_pred_control(brw
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
304 /* v1ptr = &inlist[nr_verts]
307 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
308 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
309 brw_ADD(p
, get_addr_reg(v1ptr
), get_addr_reg(v0ptr
), retype(c
->reg
.nr_verts
, BRW_REGISTER_TYPE_UW
));
310 brw_ADD(p
, get_addr_reg(v1ptr
), get_addr_reg(v1ptr
), retype(c
->reg
.nr_verts
, BRW_REGISTER_TYPE_UW
));
311 brw_MOV(p
, deref_1uw(v1ptr
, 0), deref_1uw(v0ptr
, 0));
313 brw_DO(p
, BRW_EXECUTE_1
);
315 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
316 brw_MOV(p
, get_addr_reg(v1
), deref_1uw(v0ptr
, 2));
317 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
319 /* draw edge if edgeflag != 0 */
321 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
,
322 deref_1f(v0
, brw_varying_to_offset(&c
->vue_map
,
325 brw_IF(p
, BRW_EXECUTE_1
);
327 brw_clip_emit_vue(c
, v0
, BRW_URB_WRITE_ALLOCATE_COMPLETE
,
328 (_3DPRIM_LINESTRIP
<< URB_WRITE_PRIM_TYPE_SHIFT
)
329 | URB_WRITE_PRIM_START
);
330 brw_clip_emit_vue(c
, v1
, BRW_URB_WRITE_ALLOCATE_COMPLETE
,
331 (_3DPRIM_LINESTRIP
<< URB_WRITE_PRIM_TYPE_SHIFT
)
332 | URB_WRITE_PRIM_END
);
336 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
337 brw_inst_set_cond_modifier(brw
, brw_last_inst
, BRW_CONDITIONAL_NZ
);
340 brw_inst_set_pred_control(brw
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
345 static void emit_points(struct brw_clip_compile
*c
,
348 struct brw_compile
*p
= &c
->func
;
349 const struct brw_context
*brw
= p
->brw
;
351 struct brw_indirect v0
= brw_indirect(0, 0);
352 struct brw_indirect v0ptr
= brw_indirect(2, 0);
354 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
355 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
357 brw_DO(p
, BRW_EXECUTE_1
);
359 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
360 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
362 /* draw if edgeflag != 0
365 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
,
366 deref_1f(v0
, brw_varying_to_offset(&c
->vue_map
,
369 brw_IF(p
, BRW_EXECUTE_1
);
372 apply_one_offset(c
, v0
);
374 brw_clip_emit_vue(c
, v0
, BRW_URB_WRITE_ALLOCATE_COMPLETE
,
375 (_3DPRIM_POINTLIST
<< URB_WRITE_PRIM_TYPE_SHIFT
)
376 | URB_WRITE_PRIM_START
| URB_WRITE_PRIM_END
);
380 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
381 brw_inst_set_cond_modifier(brw
, brw_last_inst
, BRW_CONDITIONAL_NZ
);
384 brw_inst_set_pred_control(brw
, brw_last_inst
, BRW_PREDICATE_NORMAL
);
393 static void emit_primitives( struct brw_clip_compile
*c
,
399 brw_clip_tri_emit_polygon(c
);
403 emit_lines(c
, do_offset
);
407 emit_points(c
, do_offset
);
411 unreachable("not reached");
417 static void emit_unfilled_primitives( struct brw_clip_compile
*c
)
419 struct brw_compile
*p
= &c
->func
;
421 /* Direction culling has already been done.
423 if (c
->key
.fill_ccw
!= c
->key
.fill_cw
&&
424 c
->key
.fill_ccw
!= CLIP_CULL
&&
425 c
->key
.fill_cw
!= CLIP_CULL
)
428 vec1(brw_null_reg()),
430 get_element(c
->reg
.dir
, 2),
433 brw_IF(p
, BRW_EXECUTE_1
);
435 emit_primitives(c
, c
->key
.fill_ccw
, c
->key
.offset_ccw
);
439 emit_primitives(c
, c
->key
.fill_cw
, c
->key
.offset_cw
);
443 else if (c
->key
.fill_cw
!= CLIP_CULL
) {
444 emit_primitives(c
, c
->key
.fill_cw
, c
->key
.offset_cw
);
446 else if (c
->key
.fill_ccw
!= CLIP_CULL
) {
447 emit_primitives(c
, c
->key
.fill_ccw
, c
->key
.offset_ccw
);
454 static void check_nr_verts( struct brw_clip_compile
*c
)
456 struct brw_compile
*p
= &c
->func
;
458 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_L
, c
->reg
.nr_verts
, brw_imm_d(3));
459 brw_IF(p
, BRW_EXECUTE_1
);
461 brw_clip_kill_thread(c
);
467 void brw_emit_unfilled_clip( struct brw_clip_compile
*c
)
469 struct brw_compile
*p
= &c
->func
;
471 c
->need_direction
= ((c
->key
.offset_ccw
|| c
->key
.offset_cw
) ||
472 (c
->key
.fill_ccw
!= c
->key
.fill_cw
) ||
473 c
->key
.fill_ccw
== CLIP_CULL
||
474 c
->key
.fill_cw
== CLIP_CULL
||
475 c
->key
.copy_bfc_cw
||
476 c
->key
.copy_bfc_ccw
);
478 brw_clip_tri_alloc_regs(c
, 3 + c
->key
.nr_userclip
+ 6);
479 brw_clip_tri_init_vertices(c
);
480 brw_clip_init_ff_sync(c
);
482 assert(brw_clip_have_varying(c
, VARYING_SLOT_EDGE
));
484 if (c
->key
.fill_ccw
== CLIP_CULL
&&
485 c
->key
.fill_cw
== CLIP_CULL
) {
486 brw_clip_kill_thread(c
);
492 /* Need to use the inlist indirection here:
494 if (c
->need_direction
)
495 compute_tri_direction(c
);
497 if (c
->key
.fill_ccw
== CLIP_CULL
||
498 c
->key
.fill_cw
== CLIP_CULL
)
501 if (c
->key
.offset_ccw
||
505 if (c
->key
.copy_bfc_ccw
||
509 /* Need to do this whether we clip or not:
511 if (c
->has_flat_shading
)
512 brw_clip_tri_flat_shade(c
);
514 brw_clip_init_clipmask(c
);
515 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
, c
->reg
.planemask
, brw_imm_ud(0));
516 brw_IF(p
, BRW_EXECUTE_1
);
518 brw_clip_init_planes(c
);
524 emit_unfilled_primitives(c
);
525 brw_clip_kill_thread(c
);