2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 /** @file brw_fs_generator.cpp
26 * This file supports generating code from the FS LIR to the actual
27 * native instructions.
31 #include "main/macros.h"
32 #include "brw_context.h"
39 fs_generator::fs_generator(struct brw_context
*brw
,
40 struct brw_wm_compile
*c
,
41 struct gl_shader_program
*prog
,
42 struct gl_fragment_program
*fp
,
43 bool dual_source_output
)
45 : brw(brw
), c(c
), prog(prog
), fp(fp
), dual_source_output(dual_source_output
)
51 p
= rzalloc(mem_ctx
, struct brw_compile
);
52 brw_init_compile(brw
, p
, mem_ctx
);
55 fs_generator::~fs_generator()
60 fs_generator::mark_surface_used(unsigned surf_index
)
62 assert(surf_index
< BRW_MAX_SURFACES
);
64 c
->prog_data
.base
.binding_table
.size_bytes
=
65 MAX2(c
->prog_data
.base
.binding_table
.size_bytes
, (surf_index
+ 1) * 4);
69 fs_generator::patch_discard_jumps_to_fb_writes()
71 if (brw
->gen
< 6 || this->discard_halt_patches
.is_empty())
74 /* There is a somewhat strange undocumented requirement of using
75 * HALT, according to the simulator. If some channel has HALTed to
76 * a particular UIP, then by the end of the program, every channel
77 * must have HALTed to that UIP. Furthermore, the tracking is a
78 * stack, so you can't do the final halt of a UIP after starting
79 * halting to a new UIP.
81 * Symptoms of not emitting this instruction on actual hardware
82 * included GPU hangs and sparkly rendering on the piglit discard
85 struct brw_instruction
*last_halt
= gen6_HALT(p
);
86 last_halt
->bits3
.break_cont
.uip
= 2;
87 last_halt
->bits3
.break_cont
.jip
= 2;
91 foreach_list(node
, &this->discard_halt_patches
) {
92 ip_record
*patch_ip
= (ip_record
*)node
;
93 struct brw_instruction
*patch
= &p
->store
[patch_ip
->ip
];
95 assert(patch
->header
.opcode
== BRW_OPCODE_HALT
);
96 /* HALT takes a half-instruction distance from the pre-incremented IP. */
97 patch
->bits3
.break_cont
.uip
= (ip
- patch_ip
->ip
) * 2;
100 this->discard_halt_patches
.make_empty();
104 fs_generator::generate_fb_write(fs_inst
*inst
)
106 bool eot
= inst
->eot
;
107 struct brw_reg implied_header
;
108 uint32_t msg_control
;
110 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
113 brw_push_insn_state(p
);
114 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
115 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
117 if ((fp
&& fp
->UsesKill
) || c
->key
.alpha_test_func
) {
118 struct brw_reg pixel_mask
;
121 pixel_mask
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
123 pixel_mask
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
125 brw_MOV(p
, pixel_mask
, brw_flag_reg(0, 1));
128 if (inst
->header_present
) {
130 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
132 retype(brw_message_reg(inst
->base_mrf
), BRW_REGISTER_TYPE_UD
),
133 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
134 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
136 if (inst
->target
> 0 && c
->key
.replicate_alpha
) {
137 /* Set "Source0 Alpha Present to RenderTarget" bit in message
141 vec1(retype(brw_message_reg(inst
->base_mrf
), BRW_REGISTER_TYPE_UD
)),
142 vec1(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
143 brw_imm_ud(0x1 << 11));
146 if (inst
->target
> 0) {
147 /* Set the render target index for choosing BLEND_STATE. */
148 brw_MOV(p
, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE
,
150 BRW_REGISTER_TYPE_UD
),
151 brw_imm_ud(inst
->target
));
154 implied_header
= brw_null_reg();
156 implied_header
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
159 brw_message_reg(inst
->base_mrf
+ 1),
163 implied_header
= brw_null_reg();
166 if (this->dual_source_output
)
167 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01
;
168 else if (dispatch_width
== 16)
169 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
171 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01
;
173 brw_pop_insn_state(p
);
175 uint32_t surf_index
=
176 c
->prog_data
.binding_table
.render_target_start
+ inst
->target
;
186 inst
->header_present
);
188 mark_surface_used(surf_index
);
192 fs_generator::generate_blorp_fb_write(fs_inst
*inst
)
195 16 /* dispatch_width */,
197 brw_reg_from_fs_reg(&inst
->src
[0]),
198 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
,
203 inst
->header_present
);
206 /* Computes the integer pixel x,y values from the origin.
208 * This is the basis of gl_FragCoord computation, but is also used
209 * pre-gen6 for computing the deltas from v0 for computing
213 fs_generator::generate_pixel_xy(struct brw_reg dst
, bool is_x
)
215 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
217 struct brw_reg deltas
;
220 src
= stride(suboffset(g1_uw
, 4), 2, 4, 0);
221 deltas
= brw_imm_v(0x10101010);
223 src
= stride(suboffset(g1_uw
, 5), 2, 4, 0);
224 deltas
= brw_imm_v(0x11001100);
227 if (dispatch_width
== 16) {
231 /* We do this SIMD8 or SIMD16, but since the destination is UW we
232 * don't do compression in the SIMD16 case.
234 brw_push_insn_state(p
);
235 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
236 brw_ADD(p
, dst
, src
, deltas
);
237 brw_pop_insn_state(p
);
241 fs_generator::generate_linterp(fs_inst
*inst
,
242 struct brw_reg dst
, struct brw_reg
*src
)
244 struct brw_reg delta_x
= src
[0];
245 struct brw_reg delta_y
= src
[1];
246 struct brw_reg interp
= src
[2];
249 delta_y
.nr
== delta_x
.nr
+ 1 &&
250 (brw
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
251 brw_PLN(p
, dst
, interp
, delta_x
);
253 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
254 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
259 fs_generator::generate_math1_gen7(fs_inst
*inst
,
263 assert(inst
->mlen
== 0);
265 brw_math_function(inst
->opcode
),
267 BRW_MATH_DATA_VECTOR
,
268 BRW_MATH_PRECISION_FULL
);
272 fs_generator::generate_math2_gen7(fs_inst
*inst
,
277 assert(inst
->mlen
== 0);
278 brw_math2(p
, dst
, brw_math_function(inst
->opcode
), src0
, src1
);
282 fs_generator::generate_math1_gen6(fs_inst
*inst
,
286 int op
= brw_math_function(inst
->opcode
);
288 assert(inst
->mlen
== 0);
290 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
294 BRW_MATH_DATA_VECTOR
,
295 BRW_MATH_PRECISION_FULL
);
297 if (dispatch_width
== 16) {
298 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
299 brw_math(p
, sechalf(dst
),
302 BRW_MATH_DATA_VECTOR
,
303 BRW_MATH_PRECISION_FULL
);
304 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
309 fs_generator::generate_math2_gen6(fs_inst
*inst
,
314 int op
= brw_math_function(inst
->opcode
);
316 assert(inst
->mlen
== 0);
318 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
319 brw_math2(p
, dst
, op
, src0
, src1
);
321 if (dispatch_width
== 16) {
322 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
323 brw_math2(p
, sechalf(dst
), op
, sechalf(src0
), sechalf(src1
));
324 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
329 fs_generator::generate_math_gen4(fs_inst
*inst
,
333 int op
= brw_math_function(inst
->opcode
);
335 assert(inst
->mlen
>= 1);
337 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
341 BRW_MATH_DATA_VECTOR
,
342 BRW_MATH_PRECISION_FULL
);
344 if (dispatch_width
== 16) {
345 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
346 brw_math(p
, sechalf(dst
),
348 inst
->base_mrf
+ 1, sechalf(src
),
349 BRW_MATH_DATA_VECTOR
,
350 BRW_MATH_PRECISION_FULL
);
352 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
357 fs_generator::generate_math_g45(fs_inst
*inst
,
361 if (inst
->opcode
== SHADER_OPCODE_POW
||
362 inst
->opcode
== SHADER_OPCODE_INT_QUOTIENT
||
363 inst
->opcode
== SHADER_OPCODE_INT_REMAINDER
) {
364 generate_math_gen4(inst
, dst
, src
);
368 int op
= brw_math_function(inst
->opcode
);
370 assert(inst
->mlen
>= 1);
375 BRW_MATH_DATA_VECTOR
,
376 BRW_MATH_PRECISION_FULL
);
380 fs_generator::generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
384 uint32_t simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
385 uint32_t return_format
;
388 case BRW_REGISTER_TYPE_D
:
389 return_format
= BRW_SAMPLER_RETURN_FORMAT_SINT32
;
391 case BRW_REGISTER_TYPE_UD
:
392 return_format
= BRW_SAMPLER_RETURN_FORMAT_UINT32
;
395 return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
399 if (dispatch_width
== 16 &&
400 !inst
->force_uncompressed
&& !inst
->force_sechalf
)
401 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
404 switch (inst
->opcode
) {
405 case SHADER_OPCODE_TEX
:
406 if (inst
->shadow_compare
) {
407 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
;
409 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE
;
413 if (inst
->shadow_compare
) {
414 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
;
416 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
419 case SHADER_OPCODE_TXL
:
420 if (inst
->shadow_compare
) {
421 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
423 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
426 case SHADER_OPCODE_TXS
:
427 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
429 case SHADER_OPCODE_TXD
:
430 if (inst
->shadow_compare
) {
431 /* Gen7.5+. Otherwise, lowered by brw_lower_texture_gradients(). */
432 assert(brw
->is_haswell
);
433 msg_type
= HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE
;
435 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
438 case SHADER_OPCODE_TXF
:
439 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
441 case SHADER_OPCODE_TXF_CMS
:
443 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
;
445 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
447 case SHADER_OPCODE_TXF_UMS
:
448 assert(brw
->gen
>= 7);
449 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
;
451 case SHADER_OPCODE_TXF_MCS
:
452 assert(brw
->gen
>= 7);
453 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
;
455 case SHADER_OPCODE_LOD
:
456 msg_type
= GEN5_SAMPLER_MESSAGE_LOD
;
458 case SHADER_OPCODE_TG4
:
459 if (inst
->shadow_compare
) {
460 assert(brw
->gen
>= 7);
461 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C
;
463 assert(brw
->gen
>= 6);
464 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4
;
467 case SHADER_OPCODE_TG4_OFFSET
:
468 assert(brw
->gen
>= 7);
469 if (inst
->shadow_compare
) {
470 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C
;
472 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO
;
476 assert(!"not reached");
480 switch (inst
->opcode
) {
481 case SHADER_OPCODE_TEX
:
482 /* Note that G45 and older determines shadow compare and dispatch width
483 * from message length for most messages.
485 assert(dispatch_width
== 8);
486 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE
;
487 if (inst
->shadow_compare
) {
488 assert(inst
->mlen
== 6);
490 assert(inst
->mlen
<= 4);
494 if (inst
->shadow_compare
) {
495 assert(inst
->mlen
== 6);
496 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE
;
498 assert(inst
->mlen
== 9);
499 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
500 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
503 case SHADER_OPCODE_TXL
:
504 if (inst
->shadow_compare
) {
505 assert(inst
->mlen
== 6);
506 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE
;
508 assert(inst
->mlen
== 9);
509 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD
;
510 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
513 case SHADER_OPCODE_TXD
:
514 /* There is no sample_d_c message; comparisons are done manually */
515 assert(inst
->mlen
== 7 || inst
->mlen
== 10);
516 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS
;
518 case SHADER_OPCODE_TXF
:
519 assert(inst
->mlen
== 9);
520 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
521 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
523 case SHADER_OPCODE_TXS
:
524 assert(inst
->mlen
== 3);
525 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_RESINFO
;
526 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
529 assert(!"not reached");
533 assert(msg_type
!= -1);
535 if (simd_mode
== BRW_SAMPLER_SIMD_MODE_SIMD16
) {
540 if (brw
->gen
>= 7 && inst
->header_present
&& dispatch_width
== 16) {
541 /* The send-from-GRF for SIMD16 texturing with a header has an extra
542 * hardware register allocated to it, which we need to skip over (since
543 * our coordinates in the payload are in the even-numbered registers,
544 * and the header comes right before the first one).
546 assert(src
.file
== BRW_GENERAL_REGISTER_FILE
);
550 /* Load the message header if present. If there's a texture offset,
551 * we need to set it up explicitly and load the offset bitfield.
552 * Otherwise, we can use an implied move from g0 to the first message reg.
554 if (inst
->header_present
) {
555 if (brw
->gen
< 6 && !inst
->texture_offset
) {
556 /* Set up an implied move from g0 to the MRF. */
557 src
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
559 struct brw_reg header_reg
;
564 assert(inst
->base_mrf
!= -1);
565 header_reg
= brw_message_reg(inst
->base_mrf
);
568 brw_push_insn_state(p
);
569 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
570 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
571 /* Explicitly set up the message header by copying g0 to the MRF. */
572 brw_MOV(p
, header_reg
, brw_vec8_grf(0, 0));
574 if (inst
->texture_offset
) {
575 /* Set the offset bits in DWord 2. */
576 brw_MOV(p
, get_element_ud(header_reg
, 2),
577 brw_imm_ud(inst
->texture_offset
));
580 if (inst
->sampler
>= 16) {
581 /* The "Sampler Index" field can only store values between 0 and 15.
582 * However, we can add an offset to the "Sampler State Pointer"
583 * field, effectively selecting a different set of 16 samplers.
585 * The "Sampler State Pointer" needs to be aligned to a 32-byte
586 * offset, and each sampler state is only 16-bytes, so we can't
587 * exclusively use the offset - we have to use both.
589 assert(brw
->is_haswell
); /* field only exists on Haswell */
591 get_element_ud(header_reg
, 3),
592 get_element_ud(brw_vec8_grf(0, 0), 3),
593 brw_imm_ud(16 * (inst
->sampler
/ 16) *
594 sizeof(gen7_sampler_state
)));
596 brw_pop_insn_state(p
);
600 uint32_t surface_index
= ((inst
->opcode
== SHADER_OPCODE_TG4
||
601 inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
602 ? c
->prog_data
.base
.binding_table
.gather_texture_start
603 : c
->prog_data
.base
.binding_table
.texture_start
) + inst
->sampler
;
606 retype(dst
, BRW_REGISTER_TYPE_UW
),
614 inst
->header_present
,
618 mark_surface_used(surface_index
);
622 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
625 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
627 * Ideally, we want to produce:
630 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
631 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
632 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
633 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
634 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
635 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
636 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
637 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
639 * and add another set of two more subspans if in 16-pixel dispatch mode.
641 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
642 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
643 * pair. But the ideal approximation may impose a huge performance cost on
644 * sample_d. On at least Haswell, sample_d instruction does some
645 * optimizations if the same LOD is used for all pixels in the subspan.
647 * For DDY, we need to use ALIGN16 mode since it's capable of doing the
648 * appropriate swizzling.
651 fs_generator::generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
653 unsigned vstride
, width
;
655 if (c
->key
.high_quality_derivatives
) {
656 /* produce accurate derivatives */
657 vstride
= BRW_VERTICAL_STRIDE_2
;
661 /* replicate the derivative at the top-left pixel to other pixels */
662 vstride
= BRW_VERTICAL_STRIDE_4
;
666 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 1,
670 BRW_HORIZONTAL_STRIDE_0
,
671 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
672 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
676 BRW_HORIZONTAL_STRIDE_0
,
677 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
678 brw_ADD(p
, dst
, src0
, negate(src1
));
681 /* The negate_value boolean is used to negate the derivative computation for
682 * FBOs, since they place the origin at the upper left instead of the lower
686 fs_generator::generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
,
689 if (c
->key
.high_quality_derivatives
) {
690 /* From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
691 * Region Restrictions):
693 * In Align16 access mode, SIMD16 is not allowed for DW operations
694 * and SIMD8 is not allowed for DF operations.
696 * In this context, "DW operations" means "operations acting on 32-bit
697 * values", so it includes operations on floats.
699 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
700 * (Instruction Compression -> Rules and Restrictions):
702 * A compressed instruction must be in Align1 access mode. Align16
703 * mode instructions cannot be compressed.
705 * Similar text exists in the g45 PRM.
707 * On these platforms, if we're building a SIMD16 shader, we need to
708 * manually unroll to a pair of SIMD8 instructions.
710 bool unroll_to_simd8
=
711 (dispatch_width
== 16 &&
712 (brw
->gen
== 4 || (brw
->gen
== 7 && !brw
->is_haswell
)));
714 /* produce accurate derivatives */
715 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
717 BRW_VERTICAL_STRIDE_4
,
719 BRW_HORIZONTAL_STRIDE_1
,
720 BRW_SWIZZLE_XYXY
, WRITEMASK_XYZW
);
721 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
723 BRW_VERTICAL_STRIDE_4
,
725 BRW_HORIZONTAL_STRIDE_1
,
726 BRW_SWIZZLE_ZWZW
, WRITEMASK_XYZW
);
727 brw_push_insn_state(p
);
728 brw_set_access_mode(p
, BRW_ALIGN_16
);
730 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
732 brw_ADD(p
, dst
, src1
, negate(src0
));
734 brw_ADD(p
, dst
, src0
, negate(src1
));
735 if (unroll_to_simd8
) {
736 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
737 src0
= sechalf(src0
);
738 src1
= sechalf(src1
);
741 brw_ADD(p
, dst
, src1
, negate(src0
));
743 brw_ADD(p
, dst
, src0
, negate(src1
));
745 brw_pop_insn_state(p
);
747 /* replicate the derivative at the top-left pixel to other pixels */
748 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
750 BRW_VERTICAL_STRIDE_4
,
752 BRW_HORIZONTAL_STRIDE_0
,
753 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
754 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 2,
756 BRW_VERTICAL_STRIDE_4
,
758 BRW_HORIZONTAL_STRIDE_0
,
759 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
761 brw_ADD(p
, dst
, src1
, negate(src0
));
763 brw_ADD(p
, dst
, src0
, negate(src1
));
768 fs_generator::generate_discard_jump(fs_inst
*inst
)
770 assert(brw
->gen
>= 6);
772 /* This HALT will be patched up at FB write time to point UIP at the end of
773 * the program, and at brw_uip_jip() JIP will be set to the end of the
774 * current block (or the program).
776 this->discard_halt_patches
.push_tail(new(mem_ctx
) ip_record(p
->nr_insn
));
778 brw_push_insn_state(p
);
779 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
781 brw_pop_insn_state(p
);
785 fs_generator::generate_scratch_write(fs_inst
*inst
, struct brw_reg src
)
787 assert(inst
->mlen
!= 0);
790 retype(brw_message_reg(inst
->base_mrf
+ 1), BRW_REGISTER_TYPE_UD
),
791 retype(src
, BRW_REGISTER_TYPE_UD
));
792 brw_oword_block_write_scratch(p
, brw_message_reg(inst
->base_mrf
),
793 dispatch_width
/ 8, inst
->offset
);
797 fs_generator::generate_scratch_read(fs_inst
*inst
, struct brw_reg dst
)
799 assert(inst
->mlen
!= 0);
801 brw_oword_block_read_scratch(p
, dst
, brw_message_reg(inst
->base_mrf
),
802 dispatch_width
/ 8, inst
->offset
);
806 fs_generator::generate_scratch_read_gen7(fs_inst
*inst
, struct brw_reg dst
)
808 gen7_block_read_scratch(p
, dst
, dispatch_width
/ 8, inst
->offset
);
812 fs_generator::generate_uniform_pull_constant_load(fs_inst
*inst
,
814 struct brw_reg index
,
815 struct brw_reg offset
)
817 assert(inst
->mlen
!= 0);
819 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
820 index
.type
== BRW_REGISTER_TYPE_UD
);
821 uint32_t surf_index
= index
.dw1
.ud
;
823 assert(offset
.file
== BRW_IMMEDIATE_VALUE
&&
824 offset
.type
== BRW_REGISTER_TYPE_UD
);
825 uint32_t read_offset
= offset
.dw1
.ud
;
827 brw_oword_block_read(p
, dst
, brw_message_reg(inst
->base_mrf
),
828 read_offset
, surf_index
);
830 mark_surface_used(surf_index
);
834 fs_generator::generate_uniform_pull_constant_load_gen7(fs_inst
*inst
,
836 struct brw_reg index
,
837 struct brw_reg offset
)
839 assert(inst
->mlen
== 0);
841 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
842 index
.type
== BRW_REGISTER_TYPE_UD
);
843 uint32_t surf_index
= index
.dw1
.ud
;
845 assert(offset
.file
== BRW_GENERAL_REGISTER_FILE
);
846 /* Reference just the dword we need, to avoid angering validate_reg(). */
847 offset
= brw_vec1_grf(offset
.nr
, 0);
849 brw_push_insn_state(p
);
850 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
851 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
852 struct brw_instruction
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
853 brw_pop_insn_state(p
);
855 /* We use the SIMD4x2 mode because we want to end up with 4 components in
856 * the destination loaded consecutively from the same offset (which appears
857 * in the first component, and the rest are ignored).
859 dst
.width
= BRW_WIDTH_4
;
860 brw_set_dest(p
, send
, dst
);
861 brw_set_src0(p
, send
, offset
);
862 brw_set_sampler_message(p
, send
,
864 0, /* LD message ignores sampler unit */
865 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
868 false, /* no header */
869 BRW_SAMPLER_SIMD_MODE_SIMD4X2
,
872 mark_surface_used(surf_index
);
876 fs_generator::generate_varying_pull_constant_load(fs_inst
*inst
,
878 struct brw_reg index
,
879 struct brw_reg offset
)
881 assert(brw
->gen
< 7); /* Should use the gen7 variant. */
882 assert(inst
->header_present
);
885 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
886 index
.type
== BRW_REGISTER_TYPE_UD
);
887 uint32_t surf_index
= index
.dw1
.ud
;
889 uint32_t simd_mode
, rlen
, msg_type
;
890 if (dispatch_width
== 16) {
891 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
894 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
899 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
901 /* We always use the SIMD16 message so that we only have to load U, and
904 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
905 assert(inst
->mlen
== 3);
906 assert(inst
->regs_written
== 8);
908 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
911 struct brw_reg offset_mrf
= retype(brw_message_reg(inst
->base_mrf
+ 1),
912 BRW_REGISTER_TYPE_D
);
913 brw_MOV(p
, offset_mrf
, offset
);
915 struct brw_reg header
= brw_vec8_grf(0, 0);
916 gen6_resolve_implied_move(p
, &header
, inst
->base_mrf
);
918 struct brw_instruction
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
919 send
->header
.compression_control
= BRW_COMPRESSION_NONE
;
920 brw_set_dest(p
, send
, dst
);
921 brw_set_src0(p
, send
, header
);
923 send
->header
.destreg__conditionalmod
= inst
->base_mrf
;
925 /* Our surface is set up as floats, regardless of what actual data is
928 uint32_t return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
929 brw_set_sampler_message(p
, send
,
931 0, /* sampler (unused) */
935 inst
->header_present
,
939 mark_surface_used(surf_index
);
943 fs_generator::generate_varying_pull_constant_load_gen7(fs_inst
*inst
,
945 struct brw_reg index
,
946 struct brw_reg offset
)
948 assert(brw
->gen
>= 7);
949 /* Varying-offset pull constant loads are treated as a normal expression on
950 * gen7, so the fact that it's a send message is hidden at the IR level.
952 assert(!inst
->header_present
);
955 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
956 index
.type
== BRW_REGISTER_TYPE_UD
);
957 uint32_t surf_index
= index
.dw1
.ud
;
959 uint32_t simd_mode
, rlen
, mlen
;
960 if (dispatch_width
== 16) {
963 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
967 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
970 struct brw_instruction
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
971 brw_set_dest(p
, send
, dst
);
972 brw_set_src0(p
, send
, offset
);
973 brw_set_sampler_message(p
, send
,
975 0, /* LD message ignores sampler unit */
976 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
979 false, /* no header */
983 mark_surface_used(surf_index
);
987 * Cause the current pixel/sample mask (from R1.7 bits 15:0) to be transferred
988 * into the flags register (f0.0).
990 * Used only on Gen6 and above.
993 fs_generator::generate_mov_dispatch_to_flags(fs_inst
*inst
)
995 struct brw_reg flags
= brw_flag_reg(0, inst
->flag_subreg
);
996 struct brw_reg dispatch_mask
;
999 dispatch_mask
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
1001 dispatch_mask
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
1003 brw_push_insn_state(p
);
1004 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1005 brw_MOV(p
, flags
, dispatch_mask
);
1006 brw_pop_insn_state(p
);
1010 static uint32_t brw_file_from_reg(fs_reg
*reg
)
1012 switch (reg
->file
) {
1014 return BRW_GENERAL_REGISTER_FILE
;
1016 return BRW_MESSAGE_REGISTER_FILE
;
1018 return BRW_IMMEDIATE_VALUE
;
1020 assert(!"not reached");
1021 return BRW_GENERAL_REGISTER_FILE
;
1026 brw_reg_from_fs_reg(fs_reg
*reg
)
1028 struct brw_reg brw_reg
;
1030 switch (reg
->file
) {
1033 if (reg
->smear
== -1) {
1034 brw_reg
= brw_vec8_reg(brw_file_from_reg(reg
), reg
->reg
, 0);
1036 brw_reg
= brw_vec1_reg(brw_file_from_reg(reg
), reg
->reg
, reg
->smear
);
1038 brw_reg
= retype(brw_reg
, reg
->type
);
1040 brw_reg
= sechalf(brw_reg
);
1041 brw_reg
= byte_offset(brw_reg
, reg
->subreg_offset
);
1044 switch (reg
->type
) {
1045 case BRW_REGISTER_TYPE_F
:
1046 brw_reg
= brw_imm_f(reg
->imm
.f
);
1048 case BRW_REGISTER_TYPE_D
:
1049 brw_reg
= brw_imm_d(reg
->imm
.i
);
1051 case BRW_REGISTER_TYPE_UD
:
1052 brw_reg
= brw_imm_ud(reg
->imm
.u
);
1055 assert(!"not reached");
1056 brw_reg
= brw_null_reg();
1061 brw_reg
= reg
->fixed_hw_reg
;
1064 /* Probably unused. */
1065 brw_reg
= brw_null_reg();
1068 assert(!"not reached");
1069 brw_reg
= brw_null_reg();
1072 assert(!"not reached");
1073 brw_reg
= brw_null_reg();
1077 brw_reg
= brw_abs(brw_reg
);
1079 brw_reg
= negate(brw_reg
);
1085 * Sets the first word of a vgrf for gen7+ simd4x2 uniform pull constant
1086 * sampler LD messages.
1088 * We don't want to bake it into the send message's code generation because
1089 * that means we don't get a chance to schedule the instructions.
1092 fs_generator::generate_set_simd4x2_offset(fs_inst
*inst
,
1094 struct brw_reg value
)
1096 assert(value
.file
== BRW_IMMEDIATE_VALUE
);
1098 brw_push_insn_state(p
);
1099 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1100 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1101 brw_MOV(p
, retype(brw_vec1_reg(dst
.file
, dst
.nr
, 0), value
.type
), value
);
1102 brw_pop_insn_state(p
);
1105 /* Sets vstride=16, width=8, hstride=2 or vstride=0, width=1, hstride=0
1106 * (when mask is passed as a uniform) of register mask before moving it
1110 fs_generator::generate_set_omask(fs_inst
*inst
,
1112 struct brw_reg mask
)
1115 (mask
.vstride
== BRW_VERTICAL_STRIDE_8
&&
1116 mask
.width
== BRW_WIDTH_8
&&
1117 mask
.hstride
== BRW_HORIZONTAL_STRIDE_1
);
1120 (mask
.vstride
== BRW_VERTICAL_STRIDE_0
&&
1121 mask
.width
== BRW_WIDTH_1
&&
1122 mask
.hstride
== BRW_HORIZONTAL_STRIDE_0
);
1124 assert(stride_8_8_1
|| stride_0_1_0
);
1125 assert(dst
.type
== BRW_REGISTER_TYPE_UW
);
1127 if (dispatch_width
== 16)
1129 brw_push_insn_state(p
);
1130 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1131 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1134 brw_MOV(p
, dst
, retype(stride(mask
, 16, 8, 2), dst
.type
));
1135 } else if (stride_0_1_0
) {
1136 brw_MOV(p
, dst
, retype(mask
, dst
.type
));
1138 brw_pop_insn_state(p
);
1141 /* Sets vstride=1, width=4, hstride=0 of register src1 during
1142 * the ADD instruction.
1145 fs_generator::generate_set_sample_id(fs_inst
*inst
,
1147 struct brw_reg src0
,
1148 struct brw_reg src1
)
1150 assert(dst
.type
== BRW_REGISTER_TYPE_D
||
1151 dst
.type
== BRW_REGISTER_TYPE_UD
);
1152 assert(src0
.type
== BRW_REGISTER_TYPE_D
||
1153 src0
.type
== BRW_REGISTER_TYPE_UD
);
1155 brw_push_insn_state(p
);
1156 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1157 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1158 struct brw_reg reg
= retype(stride(src1
, 1, 4, 0), BRW_REGISTER_TYPE_UW
);
1159 brw_ADD(p
, dst
, src0
, reg
);
1160 if (dispatch_width
== 16)
1161 brw_ADD(p
, offset(dst
, 1), offset(src0
, 1), suboffset(reg
, 2));
1162 brw_pop_insn_state(p
);
1166 * Change the register's data type from UD to W, doubling the strides in order
1167 * to compensate for halving the data type width.
1169 static struct brw_reg
1170 ud_reg_to_w(struct brw_reg r
)
1172 assert(r
.type
== BRW_REGISTER_TYPE_UD
);
1173 r
.type
= BRW_REGISTER_TYPE_W
;
1175 /* The BRW_*_STRIDE enums are defined so that incrementing the field
1176 * doubles the real stride.
1187 fs_generator::generate_pack_half_2x16_split(fs_inst
*inst
,
1192 assert(brw
->gen
>= 7);
1193 assert(dst
.type
== BRW_REGISTER_TYPE_UD
);
1194 assert(x
.type
== BRW_REGISTER_TYPE_F
);
1195 assert(y
.type
== BRW_REGISTER_TYPE_F
);
1197 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
1199 * Because this instruction does not have a 16-bit floating-point type,
1200 * the destination data type must be Word (W).
1202 * The destination must be DWord-aligned and specify a horizontal stride
1203 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
1204 * each destination channel and the upper word is not modified.
1206 struct brw_reg dst_w
= ud_reg_to_w(dst
);
1208 /* Give each 32-bit channel of dst the form below , where "." means
1212 brw_F32TO16(p
, dst_w
, y
);
1217 brw_SHL(p
, dst
, dst
, brw_imm_ud(16u));
1219 /* And, finally the form of packHalf2x16's output:
1222 brw_F32TO16(p
, dst_w
, x
);
1226 fs_generator::generate_unpack_half_2x16_split(fs_inst
*inst
,
1230 assert(brw
->gen
>= 7);
1231 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1232 assert(src
.type
== BRW_REGISTER_TYPE_UD
);
1234 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1236 * Because this instruction does not have a 16-bit floating-point type,
1237 * the source data type must be Word (W). The destination type must be
1240 struct brw_reg src_w
= ud_reg_to_w(src
);
1242 /* Each channel of src has the form of unpackHalf2x16's input: 0xhhhhllll.
1243 * For the Y case, we wish to access only the upper word; therefore
1244 * a 16-bit subregister offset is needed.
1246 assert(inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
||
1247 inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
);
1248 if (inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
)
1251 brw_F16TO32(p
, dst
, src_w
);
1255 fs_generator::generate_shader_time_add(fs_inst
*inst
,
1256 struct brw_reg payload
,
1257 struct brw_reg offset
,
1258 struct brw_reg value
)
1260 assert(brw
->gen
>= 7);
1261 brw_push_insn_state(p
);
1262 brw_set_mask_control(p
, true);
1264 assert(payload
.file
== BRW_GENERAL_REGISTER_FILE
);
1265 struct brw_reg payload_offset
= retype(brw_vec1_grf(payload
.nr
, 0),
1267 struct brw_reg payload_value
= retype(brw_vec1_grf(payload
.nr
+ 1, 0),
1270 assert(offset
.file
== BRW_IMMEDIATE_VALUE
);
1271 if (value
.file
== BRW_GENERAL_REGISTER_FILE
) {
1272 value
.width
= BRW_WIDTH_1
;
1273 value
.hstride
= BRW_HORIZONTAL_STRIDE_0
;
1274 value
.vstride
= BRW_VERTICAL_STRIDE_0
;
1276 assert(value
.file
== BRW_IMMEDIATE_VALUE
);
1279 /* Trying to deal with setup of the params from the IR is crazy in the FS8
1280 * case, and we don't really care about squeezing every bit of performance
1281 * out of this path, so we just emit the MOVs from here.
1283 brw_MOV(p
, payload_offset
, offset
);
1284 brw_MOV(p
, payload_value
, value
);
1285 brw_shader_time_add(p
, payload
,
1286 c
->prog_data
.base
.binding_table
.shader_time_start
);
1287 brw_pop_insn_state(p
);
1289 mark_surface_used(c
->prog_data
.base
.binding_table
.shader_time_start
);
1293 fs_generator::generate_untyped_atomic(fs_inst
*inst
, struct brw_reg dst
,
1294 struct brw_reg atomic_op
,
1295 struct brw_reg surf_index
)
1297 assert(atomic_op
.file
== BRW_IMMEDIATE_VALUE
&&
1298 atomic_op
.type
== BRW_REGISTER_TYPE_UD
&&
1299 surf_index
.file
== BRW_IMMEDIATE_VALUE
&&
1300 surf_index
.type
== BRW_REGISTER_TYPE_UD
);
1302 brw_untyped_atomic(p
, dst
, brw_message_reg(inst
->base_mrf
),
1303 atomic_op
.dw1
.ud
, surf_index
.dw1
.ud
,
1304 inst
->mlen
, dispatch_width
/ 8);
1306 mark_surface_used(surf_index
.dw1
.ud
);
1310 fs_generator::generate_untyped_surface_read(fs_inst
*inst
, struct brw_reg dst
,
1311 struct brw_reg surf_index
)
1313 assert(surf_index
.file
== BRW_IMMEDIATE_VALUE
&&
1314 surf_index
.type
== BRW_REGISTER_TYPE_UD
);
1316 brw_untyped_surface_read(p
, dst
, brw_message_reg(inst
->base_mrf
),
1318 inst
->mlen
, dispatch_width
/ 8);
1320 mark_surface_used(surf_index
.dw1
.ud
);
1324 fs_generator::generate_code(exec_list
*instructions
, FILE *dump_file
)
1326 int last_native_insn_offset
= p
->next_insn_offset
;
1327 const char *last_annotation_string
= NULL
;
1328 const void *last_annotation_ir
= NULL
;
1330 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1332 printf("Native code for fragment shader %d (SIMD%d dispatch):\n",
1333 prog
->Name
, dispatch_width
);
1335 printf("Native code for fragment program %d (SIMD%d dispatch):\n",
1336 fp
->Base
.Id
, dispatch_width
);
1338 printf("Native code for blorp program (SIMD%d dispatch):\n",
1344 if (unlikely(INTEL_DEBUG
& DEBUG_WM
))
1345 cfg
= new(mem_ctx
) cfg_t(instructions
);
1347 foreach_list(node
, instructions
) {
1348 fs_inst
*inst
= (fs_inst
*)node
;
1349 struct brw_reg src
[3], dst
;
1351 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1352 foreach_list(node
, &cfg
->block_list
) {
1353 bblock_link
*link
= (bblock_link
*)node
;
1354 bblock_t
*block
= link
->block
;
1356 if (block
->start
== inst
) {
1357 printf(" START B%d", block
->block_num
);
1358 foreach_list(predecessor_node
, &block
->parents
) {
1359 bblock_link
*predecessor_link
=
1360 (bblock_link
*)predecessor_node
;
1361 bblock_t
*predecessor_block
= predecessor_link
->block
;
1362 printf(" <-B%d", predecessor_block
->block_num
);
1368 if (last_annotation_ir
!= inst
->ir
) {
1369 last_annotation_ir
= inst
->ir
;
1370 if (last_annotation_ir
) {
1373 ((ir_instruction
*)inst
->ir
)->print();
1375 const prog_instruction
*fpi
;
1376 fpi
= (const prog_instruction
*)inst
->ir
;
1377 printf("%d: ", (int)(fpi
- (fp
? fp
->Base
.Instructions
: 0)));
1378 _mesa_fprint_instruction_opt(stdout
,
1380 0, PROG_PRINT_DEBUG
, NULL
);
1385 if (last_annotation_string
!= inst
->annotation
) {
1386 last_annotation_string
= inst
->annotation
;
1387 if (last_annotation_string
)
1388 printf(" %s\n", last_annotation_string
);
1392 for (unsigned int i
= 0; i
< 3; i
++) {
1393 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
1395 /* The accumulator result appears to get used for the
1396 * conditional modifier generation. When negating a UD
1397 * value, there is a 33rd bit generated for the sign in the
1398 * accumulator value, so now you can't check, for example,
1399 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
1401 assert(!inst
->conditional_mod
||
1402 inst
->src
[i
].type
!= BRW_REGISTER_TYPE_UD
||
1403 !inst
->src
[i
].negate
);
1405 dst
= brw_reg_from_fs_reg(&inst
->dst
);
1407 brw_set_conditionalmod(p
, inst
->conditional_mod
);
1408 brw_set_predicate_control(p
, inst
->predicate
);
1409 brw_set_predicate_inverse(p
, inst
->predicate_inverse
);
1410 brw_set_flag_reg(p
, 0, inst
->flag_subreg
);
1411 brw_set_saturate(p
, inst
->saturate
);
1412 brw_set_mask_control(p
, inst
->force_writemask_all
);
1414 if (inst
->force_uncompressed
|| dispatch_width
== 8) {
1415 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1416 } else if (inst
->force_sechalf
) {
1417 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1419 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1422 switch (inst
->opcode
) {
1423 case BRW_OPCODE_MOV
:
1424 brw_MOV(p
, dst
, src
[0]);
1426 case BRW_OPCODE_ADD
:
1427 brw_ADD(p
, dst
, src
[0], src
[1]);
1429 case BRW_OPCODE_MUL
:
1430 brw_MUL(p
, dst
, src
[0], src
[1]);
1432 case BRW_OPCODE_AVG
:
1433 brw_AVG(p
, dst
, src
[0], src
[1]);
1435 case BRW_OPCODE_MACH
:
1436 brw_set_acc_write_control(p
, 1);
1437 brw_MACH(p
, dst
, src
[0], src
[1]);
1438 brw_set_acc_write_control(p
, 0);
1441 case BRW_OPCODE_MAD
:
1442 assert(brw
->gen
>= 6);
1443 brw_set_access_mode(p
, BRW_ALIGN_16
);
1444 if (dispatch_width
== 16 && !brw
->is_haswell
) {
1445 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1446 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
1447 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1448 brw_MAD(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1449 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1451 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
1453 brw_set_access_mode(p
, BRW_ALIGN_1
);
1456 case BRW_OPCODE_LRP
:
1457 assert(brw
->gen
>= 6);
1458 brw_set_access_mode(p
, BRW_ALIGN_16
);
1459 if (dispatch_width
== 16 && !brw
->is_haswell
) {
1460 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1461 brw_LRP(p
, dst
, src
[0], src
[1], src
[2]);
1462 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1463 brw_LRP(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1464 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1466 brw_LRP(p
, dst
, src
[0], src
[1], src
[2]);
1468 brw_set_access_mode(p
, BRW_ALIGN_1
);
1471 case BRW_OPCODE_FRC
:
1472 brw_FRC(p
, dst
, src
[0]);
1474 case BRW_OPCODE_RNDD
:
1475 brw_RNDD(p
, dst
, src
[0]);
1477 case BRW_OPCODE_RNDE
:
1478 brw_RNDE(p
, dst
, src
[0]);
1480 case BRW_OPCODE_RNDZ
:
1481 brw_RNDZ(p
, dst
, src
[0]);
1484 case BRW_OPCODE_AND
:
1485 brw_AND(p
, dst
, src
[0], src
[1]);
1488 brw_OR(p
, dst
, src
[0], src
[1]);
1490 case BRW_OPCODE_XOR
:
1491 brw_XOR(p
, dst
, src
[0], src
[1]);
1493 case BRW_OPCODE_NOT
:
1494 brw_NOT(p
, dst
, src
[0]);
1496 case BRW_OPCODE_ASR
:
1497 brw_ASR(p
, dst
, src
[0], src
[1]);
1499 case BRW_OPCODE_SHR
:
1500 brw_SHR(p
, dst
, src
[0], src
[1]);
1502 case BRW_OPCODE_SHL
:
1503 brw_SHL(p
, dst
, src
[0], src
[1]);
1505 case BRW_OPCODE_F32TO16
:
1506 assert(brw
->gen
>= 7);
1507 brw_F32TO16(p
, dst
, src
[0]);
1509 case BRW_OPCODE_F16TO32
:
1510 assert(brw
->gen
>= 7);
1511 brw_F16TO32(p
, dst
, src
[0]);
1513 case BRW_OPCODE_CMP
:
1514 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
1516 case BRW_OPCODE_SEL
:
1517 brw_SEL(p
, dst
, src
[0], src
[1]);
1519 case BRW_OPCODE_BFREV
:
1520 assert(brw
->gen
>= 7);
1521 /* BFREV only supports UD type for src and dst. */
1522 brw_BFREV(p
, retype(dst
, BRW_REGISTER_TYPE_UD
),
1523 retype(src
[0], BRW_REGISTER_TYPE_UD
));
1525 case BRW_OPCODE_FBH
:
1526 assert(brw
->gen
>= 7);
1527 /* FBH only supports UD type for dst. */
1528 brw_FBH(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1530 case BRW_OPCODE_FBL
:
1531 assert(brw
->gen
>= 7);
1532 /* FBL only supports UD type for dst. */
1533 brw_FBL(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1535 case BRW_OPCODE_CBIT
:
1536 assert(brw
->gen
>= 7);
1537 /* CBIT only supports UD type for dst. */
1538 brw_CBIT(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1540 case BRW_OPCODE_ADDC
:
1541 assert(brw
->gen
>= 7);
1542 brw_set_acc_write_control(p
, 1);
1543 brw_ADDC(p
, dst
, src
[0], src
[1]);
1544 brw_set_acc_write_control(p
, 0);
1546 case BRW_OPCODE_SUBB
:
1547 assert(brw
->gen
>= 7);
1548 brw_set_acc_write_control(p
, 1);
1549 brw_SUBB(p
, dst
, src
[0], src
[1]);
1550 brw_set_acc_write_control(p
, 0);
1553 case BRW_OPCODE_BFE
:
1554 assert(brw
->gen
>= 7);
1555 brw_set_access_mode(p
, BRW_ALIGN_16
);
1556 if (dispatch_width
== 16 && !brw
->is_haswell
) {
1557 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1558 brw_BFE(p
, dst
, src
[0], src
[1], src
[2]);
1559 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1560 brw_BFE(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1561 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1563 brw_BFE(p
, dst
, src
[0], src
[1], src
[2]);
1565 brw_set_access_mode(p
, BRW_ALIGN_1
);
1568 case BRW_OPCODE_BFI1
:
1569 assert(brw
->gen
>= 7);
1570 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
1573 * "Force BFI instructions to be executed always in SIMD8."
1575 if (dispatch_width
== 16 && brw
->is_haswell
) {
1576 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1577 brw_BFI1(p
, dst
, src
[0], src
[1]);
1578 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1579 brw_BFI1(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]));
1580 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1582 brw_BFI1(p
, dst
, src
[0], src
[1]);
1585 case BRW_OPCODE_BFI2
:
1586 assert(brw
->gen
>= 7);
1587 brw_set_access_mode(p
, BRW_ALIGN_16
);
1588 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
1591 * "Force BFI instructions to be executed always in SIMD8."
1593 * Otherwise we would be able to emit compressed instructions like we
1594 * do for the other three-source instructions.
1596 if (dispatch_width
== 16) {
1597 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1598 brw_BFI2(p
, dst
, src
[0], src
[1], src
[2]);
1599 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1600 brw_BFI2(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1601 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1603 brw_BFI2(p
, dst
, src
[0], src
[1], src
[2]);
1605 brw_set_access_mode(p
, BRW_ALIGN_1
);
1609 if (inst
->src
[0].file
!= BAD_FILE
) {
1610 /* The instruction has an embedded compare (only allowed on gen6) */
1611 assert(brw
->gen
== 6);
1612 gen6_IF(p
, inst
->conditional_mod
, src
[0], src
[1]);
1614 brw_IF(p
, dispatch_width
== 16 ? BRW_EXECUTE_16
: BRW_EXECUTE_8
);
1618 case BRW_OPCODE_ELSE
:
1621 case BRW_OPCODE_ENDIF
:
1626 brw_DO(p
, BRW_EXECUTE_8
);
1629 case BRW_OPCODE_BREAK
:
1631 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
1633 case BRW_OPCODE_CONTINUE
:
1634 /* FINISHME: We need to write the loop instruction support still. */
1639 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
1642 case BRW_OPCODE_WHILE
:
1646 case SHADER_OPCODE_RCP
:
1647 case SHADER_OPCODE_RSQ
:
1648 case SHADER_OPCODE_SQRT
:
1649 case SHADER_OPCODE_EXP2
:
1650 case SHADER_OPCODE_LOG2
:
1651 case SHADER_OPCODE_SIN
:
1652 case SHADER_OPCODE_COS
:
1653 if (brw
->gen
>= 7) {
1654 generate_math1_gen7(inst
, dst
, src
[0]);
1655 } else if (brw
->gen
== 6) {
1656 generate_math1_gen6(inst
, dst
, src
[0]);
1657 } else if (brw
->gen
== 5 || brw
->is_g4x
) {
1658 generate_math_g45(inst
, dst
, src
[0]);
1660 generate_math_gen4(inst
, dst
, src
[0]);
1663 case SHADER_OPCODE_INT_QUOTIENT
:
1664 case SHADER_OPCODE_INT_REMAINDER
:
1665 case SHADER_OPCODE_POW
:
1666 if (brw
->gen
>= 7) {
1667 generate_math2_gen7(inst
, dst
, src
[0], src
[1]);
1668 } else if (brw
->gen
== 6) {
1669 generate_math2_gen6(inst
, dst
, src
[0], src
[1]);
1671 generate_math_gen4(inst
, dst
, src
[0]);
1674 case FS_OPCODE_PIXEL_X
:
1675 generate_pixel_xy(dst
, true);
1677 case FS_OPCODE_PIXEL_Y
:
1678 generate_pixel_xy(dst
, false);
1680 case FS_OPCODE_CINTERP
:
1681 brw_MOV(p
, dst
, src
[0]);
1683 case FS_OPCODE_LINTERP
:
1684 generate_linterp(inst
, dst
, src
);
1686 case SHADER_OPCODE_TEX
:
1688 case SHADER_OPCODE_TXD
:
1689 case SHADER_OPCODE_TXF
:
1690 case SHADER_OPCODE_TXF_CMS
:
1691 case SHADER_OPCODE_TXF_UMS
:
1692 case SHADER_OPCODE_TXF_MCS
:
1693 case SHADER_OPCODE_TXL
:
1694 case SHADER_OPCODE_TXS
:
1695 case SHADER_OPCODE_LOD
:
1696 case SHADER_OPCODE_TG4
:
1697 case SHADER_OPCODE_TG4_OFFSET
:
1698 generate_tex(inst
, dst
, src
[0]);
1701 generate_ddx(inst
, dst
, src
[0]);
1704 /* Make sure fp->UsesDFdy flag got set (otherwise there's no
1705 * guarantee that c->key.render_to_fbo is set).
1707 assert(fp
->UsesDFdy
);
1708 generate_ddy(inst
, dst
, src
[0], c
->key
.render_to_fbo
);
1711 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
1712 generate_scratch_write(inst
, src
[0]);
1715 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
1716 generate_scratch_read(inst
, dst
);
1719 case SHADER_OPCODE_GEN7_SCRATCH_READ
:
1720 generate_scratch_read_gen7(inst
, dst
);
1723 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
1724 generate_uniform_pull_constant_load(inst
, dst
, src
[0], src
[1]);
1727 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
1728 generate_uniform_pull_constant_load_gen7(inst
, dst
, src
[0], src
[1]);
1731 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
1732 generate_varying_pull_constant_load(inst
, dst
, src
[0], src
[1]);
1735 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
1736 generate_varying_pull_constant_load_gen7(inst
, dst
, src
[0], src
[1]);
1739 case FS_OPCODE_FB_WRITE
:
1740 generate_fb_write(inst
);
1743 case FS_OPCODE_BLORP_FB_WRITE
:
1744 generate_blorp_fb_write(inst
);
1747 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS
:
1748 generate_mov_dispatch_to_flags(inst
);
1751 case FS_OPCODE_DISCARD_JUMP
:
1752 generate_discard_jump(inst
);
1755 case SHADER_OPCODE_SHADER_TIME_ADD
:
1756 generate_shader_time_add(inst
, src
[0], src
[1], src
[2]);
1759 case SHADER_OPCODE_UNTYPED_ATOMIC
:
1760 generate_untyped_atomic(inst
, dst
, src
[0], src
[1]);
1763 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
1764 generate_untyped_surface_read(inst
, dst
, src
[0]);
1767 case FS_OPCODE_SET_SIMD4X2_OFFSET
:
1768 generate_set_simd4x2_offset(inst
, dst
, src
[0]);
1771 case FS_OPCODE_SET_OMASK
:
1772 generate_set_omask(inst
, dst
, src
[0]);
1775 case FS_OPCODE_SET_SAMPLE_ID
:
1776 generate_set_sample_id(inst
, dst
, src
[0], src
[1]);
1779 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
1780 generate_pack_half_2x16_split(inst
, dst
, src
[0], src
[1]);
1783 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
:
1784 case FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
:
1785 generate_unpack_half_2x16_split(inst
, dst
, src
[0]);
1788 case FS_OPCODE_PLACEHOLDER_HALT
:
1789 /* This is the place where the final HALT needs to be inserted if
1790 * we've emitted any discards. If not, this will emit no code.
1792 patch_discard_jumps_to_fb_writes();
1796 if (inst
->opcode
< (int) ARRAY_SIZE(opcode_descs
)) {
1797 _mesa_problem(ctx
, "Unsupported opcode `%s' in FS",
1798 opcode_descs
[inst
->opcode
].name
);
1800 _mesa_problem(ctx
, "Unsupported opcode %d in FS", inst
->opcode
);
1805 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1806 brw_dump_compile(p
, stdout
,
1807 last_native_insn_offset
, p
->next_insn_offset
);
1809 foreach_list(node
, &cfg
->block_list
) {
1810 bblock_link
*link
= (bblock_link
*)node
;
1811 bblock_t
*block
= link
->block
;
1813 if (block
->end
== inst
) {
1814 printf(" END B%d", block
->block_num
);
1815 foreach_list(successor_node
, &block
->children
) {
1816 bblock_link
*successor_link
=
1817 (bblock_link
*)successor_node
;
1818 bblock_t
*successor_block
= successor_link
->block
;
1819 printf(" ->B%d", successor_block
->block_num
);
1826 last_native_insn_offset
= p
->next_insn_offset
;
1829 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1835 /* OK, while the INTEL_DEBUG=wm above is very nice for debugging FS
1836 * emit issues, it doesn't get the jump distances into the output,
1837 * which is often something we want to debug. So this is here in
1838 * case you're doing that.
1841 brw_dump_compile(p
, dump_file
, 0, p
->next_insn_offset
);
1846 fs_generator::generate_assembly(exec_list
*simd8_instructions
,
1847 exec_list
*simd16_instructions
,
1848 unsigned *assembly_size
,
1851 assert(simd8_instructions
|| simd16_instructions
);
1853 if (simd8_instructions
) {
1855 generate_code(simd8_instructions
, dump_file
);
1858 if (simd16_instructions
) {
1859 /* We have to do a compaction pass now, or the one at the end of
1860 * execution will squash down where our prog_offset start needs
1863 brw_compact_instructions(p
);
1865 /* align to 64 byte boundary. */
1866 while ((p
->nr_insn
* sizeof(struct brw_instruction
)) % 64) {
1870 /* Save off the start of this SIMD16 program */
1871 c
->prog_data
.prog_offset_16
= p
->nr_insn
* sizeof(struct brw_instruction
);
1873 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1875 dispatch_width
= 16;
1876 generate_code(simd16_instructions
, dump_file
);
1879 return brw_get_program(p
, assembly_size
);