2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) 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 <keith@tungstengraphics.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_vert_result_to_offset(&c
->vue_map
,
57 struct brw_reg v0
= byte_offset(c
->reg
.vertex
[0], hpos_offset
);
58 struct brw_reg v1
= byte_offset(c
->reg
.vertex
[1], hpos_offset
);
59 struct brw_reg v2
= byte_offset(c
->reg
.vertex
[2], hpos_offset
);
62 struct brw_reg v0n
= get_tmp(c
);
63 struct brw_reg v1n
= get_tmp(c
);
64 struct brw_reg v2n
= get_tmp(c
);
67 * NOTE: We can't modify the original vertex coordinates,
68 * as it may impact further operations.
69 * So, we have to keep normalized coordinates in temp registers.
72 * Try to optimize unnecessary MOV's.
78 brw_clip_project_position(c
, v0n
);
79 brw_clip_project_position(c
, v1n
);
80 brw_clip_project_position(c
, v2n
);
82 /* Calculate the vectors of two edges of the triangle:
84 brw_ADD(p
, e
, v0n
, negate(v2n
));
85 brw_ADD(p
, f
, v1n
, negate(v2n
));
87 /* Take their crossproduct:
89 brw_set_access_mode(p
, BRW_ALIGN_16
);
90 brw_MUL(p
, vec4(brw_null_reg()), brw_swizzle(e
, 1,2,0,3), brw_swizzle(f
,2,0,1,3));
91 brw_MAC(p
, vec4(e
), negate(brw_swizzle(e
, 2,0,1,3)), brw_swizzle(f
,1,2,0,3));
92 brw_set_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_compile
*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_compile
*p
= &c
->func
;
131 /* Do we have any colors to copy?
133 if (!(c
->offset
[VERT_RESULT_COL0
] && c
->offset
[VERT_RESULT_BFC0
]) &&
134 !(c
->offset
[VERT_RESULT_COL1
] && c
->offset
[VERT_RESULT_BFC1
]))
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.
141 if (c
->key
.copy_bfc_ccw
)
142 conditional
= BRW_CONDITIONAL_GE
;
144 conditional
= BRW_CONDITIONAL_L
;
147 vec1(brw_null_reg()),
149 get_element(c
->reg
.dir
, 2),
152 brw_IF(p
, BRW_EXECUTE_1
);
156 for (i
= 0; i
< 3; i
++) {
157 if (c
->offset
[VERT_RESULT_COL0
] && c
->offset
[VERT_RESULT_BFC0
])
159 byte_offset(c
->reg
.vertex
[i
], c
->offset
[VERT_RESULT_COL0
]),
160 byte_offset(c
->reg
.vertex
[i
], c
->offset
[VERT_RESULT_BFC0
]));
162 if (c
->offset
[VERT_RESULT_COL1
] && c
->offset
[VERT_RESULT_BFC1
])
164 byte_offset(c
->reg
.vertex
[i
], c
->offset
[VERT_RESULT_COL1
]),
165 byte_offset(c
->reg
.vertex
[i
], c
->offset
[VERT_RESULT_BFC1
]));
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;
182 static void compute_offset( struct brw_clip_compile
*c
)
184 struct brw_compile
*p
= &c
->func
;
185 struct brw_reg off
= c
->reg
.offset
;
186 struct brw_reg dir
= c
->reg
.dir
;
188 brw_math_invert(p
, get_element(off
, 2), get_element(dir
, 2));
189 brw_MUL(p
, vec2(off
), dir
, get_element(off
, 2));
192 vec1(brw_null_reg()),
194 brw_abs(get_element(off
, 0)),
195 brw_abs(get_element(off
, 1)));
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
);
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
));
205 static void merge_edgeflags( struct brw_clip_compile
*c
)
207 struct brw_compile
*p
= &c
->func
;
208 struct brw_reg tmp0
= get_element_ud(c
->reg
.tmp0
, 0);
210 brw_AND(p
, tmp0
, get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(PRIM_MASK
));
212 vec1(brw_null_reg()),
215 brw_imm_ud(_3DPRIM_POLYGON
));
217 /* Get away with using reg.vertex because we know that this is not
218 * a _3DPRIM_TRISTRIP_REVERSE:
220 brw_IF(p
, BRW_EXECUTE_1
);
222 brw_set_conditionalmod(p
, BRW_CONDITIONAL_EQ
);
223 brw_AND(p
, vec1(brw_null_reg()), get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(1<<8));
224 brw_MOV(p
, byte_offset(c
->reg
.vertex
[0], c
->offset
[VERT_RESULT_EDGE
]), brw_imm_f(0));
225 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
227 brw_set_conditionalmod(p
, BRW_CONDITIONAL_EQ
);
228 brw_AND(p
, vec1(brw_null_reg()), get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(1<<9));
229 brw_MOV(p
, byte_offset(c
->reg
.vertex
[2], c
->offset
[VERT_RESULT_EDGE
]), brw_imm_f(0));
230 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
237 static void apply_one_offset( struct brw_clip_compile
*c
,
238 struct brw_indirect vert
)
240 struct brw_compile
*p
= &c
->func
;
241 GLuint ndc_offset
= brw_vert_result_to_offset(&c
->vue_map
,
242 BRW_VERT_RESULT_NDC
);
243 struct brw_reg z
= deref_1f(vert
, ndc_offset
+
244 2 * type_sz(BRW_REGISTER_TYPE_F
));
246 brw_ADD(p
, z
, z
, vec1(c
->reg
.offset
));
251 /***********************************************************************
252 * Output clipped polygon as an unfilled primitive:
254 static void emit_lines(struct brw_clip_compile
*c
,
257 struct brw_compile
*p
= &c
->func
;
258 struct brw_instruction
*loop
;
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);
264 /* Need a seperate loop for 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
));
270 loop
= brw_DO(p
, BRW_EXECUTE_1
);
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));
275 apply_one_offset(c
, v0
);
277 brw_set_conditionalmod(p
, BRW_CONDITIONAL_G
);
278 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
283 /* v1ptr = &inlist[nr_verts]
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));
292 loop
= brw_DO(p
, BRW_EXECUTE_1
);
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));
298 /* draw edge if edgeflag != 0 */
300 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
,
301 deref_1f(v0
, c
->offset
[VERT_RESULT_EDGE
]),
303 brw_IF(p
, BRW_EXECUTE_1
);
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
);
310 brw_set_conditionalmod(p
, BRW_CONDITIONAL_NZ
);
311 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
318 static void emit_points(struct brw_clip_compile
*c
,
319 GLboolean do_offset
)
321 struct brw_compile
*p
= &c
->func
;
322 struct brw_instruction
*loop
;
324 struct brw_indirect v0
= brw_indirect(0, 0);
325 struct brw_indirect v0ptr
= brw_indirect(2, 0);
327 brw_MOV(p
, c
->reg
.loopcount
, c
->reg
.nr_verts
);
328 brw_MOV(p
, get_addr_reg(v0ptr
), brw_address(c
->reg
.inlist
));
330 loop
= brw_DO(p
, BRW_EXECUTE_1
);
332 brw_MOV(p
, get_addr_reg(v0
), deref_1uw(v0ptr
, 0));
333 brw_ADD(p
, get_addr_reg(v0ptr
), get_addr_reg(v0ptr
), brw_imm_uw(2));
335 /* draw if edgeflag != 0
338 vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
,
339 deref_1f(v0
, c
->offset
[VERT_RESULT_EDGE
]),
341 brw_IF(p
, BRW_EXECUTE_1
);
344 apply_one_offset(c
, v0
);
346 brw_clip_emit_vue(c
, v0
, 1, 0, (_3DPRIM_POINTLIST
<< 2) | R02_PRIM_START
| R02_PRIM_END
);
350 brw_set_conditionalmod(p
, BRW_CONDITIONAL_NZ
);
351 brw_ADD(p
, c
->reg
.loopcount
, c
->reg
.loopcount
, brw_imm_d(-1));
362 static void emit_primitives( struct brw_clip_compile
*c
,
364 GLboolean do_offset
)
368 brw_clip_tri_emit_polygon(c
);
372 emit_lines(c
, do_offset
);
376 emit_points(c
, do_offset
);
387 static void emit_unfilled_primitives( struct brw_clip_compile
*c
)
389 struct brw_compile
*p
= &c
->func
;
391 /* Direction culling has already been done.
393 if (c
->key
.fill_ccw
!= c
->key
.fill_cw
&&
394 c
->key
.fill_ccw
!= CLIP_CULL
&&
395 c
->key
.fill_cw
!= CLIP_CULL
)
398 vec1(brw_null_reg()),
400 get_element(c
->reg
.dir
, 2),
403 brw_IF(p
, BRW_EXECUTE_1
);
405 emit_primitives(c
, c
->key
.fill_ccw
, c
->key
.offset_ccw
);
409 emit_primitives(c
, c
->key
.fill_cw
, c
->key
.offset_cw
);
413 else if (c
->key
.fill_cw
!= CLIP_CULL
) {
414 emit_primitives(c
, c
->key
.fill_cw
, c
->key
.offset_cw
);
416 else if (c
->key
.fill_ccw
!= CLIP_CULL
) {
417 emit_primitives(c
, c
->key
.fill_ccw
, c
->key
.offset_ccw
);
424 static void check_nr_verts( struct brw_clip_compile
*c
)
426 struct brw_compile
*p
= &c
->func
;
428 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_L
, c
->reg
.nr_verts
, brw_imm_d(3));
429 brw_IF(p
, BRW_EXECUTE_1
);
431 brw_clip_kill_thread(c
);
437 void brw_emit_unfilled_clip( struct brw_clip_compile
*c
)
439 struct brw_compile
*p
= &c
->func
;
441 c
->need_direction
= ((c
->key
.offset_ccw
|| c
->key
.offset_cw
) ||
442 (c
->key
.fill_ccw
!= c
->key
.fill_cw
) ||
443 c
->key
.fill_ccw
== CLIP_CULL
||
444 c
->key
.fill_cw
== CLIP_CULL
||
445 c
->key
.copy_bfc_cw
||
446 c
->key
.copy_bfc_ccw
);
448 brw_clip_tri_alloc_regs(c
, 3 + c
->key
.nr_userclip
+ 6);
449 brw_clip_tri_init_vertices(c
);
450 brw_clip_init_ff_sync(c
);
452 assert(c
->offset
[VERT_RESULT_EDGE
]);
454 if (c
->key
.fill_ccw
== CLIP_CULL
&&
455 c
->key
.fill_cw
== CLIP_CULL
) {
456 brw_clip_kill_thread(c
);
462 /* Need to use the inlist indirection here:
464 if (c
->need_direction
)
465 compute_tri_direction(c
);
467 if (c
->key
.fill_ccw
== CLIP_CULL
||
468 c
->key
.fill_cw
== CLIP_CULL
)
471 if (c
->key
.offset_ccw
||
475 if (c
->key
.copy_bfc_ccw
||
479 /* Need to do this whether we clip or not:
481 if (c
->key
.do_flat_shading
)
482 brw_clip_tri_flat_shade(c
);
484 brw_clip_init_clipmask(c
);
485 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_NZ
, c
->reg
.planemask
, brw_imm_ud(0));
486 brw_IF(p
, BRW_EXECUTE_1
);
488 brw_clip_init_planes(c
);
494 emit_unfilled_primitives(c
);
495 brw_clip_kill_thread(c
);