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_emit.cpp
26 * This file supports emitting code from the FS LIR to the actual
27 * native instructions.
31 #include "main/macros.h"
32 #include "brw_context.h"
37 #include "brw_fs_cfg.h"
38 #include "glsl/ir_print_visitor.h"
41 fs_visitor::generate_fb_write(fs_inst
*inst
)
44 struct brw_reg implied_header
;
47 /* Header is 2 regs, g0 and g1 are the contents. g0 will be implied
50 brw_push_insn_state(p
);
51 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
52 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
54 if (inst
->header_present
) {
55 if (intel
->gen
>= 6) {
56 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
58 retype(brw_message_reg(inst
->base_mrf
), BRW_REGISTER_TYPE_UD
),
59 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
60 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
62 if (inst
->target
> 0) {
63 /* Set the render target index for choosing BLEND_STATE. */
64 brw_MOV(p
, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE
,
66 BRW_REGISTER_TYPE_UD
),
67 brw_imm_ud(inst
->target
));
70 implied_header
= brw_null_reg();
72 implied_header
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
75 brw_message_reg(inst
->base_mrf
+ 1),
79 implied_header
= brw_null_reg();
82 if (this->dual_src_output
.file
!= BAD_FILE
)
83 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01
;
84 else if (c
->dispatch_width
== 16)
85 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
87 msg_control
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01
;
89 brw_pop_insn_state(p
);
100 inst
->header_present
);
103 /* Computes the integer pixel x,y values from the origin.
105 * This is the basis of gl_FragCoord computation, but is also used
106 * pre-gen6 for computing the deltas from v0 for computing
110 fs_visitor::generate_pixel_xy(struct brw_reg dst
, bool is_x
)
112 struct brw_reg g1_uw
= retype(brw_vec1_grf(1, 0), BRW_REGISTER_TYPE_UW
);
114 struct brw_reg deltas
;
117 src
= stride(suboffset(g1_uw
, 4), 2, 4, 0);
118 deltas
= brw_imm_v(0x10101010);
120 src
= stride(suboffset(g1_uw
, 5), 2, 4, 0);
121 deltas
= brw_imm_v(0x11001100);
124 if (c
->dispatch_width
== 16) {
128 /* We do this 8 or 16-wide, but since the destination is UW we
129 * don't do compression in the 16-wide case.
131 brw_push_insn_state(p
);
132 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
133 brw_ADD(p
, dst
, src
, deltas
);
134 brw_pop_insn_state(p
);
138 fs_visitor::generate_linterp(fs_inst
*inst
,
139 struct brw_reg dst
, struct brw_reg
*src
)
141 struct brw_reg delta_x
= src
[0];
142 struct brw_reg delta_y
= src
[1];
143 struct brw_reg interp
= src
[2];
146 delta_y
.nr
== delta_x
.nr
+ 1 &&
147 (intel
->gen
>= 6 || (delta_x
.nr
& 1) == 0)) {
148 brw_PLN(p
, dst
, interp
, delta_x
);
150 brw_LINE(p
, brw_null_reg(), interp
, delta_x
);
151 brw_MAC(p
, dst
, suboffset(interp
, 1), delta_y
);
156 fs_visitor::generate_math1_gen7(fs_inst
*inst
,
160 assert(inst
->mlen
== 0);
162 brw_math_function(inst
->opcode
),
163 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
164 : BRW_MATH_SATURATE_NONE
,
166 BRW_MATH_DATA_VECTOR
,
167 BRW_MATH_PRECISION_FULL
);
171 fs_visitor::generate_math2_gen7(fs_inst
*inst
,
176 assert(inst
->mlen
== 0);
177 brw_math2(p
, dst
, brw_math_function(inst
->opcode
), src0
, src1
);
181 fs_visitor::generate_math1_gen6(fs_inst
*inst
,
185 int op
= brw_math_function(inst
->opcode
);
187 assert(inst
->mlen
== 0);
189 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
192 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
193 BRW_MATH_SATURATE_NONE
,
195 BRW_MATH_DATA_VECTOR
,
196 BRW_MATH_PRECISION_FULL
);
198 if (c
->dispatch_width
== 16) {
199 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
200 brw_math(p
, sechalf(dst
),
202 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
203 BRW_MATH_SATURATE_NONE
,
205 BRW_MATH_DATA_VECTOR
,
206 BRW_MATH_PRECISION_FULL
);
207 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
212 fs_visitor::generate_math2_gen6(fs_inst
*inst
,
217 int op
= brw_math_function(inst
->opcode
);
219 assert(inst
->mlen
== 0);
221 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
222 brw_math2(p
, dst
, op
, src0
, src1
);
224 if (c
->dispatch_width
== 16) {
225 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
226 brw_math2(p
, sechalf(dst
), op
, sechalf(src0
), sechalf(src1
));
227 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
232 fs_visitor::generate_math_gen4(fs_inst
*inst
,
236 int op
= brw_math_function(inst
->opcode
);
238 assert(inst
->mlen
>= 1);
240 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
243 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
244 BRW_MATH_SATURATE_NONE
,
246 BRW_MATH_DATA_VECTOR
,
247 BRW_MATH_PRECISION_FULL
);
249 if (c
->dispatch_width
== 16) {
250 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
251 brw_math(p
, sechalf(dst
),
253 inst
->saturate
? BRW_MATH_SATURATE_SATURATE
:
254 BRW_MATH_SATURATE_NONE
,
255 inst
->base_mrf
+ 1, sechalf(src
),
256 BRW_MATH_DATA_VECTOR
,
257 BRW_MATH_PRECISION_FULL
);
259 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
264 fs_visitor::generate_tex(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
268 uint32_t simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD8
;
269 uint32_t return_format
;
272 case BRW_REGISTER_TYPE_D
:
273 return_format
= BRW_SAMPLER_RETURN_FORMAT_SINT32
;
275 case BRW_REGISTER_TYPE_UD
:
276 return_format
= BRW_SAMPLER_RETURN_FORMAT_UINT32
;
279 return_format
= BRW_SAMPLER_RETURN_FORMAT_FLOAT32
;
283 if (c
->dispatch_width
== 16)
284 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
286 if (intel
->gen
>= 5) {
287 switch (inst
->opcode
) {
288 case SHADER_OPCODE_TEX
:
289 if (inst
->shadow_compare
) {
290 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
;
292 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE
;
296 if (inst
->shadow_compare
) {
297 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
;
299 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
302 case SHADER_OPCODE_TXL
:
303 if (inst
->shadow_compare
) {
304 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
;
306 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
309 case SHADER_OPCODE_TXS
:
310 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
312 case SHADER_OPCODE_TXD
:
313 /* There is no sample_d_c message; comparisons are done manually */
314 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
316 case SHADER_OPCODE_TXF
:
317 msg_type
= GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
320 assert(!"not reached");
324 switch (inst
->opcode
) {
325 case SHADER_OPCODE_TEX
:
326 /* Note that G45 and older determines shadow compare and dispatch width
327 * from message length for most messages.
329 assert(c
->dispatch_width
== 8);
330 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE
;
331 if (inst
->shadow_compare
) {
332 assert(inst
->mlen
== 6);
334 assert(inst
->mlen
<= 4);
338 if (inst
->shadow_compare
) {
339 assert(inst
->mlen
== 6);
340 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_BIAS_COMPARE
;
342 assert(inst
->mlen
== 9);
343 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_BIAS
;
344 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
347 case SHADER_OPCODE_TXL
:
348 if (inst
->shadow_compare
) {
349 assert(inst
->mlen
== 6);
350 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_LOD_COMPARE
;
352 assert(inst
->mlen
== 9);
353 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_SAMPLE_LOD
;
354 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
357 case SHADER_OPCODE_TXD
:
358 /* There is no sample_d_c message; comparisons are done manually */
359 assert(inst
->mlen
== 7 || inst
->mlen
== 10);
360 msg_type
= BRW_SAMPLER_MESSAGE_SIMD8_SAMPLE_GRADIENTS
;
362 case SHADER_OPCODE_TXF
:
363 assert(inst
->mlen
== 9);
364 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_LD
;
365 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
367 case SHADER_OPCODE_TXS
:
368 assert(inst
->mlen
== 3);
369 msg_type
= BRW_SAMPLER_MESSAGE_SIMD16_RESINFO
;
370 simd_mode
= BRW_SAMPLER_SIMD_MODE_SIMD16
;
373 assert(!"not reached");
377 assert(msg_type
!= -1);
379 if (simd_mode
== BRW_SAMPLER_SIMD_MODE_SIMD16
) {
384 /* Load the message header if present. If there's a texture offset,
385 * we need to set it up explicitly and load the offset bitfield.
386 * Otherwise, we can use an implied move from g0 to the first message reg.
388 if (inst
->texture_offset
) {
389 brw_push_insn_state(p
);
390 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
391 /* Explicitly set up the message header by copying g0 to the MRF. */
392 brw_MOV(p
, retype(brw_message_reg(inst
->base_mrf
), BRW_REGISTER_TYPE_UD
),
393 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
395 /* Then set the offset bits in DWord 2. */
396 brw_MOV(p
, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE
,
397 inst
->base_mrf
, 2), BRW_REGISTER_TYPE_UD
),
398 brw_imm_ud(inst
->texture_offset
));
399 brw_pop_insn_state(p
);
400 } else if (inst
->header_present
) {
401 /* Set up an implied move from g0 to the MRF. */
402 src
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
406 retype(dst
, BRW_REGISTER_TYPE_UW
),
409 SURF_INDEX_TEXTURE(inst
->sampler
),
415 inst
->header_present
,
421 /* For OPCODE_DDX and OPCODE_DDY, per channel of output we've got input
424 * arg0: ss0.tl ss0.tr ss0.bl ss0.br ss1.tl ss1.tr ss1.bl ss1.br
426 * and we're trying to produce:
429 * dst: (ss0.tr - ss0.tl) (ss0.tl - ss0.bl)
430 * (ss0.tr - ss0.tl) (ss0.tr - ss0.br)
431 * (ss0.br - ss0.bl) (ss0.tl - ss0.bl)
432 * (ss0.br - ss0.bl) (ss0.tr - ss0.br)
433 * (ss1.tr - ss1.tl) (ss1.tl - ss1.bl)
434 * (ss1.tr - ss1.tl) (ss1.tr - ss1.br)
435 * (ss1.br - ss1.bl) (ss1.tl - ss1.bl)
436 * (ss1.br - ss1.bl) (ss1.tr - ss1.br)
438 * and add another set of two more subspans if in 16-pixel dispatch mode.
440 * For DDX, it ends up being easy: width = 2, horiz=0 gets us the same result
441 * for each pair, and vertstride = 2 jumps us 2 elements after processing a
442 * pair. But for DDY, it's harder, as we want to produce the pairs swizzled
443 * between each other. We could probably do it like ddx and swizzle the right
444 * order later, but bail for now and just produce
445 * ((ss0.tl - ss0.bl)x4 (ss1.tl - ss1.bl)x4)
448 fs_visitor::generate_ddx(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
)
450 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 1,
452 BRW_VERTICAL_STRIDE_2
,
454 BRW_HORIZONTAL_STRIDE_0
,
455 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
456 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 0,
458 BRW_VERTICAL_STRIDE_2
,
460 BRW_HORIZONTAL_STRIDE_0
,
461 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
462 brw_ADD(p
, dst
, src0
, negate(src1
));
465 /* The negate_value boolean is used to negate the derivative computation for
466 * FBOs, since they place the origin at the upper left instead of the lower
470 fs_visitor::generate_ddy(fs_inst
*inst
, struct brw_reg dst
, struct brw_reg src
,
473 struct brw_reg src0
= brw_reg(src
.file
, src
.nr
, 0,
475 BRW_VERTICAL_STRIDE_4
,
477 BRW_HORIZONTAL_STRIDE_0
,
478 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
479 struct brw_reg src1
= brw_reg(src
.file
, src
.nr
, 2,
481 BRW_VERTICAL_STRIDE_4
,
483 BRW_HORIZONTAL_STRIDE_0
,
484 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
486 brw_ADD(p
, dst
, src1
, negate(src0
));
488 brw_ADD(p
, dst
, src0
, negate(src1
));
492 fs_visitor::generate_discard(fs_inst
*inst
)
494 struct brw_reg f0
= brw_flag_reg();
496 if (intel
->gen
>= 6) {
497 struct brw_reg g1
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
498 struct brw_reg some_register
;
500 /* As of gen6, we no longer have the mask register to look at,
501 * so life gets a bit more complicated.
504 /* Load the flag register with all ones. */
505 brw_push_insn_state(p
);
506 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
507 brw_MOV(p
, f0
, brw_imm_uw(0xffff));
508 brw_pop_insn_state(p
);
510 /* Do a comparison that should always fail, to produce 0s in the flag
511 * reg where we have active channels.
513 some_register
= retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UW
);
514 brw_CMP(p
, retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
),
515 BRW_CONDITIONAL_NZ
, some_register
, some_register
);
517 /* Undo CMP's whacking of predication*/
518 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
520 brw_push_insn_state(p
);
521 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
522 brw_AND(p
, g1
, f0
, g1
);
523 brw_pop_insn_state(p
);
525 struct brw_reg g0
= retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
);
527 brw_push_insn_state(p
);
528 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
529 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
531 /* Unlike the 965, we have the mask reg, so we just need
532 * somewhere to invert that (containing channels to be disabled)
533 * so it can be ANDed with the mask of pixels still to be
534 * written. Use the flag reg for consistency with gen6+.
536 brw_NOT(p
, f0
, brw_mask_reg(1)); /* IMASK */
537 brw_AND(p
, g0
, f0
, g0
);
539 brw_pop_insn_state(p
);
544 fs_visitor::generate_spill(fs_inst
*inst
, struct brw_reg src
)
546 assert(inst
->mlen
!= 0);
549 retype(brw_message_reg(inst
->base_mrf
+ 1), BRW_REGISTER_TYPE_UD
),
550 retype(src
, BRW_REGISTER_TYPE_UD
));
551 brw_oword_block_write_scratch(p
, brw_message_reg(inst
->base_mrf
), 1,
556 fs_visitor::generate_unspill(fs_inst
*inst
, struct brw_reg dst
)
558 assert(inst
->mlen
!= 0);
560 /* Clear any post destination dependencies that would be ignored by
561 * the block read. See the B-Spec for pre-gen5 send instruction.
563 * This could use a better solution, since texture sampling and
564 * math reads could potentially run into it as well -- anywhere
565 * that we have a SEND with a destination that is a register that
566 * was written but not read within the last N instructions (what's
567 * N? unsure). This is rare because of dead code elimination, but
570 if (intel
->gen
== 4 && !intel
->is_g4x
)
571 brw_MOV(p
, brw_null_reg(), dst
);
573 brw_oword_block_read_scratch(p
, dst
, brw_message_reg(inst
->base_mrf
), 1,
576 if (intel
->gen
== 4 && !intel
->is_g4x
) {
577 /* gen4 errata: destination from a send can't be used as a
578 * destination until it's been read. Just read it so we don't
581 brw_MOV(p
, brw_null_reg(), dst
);
586 fs_visitor::generate_pull_constant_load(fs_inst
*inst
, struct brw_reg dst
)
588 assert(inst
->mlen
!= 0);
590 /* Clear any post destination dependencies that would be ignored by
591 * the block read. See the B-Spec for pre-gen5 send instruction.
593 * This could use a better solution, since texture sampling and
594 * math reads could potentially run into it as well -- anywhere
595 * that we have a SEND with a destination that is a register that
596 * was written but not read within the last N instructions (what's
597 * N? unsure). This is rare because of dead code elimination, but
600 if (intel
->gen
== 4 && !intel
->is_g4x
)
601 brw_MOV(p
, brw_null_reg(), dst
);
603 brw_oword_block_read(p
, dst
, brw_message_reg(inst
->base_mrf
),
604 inst
->offset
, SURF_INDEX_FRAG_CONST_BUFFER
);
606 if (intel
->gen
== 4 && !intel
->is_g4x
) {
607 /* gen4 errata: destination from a send can't be used as a
608 * destination until it's been read. Just read it so we don't
611 brw_MOV(p
, brw_null_reg(), dst
);
617 * Cause the current pixel/sample mask (from R1.7 bits 15:0) to be transferred
618 * into the flags register (f0.0).
620 * Used only on Gen6 and above.
623 fs_visitor::generate_mov_dispatch_to_flags()
625 struct brw_reg f0
= brw_flag_reg();
626 struct brw_reg g1
= retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UW
);
628 assert (intel
->gen
>= 6);
629 brw_push_insn_state(p
);
630 brw_set_mask_control(p
, BRW_MASK_DISABLE
);
632 brw_pop_insn_state(p
);
636 static uint32_t brw_file_from_reg(fs_reg
*reg
)
640 return BRW_ARCHITECTURE_REGISTER_FILE
;
642 return BRW_GENERAL_REGISTER_FILE
;
644 return BRW_MESSAGE_REGISTER_FILE
;
646 return BRW_IMMEDIATE_VALUE
;
648 assert(!"not reached");
649 return BRW_GENERAL_REGISTER_FILE
;
653 static struct brw_reg
654 brw_reg_from_fs_reg(fs_reg
*reg
)
656 struct brw_reg brw_reg
;
662 if (reg
->smear
== -1) {
663 brw_reg
= brw_vec8_reg(brw_file_from_reg(reg
), reg
->reg
, 0);
665 brw_reg
= brw_vec1_reg(brw_file_from_reg(reg
), reg
->reg
, reg
->smear
);
667 brw_reg
= retype(brw_reg
, reg
->type
);
669 brw_reg
= sechalf(brw_reg
);
673 case BRW_REGISTER_TYPE_F
:
674 brw_reg
= brw_imm_f(reg
->imm
.f
);
676 case BRW_REGISTER_TYPE_D
:
677 brw_reg
= brw_imm_d(reg
->imm
.i
);
679 case BRW_REGISTER_TYPE_UD
:
680 brw_reg
= brw_imm_ud(reg
->imm
.u
);
683 assert(!"not reached");
684 brw_reg
= brw_null_reg();
689 brw_reg
= reg
->fixed_hw_reg
;
692 /* Probably unused. */
693 brw_reg
= brw_null_reg();
696 assert(!"not reached");
697 brw_reg
= brw_null_reg();
700 assert(!"not reached");
701 brw_reg
= brw_null_reg();
705 brw_reg
= brw_abs(brw_reg
);
707 brw_reg
= negate(brw_reg
);
713 fs_visitor::generate_code()
715 int last_native_inst
= p
->nr_insn
;
716 const char *last_annotation_string
= NULL
;
717 ir_instruction
*last_annotation_ir
= NULL
;
719 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
720 printf("Native code for fragment shader %d (%d-wide dispatch):\n",
721 prog
->Name
, c
->dispatch_width
);
725 if (unlikely(INTEL_DEBUG
& DEBUG_WM
))
726 cfg
= new(mem_ctx
) fs_cfg(this);
728 foreach_list(node
, &this->instructions
) {
729 fs_inst
*inst
= (fs_inst
*)node
;
730 struct brw_reg src
[3], dst
;
732 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
733 foreach_list(node
, &cfg
->block_list
) {
734 fs_bblock_link
*link
= (fs_bblock_link
*)node
;
735 fs_bblock
*block
= link
->block
;
737 if (block
->start
== inst
) {
738 printf(" START B%d", block
->block_num
);
739 foreach_list(predecessor_node
, &block
->parents
) {
740 fs_bblock_link
*predecessor_link
=
741 (fs_bblock_link
*)predecessor_node
;
742 fs_bblock
*predecessor_block
= predecessor_link
->block
;
743 printf(" <-B%d", predecessor_block
->block_num
);
749 if (last_annotation_ir
!= inst
->ir
) {
750 last_annotation_ir
= inst
->ir
;
751 if (last_annotation_ir
) {
753 last_annotation_ir
->print();
757 if (last_annotation_string
!= inst
->annotation
) {
758 last_annotation_string
= inst
->annotation
;
759 if (last_annotation_string
)
760 printf(" %s\n", last_annotation_string
);
764 for (unsigned int i
= 0; i
< 3; i
++) {
765 src
[i
] = brw_reg_from_fs_reg(&inst
->src
[i
]);
767 /* The accumulator result appears to get used for the
768 * conditional modifier generation. When negating a UD
769 * value, there is a 33rd bit generated for the sign in the
770 * accumulator value, so now you can't check, for example,
771 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
773 assert(!inst
->conditional_mod
||
774 inst
->src
[i
].type
!= BRW_REGISTER_TYPE_UD
||
775 !inst
->src
[i
].negate
);
777 dst
= brw_reg_from_fs_reg(&inst
->dst
);
779 brw_set_conditionalmod(p
, inst
->conditional_mod
);
780 brw_set_predicate_control(p
, inst
->predicated
);
781 brw_set_predicate_inverse(p
, inst
->predicate_inverse
);
782 brw_set_saturate(p
, inst
->saturate
);
784 if (inst
->force_uncompressed
|| c
->dispatch_width
== 8) {
785 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
786 } else if (inst
->force_sechalf
) {
787 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
789 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
792 switch (inst
->opcode
) {
794 brw_MOV(p
, dst
, src
[0]);
797 brw_ADD(p
, dst
, src
[0], src
[1]);
800 brw_MUL(p
, dst
, src
[0], src
[1]);
802 case BRW_OPCODE_MACH
:
803 brw_set_acc_write_control(p
, 1);
804 brw_MACH(p
, dst
, src
[0], src
[1]);
805 brw_set_acc_write_control(p
, 0);
809 brw_set_access_mode(p
, BRW_ALIGN_16
);
810 if (c
->dispatch_width
== 16) {
811 brw_set_compression_control(p
, BRW_COMPRESSION_NONE
);
812 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
813 brw_set_compression_control(p
, BRW_COMPRESSION_2NDHALF
);
814 brw_MAD(p
, sechalf(dst
), sechalf(src
[0]), sechalf(src
[1]), sechalf(src
[2]));
815 brw_set_compression_control(p
, BRW_COMPRESSION_COMPRESSED
);
817 brw_MAD(p
, dst
, src
[0], src
[1], src
[2]);
819 brw_set_access_mode(p
, BRW_ALIGN_1
);
823 brw_FRC(p
, dst
, src
[0]);
825 case BRW_OPCODE_RNDD
:
826 brw_RNDD(p
, dst
, src
[0]);
828 case BRW_OPCODE_RNDE
:
829 brw_RNDE(p
, dst
, src
[0]);
831 case BRW_OPCODE_RNDZ
:
832 brw_RNDZ(p
, dst
, src
[0]);
836 brw_AND(p
, dst
, src
[0], src
[1]);
839 brw_OR(p
, dst
, src
[0], src
[1]);
842 brw_XOR(p
, dst
, src
[0], src
[1]);
845 brw_NOT(p
, dst
, src
[0]);
848 brw_ASR(p
, dst
, src
[0], src
[1]);
851 brw_SHR(p
, dst
, src
[0], src
[1]);
854 brw_SHL(p
, dst
, src
[0], src
[1]);
858 brw_CMP(p
, dst
, inst
->conditional_mod
, src
[0], src
[1]);
861 brw_SEL(p
, dst
, src
[0], src
[1]);
865 if (inst
->src
[0].file
!= BAD_FILE
) {
866 /* The instruction has an embedded compare (only allowed on gen6) */
867 assert(intel
->gen
== 6);
868 gen6_IF(p
, inst
->conditional_mod
, src
[0], src
[1]);
870 brw_IF(p
, c
->dispatch_width
== 16 ? BRW_EXECUTE_16
: BRW_EXECUTE_8
);
874 case BRW_OPCODE_ELSE
:
877 case BRW_OPCODE_ENDIF
:
882 brw_DO(p
, BRW_EXECUTE_8
);
885 case BRW_OPCODE_BREAK
:
887 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
889 case BRW_OPCODE_CONTINUE
:
890 /* FINISHME: We need to write the loop instruction support still. */
895 brw_set_predicate_control(p
, BRW_PREDICATE_NONE
);
898 case BRW_OPCODE_WHILE
:
902 case SHADER_OPCODE_RCP
:
903 case SHADER_OPCODE_RSQ
:
904 case SHADER_OPCODE_SQRT
:
905 case SHADER_OPCODE_EXP2
:
906 case SHADER_OPCODE_LOG2
:
907 case SHADER_OPCODE_SIN
:
908 case SHADER_OPCODE_COS
:
909 if (intel
->gen
>= 7) {
910 generate_math1_gen7(inst
, dst
, src
[0]);
911 } else if (intel
->gen
== 6) {
912 generate_math1_gen6(inst
, dst
, src
[0]);
914 generate_math_gen4(inst
, dst
, src
[0]);
917 case SHADER_OPCODE_INT_QUOTIENT
:
918 case SHADER_OPCODE_INT_REMAINDER
:
919 case SHADER_OPCODE_POW
:
920 if (intel
->gen
>= 7) {
921 generate_math2_gen7(inst
, dst
, src
[0], src
[1]);
922 } else if (intel
->gen
== 6) {
923 generate_math2_gen6(inst
, dst
, src
[0], src
[1]);
925 generate_math_gen4(inst
, dst
, src
[0]);
928 case FS_OPCODE_PIXEL_X
:
929 generate_pixel_xy(dst
, true);
931 case FS_OPCODE_PIXEL_Y
:
932 generate_pixel_xy(dst
, false);
934 case FS_OPCODE_CINTERP
:
935 brw_MOV(p
, dst
, src
[0]);
937 case FS_OPCODE_LINTERP
:
938 generate_linterp(inst
, dst
, src
);
940 case SHADER_OPCODE_TEX
:
942 case SHADER_OPCODE_TXD
:
943 case SHADER_OPCODE_TXF
:
944 case SHADER_OPCODE_TXL
:
945 case SHADER_OPCODE_TXS
:
946 generate_tex(inst
, dst
, src
[0]);
948 case FS_OPCODE_DISCARD
:
949 generate_discard(inst
);
952 generate_ddx(inst
, dst
, src
[0]);
955 /* Make sure fp->UsesDFdy flag got set (otherwise there's no
956 * guarantee that c->key.render_to_fbo is set).
958 assert(fp
->UsesDFdy
);
959 generate_ddy(inst
, dst
, src
[0], c
->key
.render_to_fbo
);
962 case FS_OPCODE_SPILL
:
963 generate_spill(inst
, src
[0]);
966 case FS_OPCODE_UNSPILL
:
967 generate_unspill(inst
, dst
);
970 case FS_OPCODE_PULL_CONSTANT_LOAD
:
971 generate_pull_constant_load(inst
, dst
);
974 case FS_OPCODE_FB_WRITE
:
975 generate_fb_write(inst
);
978 case FS_OPCODE_MOV_DISPATCH_TO_FLAGS
:
979 generate_mov_dispatch_to_flags();
983 if (inst
->opcode
< (int)ARRAY_SIZE(brw_opcodes
)) {
984 _mesa_problem(ctx
, "Unsupported opcode `%s' in FS",
985 brw_opcodes
[inst
->opcode
].name
);
987 _mesa_problem(ctx
, "Unsupported opcode %d in FS", inst
->opcode
);
989 fail("unsupported opcode in FS\n");
992 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
993 for (unsigned int i
= last_native_inst
; i
< p
->nr_insn
; i
++) {
995 printf("0x%08x 0x%08x 0x%08x 0x%08x ",
996 ((uint32_t *)&p
->store
[i
])[3],
997 ((uint32_t *)&p
->store
[i
])[2],
998 ((uint32_t *)&p
->store
[i
])[1],
999 ((uint32_t *)&p
->store
[i
])[0]);
1001 brw_disasm(stdout
, &p
->store
[i
], intel
->gen
);
1004 foreach_list(node
, &cfg
->block_list
) {
1005 fs_bblock_link
*link
= (fs_bblock_link
*)node
;
1006 fs_bblock
*block
= link
->block
;
1008 if (block
->end
== inst
) {
1009 printf(" END B%d", block
->block_num
);
1010 foreach_list(successor_node
, &block
->children
) {
1011 fs_bblock_link
*successor_link
=
1012 (fs_bblock_link
*)successor_node
;
1013 fs_bblock
*successor_block
= successor_link
->block
;
1014 printf(" ->B%d", successor_block
->block_num
);
1021 last_native_inst
= p
->nr_insn
;
1024 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1030 /* OK, while the INTEL_DEBUG=wm above is very nice for debugging FS
1031 * emit issues, it doesn't get the jump distances into the output,
1032 * which is often something we want to debug. So this is here in
1033 * case you're doing that.
1036 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
1037 for (unsigned int i
= 0; i
< p
->nr_insn
; i
++) {
1038 printf("0x%08x 0x%08x 0x%08x 0x%08x ",
1039 ((uint32_t *)&p
->store
[i
])[3],
1040 ((uint32_t *)&p
->store
[i
])[2],
1041 ((uint32_t *)&p
->store
[i
])[1],
1042 ((uint32_t *)&p
->store
[i
])[0]);
1043 brw_disasm(stdout
, &p
->store
[i
], intel
->gen
);