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
,
41 const struct brw_wm_prog_key
*key
,
42 struct brw_wm_prog_data
*prog_data
,
43 struct gl_shader_program
*prog
,
44 struct gl_fragment_program
*fp
,
45 bool dual_source_output
)
47 : brw(brw
), key(key
), prog_data(prog_data
), prog(prog
), fp(fp
),
48 dual_source_output(dual_source_output
), mem_ctx(mem_ctx
)
52 p
= rzalloc(mem_ctx
, struct brw_compile
);
53 brw_init_compile(brw
, p
, mem_ctx
);
56 fs_generator::~fs_generator()
61 fs_generator::patch_discard_jumps_to_fb_writes()
63 if (brw
->gen
< 6 || this->discard_halt_patches
.is_empty())
66 /* There is a somewhat strange undocumented requirement of using
67 * HALT, according to the simulator. If some channel has HALTed to
68 * a particular UIP, then by the end of the program, every channel
69 * must have HALTed to that UIP. Furthermore, the tracking is a
70 * stack, so you can't do the final halt of a UIP after starting
71 * halting to a new UIP.
73 * Symptoms of not emitting this instruction on actual hardware
74 * included GPU hangs and sparkly rendering on the piglit discard
77 struct brw_instruction
*last_halt
= gen6_HALT(p
);
78 last_halt
->bits3
.break_cont
.uip
= 2;
79 last_halt
->bits3
.break_cont
.jip
= 2;
83 foreach_list(node
, &this->discard_halt_patches
) {
84 ip_record
*patch_ip
= (ip_record
*)node
;
85 struct brw_instruction
*patch
= &p
->store
[patch_ip
->ip
];
87 assert(patch
->header
.opcode
== BRW_OPCODE_HALT
);
88 /* HALT takes a half-instruction distance from the pre-incremented IP. */
89 patch
->bits3
.break_cont
.uip
= (ip
- patch_ip
->ip
) * 2;
92 this->discard_halt_patches
.make_empty();
96 fs_generator::generate_fb_write(fs_inst
*inst
)
99 struct brw_reg implied_header
;
100 uint32_t msg_control
;
102 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
105 brw_push_insn_state(p
);
106 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
107 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
108 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
110 if (inst
->header_present
) {
111 /* On HSW, the GPU will use the predicate on SENDC, unless the header is
114 if ((fp
&& fp
->UsesKill
) || key
->alpha_test_func
) {
115 struct brw_reg pixel_mask
;
118 pixel_mask
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
120 pixel_mask
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
122 brw_MOV(p
, pixel_mask
, brw_flag_reg(0, 1));
126 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
128 retype(brw_message_reg(inst
->base_mrf
), BRW_REGISTER_TYPE_UD
),
129 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
130 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
132 if (inst
->target
> 0 && key
->replicate_alpha
) {
133 /* Set "Source0 Alpha Present to RenderTarget" bit in message
137 vec1(retype(brw_message_reg(inst
->base_mrf
), BRW_REGISTER_TYPE_UD
)),
138 vec1(retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
139 brw_imm_ud(0x1 << 11));
142 if (inst
->target
> 0) {
143 /* Set the render target index for choosing BLEND_STATE. */
144 brw_MOV(p
, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE
,
146 BRW_REGISTER_TYPE_UD
),
147 brw_imm_ud(inst
->target
));
150 implied_header
= brw_null_reg();
152 implied_header
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
155 brw_message_reg(inst
->base_mrf
+ 1),
159 implied_header
= brw_null_reg();
162 if (this->dual_source_output
)
163 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01
;
164 else if (dispatch_width
== 16)
165 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
167 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01
;
169 brw_pop_insn_state(p
);
171 uint32_t surf_index
=
172 prog_data
->binding_table
.render_target_start
+ inst
->target
;
182 inst
->header_present
);
184 brw_mark_surface_used(&prog_data
->base
, surf_index
);
188 fs_generator::generate_blorp_fb_write(fs_inst
*inst
)
191 16 /* dispatch_width */,
193 brw_reg_from_fs_reg(&inst
->src
[0]),
194 BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
,
199 inst
->header_present
);
202 /* Computes the integer pixel x,y values from the origin.
204 * This is the basis of gl_FragCoord computation, but is also used
205 * pre-gen6 for computing the deltas from v0 for computing
209 fs_generator::generate_pixel_xy(struct brw_reg dst
, bool is_x
)
211 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
213 struct brw_reg deltas
;
216 src
= stride(suboffset(g1_uw
, 4), 2, 4, 0);
217 deltas
= brw_imm_v(0x10101010);
219 src
= stride(suboffset(g1_uw
, 5), 2, 4, 0);
220 deltas
= brw_imm_v(0x11001100);
223 if (dispatch_width
== 16) {
227 /* We do this SIMD8 or SIMD16, but since the destination is UW we
228 * don't do compression in the SIMD16 case.
230 brw_push_insn_state(p
);
231 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
232 brw_ADD(p
, dst
, src
, deltas
);
233 brw_pop_insn_state(p
);
237 fs_generator::generate_linterp(fs_inst
*inst
,
238 struct brw_reg dst
, struct brw_reg
*src
)
240 struct brw_reg delta_x
= src
[0];
241 struct brw_reg delta_y
= src
[1];
242 struct brw_reg interp
= src
[2];
245 delta_y
.nr
== delta_x
.nr
+ 1 &&
246 (brw
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
247 brw_PLN(p
, dst
, interp
, delta_x
);
249 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
250 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
255 fs_generator::generate_math1_gen7(fs_inst
*inst
,
259 assert(inst
->mlen
== 0);
261 brw_math_function(inst
->opcode
),
263 BRW_MATH_DATA_VECTOR
,
264 BRW_MATH_PRECISION_FULL
);
268 fs_generator::generate_math2_gen7(fs_inst
*inst
,
273 assert(inst
->mlen
== 0);
274 brw_math2(p
, dst
, brw_math_function(inst
->opcode
), src0
, src1
);
278 fs_generator::generate_math1_gen6(fs_inst
*inst
,
282 int op
= brw_math_function(inst
->opcode
);
284 assert(inst
->mlen
== 0);
286 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
290 BRW_MATH_DATA_VECTOR
,
291 BRW_MATH_PRECISION_FULL
);
293 if (dispatch_width
== 16) {
294 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
295 brw_math(p
, sechalf(dst
),
298 BRW_MATH_DATA_VECTOR
,
299 BRW_MATH_PRECISION_FULL
);
300 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
305 fs_generator::generate_math2_gen6(fs_inst
*inst
,
310 int op
= brw_math_function(inst
->opcode
);
312 assert(inst
->mlen
== 0);
314 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
315 brw_math2(p
, dst
, op
, src0
, src1
);
317 if (dispatch_width
== 16) {
318 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
319 brw_math2(p
, sechalf(dst
), op
, sechalf(src0
), sechalf(src1
));
320 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
325 fs_generator::generate_math_gen4(fs_inst
*inst
,
329 int op
= brw_math_function(inst
->opcode
);
331 assert(inst
->mlen
>= 1);
333 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
337 BRW_MATH_DATA_VECTOR
,
338 BRW_MATH_PRECISION_FULL
);
340 if (dispatch_width
== 16) {
341 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
342 brw_math(p
, sechalf(dst
),
344 inst
->base_mrf
+ 1, sechalf(src
),
345 BRW_MATH_DATA_VECTOR
,
346 BRW_MATH_PRECISION_FULL
);
348 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
353 fs_generator::generate_math_g45(fs_inst
*inst
,
357 if (inst
->opcode
== SHADER_OPCODE_POW
||
358 inst
->opcode
== SHADER_OPCODE_INT_QUOTIENT
||
359 inst
->opcode
== SHADER_OPCODE_INT_REMAINDER
) {
360 generate_math_gen4(inst
, dst
, src
);
364 int op
= brw_math_function(inst
->opcode
);
366 assert(inst
->mlen
>= 1);
371 BRW_MATH_DATA_VECTOR
,
372 BRW_MATH_PRECISION_FULL
);
376 fs_generator::generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
380 uint32_t simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
381 uint32_t return_format
;
384 case BRW_REGISTER_TYPE_D
:
385 return_format
= BRW_SAMPLER_RETURN_FORMAT_SINT32
;
387 case BRW_REGISTER_TYPE_UD
:
388 return_format
= BRW_SAMPLER_RETURN_FORMAT_UINT32
;
391 return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
395 if (dispatch_width
== 16 &&
396 !inst
->force_uncompressed
&& !inst
->force_sechalf
)
397 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
400 switch (inst
->opcode
) {
401 case SHADER_OPCODE_TEX
:
402 if (inst
->shadow_compare
) {
403 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
;
405 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE
;
409 if (inst
->shadow_compare
) {
410 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
;
412 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
415 case SHADER_OPCODE_TXL
:
416 if (inst
->shadow_compare
) {
417 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
419 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
422 case SHADER_OPCODE_TXS
:
423 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
425 case SHADER_OPCODE_TXD
:
426 if (inst
->shadow_compare
) {
427 /* Gen7.5+. Otherwise, lowered by brw_lower_texture_gradients(). */
428 assert(brw
->is_haswell
);
429 msg_type
= HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE
;
431 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
434 case SHADER_OPCODE_TXF
:
435 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
437 case SHADER_OPCODE_TXF_CMS
:
439 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
;
441 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
443 case SHADER_OPCODE_TXF_UMS
:
444 assert(brw
->gen
>= 7);
445 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
;
447 case SHADER_OPCODE_TXF_MCS
:
448 assert(brw
->gen
>= 7);
449 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
;
451 case SHADER_OPCODE_LOD
:
452 msg_type
= GEN5_SAMPLER_MESSAGE_LOD
;
454 case SHADER_OPCODE_TG4
:
455 if (inst
->shadow_compare
) {
456 assert(brw
->gen
>= 7);
457 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C
;
459 assert(brw
->gen
>= 6);
460 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4
;
463 case SHADER_OPCODE_TG4_OFFSET
:
464 assert(brw
->gen
>= 7);
465 if (inst
->shadow_compare
) {
466 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C
;
468 msg_type
= GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO
;
472 assert(!"not reached");
476 switch (inst
->opcode
) {
477 case SHADER_OPCODE_TEX
:
478 /* Note that G45 and older determines shadow compare and dispatch width
479 * from message length for most messages.
481 assert(dispatch_width
== 8);
482 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE
;
483 if (inst
->shadow_compare
) {
484 assert(inst
->mlen
== 6);
486 assert(inst
->mlen
<= 4);
490 if (inst
->shadow_compare
) {
491 assert(inst
->mlen
== 6);
492 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE
;
494 assert(inst
->mlen
== 9);
495 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
496 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
499 case SHADER_OPCODE_TXL
:
500 if (inst
->shadow_compare
) {
501 assert(inst
->mlen
== 6);
502 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE
;
504 assert(inst
->mlen
== 9);
505 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD
;
506 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
509 case SHADER_OPCODE_TXD
:
510 /* There is no sample_d_c message; comparisons are done manually */
511 assert(inst
->mlen
== 7 || inst
->mlen
== 10);
512 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS
;
514 case SHADER_OPCODE_TXF
:
515 assert(inst
->mlen
== 9);
516 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
517 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
519 case SHADER_OPCODE_TXS
:
520 assert(inst
->mlen
== 3);
521 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_RESINFO
;
522 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
525 assert(!"not reached");
529 assert(msg_type
!= -1);
531 if (simd_mode
== BRW_SAMPLER_SIMD_MODE_SIMD16
) {
536 if (brw
->gen
>= 7 && inst
->header_present
&& dispatch_width
== 16) {
537 /* The send-from-GRF for SIMD16 texturing with a header has an extra
538 * hardware register allocated to it, which we need to skip over (since
539 * our coordinates in the payload are in the even-numbered registers,
540 * and the header comes right before the first one).
542 assert(src
.file
== BRW_GENERAL_REGISTER_FILE
);
546 /* Load the message header if present. If there's a texture offset,
547 * we need to set it up explicitly and load the offset bitfield.
548 * Otherwise, we can use an implied move from g0 to the first message reg.
550 if (inst
->header_present
) {
551 if (brw
->gen
< 6 && !inst
->texture_offset
) {
552 /* Set up an implied move from g0 to the MRF. */
553 src
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
555 struct brw_reg header_reg
;
560 assert(inst
->base_mrf
!= -1);
561 header_reg
= brw_message_reg(inst
->base_mrf
);
564 brw_push_insn_state(p
);
565 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
566 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
567 /* Explicitly set up the message header by copying g0 to the MRF. */
568 brw_MOV(p
, header_reg
, brw_vec8_grf(0, 0));
570 if (inst
->texture_offset
) {
571 /* Set the offset bits in DWord 2. */
572 brw_MOV(p
, get_element_ud(header_reg
, 2),
573 brw_imm_ud(inst
->texture_offset
));
576 if (inst
->sampler
>= 16) {
577 /* The "Sampler Index" field can only store values between 0 and 15.
578 * However, we can add an offset to the "Sampler State Pointer"
579 * field, effectively selecting a different set of 16 samplers.
581 * The "Sampler State Pointer" needs to be aligned to a 32-byte
582 * offset, and each sampler state is only 16-bytes, so we can't
583 * exclusively use the offset - we have to use both.
585 assert(brw
->is_haswell
); /* field only exists on Haswell */
587 get_element_ud(header_reg
, 3),
588 get_element_ud(brw_vec8_grf(0, 0), 3),
589 brw_imm_ud(16 * (inst
->sampler
/ 16) *
590 sizeof(gen7_sampler_state
)));
592 brw_pop_insn_state(p
);
596 uint32_t surface_index
= ((inst
->opcode
== SHADER_OPCODE_TG4
||
597 inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
598 ? prog_data
->base
.binding_table
.gather_texture_start
599 : prog_data
->base
.binding_table
.texture_start
) + inst
->sampler
;
602 retype(dst
, BRW_REGISTER_TYPE_UW
),
610 inst
->header_present
,
614 brw_mark_surface_used(&prog_data
->base
, surface_index
);
618 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
621 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
623 * Ideally, we want to produce:
626 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
627 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
628 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
629 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
630 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
631 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
632 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
633 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
635 * and add another set of two more subspans if in 16-pixel dispatch mode.
637 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
638 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
639 * pair. But the ideal approximation may impose a huge performance cost on
640 * sample_d. On at least Haswell, sample_d instruction does some
641 * optimizations if the same LOD is used for all pixels in the subspan.
643 * For DDY, we need to use ALIGN16 mode since it's capable of doing the
644 * appropriate swizzling.
647 fs_generator::generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
649 unsigned vstride
, width
;
651 if (key
->high_quality_derivatives
) {
652 /* produce accurate derivatives */
653 vstride
= BRW_VERTICAL_STRIDE_2
;
657 /* replicate the derivative at the top-left pixel to other pixels */
658 vstride
= BRW_VERTICAL_STRIDE_4
;
662 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 1,
666 BRW_HORIZONTAL_STRIDE_0
,
667 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
668 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
672 BRW_HORIZONTAL_STRIDE_0
,
673 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
674 brw_ADD(p
, dst
, src0
, negate(src1
));
677 /* The negate_value boolean is used to negate the derivative computation for
678 * FBOs, since they place the origin at the upper left instead of the lower
682 fs_generator::generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
,
685 if (key
->high_quality_derivatives
) {
686 /* From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
687 * Region Restrictions):
689 * In Align16 access mode, SIMD16 is not allowed for DW operations
690 * and SIMD8 is not allowed for DF operations.
692 * In this context, "DW operations" means "operations acting on 32-bit
693 * values", so it includes operations on floats.
695 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
696 * (Instruction Compression -> Rules and Restrictions):
698 * A compressed instruction must be in Align1 access mode. Align16
699 * mode instructions cannot be compressed.
701 * Similar text exists in the g45 PRM.
703 * On these platforms, if we're building a SIMD16 shader, we need to
704 * manually unroll to a pair of SIMD8 instructions.
706 bool unroll_to_simd8
=
707 (dispatch_width
== 16 &&
708 (brw
->gen
== 4 || (brw
->gen
== 7 && !brw
->is_haswell
)));
710 /* produce accurate derivatives */
711 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
713 BRW_VERTICAL_STRIDE_4
,
715 BRW_HORIZONTAL_STRIDE_1
,
716 BRW_SWIZZLE_XYXY
, WRITEMASK_XYZW
);
717 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
719 BRW_VERTICAL_STRIDE_4
,
721 BRW_HORIZONTAL_STRIDE_1
,
722 BRW_SWIZZLE_ZWZW
, WRITEMASK_XYZW
);
723 brw_push_insn_state(p
);
724 brw_set_access_mode(p
, BRW_ALIGN_16
);
726 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
728 brw_ADD(p
, dst
, src1
, negate(src0
));
730 brw_ADD(p
, dst
, src0
, negate(src1
));
731 if (unroll_to_simd8
) {
732 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
733 src0
= sechalf(src0
);
734 src1
= sechalf(src1
);
737 brw_ADD(p
, dst
, src1
, negate(src0
));
739 brw_ADD(p
, dst
, src0
, negate(src1
));
741 brw_pop_insn_state(p
);
743 /* replicate the derivative at the top-left pixel to other pixels */
744 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
746 BRW_VERTICAL_STRIDE_4
,
748 BRW_HORIZONTAL_STRIDE_0
,
749 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
750 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 2,
752 BRW_VERTICAL_STRIDE_4
,
754 BRW_HORIZONTAL_STRIDE_0
,
755 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
757 brw_ADD(p
, dst
, src1
, negate(src0
));
759 brw_ADD(p
, dst
, src0
, negate(src1
));
764 fs_generator::generate_discard_jump(fs_inst
*inst
)
766 assert(brw
->gen
>= 6);
768 /* This HALT will be patched up at FB write time to point UIP at the end of
769 * the program, and at brw_uip_jip() JIP will be set to the end of the
770 * current block (or the program).
772 this->discard_halt_patches
.push_tail(new(mem_ctx
) ip_record(p
->nr_insn
));
774 brw_push_insn_state(p
);
775 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
777 brw_pop_insn_state(p
);
781 fs_generator::generate_scratch_write(fs_inst
*inst
, struct brw_reg src
)
783 assert(inst
->mlen
!= 0);
786 retype(brw_message_reg(inst
->base_mrf
+ 1), BRW_REGISTER_TYPE_UD
),
787 retype(src
, BRW_REGISTER_TYPE_UD
));
788 brw_oword_block_write_scratch(p
, brw_message_reg(inst
->base_mrf
),
789 dispatch_width
/ 8, inst
->offset
);
793 fs_generator::generate_scratch_read(fs_inst
*inst
, struct brw_reg dst
)
795 assert(inst
->mlen
!= 0);
797 brw_oword_block_read_scratch(p
, dst
, brw_message_reg(inst
->base_mrf
),
798 dispatch_width
/ 8, inst
->offset
);
802 fs_generator::generate_scratch_read_gen7(fs_inst
*inst
, struct brw_reg dst
)
804 gen7_block_read_scratch(p
, dst
, dispatch_width
/ 8, inst
->offset
);
808 fs_generator::generate_uniform_pull_constant_load(fs_inst
*inst
,
810 struct brw_reg index
,
811 struct brw_reg offset
)
813 assert(inst
->mlen
!= 0);
815 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
816 index
.type
== BRW_REGISTER_TYPE_UD
);
817 uint32_t surf_index
= index
.dw1
.ud
;
819 assert(offset
.file
== BRW_IMMEDIATE_VALUE
&&
820 offset
.type
== BRW_REGISTER_TYPE_UD
);
821 uint32_t read_offset
= offset
.dw1
.ud
;
823 brw_oword_block_read(p
, dst
, brw_message_reg(inst
->base_mrf
),
824 read_offset
, surf_index
);
826 brw_mark_surface_used(&prog_data
->base
, surf_index
);
830 fs_generator::generate_uniform_pull_constant_load_gen7(fs_inst
*inst
,
832 struct brw_reg index
,
833 struct brw_reg offset
)
835 assert(inst
->mlen
== 0);
837 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
838 index
.type
== BRW_REGISTER_TYPE_UD
);
839 uint32_t surf_index
= index
.dw1
.ud
;
841 assert(offset
.file
== BRW_GENERAL_REGISTER_FILE
);
842 /* Reference just the dword we need, to avoid angering validate_reg(). */
843 offset
= brw_vec1_grf(offset
.nr
, 0);
845 brw_push_insn_state(p
);
846 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
847 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
848 struct brw_instruction
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
849 brw_pop_insn_state(p
);
851 /* We use the SIMD4x2 mode because we want to end up with 4 components in
852 * the destination loaded consecutively from the same offset (which appears
853 * in the first component, and the rest are ignored).
855 dst
.width
= BRW_WIDTH_4
;
856 brw_set_dest(p
, send
, dst
);
857 brw_set_src0(p
, send
, offset
);
858 brw_set_sampler_message(p
, send
,
860 0, /* LD message ignores sampler unit */
861 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
864 false, /* no header */
865 BRW_SAMPLER_SIMD_MODE_SIMD4X2
,
868 brw_mark_surface_used(&prog_data
->base
, surf_index
);
872 fs_generator::generate_varying_pull_constant_load(fs_inst
*inst
,
874 struct brw_reg index
,
875 struct brw_reg offset
)
877 assert(brw
->gen
< 7); /* Should use the gen7 variant. */
878 assert(inst
->header_present
);
881 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
882 index
.type
== BRW_REGISTER_TYPE_UD
);
883 uint32_t surf_index
= index
.dw1
.ud
;
885 uint32_t simd_mode
, rlen
, msg_type
;
886 if (dispatch_width
== 16) {
887 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
890 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
895 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
897 /* We always use the SIMD16 message so that we only have to load U, and
900 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
901 assert(inst
->mlen
== 3);
902 assert(inst
->regs_written
== 8);
904 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
907 struct brw_reg offset_mrf
= retype(brw_message_reg(inst
->base_mrf
+ 1),
908 BRW_REGISTER_TYPE_D
);
909 brw_MOV(p
, offset_mrf
, offset
);
911 struct brw_reg header
= brw_vec8_grf(0, 0);
912 gen6_resolve_implied_move(p
, &header
, inst
->base_mrf
);
914 struct brw_instruction
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
915 send
->header
.compression_control
= BRW_COMPRESSION_NONE
;
916 brw_set_dest(p
, send
, retype(dst
, BRW_REGISTER_TYPE_UW
));
917 brw_set_src0(p
, send
, header
);
919 send
->header
.destreg__conditionalmod
= inst
->base_mrf
;
921 /* Our surface is set up as floats, regardless of what actual data is
924 uint32_t return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
925 brw_set_sampler_message(p
, send
,
927 0, /* sampler (unused) */
931 inst
->header_present
,
935 brw_mark_surface_used(&prog_data
->base
, surf_index
);
939 fs_generator::generate_varying_pull_constant_load_gen7(fs_inst
*inst
,
941 struct brw_reg index
,
942 struct brw_reg offset
)
944 assert(brw
->gen
>= 7);
945 /* Varying-offset pull constant loads are treated as a normal expression on
946 * gen7, so the fact that it's a send message is hidden at the IR level.
948 assert(!inst
->header_present
);
951 assert(index
.file
== BRW_IMMEDIATE_VALUE
&&
952 index
.type
== BRW_REGISTER_TYPE_UD
);
953 uint32_t surf_index
= index
.dw1
.ud
;
955 uint32_t simd_mode
, rlen
, mlen
;
956 if (dispatch_width
== 16) {
959 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
963 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
966 struct brw_instruction
*send
= brw_next_insn(p
, BRW_OPCODE_SEND
);
967 brw_set_dest(p
, send
, dst
);
968 brw_set_src0(p
, send
, offset
);
969 brw_set_sampler_message(p
, send
,
971 0, /* LD message ignores sampler unit */
972 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
975 false, /* no header */
979 brw_mark_surface_used(&prog_data
->base
, surf_index
);
983 * Cause the current pixel/sample mask (from R1.7 bits 15:0) to be transferred
984 * into the flags register (f0.0).
986 * Used only on Gen6 and above.
989 fs_generator::generate_mov_dispatch_to_flags(fs_inst
*inst
)
991 struct brw_reg flags
= brw_flag_reg(0, inst
->flag_subreg
);
992 struct brw_reg dispatch_mask
;
995 dispatch_mask
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
997 dispatch_mask
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
999 brw_push_insn_state(p
);
1000 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1001 brw_MOV(p
, flags
, dispatch_mask
);
1002 brw_pop_insn_state(p
);
1006 static uint32_t brw_file_from_reg(fs_reg
*reg
)
1008 switch (reg
->file
) {
1010 return BRW_GENERAL_REGISTER_FILE
;
1012 return BRW_MESSAGE_REGISTER_FILE
;
1014 return BRW_IMMEDIATE_VALUE
;
1016 assert(!"not reached");
1017 return BRW_GENERAL_REGISTER_FILE
;
1022 brw_reg_from_fs_reg(fs_reg
*reg
)
1024 struct brw_reg brw_reg
;
1026 switch (reg
->file
) {
1029 if (reg
->stride
== 0) {
1030 brw_reg
= brw_vec1_reg(brw_file_from_reg(reg
), reg
->reg
, 0);
1032 brw_reg
= brw_vec8_reg(brw_file_from_reg(reg
), reg
->reg
, 0);
1033 brw_reg
= stride(brw_reg
, 8 * reg
->stride
, 8, reg
->stride
);
1036 brw_reg
= retype(brw_reg
, reg
->type
);
1037 brw_reg
= byte_offset(brw_reg
, reg
->subreg_offset
);
1040 switch (reg
->type
) {
1041 case BRW_REGISTER_TYPE_F
:
1042 brw_reg
= brw_imm_f(reg
->imm
.f
);
1044 case BRW_REGISTER_TYPE_D
:
1045 brw_reg
= brw_imm_d(reg
->imm
.i
);
1047 case BRW_REGISTER_TYPE_UD
:
1048 brw_reg
= brw_imm_ud(reg
->imm
.u
);
1051 assert(!"not reached");
1052 brw_reg
= brw_null_reg();
1057 assert(reg
->type
== reg
->fixed_hw_reg
.type
);
1058 brw_reg
= reg
->fixed_hw_reg
;
1061 /* Probably unused. */
1062 brw_reg
= brw_null_reg();
1065 assert(!"not reached");
1066 brw_reg
= brw_null_reg();
1069 assert(!"not reached");
1070 brw_reg
= brw_null_reg();
1074 brw_reg
= brw_abs(brw_reg
);
1076 brw_reg
= negate(brw_reg
);
1082 * Sets the first word of a vgrf for gen7+ simd4x2 uniform pull constant
1083 * sampler LD messages.
1085 * We don't want to bake it into the send message's code generation because
1086 * that means we don't get a chance to schedule the instructions.
1089 fs_generator::generate_set_simd4x2_offset(fs_inst
*inst
,
1091 struct brw_reg value
)
1093 assert(value
.file
== BRW_IMMEDIATE_VALUE
);
1095 brw_push_insn_state(p
);
1096 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1097 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1098 brw_MOV(p
, retype(brw_vec1_reg(dst
.file
, dst
.nr
, 0), value
.type
), value
);
1099 brw_pop_insn_state(p
);
1102 /* Sets vstride=16, width=8, hstride=2 or vstride=0, width=1, hstride=0
1103 * (when mask is passed as a uniform) of register mask before moving it
1107 fs_generator::generate_set_omask(fs_inst
*inst
,
1109 struct brw_reg mask
)
1112 (mask
.vstride
== BRW_VERTICAL_STRIDE_8
&&
1113 mask
.width
== BRW_WIDTH_8
&&
1114 mask
.hstride
== BRW_HORIZONTAL_STRIDE_1
);
1117 (mask
.vstride
== BRW_VERTICAL_STRIDE_0
&&
1118 mask
.width
== BRW_WIDTH_1
&&
1119 mask
.hstride
== BRW_HORIZONTAL_STRIDE_0
);
1121 assert(stride_8_8_1
|| stride_0_1_0
);
1122 assert(dst
.type
== BRW_REGISTER_TYPE_UW
);
1124 if (dispatch_width
== 16)
1126 brw_push_insn_state(p
);
1127 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1128 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1131 brw_MOV(p
, dst
, retype(stride(mask
, 16, 8, 2), dst
.type
));
1132 } else if (stride_0_1_0
) {
1133 brw_MOV(p
, dst
, retype(mask
, dst
.type
));
1135 brw_pop_insn_state(p
);
1138 /* Sets vstride=1, width=4, hstride=0 of register src1 during
1139 * the ADD instruction.
1142 fs_generator::generate_set_sample_id(fs_inst
*inst
,
1144 struct brw_reg src0
,
1145 struct brw_reg src1
)
1147 assert(dst
.type
== BRW_REGISTER_TYPE_D
||
1148 dst
.type
== BRW_REGISTER_TYPE_UD
);
1149 assert(src0
.type
== BRW_REGISTER_TYPE_D
||
1150 src0
.type
== BRW_REGISTER_TYPE_UD
);
1152 brw_push_insn_state(p
);
1153 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1154 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
1155 struct brw_reg reg
= retype(stride(src1
, 1, 4, 0), BRW_REGISTER_TYPE_UW
);
1156 brw_ADD(p
, dst
, src0
, reg
);
1157 if (dispatch_width
== 16)
1158 brw_ADD(p
, offset(dst
, 1), offset(src0
, 1), suboffset(reg
, 2));
1159 brw_pop_insn_state(p
);
1163 * Change the register's data type from UD to W, doubling the strides in order
1164 * to compensate for halving the data type width.
1166 static struct brw_reg
1167 ud_reg_to_w(struct brw_reg r
)
1169 assert(r
.type
== BRW_REGISTER_TYPE_UD
);
1170 r
.type
= BRW_REGISTER_TYPE_W
;
1172 /* The BRW_*_STRIDE enums are defined so that incrementing the field
1173 * doubles the real stride.
1184 fs_generator::generate_pack_half_2x16_split(fs_inst
*inst
,
1189 assert(brw
->gen
>= 7);
1190 assert(dst
.type
== BRW_REGISTER_TYPE_UD
);
1191 assert(x
.type
== BRW_REGISTER_TYPE_F
);
1192 assert(y
.type
== BRW_REGISTER_TYPE_F
);
1194 /* From the Ivybridge PRM, Vol4, Part3, Section 6.27 f32to16:
1196 * Because this instruction does not have a 16-bit floating-point type,
1197 * the destination data type must be Word (W).
1199 * The destination must be DWord-aligned and specify a horizontal stride
1200 * (HorzStride) of 2. The 16-bit result is stored in the lower word of
1201 * each destination channel and the upper word is not modified.
1203 struct brw_reg dst_w
= ud_reg_to_w(dst
);
1205 /* Give each 32-bit channel of dst the form below , where "." means
1209 brw_F32TO16(p
, dst_w
, y
);
1214 brw_SHL(p
, dst
, dst
, brw_imm_ud(16u));
1216 /* And, finally the form of packHalf2x16's output:
1219 brw_F32TO16(p
, dst_w
, x
);
1223 fs_generator::generate_unpack_half_2x16_split(fs_inst
*inst
,
1227 assert(brw
->gen
>= 7);
1228 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1229 assert(src
.type
== BRW_REGISTER_TYPE_UD
);
1231 /* From the Ivybridge PRM, Vol4, Part3, Section 6.26 f16to32:
1233 * Because this instruction does not have a 16-bit floating-point type,
1234 * the source data type must be Word (W). The destination type must be
1237 struct brw_reg src_w
= ud_reg_to_w(src
);
1239 /* Each channel of src has the form of unpackHalf2x16's input: 0xhhhhllll.
1240 * For the Y case, we wish to access only the upper word; therefore
1241 * a 16-bit subregister offset is needed.
1243 assert(inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
||
1244 inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
);
1245 if (inst
->opcode
== FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
)
1248 brw_F16TO32(p
, dst
, src_w
);
1252 fs_generator::generate_shader_time_add(fs_inst
*inst
,
1253 struct brw_reg payload
,
1254 struct brw_reg offset
,
1255 struct brw_reg value
)
1257 assert(brw
->gen
>= 7);
1258 brw_push_insn_state(p
);
1259 brw_set_mask_control(p
, true);
1261 assert(payload
.file
== BRW_GENERAL_REGISTER_FILE
);
1262 struct brw_reg payload_offset
= retype(brw_vec1_grf(payload
.nr
, 0),
1264 struct brw_reg payload_value
= retype(brw_vec1_grf(payload
.nr
+ 1, 0),
1267 assert(offset
.file
== BRW_IMMEDIATE_VALUE
);
1268 if (value
.file
== BRW_GENERAL_REGISTER_FILE
) {
1269 value
.width
= BRW_WIDTH_1
;
1270 value
.hstride
= BRW_HORIZONTAL_STRIDE_0
;
1271 value
.vstride
= BRW_VERTICAL_STRIDE_0
;
1273 assert(value
.file
== BRW_IMMEDIATE_VALUE
);
1276 /* Trying to deal with setup of the params from the IR is crazy in the FS8
1277 * case, and we don't really care about squeezing every bit of performance
1278 * out of this path, so we just emit the MOVs from here.
1280 brw_MOV(p
, payload_offset
, offset
);
1281 brw_MOV(p
, payload_value
, value
);
1282 brw_shader_time_add(p
, payload
,
1283 prog_data
->base
.binding_table
.shader_time_start
);
1284 brw_pop_insn_state(p
);
1286 brw_mark_surface_used(&prog_data
->base
,
1287 prog_data
->base
.binding_table
.shader_time_start
);
1291 fs_generator::generate_untyped_atomic(fs_inst
*inst
, struct brw_reg dst
,
1292 struct brw_reg atomic_op
,
1293 struct brw_reg surf_index
)
1295 assert(atomic_op
.file
== BRW_IMMEDIATE_VALUE
&&
1296 atomic_op
.type
== BRW_REGISTER_TYPE_UD
&&
1297 surf_index
.file
== BRW_IMMEDIATE_VALUE
&&
1298 surf_index
.type
== BRW_REGISTER_TYPE_UD
);
1300 brw_untyped_atomic(p
, dst
, brw_message_reg(inst
->base_mrf
),
1301 atomic_op
.dw1
.ud
, surf_index
.dw1
.ud
,
1302 inst
->mlen
, dispatch_width
/ 8);
1304 brw_mark_surface_used(&prog_data
->base
, surf_index
.dw1
.ud
);
1308 fs_generator::generate_untyped_surface_read(fs_inst
*inst
, struct brw_reg dst
,
1309 struct brw_reg surf_index
)
1311 assert(surf_index
.file
== BRW_IMMEDIATE_VALUE
&&
1312 surf_index
.type
== BRW_REGISTER_TYPE_UD
);
1314 brw_untyped_surface_read(p
, dst
, brw_message_reg(inst
->base_mrf
),
1316 inst
->mlen
, dispatch_width
/ 8);
1318 brw_mark_surface_used(&prog_data
->base
, surf_index
.dw1
.ud
);
1322 fs_generator::generate_code(exec_list
*instructions
, FILE *dump_file
)
1324 int last_native_insn_offset
= p
->next_insn_offset
;
1325 const char *last_annotation_string
= NULL
;
1326 const void *last_annotation_ir
= NULL
;
1328 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1331 "Native code for %s fragment shader %d (SIMD%d dispatch):\n",
1332 prog
->Label
? prog
->Label
: "unnamed",
1333 prog
->Name
, dispatch_width
);
1336 "Native code for fragment program %d (SIMD%d dispatch):\n",
1337 fp
->Base
.Id
, dispatch_width
);
1339 fprintf(stderr
, "Native code for blorp program (SIMD%d dispatch):\n",
1345 if (unlikely(INTEL_DEBUG
& DEBUG_WM
))
1346 cfg
= new(mem_ctx
) cfg_t(instructions
);
1348 foreach_list(node
, instructions
) {
1349 fs_inst
*inst
= (fs_inst
*)node
;
1350 struct brw_reg src
[3], dst
;
1352 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1353 foreach_list(node
, &cfg
->block_list
) {
1354 bblock_link
*link
= (bblock_link
*)node
;
1355 bblock_t
*block
= link
->block
;
1357 if (block
->start
== inst
) {
1358 fprintf(stderr
, " START B%d", block
->block_num
);
1359 foreach_list(predecessor_node
, &block
->parents
) {
1360 bblock_link
*predecessor_link
=
1361 (bblock_link
*)predecessor_node
;
1362 bblock_t
*predecessor_block
= predecessor_link
->block
;
1363 fprintf(stderr
, " <-B%d", predecessor_block
->block_num
);
1365 fprintf(stderr
, "\n");
1369 if (last_annotation_ir
!= inst
->ir
) {
1370 last_annotation_ir
= inst
->ir
;
1371 if (last_annotation_ir
) {
1372 fprintf(stderr
, " ");
1374 ((ir_instruction
*)inst
->ir
)->fprint(stderr
);
1376 const prog_instruction
*fpi
;
1377 fpi
= (const prog_instruction
*)inst
->ir
;
1378 fprintf(stderr
, "%d: ",
1379 (int)(fpi
- (fp
? fp
->Base
.Instructions
: 0)));
1380 _mesa_fprint_instruction_opt(stderr
,
1382 0, PROG_PRINT_DEBUG
, NULL
);
1384 fprintf(stderr
, "\n");
1387 if (last_annotation_string
!= inst
->annotation
) {
1388 last_annotation_string
= inst
->annotation
;
1389 if (last_annotation_string
)
1390 fprintf(stderr
, " %s\n", last_annotation_string
);
1394 for (unsigned int i
= 0; i
< 3; i
++) {
1395 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
1397 /* The accumulator result appears to get used for the
1398 * conditional modifier generation. When negating a UD
1399 * value, there is a 33rd bit generated for the sign in the
1400 * accumulator value, so now you can't check, for example,
1401 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
1403 assert(!inst
->conditional_mod
||
1404 inst
->src
[i
].type
!= BRW_REGISTER_TYPE_UD
||
1405 !inst
->src
[i
].negate
);
1407 dst
= brw_reg_from_fs_reg(&inst
->dst
);
1409 brw_set_conditionalmod(p
, inst
->conditional_mod
);
1410 brw_set_predicate_control(p
, inst
->predicate
);
1411 brw_set_predicate_inverse(p
, inst
->predicate_inverse
);
1412 brw_set_flag_reg(p
, 0, inst
->flag_subreg
);
1413 brw_set_saturate(p
, inst
->saturate
);
1414 brw_set_mask_control(p
, inst
->force_writemask_all
);
1415 brw_set_acc_write_control(p
, inst
->writes_accumulator
);
1417 if (inst
->force_uncompressed
|| dispatch_width
== 8) {
1418 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1419 } else if (inst
->force_sechalf
) {
1420 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1422 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1425 switch (inst
->opcode
) {
1426 case BRW_OPCODE_MOV
:
1427 brw_MOV(p
, dst
, src
[0]);
1429 case BRW_OPCODE_ADD
:
1430 brw_ADD(p
, dst
, src
[0], src
[1]);
1432 case BRW_OPCODE_MUL
:
1433 brw_MUL(p
, dst
, src
[0], src
[1]);
1435 case BRW_OPCODE_AVG
:
1436 brw_AVG(p
, dst
, src
[0], src
[1]);
1438 case BRW_OPCODE_MACH
:
1439 brw_MACH(p
, dst
, src
[0], src
[1]);
1442 case BRW_OPCODE_MAD
:
1443 assert(brw
->gen
>= 6);
1444 brw_set_access_mode(p
, BRW_ALIGN_16
);
1445 if (dispatch_width
== 16 && !brw
->is_haswell
) {
1446 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1447 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
1448 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1449 brw_MAD(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1450 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1452 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
1454 brw_set_access_mode(p
, BRW_ALIGN_1
);
1457 case BRW_OPCODE_LRP
:
1458 assert(brw
->gen
>= 6);
1459 brw_set_access_mode(p
, BRW_ALIGN_16
);
1460 if (dispatch_width
== 16 && !brw
->is_haswell
) {
1461 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
1462 brw_LRP(p
, dst
, src
[0], src
[1], src
[2]);
1463 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
1464 brw_LRP(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
1465 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1467 brw_LRP(p
, dst
, src
[0], src
[1], src
[2]);
1469 brw_set_access_mode(p
, BRW_ALIGN_1
);
1472 case BRW_OPCODE_FRC
:
1473 brw_FRC(p
, dst
, src
[0]);
1475 case BRW_OPCODE_RNDD
:
1476 brw_RNDD(p
, dst
, src
[0]);
1478 case BRW_OPCODE_RNDE
:
1479 brw_RNDE(p
, dst
, src
[0]);
1481 case BRW_OPCODE_RNDZ
:
1482 brw_RNDZ(p
, dst
, src
[0]);
1485 case BRW_OPCODE_AND
:
1486 brw_AND(p
, dst
, src
[0], src
[1]);
1489 brw_OR(p
, dst
, src
[0], src
[1]);
1491 case BRW_OPCODE_XOR
:
1492 brw_XOR(p
, dst
, src
[0], src
[1]);
1494 case BRW_OPCODE_NOT
:
1495 brw_NOT(p
, dst
, src
[0]);
1497 case BRW_OPCODE_ASR
:
1498 brw_ASR(p
, dst
, src
[0], src
[1]);
1500 case BRW_OPCODE_SHR
:
1501 brw_SHR(p
, dst
, src
[0], src
[1]);
1503 case BRW_OPCODE_SHL
:
1504 brw_SHL(p
, dst
, src
[0], src
[1]);
1506 case BRW_OPCODE_F32TO16
:
1507 assert(brw
->gen
>= 7);
1508 brw_F32TO16(p
, dst
, src
[0]);
1510 case BRW_OPCODE_F16TO32
:
1511 assert(brw
->gen
>= 7);
1512 brw_F16TO32(p
, dst
, src
[0]);
1514 case BRW_OPCODE_CMP
:
1515 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
1517 case BRW_OPCODE_SEL
:
1518 brw_SEL(p
, dst
, src
[0], src
[1]);
1520 case BRW_OPCODE_BFREV
:
1521 assert(brw
->gen
>= 7);
1522 /* BFREV only supports UD type for src and dst. */
1523 brw_BFREV(p
, retype(dst
, BRW_REGISTER_TYPE_UD
),
1524 retype(src
[0], BRW_REGISTER_TYPE_UD
));
1526 case BRW_OPCODE_FBH
:
1527 assert(brw
->gen
>= 7);
1528 /* FBH only supports UD type for dst. */
1529 brw_FBH(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1531 case BRW_OPCODE_FBL
:
1532 assert(brw
->gen
>= 7);
1533 /* FBL only supports UD type for dst. */
1534 brw_FBL(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1536 case BRW_OPCODE_CBIT
:
1537 assert(brw
->gen
>= 7);
1538 /* CBIT only supports UD type for dst. */
1539 brw_CBIT(p
, retype(dst
, BRW_REGISTER_TYPE_UD
), src
[0]);
1541 case BRW_OPCODE_ADDC
:
1542 assert(brw
->gen
>= 7);
1543 brw_ADDC(p
, dst
, src
[0], src
[1]);
1545 case BRW_OPCODE_SUBB
:
1546 assert(brw
->gen
>= 7);
1547 brw_SUBB(p
, dst
, src
[0], src
[1]);
1549 case BRW_OPCODE_MAC
:
1550 brw_MAC(p
, dst
, src
[0], src
[1]);
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 key->render_to_fbo is set).
1707 assert(fp
->UsesDFdy
);
1708 generate_ddy(inst
, dst
, src
[0], 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_disassemble(brw
, p
->store
, last_native_insn_offset
, p
->next_insn_offset
, stderr
);
1808 foreach_list(node
, &cfg
->block_list
) {
1809 bblock_link
*link
= (bblock_link
*)node
;
1810 bblock_t
*block
= link
->block
;
1812 if (block
->end
== inst
) {
1813 fprintf(stderr
, " END B%d", block
->block_num
);
1814 foreach_list(successor_node
, &block
->children
) {
1815 bblock_link
*successor_link
=
1816 (bblock_link
*)successor_node
;
1817 bblock_t
*successor_block
= successor_link
->block
;
1818 fprintf(stderr
, " ->B%d", successor_block
->block_num
);
1820 fprintf(stderr
, "\n");
1825 last_native_insn_offset
= p
->next_insn_offset
;
1828 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1829 fprintf(stderr
, "\n");
1834 /* OK, while the INTEL_DEBUG=wm above is very nice for debugging FS
1835 * emit issues, it doesn't get the jump distances into the output,
1836 * which is often something we want to debug. So this is here in
1837 * case you're doing that.
1840 brw_disassemble(brw
, p
->store
, 0, p
->next_insn_offset
, dump_file
);
1845 fs_generator::generate_assembly(exec_list
*simd8_instructions
,
1846 exec_list
*simd16_instructions
,
1847 unsigned *assembly_size
,
1850 assert(simd8_instructions
|| simd16_instructions
);
1852 if (simd8_instructions
) {
1854 generate_code(simd8_instructions
, dump_file
);
1855 brw_compact_instructions(p
);
1858 if (simd16_instructions
) {
1859 /* align to 64 byte boundary. */
1860 while ((p
->nr_insn
* sizeof(struct brw_instruction
)) % 64) {
1864 /* Save off the start of this SIMD16 program */
1865 prog_data
->prog_offset_16
= p
->nr_insn
* sizeof(struct brw_instruction
);
1867 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
1869 dispatch_width
= 16;
1870 generate_code(simd16_instructions
, dump_file
);
1871 brw_compact_instructions(p
);
1874 return brw_get_program(p
, assembly_size
);