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>
33 #include "brw_batchbuffer.h"
35 #include "brw_defines.h"
36 #include "brw_context.h"
42 static struct brw_reg
get_vert_attr(struct brw_sf_compile
*c
,
46 GLuint off
= attr
/ 2;
47 GLuint sub
= attr
% 2;
49 return brw_vec4_grf(vert
.nr
+ off
, sub
* 4);
53 /***********************************************************************
56 static void copy_bfc( struct brw_sf_compile
*c
,
59 struct brw_compile
*p
= &c
->func
;
61 if (c
->key
.attr_col0
&& c
->key
.attr_bfc0
)
63 get_vert_attr(c
, vert
, c
->key
.attr_col0
),
64 get_vert_attr(c
, vert
, c
->key
.attr_bfc0
));
66 if (c
->key
.attr_col1
&& c
->key
.attr_bfc1
)
68 get_vert_attr(c
, vert
, c
->key
.attr_col1
),
69 get_vert_attr(c
, vert
, c
->key
.attr_bfc1
));
73 static void do_twoside_color( struct brw_sf_compile
*c
)
75 struct brw_compile
*p
= &c
->func
;
76 struct brw_instruction
*if_insn
;
77 GLuint backface_conditional
= c
->key
.frontface_ccw
? BRW_CONDITIONAL_G
: BRW_CONDITIONAL_L
;
79 /* Already done in clip program:
81 if (c
->key
.primitive
== SF_UNFILLED_TRIS
)
84 /* XXX: What happens if BFC isn't present? This could only happen
85 * for user-supplied vertex programs, as t_vp_build.c always does
88 if (!(c
->key
.attr_col0
&& c
->key
.attr_bfc0
) &&
89 !(c
->key
.attr_col1
&& c
->key
.attr_bfc1
))
92 /* Need to use BRW_EXECUTE_4 and also do an 4-wide compare in order
93 * to get all channels active inside the IF. In the clipping code
94 * we run with NoMask, so it's not an option and we can use
95 * BRW_EXECUTE_1 for all comparisions.
97 brw_push_insn_state(p
);
98 brw_CMP(p
, vec4(brw_null_reg()), backface_conditional
, c
->det
, brw_imm_f(0));
99 if_insn
= brw_IF(p
, BRW_EXECUTE_4
);
101 switch (c
->nr_verts
) {
102 case 3: copy_bfc(c
, c
->vert
[2]);
103 case 2: copy_bfc(c
, c
->vert
[1]);
104 case 1: copy_bfc(c
, c
->vert
[0]);
107 brw_ENDIF(p
, if_insn
);
108 brw_pop_insn_state(p
);
113 /***********************************************************************
117 #define VERT_RESULT_COLOR_BITS ((1<<VERT_RESULT_COL0) | \
118 (1<<VERT_RESULT_COL1))
120 static void copy_colors( struct brw_sf_compile
*c
,
124 struct brw_compile
*p
= &c
->func
;
126 if (c
->key
.attr_col0
)
128 get_vert_attr(c
, dst
, c
->key
.attr_col0
),
129 get_vert_attr(c
, src
, c
->key
.attr_col0
));
131 if (c
->key
.attr_col1
)
133 get_vert_attr(c
, dst
, c
->key
.attr_col1
),
134 get_vert_attr(c
, src
, c
->key
.attr_col1
));
140 /* Need to use a computed jump to copy flatshaded attributes as the
141 * vertices are ordered according to y-coordinate before reaching this
142 * point, so the PV could be anywhere.
144 static void do_flatshade_triangle( struct brw_sf_compile
*c
)
146 struct brw_compile
*p
= &c
->func
;
147 struct brw_reg ip
= brw_ip_reg();
151 if (c
->key
.attr_col0
)
154 if (c
->key
.attr_col1
)
160 /* Already done in clip program:
162 if (c
->key
.primitive
== SF_UNFILLED_TRIS
)
165 if (BRW_IS_IGDNG(p
->brw
))
168 brw_push_insn_state(p
);
170 brw_MUL(p
, c
->pv
, c
->pv
, brw_imm_d(jmpi
*(nr
*2+1)));
171 brw_JMPI(p
, ip
, ip
, c
->pv
);
173 copy_colors(c
, c
->vert
[1], c
->vert
[0]);
174 copy_colors(c
, c
->vert
[2], c
->vert
[0]);
175 brw_JMPI(p
, ip
, ip
, brw_imm_d(jmpi
*(nr
*4+1)));
177 copy_colors(c
, c
->vert
[0], c
->vert
[1]);
178 copy_colors(c
, c
->vert
[2], c
->vert
[1]);
179 brw_JMPI(p
, ip
, ip
, brw_imm_d(jmpi
*nr
*2));
181 copy_colors(c
, c
->vert
[0], c
->vert
[2]);
182 copy_colors(c
, c
->vert
[1], c
->vert
[2]);
184 brw_pop_insn_state(p
);
188 static void do_flatshade_line( struct brw_sf_compile
*c
)
190 struct brw_compile
*p
= &c
->func
;
191 struct brw_reg ip
= brw_ip_reg();
195 if (c
->key
.attr_col0
)
198 if (c
->key
.attr_col1
)
204 /* Already done in clip program:
206 if (c
->key
.primitive
== SF_UNFILLED_TRIS
)
209 if (BRW_IS_IGDNG(p
->brw
))
212 brw_push_insn_state(p
);
214 brw_MUL(p
, c
->pv
, c
->pv
, brw_imm_d(jmpi
*(nr
+1)));
215 brw_JMPI(p
, ip
, ip
, c
->pv
);
216 copy_colors(c
, c
->vert
[1], c
->vert
[0]);
218 brw_JMPI(p
, ip
, ip
, brw_imm_ud(jmpi
*nr
));
219 copy_colors(c
, c
->vert
[0], c
->vert
[1]);
221 brw_pop_insn_state(p
);
226 /***********************************************************************
231 static void alloc_regs( struct brw_sf_compile
*c
)
235 /* Values computed by fixed function unit:
237 c
->pv
= retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_D
);
238 c
->det
= brw_vec1_grf(1, 2);
239 c
->dx0
= brw_vec1_grf(1, 3);
240 c
->dx2
= brw_vec1_grf(1, 4);
241 c
->dy0
= brw_vec1_grf(1, 5);
242 c
->dy2
= brw_vec1_grf(1, 6);
244 /* z and 1/w passed in seperately:
246 c
->z
[0] = brw_vec1_grf(2, 0);
247 c
->inv_w
[0] = brw_vec1_grf(2, 1);
248 c
->z
[1] = brw_vec1_grf(2, 2);
249 c
->inv_w
[1] = brw_vec1_grf(2, 3);
250 c
->z
[2] = brw_vec1_grf(2, 4);
251 c
->inv_w
[2] = brw_vec1_grf(2, 5);
256 for (i
= 0; i
< c
->nr_verts
; i
++) {
257 c
->vert
[i
] = brw_vec8_grf(reg
, 0);
258 reg
+= c
->nr_attr_regs
;
261 /* Temporaries, allocated after last vertex reg.
263 c
->inv_det
= brw_vec1_grf(reg
, 0); reg
++;
264 c
->a1_sub_a0
= brw_vec8_grf(reg
, 0); reg
++;
265 c
->a2_sub_a0
= brw_vec8_grf(reg
, 0); reg
++;
266 c
->tmp
= brw_vec8_grf(reg
, 0); reg
++;
268 /* Note grf allocation:
270 c
->prog_data
.total_grf
= reg
;
273 /* Outputs of this program - interpolation coefficients for
276 c
->m1Cx
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 1, 0);
277 c
->m2Cy
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 2, 0);
278 c
->m3C0
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 3, 0);
282 static void copy_z_inv_w( struct brw_sf_compile
*c
)
284 struct brw_compile
*p
= &c
->func
;
287 brw_push_insn_state(p
);
289 /* Copy both scalars with a single MOV:
291 for (i
= 0; i
< c
->nr_verts
; i
++)
292 brw_MOV(p
, vec2(suboffset(c
->vert
[i
], 2)), vec2(c
->z
[i
]));
294 brw_pop_insn_state(p
);
298 static void invert_det( struct brw_sf_compile
*c
)
300 /* Looks like we invert all 8 elements just to get 1/det in
305 BRW_MATH_FUNCTION_INV
,
306 BRW_MATH_SATURATE_NONE
,
309 BRW_MATH_DATA_SCALAR
,
310 BRW_MATH_PRECISION_FULL
);
315 /* Two attributes packed into a wide register. Figure out if either
316 * or both of them need linear/perspective interpolation. Constant
317 * regs are left as-is.
319 static GLboolean
calculate_masks( struct brw_sf_compile
*c
,
325 GLboolean is_last_attr
= (reg
== c
->nr_setup_regs
- 1);
326 GLuint persp_mask
= c
->key
.persp_attrs
;
327 GLuint linear_mask
= (c
->key
.persp_attrs
| c
->key
.linear_attrs
);
333 if (persp_mask
& (1 << (reg
*2)))
336 if (linear_mask
& (1 << (reg
*2)))
339 /* Maybe only processs one attribute on the final round:
341 if (reg
*2+1 < c
->nr_setup_attrs
) {
344 if (persp_mask
& (1 << (reg
*2+1)))
347 if (linear_mask
& (1 << (reg
*2+1)))
355 void brw_emit_null_setup( struct brw_sf_compile
*c
)
357 struct brw_compile
*p
= &c
->func
;
359 /* m0 is implicitly copied from r0 in the send instruction:
364 brw_vec8_grf(0, 0), /* r0, will be copied to m0 */
368 0, /* response len */
370 1, /* writes complete */
372 BRW_URB_SWIZZLE_TRANSPOSE
);
375 void brw_emit_tri_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
377 struct brw_compile
*p
= &c
->func
;
388 if (c
->key
.do_twoside_color
)
391 if (c
->key
.do_flat_shading
)
392 do_flatshade_triangle(c
);
395 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
397 /* Pair of incoming attributes:
399 struct brw_reg a0
= offset(c
->vert
[0], i
);
400 struct brw_reg a1
= offset(c
->vert
[1], i
);
401 struct brw_reg a2
= offset(c
->vert
[2], i
);
402 GLushort pc
, pc_persp
, pc_linear
;
403 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
407 brw_set_predicate_control_flag_value(p
, pc_persp
);
408 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
409 brw_MUL(p
, a1
, a1
, c
->inv_w
[1]);
410 brw_MUL(p
, a2
, a2
, c
->inv_w
[2]);
414 /* Calculate coefficients for interpolated values:
418 brw_set_predicate_control_flag_value(p
, pc_linear
);
420 brw_ADD(p
, c
->a1_sub_a0
, a1
, negate(a0
));
421 brw_ADD(p
, c
->a2_sub_a0
, a2
, negate(a0
));
425 brw_MUL(p
, brw_null_reg(), c
->a1_sub_a0
, c
->dy2
);
426 brw_MAC(p
, c
->tmp
, c
->a2_sub_a0
, negate(c
->dy0
));
427 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_det
);
431 brw_MUL(p
, brw_null_reg(), c
->a2_sub_a0
, c
->dx0
);
432 brw_MAC(p
, c
->tmp
, c
->a1_sub_a0
, negate(c
->dx2
));
433 brw_MUL(p
, c
->m2Cy
, c
->tmp
, c
->inv_det
);
437 brw_set_predicate_control_flag_value(p
, pc
);
438 /* start point for interpolation
440 brw_MOV(p
, c
->m3C0
, a0
);
442 /* Copy m0..m3 to URB. m0 is implicitly copied from r0 in
443 * the send instruction:
448 brw_vec8_grf(0, 0), /* r0, will be copied to m0 */
452 0, /* response len */
454 last
, /* writes complete */
456 BRW_URB_SWIZZLE_TRANSPOSE
); /* XXX: Swizzle control "SF to windower" */
463 void brw_emit_line_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
465 struct brw_compile
*p
= &c
->func
;
477 if (c
->key
.do_flat_shading
)
478 do_flatshade_line(c
);
480 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
482 /* Pair of incoming attributes:
484 struct brw_reg a0
= offset(c
->vert
[0], i
);
485 struct brw_reg a1
= offset(c
->vert
[1], i
);
486 GLushort pc
, pc_persp
, pc_linear
;
487 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
491 brw_set_predicate_control_flag_value(p
, pc_persp
);
492 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
493 brw_MUL(p
, a1
, a1
, c
->inv_w
[1]);
496 /* Calculate coefficients for position, color:
499 brw_set_predicate_control_flag_value(p
, pc_linear
);
501 brw_ADD(p
, c
->a1_sub_a0
, a1
, negate(a0
));
503 brw_MUL(p
, c
->tmp
, c
->a1_sub_a0
, c
->dx0
);
504 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_det
);
506 brw_MUL(p
, c
->tmp
, c
->a1_sub_a0
, c
->dy0
);
507 brw_MUL(p
, c
->m2Cy
, c
->tmp
, c
->inv_det
);
511 brw_set_predicate_control_flag_value(p
, pc
);
513 /* start point for interpolation
515 brw_MOV(p
, c
->m3C0
, a0
);
517 /* Copy m0..m3 to URB.
526 0, /* response len */
528 last
, /* writes complete */
529 i
*4, /* urb destination offset */
530 BRW_URB_SWIZZLE_TRANSPOSE
);
535 void brw_emit_point_sprite_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
537 struct brw_compile
*p
= &c
->func
;
547 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
549 /* XXX: only seems to check point_coord_replace_attrs for every
550 * second attribute?!?
552 boolean coord_replace
= !!(c
->key
.point_coord_replace_attrs
& (1<<(2*i
)));
553 struct brw_reg a0
= offset(c
->vert
[0], i
);
554 GLushort pc
, pc_persp
, pc_linear
;
555 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
560 brw_set_predicate_control_flag_value(p
, pc_persp
);
561 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
566 /* Caculate 1.0/PointWidth */
569 BRW_MATH_FUNCTION_INV
,
570 BRW_MATH_SATURATE_NONE
,
573 BRW_MATH_DATA_SCALAR
,
574 BRW_MATH_PRECISION_FULL
);
576 if (c
->key
.sprite_origin_lower_left
) {
577 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_w
[0]);
578 brw_MOV(p
, vec1(suboffset(c
->m1Cx
, 1)), brw_imm_f(0.0));
579 brw_MUL(p
, c
->m2Cy
, c
->tmp
, negate(c
->inv_w
[0]));
580 brw_MOV(p
, vec1(suboffset(c
->m2Cy
, 0)), brw_imm_f(0.0));
583 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_w
[0]);
584 brw_MOV(p
, vec1(suboffset(c
->m1Cx
, 1)), brw_imm_f(0.0));
585 brw_MUL(p
, c
->m2Cy
, c
->tmp
, c
->inv_w
[0]);
586 brw_MOV(p
, vec1(suboffset(c
->m2Cy
, 0)), brw_imm_f(0.0));
590 brw_MOV(p
, c
->m1Cx
, brw_imm_ud(0));
591 brw_MOV(p
, c
->m2Cy
, brw_imm_ud(0));
595 brw_set_predicate_control_flag_value(p
, pc
);
597 if (c
->key
.sprite_origin_lower_left
) {
598 brw_MUL(p
, c
->m3C0
, c
->inv_w
[0], brw_imm_f(1.0));
599 brw_MOV(p
, vec1(suboffset(c
->m3C0
, 0)), brw_imm_f(0.0));
602 brw_MOV(p
, c
->m3C0
, brw_imm_f(0.0));
606 brw_MOV(p
, c
->m3C0
, a0
); /* constant value */
609 /* Copy m0..m3 to URB.
618 0, /* response len */
620 last
, /* writes complete */
621 i
*4, /* urb destination offset */
622 BRW_URB_SWIZZLE_TRANSPOSE
);
627 /* Points setup - several simplifications as all attributes are
628 * constant across the face of the point (point sprites excluded!)
630 void brw_emit_point_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
632 struct brw_compile
*p
= &c
->func
;
642 brw_MOV(p
, c
->m1Cx
, brw_imm_ud(0)); /* zero - move out of loop */
643 brw_MOV(p
, c
->m2Cy
, brw_imm_ud(0)); /* zero - move out of loop */
645 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
647 struct brw_reg a0
= offset(c
->vert
[0], i
);
648 GLushort pc
, pc_persp
, pc_linear
;
649 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
653 /* This seems odd as the values are all constant, but the
654 * fragment shader will be expecting it:
656 brw_set_predicate_control_flag_value(p
, pc_persp
);
657 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
661 /* The delta values are always zero, just send the starting
662 * coordinate. Again, this is to fit in with the interpolation
663 * code in the fragment shader.
666 brw_set_predicate_control_flag_value(p
, pc
);
668 brw_MOV(p
, c
->m3C0
, a0
); /* constant value */
670 /* Copy m0..m3 to URB.
679 0, /* response len */
681 last
, /* writes complete */
682 i
*4, /* urb destination offset */
683 BRW_URB_SWIZZLE_TRANSPOSE
);
688 void brw_emit_anyprim_setup( struct brw_sf_compile
*c
)
690 struct brw_compile
*p
= &c
->func
;
691 struct brw_reg ip
= brw_ip_reg();
692 struct brw_reg payload_prim
= brw_uw1_reg(BRW_GENERAL_REGISTER_FILE
, 1, 0);
693 struct brw_reg payload_attr
= get_element_ud(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE
, 1, 0), 0);
694 struct brw_reg primmask
;
695 struct brw_instruction
*jmp
;
696 struct brw_reg v1_null_ud
= vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
));
703 primmask
= retype(get_element(c
->tmp
, 0), BRW_REGISTER_TYPE_UD
);
705 brw_MOV(p
, primmask
, brw_imm_ud(1));
706 brw_SHL(p
, primmask
, primmask
, payload_prim
);
708 brw_set_conditionalmod(p
, BRW_CONDITIONAL_Z
);
709 brw_AND(p
, v1_null_ud
, primmask
, brw_imm_ud((1<<_3DPRIM_TRILIST
) |
710 (1<<_3DPRIM_TRISTRIP
) |
711 (1<<_3DPRIM_TRIFAN
) |
712 (1<<_3DPRIM_TRISTRIP_REVERSE
) |
713 (1<<_3DPRIM_POLYGON
) |
714 (1<<_3DPRIM_RECTLIST
) |
715 (1<<_3DPRIM_TRIFAN_NOSTIPPLE
)));
716 jmp
= brw_JMPI(p
, ip
, ip
, brw_imm_d(0));
718 saveflag
= p
->flag_value
;
719 brw_push_insn_state(p
);
720 brw_emit_tri_setup( c
, GL_FALSE
);
721 brw_pop_insn_state(p
);
722 p
->flag_value
= saveflag
;
723 /* note - thread killed in subroutine, so must
724 * restore the flag which is changed when building
725 * the subroutine. fix #13240
728 brw_land_fwd_jump(p
, jmp
);
730 brw_set_conditionalmod(p
, BRW_CONDITIONAL_Z
);
731 brw_AND(p
, v1_null_ud
, primmask
, brw_imm_ud((1<<_3DPRIM_LINELIST
) |
732 (1<<_3DPRIM_LINESTRIP
) |
733 (1<<_3DPRIM_LINELOOP
) |
734 (1<<_3DPRIM_LINESTRIP_CONT
) |
735 (1<<_3DPRIM_LINESTRIP_BF
) |
736 (1<<_3DPRIM_LINESTRIP_CONT_BF
)));
737 jmp
= brw_JMPI(p
, ip
, ip
, brw_imm_d(0));
739 saveflag
= p
->flag_value
;
740 brw_push_insn_state(p
);
741 brw_emit_line_setup( c
, GL_FALSE
);
742 brw_pop_insn_state(p
);
743 p
->flag_value
= saveflag
;
744 /* note - thread killed in subroutine */
746 brw_land_fwd_jump(p
, jmp
);
748 brw_set_conditionalmod(p
, BRW_CONDITIONAL_Z
);
749 brw_AND(p
, v1_null_ud
, payload_attr
, brw_imm_ud(1<<BRW_SPRITE_POINT_ENABLE
));
750 jmp
= brw_JMPI(p
, ip
, ip
, brw_imm_d(0));
752 saveflag
= p
->flag_value
;
753 brw_push_insn_state(p
);
754 brw_emit_point_sprite_setup( c
, GL_FALSE
);
755 brw_pop_insn_state(p
);
756 p
->flag_value
= saveflag
;
758 brw_land_fwd_jump(p
, jmp
);
760 brw_emit_point_setup( c
, GL_FALSE
);