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 "main/glheader.h"
34 #include "main/macros.h"
35 #include "main/enums.h"
37 #include "intel_batchbuffer.h"
39 #include "brw_defines.h"
40 #include "brw_context.h"
46 static struct brw_reg
get_vert_attr(struct brw_sf_compile
*c
,
50 GLuint off
= c
->attr_to_idx
[attr
] / 2;
51 GLuint sub
= c
->attr_to_idx
[attr
] % 2;
53 return brw_vec4_grf(vert
.nr
+ off
, sub
* 4);
56 static GLboolean
have_attr(struct brw_sf_compile
*c
,
59 return (c
->key
.attrs
& (1<<attr
)) ? 1 : 0;
62 /***********************************************************************
65 static void copy_bfc( struct brw_sf_compile
*c
,
68 struct brw_compile
*p
= &c
->func
;
71 for (i
= 0; i
< 2; i
++) {
72 if (have_attr(c
, VERT_RESULT_COL0
+i
) &&
73 have_attr(c
, VERT_RESULT_BFC0
+i
))
75 get_vert_attr(c
, vert
, VERT_RESULT_COL0
+i
),
76 get_vert_attr(c
, vert
, VERT_RESULT_BFC0
+i
));
81 static void do_twoside_color( struct brw_sf_compile
*c
)
83 struct brw_compile
*p
= &c
->func
;
84 struct brw_instruction
*if_insn
;
85 GLuint backface_conditional
= c
->key
.frontface_ccw
? BRW_CONDITIONAL_G
: BRW_CONDITIONAL_L
;
87 /* Already done in clip program:
89 if (c
->key
.primitive
== SF_UNFILLED_TRIS
)
92 /* XXX: What happens if BFC isn't present? This could only happen
93 * for user-supplied vertex programs, as t_vp_build.c always does
96 if (!(have_attr(c
, VERT_RESULT_COL0
) && have_attr(c
, VERT_RESULT_BFC0
)) &&
97 !(have_attr(c
, VERT_RESULT_COL1
) && have_attr(c
, VERT_RESULT_BFC1
)))
100 /* Need to use BRW_EXECUTE_4 and also do an 4-wide compare in order
101 * to get all channels active inside the IF. In the clipping code
102 * we run with NoMask, so it's not an option and we can use
103 * BRW_EXECUTE_1 for all comparisions.
105 brw_push_insn_state(p
);
106 brw_CMP(p
, vec4(brw_null_reg()), backface_conditional
, c
->det
, brw_imm_f(0));
107 if_insn
= brw_IF(p
, BRW_EXECUTE_4
);
109 switch (c
->nr_verts
) {
110 case 3: copy_bfc(c
, c
->vert
[2]);
111 case 2: copy_bfc(c
, c
->vert
[1]);
112 case 1: copy_bfc(c
, c
->vert
[0]);
115 brw_ENDIF(p
, if_insn
);
116 brw_pop_insn_state(p
);
121 /***********************************************************************
125 #define VERT_RESULT_COLOR_BITS ((1<<VERT_RESULT_COL0) | \
126 (1<<VERT_RESULT_COL1))
128 static void copy_colors( struct brw_sf_compile
*c
,
132 struct brw_compile
*p
= &c
->func
;
135 for (i
= VERT_RESULT_COL0
; i
<= VERT_RESULT_COL1
; i
++) {
138 get_vert_attr(c
, dst
, i
),
139 get_vert_attr(c
, src
, i
));
145 /* Need to use a computed jump to copy flatshaded attributes as the
146 * vertices are ordered according to y-coordinate before reaching this
147 * point, so the PV could be anywhere.
149 static void do_flatshade_triangle( struct brw_sf_compile
*c
)
151 struct brw_compile
*p
= &c
->func
;
152 struct brw_reg ip
= brw_ip_reg();
153 GLuint nr
= brw_count_bits(c
->key
.attrs
& VERT_RESULT_COLOR_BITS
);
157 /* Already done in clip program:
159 if (c
->key
.primitive
== SF_UNFILLED_TRIS
)
162 brw_push_insn_state(p
);
164 brw_MUL(p
, c
->pv
, c
->pv
, brw_imm_ud(nr
*2+1));
165 brw_JMPI(p
, ip
, ip
, c
->pv
);
167 copy_colors(c
, c
->vert
[1], c
->vert
[0]);
168 copy_colors(c
, c
->vert
[2], c
->vert
[0]);
169 brw_JMPI(p
, ip
, ip
, brw_imm_ud(nr
*4+1));
171 copy_colors(c
, c
->vert
[0], c
->vert
[1]);
172 copy_colors(c
, c
->vert
[2], c
->vert
[1]);
173 brw_JMPI(p
, ip
, ip
, brw_imm_ud(nr
*2));
175 copy_colors(c
, c
->vert
[0], c
->vert
[2]);
176 copy_colors(c
, c
->vert
[1], c
->vert
[2]);
178 brw_pop_insn_state(p
);
182 static void do_flatshade_line( struct brw_sf_compile
*c
)
184 struct brw_compile
*p
= &c
->func
;
185 struct brw_reg ip
= brw_ip_reg();
186 GLuint nr
= brw_count_bits(c
->key
.attrs
& VERT_RESULT_COLOR_BITS
);
191 /* Already done in clip program:
193 if (c
->key
.primitive
== SF_UNFILLED_TRIS
)
196 brw_push_insn_state(p
);
198 brw_MUL(p
, c
->pv
, c
->pv
, brw_imm_ud(nr
+1));
199 brw_JMPI(p
, ip
, ip
, c
->pv
);
200 copy_colors(c
, c
->vert
[1], c
->vert
[0]);
202 brw_JMPI(p
, ip
, ip
, brw_imm_ud(nr
));
203 copy_colors(c
, c
->vert
[0], c
->vert
[1]);
205 brw_pop_insn_state(p
);
210 /***********************************************************************
215 static void alloc_regs( struct brw_sf_compile
*c
)
219 /* Values computed by fixed function unit:
221 c
->pv
= retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_UD
);
222 c
->det
= brw_vec1_grf(1, 2);
223 c
->dx0
= brw_vec1_grf(1, 3);
224 c
->dx2
= brw_vec1_grf(1, 4);
225 c
->dy0
= brw_vec1_grf(1, 5);
226 c
->dy2
= brw_vec1_grf(1, 6);
228 /* z and 1/w passed in seperately:
230 c
->z
[0] = brw_vec1_grf(2, 0);
231 c
->inv_w
[0] = brw_vec1_grf(2, 1);
232 c
->z
[1] = brw_vec1_grf(2, 2);
233 c
->inv_w
[1] = brw_vec1_grf(2, 3);
234 c
->z
[2] = brw_vec1_grf(2, 4);
235 c
->inv_w
[2] = brw_vec1_grf(2, 5);
240 for (i
= 0; i
< c
->nr_verts
; i
++) {
241 c
->vert
[i
] = brw_vec8_grf(reg
, 0);
242 reg
+= c
->nr_attr_regs
;
245 /* Temporaries, allocated after last vertex reg.
247 c
->inv_det
= brw_vec1_grf(reg
, 0); reg
++;
248 c
->a1_sub_a0
= brw_vec8_grf(reg
, 0); reg
++;
249 c
->a2_sub_a0
= brw_vec8_grf(reg
, 0); reg
++;
250 c
->tmp
= brw_vec8_grf(reg
, 0); reg
++;
252 /* Note grf allocation:
254 c
->prog_data
.total_grf
= reg
;
257 /* Outputs of this program - interpolation coefficients for
260 c
->m1Cx
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 1, 0);
261 c
->m2Cy
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 2, 0);
262 c
->m3C0
= brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
, 3, 0);
266 static void copy_z_inv_w( struct brw_sf_compile
*c
)
268 struct brw_compile
*p
= &c
->func
;
271 brw_push_insn_state(p
);
273 /* Copy both scalars with a single MOV:
275 for (i
= 0; i
< c
->nr_verts
; i
++)
276 brw_MOV(p
, vec2(suboffset(c
->vert
[i
], 2)), vec2(c
->z
[i
]));
278 brw_pop_insn_state(p
);
282 static void invert_det( struct brw_sf_compile
*c
)
284 /* Looks like we invert all 8 elements just to get 1/det in
289 BRW_MATH_FUNCTION_INV
,
290 BRW_MATH_SATURATE_NONE
,
293 BRW_MATH_DATA_SCALAR
,
294 BRW_MATH_PRECISION_FULL
);
298 #define NON_PERPECTIVE_ATTRS (FRAG_BIT_WPOS | \
302 static GLboolean
calculate_masks( struct brw_sf_compile
*c
,
308 GLboolean is_last_attr
= (reg
== c
->nr_setup_regs
- 1);
309 GLuint persp_mask
= c
->key
.attrs
& ~NON_PERPECTIVE_ATTRS
;
312 if (c
->key
.do_flat_shading
)
313 linear_mask
= c
->key
.attrs
& ~(FRAG_BIT_COL0
|FRAG_BIT_COL1
);
315 linear_mask
= c
->key
.attrs
;
321 if (persp_mask
& (1 << c
->idx_to_attr
[reg
*2]))
324 if (linear_mask
& (1 << c
->idx_to_attr
[reg
*2]))
327 /* Maybe only processs one attribute on the final round:
329 if (reg
*2+1 < c
->nr_setup_attrs
) {
332 if (persp_mask
& (1 << c
->idx_to_attr
[reg
*2+1]))
335 if (linear_mask
& (1 << c
->idx_to_attr
[reg
*2+1]))
344 void brw_emit_tri_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
346 struct brw_compile
*p
= &c
->func
;
357 if (c
->key
.do_twoside_color
)
360 if (c
->key
.do_flat_shading
)
361 do_flatshade_triangle(c
);
364 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
366 /* Pair of incoming attributes:
368 struct brw_reg a0
= offset(c
->vert
[0], i
);
369 struct brw_reg a1
= offset(c
->vert
[1], i
);
370 struct brw_reg a2
= offset(c
->vert
[2], i
);
371 GLushort pc
, pc_persp
, pc_linear
;
372 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
376 brw_set_predicate_control_flag_value(p
, pc_persp
);
377 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
378 brw_MUL(p
, a1
, a1
, c
->inv_w
[1]);
379 brw_MUL(p
, a2
, a2
, c
->inv_w
[2]);
383 /* Calculate coefficients for interpolated values:
387 brw_set_predicate_control_flag_value(p
, pc_linear
);
389 brw_ADD(p
, c
->a1_sub_a0
, a1
, negate(a0
));
390 brw_ADD(p
, c
->a2_sub_a0
, a2
, negate(a0
));
394 brw_MUL(p
, brw_null_reg(), c
->a1_sub_a0
, c
->dy2
);
395 brw_MAC(p
, c
->tmp
, c
->a2_sub_a0
, negate(c
->dy0
));
396 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_det
);
400 brw_MUL(p
, brw_null_reg(), c
->a2_sub_a0
, c
->dx0
);
401 brw_MAC(p
, c
->tmp
, c
->a1_sub_a0
, negate(c
->dx2
));
402 brw_MUL(p
, c
->m2Cy
, c
->tmp
, c
->inv_det
);
406 brw_set_predicate_control_flag_value(p
, pc
);
407 /* start point for interpolation
409 brw_MOV(p
, c
->m3C0
, a0
);
411 /* Copy m0..m3 to URB. m0 is implicitly copied from r0 in
412 * the send instruction:
417 brw_vec8_grf(0, 0), /* r0, will be copied to m0 */
421 0, /* response len */
423 last
, /* writes complete */
425 BRW_URB_SWIZZLE_TRANSPOSE
); /* XXX: Swizzle control "SF to windower" */
432 void brw_emit_line_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
434 struct brw_compile
*p
= &c
->func
;
446 if (c
->key
.do_flat_shading
)
447 do_flatshade_line(c
);
449 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
451 /* Pair of incoming attributes:
453 struct brw_reg a0
= offset(c
->vert
[0], i
);
454 struct brw_reg a1
= offset(c
->vert
[1], i
);
455 GLushort pc
, pc_persp
, pc_linear
;
456 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
460 brw_set_predicate_control_flag_value(p
, pc_persp
);
461 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
462 brw_MUL(p
, a1
, a1
, c
->inv_w
[1]);
465 /* Calculate coefficients for position, color:
468 brw_set_predicate_control_flag_value(p
, pc_linear
);
470 brw_ADD(p
, c
->a1_sub_a0
, a1
, negate(a0
));
472 brw_MUL(p
, c
->tmp
, c
->a1_sub_a0
, c
->dx0
);
473 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_det
);
475 brw_MUL(p
, c
->tmp
, c
->a1_sub_a0
, c
->dy0
);
476 brw_MUL(p
, c
->m2Cy
, c
->tmp
, c
->inv_det
);
480 brw_set_predicate_control_flag_value(p
, pc
);
482 /* start point for interpolation
484 brw_MOV(p
, c
->m3C0
, a0
);
486 /* Copy m0..m3 to URB.
495 0, /* response len */
497 last
, /* writes complete */
498 i
*4, /* urb destination offset */
499 BRW_URB_SWIZZLE_TRANSPOSE
);
504 void brw_emit_point_sprite_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
506 struct brw_compile
*p
= &c
->func
;
515 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
517 struct brw_sf_point_tex
*tex
= &c
->point_attrs
[c
->idx_to_attr
[2*i
]];
518 struct brw_reg a0
= offset(c
->vert
[0], i
);
519 GLushort pc
, pc_persp
, pc_linear
;
520 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
524 if (!tex
->CoordReplace
) {
525 brw_set_predicate_control_flag_value(p
, pc_persp
);
526 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
530 if (tex
->CoordReplace
) {
531 /* Caculate 1.0/PointWidth */
534 BRW_MATH_FUNCTION_INV
,
535 BRW_MATH_SATURATE_NONE
,
538 BRW_MATH_DATA_SCALAR
,
539 BRW_MATH_PRECISION_FULL
);
541 if (c
->key
.SpriteOrigin
== GL_LOWER_LEFT
) {
542 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_w
[0]);
543 brw_MOV(p
, vec1(suboffset(c
->m1Cx
, 1)), brw_imm_f(0.0));
544 brw_MUL(p
, c
->m2Cy
, c
->tmp
, negate(c
->inv_w
[0]));
545 brw_MOV(p
, vec1(suboffset(c
->m2Cy
, 0)), brw_imm_f(0.0));
547 brw_MUL(p
, c
->m1Cx
, c
->tmp
, c
->inv_w
[0]);
548 brw_MOV(p
, vec1(suboffset(c
->m1Cx
, 1)), brw_imm_f(0.0));
549 brw_MUL(p
, c
->m2Cy
, c
->tmp
, c
->inv_w
[0]);
550 brw_MOV(p
, vec1(suboffset(c
->m2Cy
, 0)), brw_imm_f(0.0));
553 brw_MOV(p
, c
->m1Cx
, brw_imm_ud(0));
554 brw_MOV(p
, c
->m2Cy
, brw_imm_ud(0));
558 brw_set_predicate_control_flag_value(p
, pc
);
559 if (tex
->CoordReplace
) {
560 if (c
->key
.SpriteOrigin
== GL_LOWER_LEFT
) {
561 brw_MUL(p
, c
->m3C0
, c
->inv_w
[0], brw_imm_f(1.0));
562 brw_MOV(p
, vec1(suboffset(c
->m3C0
, 0)), brw_imm_f(0.0));
565 brw_MOV(p
, c
->m3C0
, brw_imm_f(0.0));
567 brw_MOV(p
, c
->m3C0
, a0
); /* constant value */
570 /* Copy m0..m3 to URB.
579 0, /* response len */
581 last
, /* writes complete */
582 i
*4, /* urb destination offset */
583 BRW_URB_SWIZZLE_TRANSPOSE
);
588 /* Points setup - several simplifications as all attributes are
589 * constant across the face of the point (point sprites excluded!)
591 void brw_emit_point_setup( struct brw_sf_compile
*c
, GLboolean allocate
)
593 struct brw_compile
*p
= &c
->func
;
603 brw_MOV(p
, c
->m1Cx
, brw_imm_ud(0)); /* zero - move out of loop */
604 brw_MOV(p
, c
->m2Cy
, brw_imm_ud(0)); /* zero - move out of loop */
606 for (i
= 0; i
< c
->nr_setup_regs
; i
++)
608 struct brw_reg a0
= offset(c
->vert
[0], i
);
609 GLushort pc
, pc_persp
, pc_linear
;
610 GLboolean last
= calculate_masks(c
, i
, &pc
, &pc_persp
, &pc_linear
);
614 /* This seems odd as the values are all constant, but the
615 * fragment shader will be expecting it:
617 brw_set_predicate_control_flag_value(p
, pc_persp
);
618 brw_MUL(p
, a0
, a0
, c
->inv_w
[0]);
622 /* The delta values are always zero, just send the starting
623 * coordinate. Again, this is to fit in with the interpolation
624 * code in the fragment shader.
627 brw_set_predicate_control_flag_value(p
, pc
);
629 brw_MOV(p
, c
->m3C0
, a0
); /* constant value */
631 /* Copy m0..m3 to URB.
640 0, /* response len */
642 last
, /* writes complete */
643 i
*4, /* urb destination offset */
644 BRW_URB_SWIZZLE_TRANSPOSE
);
649 void brw_emit_anyprim_setup( struct brw_sf_compile
*c
)
651 struct brw_compile
*p
= &c
->func
;
652 struct brw_reg ip
= brw_ip_reg();
653 struct brw_reg payload_prim
= brw_uw1_reg(BRW_GENERAL_REGISTER_FILE
, 1, 0);
654 struct brw_reg payload_attr
= get_element_ud(brw_vec1_reg(BRW_GENERAL_REGISTER_FILE
, 1, 0), 0);
655 struct brw_reg primmask
;
656 struct brw_instruction
*jmp
;
657 struct brw_reg v1_null_ud
= vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
));
664 primmask
= retype(get_element(c
->tmp
, 0), BRW_REGISTER_TYPE_UD
);
666 brw_MOV(p
, primmask
, brw_imm_ud(1));
667 brw_SHL(p
, primmask
, primmask
, payload_prim
);
669 brw_set_conditionalmod(p
, BRW_CONDITIONAL_Z
);
670 brw_AND(p
, v1_null_ud
, primmask
, brw_imm_ud((1<<_3DPRIM_TRILIST
) |
671 (1<<_3DPRIM_TRISTRIP
) |
672 (1<<_3DPRIM_TRIFAN
) |
673 (1<<_3DPRIM_TRISTRIP_REVERSE
) |
674 (1<<_3DPRIM_POLYGON
) |
675 (1<<_3DPRIM_RECTLIST
) |
676 (1<<_3DPRIM_TRIFAN_NOSTIPPLE
)));
677 jmp
= brw_JMPI(p
, ip
, ip
, brw_imm_w(0));
679 saveflag
= p
->flag_value
;
680 brw_push_insn_state(p
);
681 brw_emit_tri_setup( c
, GL_FALSE
);
682 brw_pop_insn_state(p
);
683 p
->flag_value
= saveflag
;
684 /* note - thread killed in subroutine, so must
685 * restore the flag which is changed when building
686 * the subroutine. fix #13240
689 brw_land_fwd_jump(p
, jmp
);
691 brw_set_conditionalmod(p
, BRW_CONDITIONAL_Z
);
692 brw_AND(p
, v1_null_ud
, primmask
, brw_imm_ud((1<<_3DPRIM_LINELIST
) |
693 (1<<_3DPRIM_LINESTRIP
) |
694 (1<<_3DPRIM_LINELOOP
) |
695 (1<<_3DPRIM_LINESTRIP_CONT
) |
696 (1<<_3DPRIM_LINESTRIP_BF
) |
697 (1<<_3DPRIM_LINESTRIP_CONT_BF
)));
698 jmp
= brw_JMPI(p
, ip
, ip
, brw_imm_w(0));
700 saveflag
= p
->flag_value
;
701 brw_push_insn_state(p
);
702 brw_emit_line_setup( c
, GL_FALSE
);
703 brw_pop_insn_state(p
);
704 p
->flag_value
= saveflag
;
705 /* note - thread killed in subroutine */
707 brw_land_fwd_jump(p
, jmp
);
709 brw_set_conditionalmod(p
, BRW_CONDITIONAL_Z
);
710 brw_AND(p
, v1_null_ud
, payload_attr
, brw_imm_ud(1<<BRW_SPRITE_POINT_ENABLE
));
711 jmp
= brw_JMPI(p
, ip
, ip
, brw_imm_w(0));
713 saveflag
= p
->flag_value
;
714 brw_push_insn_state(p
);
715 brw_emit_point_sprite_setup( c
, GL_FALSE
);
716 brw_pop_insn_state(p
);
717 p
->flag_value
= saveflag
;
719 brw_land_fwd_jump(p
, jmp
);
721 brw_emit_point_setup( c
, GL_FALSE
);