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
26 * This file drives the GLSL IR -> LIR translation, contains the
27 * optimizations on the LIR, and drives the generation of native code
31 #include <sys/types.h>
33 #include "util/hash_table.h"
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/fbobject.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "util/register_allocate.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
47 #include "brw_dead_control_flow.h"
48 #include "main/uniforms.h"
49 #include "brw_fs_live_variables.h"
50 #include "glsl/nir/glsl_types.h"
51 #include "program/sampler.h"
56 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
57 const fs_reg
*src
, unsigned sources
)
59 memset(this, 0, sizeof(*this));
61 this->src
= new fs_reg
[MAX2(sources
, 3)];
62 for (unsigned i
= 0; i
< sources
; i
++)
63 this->src
[i
] = src
[i
];
65 this->opcode
= opcode
;
67 this->sources
= sources
;
68 this->exec_size
= exec_size
;
70 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
72 assert(this->exec_size
!= 0);
74 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
76 /* This will be the case for almost all instructions. */
82 this->regs_written
= DIV_ROUND_UP(dst
.component_size(exec_size
),
86 this->regs_written
= 0;
90 unreachable("Invalid destination register file");
92 unreachable("Invalid register file");
95 this->writes_accumulator
= false;
100 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
103 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
105 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
108 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
110 init(opcode
, exec_size
, dst
, NULL
, 0);
113 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
116 const fs_reg src
[1] = { src0
};
117 init(opcode
, exec_size
, dst
, src
, 1);
120 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
121 const fs_reg
&src0
, const fs_reg
&src1
)
123 const fs_reg src
[2] = { src0
, src1
};
124 init(opcode
, exec_size
, dst
, src
, 2);
127 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
128 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
130 const fs_reg src
[3] = { src0
, src1
, src2
};
131 init(opcode
, exec_size
, dst
, src
, 3);
134 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
135 const fs_reg src
[], unsigned sources
)
137 init(opcode
, exec_width
, dst
, src
, sources
);
140 fs_inst::fs_inst(const fs_inst
&that
)
142 memcpy(this, &that
, sizeof(that
));
144 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
146 for (unsigned i
= 0; i
< that
.sources
; i
++)
147 this->src
[i
] = that
.src
[i
];
156 fs_inst::resize_sources(uint8_t num_sources
)
158 if (this->sources
!= num_sources
) {
159 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
161 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
162 src
[i
] = this->src
[i
];
166 this->sources
= num_sources
;
171 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
173 const fs_reg
&surf_index
,
174 const fs_reg
&varying_offset
,
175 uint32_t const_offset
)
177 /* We have our constant surface use a pitch of 4 bytes, so our index can
178 * be any component of a vector, and then we load 4 contiguous
179 * components starting from that.
181 * We break down the const_offset to a portion added to the variable
182 * offset and a portion done using reg_offset, which means that if you
183 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
184 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
185 * CSE can later notice that those loads are all the same and eliminate
186 * the redundant ones.
188 fs_reg vec4_offset
= vgrf(glsl_type::int_type
);
189 bld
.ADD(vec4_offset
, varying_offset
, fs_reg(const_offset
& ~3));
192 if (devinfo
->gen
== 4 && bld
.dispatch_width() == 8) {
193 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
194 * u, v, r) as parameters, or we can just use the SIMD16 message
195 * consisting of (header, u). We choose the second, at the cost of a
196 * longer return length.
202 if (devinfo
->gen
>= 7)
203 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
;
205 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
;
207 int regs_written
= 4 * (bld
.dispatch_width() / 8) * scale
;
208 fs_reg vec4_result
= fs_reg(GRF
, alloc
.allocate(regs_written
), dst
.type
);
209 fs_inst
*inst
= bld
.emit(op
, vec4_result
, surf_index
, vec4_offset
);
210 inst
->regs_written
= regs_written
;
212 if (devinfo
->gen
< 7) {
213 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
);
214 inst
->header_size
= 1;
215 if (devinfo
->gen
== 4)
218 inst
->mlen
= 1 + bld
.dispatch_width() / 8;
221 bld
.MOV(dst
, offset(vec4_result
, bld
, (const_offset
& 3) * scale
));
225 * A helper for MOV generation for fixing up broken hardware SEND dependency
229 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
231 /* The caller always wants uncompressed to emit the minimal extra
232 * dependencies, and to avoid having to deal with aligning its regs to 2.
234 const fs_builder ubld
= bld
.annotate("send dependency resolve")
237 ubld
.MOV(ubld
.null_reg_f(), fs_reg(GRF
, grf
, BRW_REGISTER_TYPE_F
));
241 fs_inst::equals(fs_inst
*inst
) const
243 return (opcode
== inst
->opcode
&&
244 dst
.equals(inst
->dst
) &&
245 src
[0].equals(inst
->src
[0]) &&
246 src
[1].equals(inst
->src
[1]) &&
247 src
[2].equals(inst
->src
[2]) &&
248 saturate
== inst
->saturate
&&
249 predicate
== inst
->predicate
&&
250 conditional_mod
== inst
->conditional_mod
&&
251 mlen
== inst
->mlen
&&
252 base_mrf
== inst
->base_mrf
&&
253 target
== inst
->target
&&
255 header_size
== inst
->header_size
&&
256 shadow_compare
== inst
->shadow_compare
&&
257 exec_size
== inst
->exec_size
&&
258 offset
== inst
->offset
);
262 fs_inst::overwrites_reg(const fs_reg
®
) const
264 return reg
.in_range(dst
, regs_written
);
268 fs_inst::is_send_from_grf() const
271 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
272 case SHADER_OPCODE_SHADER_TIME_ADD
:
273 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
274 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
275 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
276 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
277 case SHADER_OPCODE_UNTYPED_ATOMIC
:
278 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
279 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
280 case SHADER_OPCODE_TYPED_ATOMIC
:
281 case SHADER_OPCODE_TYPED_SURFACE_READ
:
282 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
283 case SHADER_OPCODE_URB_WRITE_SIMD8
:
285 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
286 return src
[1].file
== GRF
;
287 case FS_OPCODE_FB_WRITE
:
288 return src
[0].file
== GRF
;
291 return src
[0].file
== GRF
;
298 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
300 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
303 fs_reg reg
= this->src
[0];
304 if (reg
.file
!= GRF
|| reg
.reg_offset
!= 0 || reg
.stride
== 0)
307 if (grf_alloc
.sizes
[reg
.reg
] != this->regs_written
)
310 for (int i
= 0; i
< this->sources
; i
++) {
311 reg
.type
= this->src
[i
].type
;
312 if (!this->src
[i
].equals(reg
))
315 if (i
< this->header_size
) {
318 reg
.reg_offset
+= this->exec_size
/ 8;
326 fs_inst::can_do_source_mods(const struct brw_device_info
*devinfo
)
328 if (devinfo
->gen
== 6 && is_math())
331 if (is_send_from_grf())
334 if (!backend_instruction::can_do_source_mods())
341 fs_inst::has_side_effects() const
343 return this->eot
|| backend_instruction::has_side_effects();
349 memset(this, 0, sizeof(*this));
353 /** Generic unset register constructor. */
357 this->file
= BAD_FILE
;
360 /** Immediate value constructor. */
361 fs_reg::fs_reg(float f
)
365 this->type
= BRW_REGISTER_TYPE_F
;
367 this->fixed_hw_reg
.dw1
.f
= f
;
370 /** Immediate value constructor. */
371 fs_reg::fs_reg(int32_t i
)
375 this->type
= BRW_REGISTER_TYPE_D
;
377 this->fixed_hw_reg
.dw1
.d
= i
;
380 /** Immediate value constructor. */
381 fs_reg::fs_reg(uint32_t u
)
385 this->type
= BRW_REGISTER_TYPE_UD
;
387 this->fixed_hw_reg
.dw1
.ud
= u
;
390 /** Vector float immediate value constructor. */
391 fs_reg::fs_reg(uint8_t vf
[4])
395 this->type
= BRW_REGISTER_TYPE_VF
;
396 memcpy(&this->fixed_hw_reg
.dw1
.ud
, vf
, sizeof(unsigned));
399 /** Vector float immediate value constructor. */
400 fs_reg::fs_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
)
404 this->type
= BRW_REGISTER_TYPE_VF
;
405 this->fixed_hw_reg
.dw1
.ud
= (vf0
<< 0) |
411 /** Fixed brw_reg. */
412 fs_reg::fs_reg(struct brw_reg fixed_hw_reg
)
416 this->fixed_hw_reg
= fixed_hw_reg
;
417 this->type
= fixed_hw_reg
.type
;
421 fs_reg::equals(const fs_reg
&r
) const
423 return (file
== r
.file
&&
425 reg_offset
== r
.reg_offset
&&
426 subreg_offset
== r
.subreg_offset
&&
428 negate
== r
.negate
&&
430 !reladdr
&& !r
.reladdr
&&
431 ((file
!= HW_REG
&& file
!= IMM
) ||
432 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
,
433 sizeof(fixed_hw_reg
)) == 0) &&
438 fs_reg::set_smear(unsigned subreg
)
440 assert(file
!= HW_REG
&& file
!= IMM
);
441 subreg_offset
= subreg
* type_sz(type
);
447 fs_reg::is_contiguous() const
453 fs_reg::component_size(unsigned width
) const
455 const unsigned stride
= (file
!= HW_REG
? this->stride
:
456 fixed_hw_reg
.hstride
== 0 ? 0 :
457 1 << (fixed_hw_reg
.hstride
- 1));
458 return MAX2(width
* stride
, 1) * type_sz(type
);
462 type_size_scalar(const struct glsl_type
*type
)
464 unsigned int size
, i
;
466 switch (type
->base_type
) {
469 case GLSL_TYPE_FLOAT
:
471 return type
->components();
472 case GLSL_TYPE_ARRAY
:
473 return type_size_scalar(type
->fields
.array
) * type
->length
;
474 case GLSL_TYPE_STRUCT
:
476 for (i
= 0; i
< type
->length
; i
++) {
477 size
+= type_size_scalar(type
->fields
.structure
[i
].type
);
480 case GLSL_TYPE_SAMPLER
:
481 /* Samplers take up no register space, since they're baked in at
485 case GLSL_TYPE_ATOMIC_UINT
:
487 case GLSL_TYPE_SUBROUTINE
:
489 case GLSL_TYPE_IMAGE
:
490 return BRW_IMAGE_PARAM_SIZE
;
492 case GLSL_TYPE_ERROR
:
493 case GLSL_TYPE_INTERFACE
:
494 case GLSL_TYPE_DOUBLE
:
495 unreachable("not reached");
502 * Create a MOV to read the timestamp register.
504 * The caller is responsible for emitting the MOV. The return value is
505 * the destination of the MOV, with extra parameters set.
508 fs_visitor::get_timestamp(const fs_builder
&bld
)
510 assert(devinfo
->gen
>= 7);
512 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
515 BRW_REGISTER_TYPE_UD
));
517 fs_reg dst
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
519 /* We want to read the 3 fields we care about even if it's not enabled in
522 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
524 /* The caller wants the low 32 bits of the timestamp. Since it's running
525 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
526 * which is plenty of time for our purposes. It is identical across the
527 * EUs, but since it's tracking GPU core speed it will increment at a
528 * varying rate as render P-states change.
530 * The caller could also check if render P-states have changed (or anything
531 * else that might disrupt timing) by setting smear to 2 and checking if
532 * that field is != 0.
540 fs_visitor::emit_shader_time_begin()
542 shader_start_time
= get_timestamp(bld
.annotate("shader time start"));
546 fs_visitor::emit_shader_time_end()
548 /* Insert our code just before the final SEND with EOT. */
549 exec_node
*end
= this->instructions
.get_tail();
550 assert(end
&& ((fs_inst
*) end
)->eot
);
551 const fs_builder ibld
= bld
.annotate("shader time end")
552 .exec_all().at(NULL
, end
);
554 fs_reg shader_end_time
= get_timestamp(ibld
);
556 /* Check that there weren't any timestamp reset events (assuming these
557 * were the only two timestamp reads that happened).
559 fs_reg reset
= shader_end_time
;
561 set_condmod(BRW_CONDITIONAL_Z
,
562 ibld
.AND(ibld
.null_reg_ud(), reset
, fs_reg(1u)));
563 ibld
.IF(BRW_PREDICATE_NORMAL
);
565 fs_reg start
= shader_start_time
;
567 fs_reg diff
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
570 const fs_builder cbld
= ibld
.group(1, 0);
571 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
573 /* If there were no instructions between the two timestamp gets, the diff
574 * is 2 cycles. Remove that overhead, so I can forget about that when
575 * trying to determine the time taken for single instructions.
577 cbld
.ADD(diff
, diff
, fs_reg(-2u));
578 SHADER_TIME_ADD(cbld
, 0, diff
);
579 SHADER_TIME_ADD(cbld
, 1, fs_reg(1u));
580 ibld
.emit(BRW_OPCODE_ELSE
);
581 SHADER_TIME_ADD(cbld
, 2, fs_reg(1u));
582 ibld
.emit(BRW_OPCODE_ENDIF
);
586 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
587 int shader_time_subindex
,
590 int index
= shader_time_index
* 3 + shader_time_subindex
;
591 fs_reg offset
= fs_reg(index
* SHADER_TIME_STRIDE
);
594 if (dispatch_width
== 8)
595 payload
= vgrf(glsl_type::uvec2_type
);
597 payload
= vgrf(glsl_type::uint_type
);
599 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
603 fs_visitor::vfail(const char *format
, va_list va
)
612 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
613 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
615 this->fail_msg
= msg
;
618 fprintf(stderr
, "%s", msg
);
623 fs_visitor::fail(const char *format
, ...)
627 va_start(va
, format
);
633 * Mark this program as impossible to compile in SIMD16 mode.
635 * During the SIMD8 compile (which happens first), we can detect and flag
636 * things that are unsupported in SIMD16 mode, so the compiler can skip
637 * the SIMD16 compile altogether.
639 * During a SIMD16 compile (if one happens anyway), this just calls fail().
642 fs_visitor::no16(const char *msg
)
644 if (dispatch_width
== 16) {
647 simd16_unsupported
= true;
649 compiler
->shader_perf_log(log_data
,
650 "SIMD16 shader failed to compile: %s", msg
);
655 * Returns true if the instruction has a flag that means it won't
656 * update an entire destination register.
658 * For example, dead code elimination and live variable analysis want to know
659 * when a write to a variable screens off any preceding values that were in
663 fs_inst::is_partial_write() const
665 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
666 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
667 !this->dst
.is_contiguous());
671 fs_inst::components_read(unsigned i
) const
674 case FS_OPCODE_LINTERP
:
680 case FS_OPCODE_PIXEL_X
:
681 case FS_OPCODE_PIXEL_Y
:
685 case FS_OPCODE_FB_WRITE_LOGICAL
:
686 assert(src
[6].file
== IMM
);
687 /* First/second FB write color. */
689 return src
[6].fixed_hw_reg
.dw1
.ud
;
693 case SHADER_OPCODE_TEX_LOGICAL
:
694 case SHADER_OPCODE_TXD_LOGICAL
:
695 case SHADER_OPCODE_TXF_LOGICAL
:
696 case SHADER_OPCODE_TXL_LOGICAL
:
697 case SHADER_OPCODE_TXS_LOGICAL
:
698 case FS_OPCODE_TXB_LOGICAL
:
699 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
700 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
701 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
702 case SHADER_OPCODE_LOD_LOGICAL
:
703 case SHADER_OPCODE_TG4_LOGICAL
:
704 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
705 assert(src
[8].file
== IMM
&& src
[9].file
== IMM
);
706 /* Texture coordinates. */
708 return src
[8].fixed_hw_reg
.dw1
.ud
;
709 /* Texture derivatives. */
710 else if ((i
== 2 || i
== 3) && opcode
== SHADER_OPCODE_TXD_LOGICAL
)
711 return src
[9].fixed_hw_reg
.dw1
.ud
;
712 /* Texture offset. */
718 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
719 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
720 assert(src
[3].file
== IMM
);
721 /* Surface coordinates. */
723 return src
[3].fixed_hw_reg
.dw1
.ud
;
724 /* Surface operation source (ignored for reads). */
730 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
731 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
732 assert(src
[3].file
== IMM
&&
734 /* Surface coordinates. */
736 return src
[3].fixed_hw_reg
.dw1
.ud
;
737 /* Surface operation source. */
739 return src
[4].fixed_hw_reg
.dw1
.ud
;
743 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
744 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
745 assert(src
[3].file
== IMM
&&
747 const unsigned op
= src
[4].fixed_hw_reg
.dw1
.ud
;
748 /* Surface coordinates. */
750 return src
[3].fixed_hw_reg
.dw1
.ud
;
751 /* Surface operation source. */
752 else if (i
== 1 && op
== BRW_AOP_CMPWR
)
754 else if (i
== 1 && (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
||
755 op
== BRW_AOP_PREDEC
))
767 fs_inst::regs_read(int arg
) const
770 case FS_OPCODE_FB_WRITE
:
771 case SHADER_OPCODE_URB_WRITE_SIMD8
:
772 case SHADER_OPCODE_UNTYPED_ATOMIC
:
773 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
774 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
775 case SHADER_OPCODE_TYPED_ATOMIC
:
776 case SHADER_OPCODE_TYPED_SURFACE_READ
:
777 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
778 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
783 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
784 /* The payload is actually stored in src1 */
789 case FS_OPCODE_LINTERP
:
794 case SHADER_OPCODE_LOAD_PAYLOAD
:
795 if (arg
< this->header_size
)
799 case CS_OPCODE_CS_TERMINATE
:
800 case SHADER_OPCODE_BARRIER
:
804 if (is_tex() && arg
== 0 && src
[0].file
== GRF
)
809 switch (src
[arg
].file
) {
818 return DIV_ROUND_UP(components_read(arg
) *
819 src
[arg
].component_size(exec_size
),
822 unreachable("MRF registers are not allowed as sources");
824 unreachable("Invalid register file");
829 fs_inst::reads_flag() const
835 fs_inst::writes_flag() const
837 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
838 opcode
!= BRW_OPCODE_IF
&&
839 opcode
!= BRW_OPCODE_WHILE
)) ||
840 opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
;
844 * Returns how many MRFs an FS opcode will write over.
846 * Note that this is not the 0 or 1 implied writes in an actual gen
847 * instruction -- the FS opcodes often generate MOVs in addition.
850 fs_visitor::implied_mrf_writes(fs_inst
*inst
)
855 if (inst
->base_mrf
== -1)
858 switch (inst
->opcode
) {
859 case SHADER_OPCODE_RCP
:
860 case SHADER_OPCODE_RSQ
:
861 case SHADER_OPCODE_SQRT
:
862 case SHADER_OPCODE_EXP2
:
863 case SHADER_OPCODE_LOG2
:
864 case SHADER_OPCODE_SIN
:
865 case SHADER_OPCODE_COS
:
866 return 1 * dispatch_width
/ 8;
867 case SHADER_OPCODE_POW
:
868 case SHADER_OPCODE_INT_QUOTIENT
:
869 case SHADER_OPCODE_INT_REMAINDER
:
870 return 2 * dispatch_width
/ 8;
871 case SHADER_OPCODE_TEX
:
873 case SHADER_OPCODE_TXD
:
874 case SHADER_OPCODE_TXF
:
875 case SHADER_OPCODE_TXF_CMS
:
876 case SHADER_OPCODE_TXF_MCS
:
877 case SHADER_OPCODE_TG4
:
878 case SHADER_OPCODE_TG4_OFFSET
:
879 case SHADER_OPCODE_TXL
:
880 case SHADER_OPCODE_TXS
:
881 case SHADER_OPCODE_LOD
:
882 case SHADER_OPCODE_SAMPLEINFO
:
884 case FS_OPCODE_FB_WRITE
:
886 case FS_OPCODE_GET_BUFFER_SIZE
:
887 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
888 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
890 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
892 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
894 case SHADER_OPCODE_UNTYPED_ATOMIC
:
895 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
896 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
897 case SHADER_OPCODE_TYPED_ATOMIC
:
898 case SHADER_OPCODE_TYPED_SURFACE_READ
:
899 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
900 case SHADER_OPCODE_URB_WRITE_SIMD8
:
901 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
902 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
903 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
904 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
907 unreachable("not reached");
912 fs_visitor::vgrf(const glsl_type
*const type
)
914 int reg_width
= dispatch_width
/ 8;
915 return fs_reg(GRF
, alloc
.allocate(type_size_scalar(type
) * reg_width
),
916 brw_type_for_base_type(type
));
919 /** Fixed HW reg constructor. */
920 fs_reg::fs_reg(enum register_file file
, int reg
)
925 this->type
= BRW_REGISTER_TYPE_F
;
926 this->stride
= (file
== UNIFORM
? 0 : 1);
929 /** Fixed HW reg constructor. */
930 fs_reg::fs_reg(enum register_file file
, int reg
, enum brw_reg_type type
)
936 this->stride
= (file
== UNIFORM
? 0 : 1);
939 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
940 * This brings in those uniform definitions
943 fs_visitor::import_uniforms(fs_visitor
*v
)
945 this->push_constant_loc
= v
->push_constant_loc
;
946 this->pull_constant_loc
= v
->pull_constant_loc
;
947 this->uniforms
= v
->uniforms
;
948 this->param_size
= v
->param_size
;
952 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer
,
953 bool origin_upper_left
)
955 assert(stage
== MESA_SHADER_FRAGMENT
);
956 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
957 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec4_type
));
959 bool flip
= !origin_upper_left
^ key
->render_to_fbo
;
962 if (pixel_center_integer
) {
963 bld
.MOV(wpos
, this->pixel_x
);
965 bld
.ADD(wpos
, this->pixel_x
, fs_reg(0.5f
));
967 wpos
= offset(wpos
, bld
, 1);
970 if (!flip
&& pixel_center_integer
) {
971 bld
.MOV(wpos
, this->pixel_y
);
973 fs_reg pixel_y
= this->pixel_y
;
974 float offset
= (pixel_center_integer
? 0.0f
: 0.5f
);
977 pixel_y
.negate
= true;
978 offset
+= key
->drawable_height
- 1.0f
;
981 bld
.ADD(wpos
, pixel_y
, fs_reg(offset
));
983 wpos
= offset(wpos
, bld
, 1);
986 if (devinfo
->gen
>= 6) {
987 bld
.MOV(wpos
, fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0)));
989 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
990 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
991 interp_reg(VARYING_SLOT_POS
, 2));
993 wpos
= offset(wpos
, bld
, 1);
995 /* gl_FragCoord.w: Already set up in emit_interpolation */
996 bld
.MOV(wpos
, this->wpos_w
);
1002 fs_visitor::emit_linterp(const fs_reg
&attr
, const fs_reg
&interp
,
1003 glsl_interp_qualifier interpolation_mode
,
1004 bool is_centroid
, bool is_sample
)
1006 brw_wm_barycentric_interp_mode barycoord_mode
;
1007 if (devinfo
->gen
>= 6) {
1009 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1010 barycoord_mode
= BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
1012 barycoord_mode
= BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
1013 } else if (is_sample
) {
1014 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1015 barycoord_mode
= BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
1017 barycoord_mode
= BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
1019 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1020 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1022 barycoord_mode
= BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
1025 /* On Ironlake and below, there is only one interpolation mode.
1026 * Centroid interpolation doesn't mean anything on this hardware --
1027 * there is no multisampling.
1029 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1031 return bld
.emit(FS_OPCODE_LINTERP
, attr
,
1032 this->delta_xy
[barycoord_mode
], interp
);
1036 fs_visitor::emit_general_interpolation(fs_reg attr
, const char *name
,
1037 const glsl_type
*type
,
1038 glsl_interp_qualifier interpolation_mode
,
1039 int location
, bool mod_centroid
,
1042 attr
.type
= brw_type_for_base_type(type
->get_scalar_type());
1044 assert(stage
== MESA_SHADER_FRAGMENT
);
1045 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1046 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1048 unsigned int array_elements
;
1050 if (type
->is_array()) {
1051 array_elements
= type
->arrays_of_arrays_size();
1052 if (array_elements
== 0) {
1053 fail("dereferenced array '%s' has length 0\n", name
);
1055 type
= type
->without_array();
1060 if (interpolation_mode
== INTERP_QUALIFIER_NONE
) {
1062 location
== VARYING_SLOT_COL0
|| location
== VARYING_SLOT_COL1
;
1063 if (key
->flat_shade
&& is_gl_Color
) {
1064 interpolation_mode
= INTERP_QUALIFIER_FLAT
;
1066 interpolation_mode
= INTERP_QUALIFIER_SMOOTH
;
1070 for (unsigned int i
= 0; i
< array_elements
; i
++) {
1071 for (unsigned int j
= 0; j
< type
->matrix_columns
; j
++) {
1072 if (prog_data
->urb_setup
[location
] == -1) {
1073 /* If there's no incoming setup data for this slot, don't
1074 * emit interpolation for it.
1076 attr
= offset(attr
, bld
, type
->vector_elements
);
1081 if (interpolation_mode
== INTERP_QUALIFIER_FLAT
) {
1082 /* Constant interpolation (flat shading) case. The SF has
1083 * handed us defined values in only the constant offset
1084 * field of the setup reg.
1086 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1087 struct brw_reg interp
= interp_reg(location
, k
);
1088 interp
= suboffset(interp
, 3);
1089 interp
.type
= attr
.type
;
1090 bld
.emit(FS_OPCODE_CINTERP
, attr
, fs_reg(interp
));
1091 attr
= offset(attr
, bld
, 1);
1094 /* Smooth/noperspective interpolation case. */
1095 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1096 struct brw_reg interp
= interp_reg(location
, k
);
1097 if (devinfo
->needs_unlit_centroid_workaround
&& mod_centroid
) {
1098 /* Get the pixel/sample mask into f0 so that we know
1099 * which pixels are lit. Then, for each channel that is
1100 * unlit, replace the centroid data with non-centroid
1103 bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
1106 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1108 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1109 inst
->predicate_inverse
= true;
1110 if (devinfo
->has_pln
)
1111 inst
->no_dd_clear
= true;
1113 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1114 mod_centroid
&& !key
->persample_shading
,
1115 mod_sample
|| key
->persample_shading
);
1116 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1117 inst
->predicate_inverse
= false;
1118 if (devinfo
->has_pln
)
1119 inst
->no_dd_check
= true;
1122 emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1123 mod_centroid
&& !key
->persample_shading
,
1124 mod_sample
|| key
->persample_shading
);
1126 if (devinfo
->gen
< 6 && interpolation_mode
== INTERP_QUALIFIER_SMOOTH
) {
1127 bld
.MUL(attr
, attr
, this->pixel_w
);
1129 attr
= offset(attr
, bld
, 1);
1139 fs_visitor::emit_frontfacing_interpolation()
1141 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1143 if (devinfo
->gen
>= 6) {
1144 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1145 * a boolean result from this (~0/true or 0/false).
1147 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1148 * this task in only one instruction:
1149 * - a negation source modifier will flip the bit; and
1150 * - a W -> D type conversion will sign extend the bit into the high
1151 * word of the destination.
1153 * An ASR 15 fills the low word of the destination.
1155 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1158 bld
.ASR(*reg
, g0
, fs_reg(15));
1160 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1161 * a boolean result from this (1/true or 0/false).
1163 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1164 * the negation source modifier to flip it. Unfortunately the SHR
1165 * instruction only operates on UD (or D with an abs source modifier)
1166 * sources without negation.
1168 * Instead, use ASR (which will give ~0/true or 0/false).
1170 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1173 bld
.ASR(*reg
, g1_6
, fs_reg(31));
1180 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1182 assert(stage
== MESA_SHADER_FRAGMENT
);
1183 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1184 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1186 if (key
->compute_pos_offset
) {
1187 /* Convert int_sample_pos to floating point */
1188 bld
.MOV(dst
, int_sample_pos
);
1189 /* Scale to the range [0, 1] */
1190 bld
.MUL(dst
, dst
, fs_reg(1 / 16.0f
));
1193 /* From ARB_sample_shading specification:
1194 * "When rendering to a non-multisample buffer, or if multisample
1195 * rasterization is disabled, gl_SamplePosition will always be
1198 bld
.MOV(dst
, fs_reg(0.5f
));
1203 fs_visitor::emit_samplepos_setup()
1205 assert(devinfo
->gen
>= 6);
1207 const fs_builder abld
= bld
.annotate("compute sample position");
1208 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1210 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1211 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1213 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1214 * mode will be enabled.
1216 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1217 * R31.1:0 Position Offset X/Y for Slot[3:0]
1218 * R31.3:2 Position Offset X/Y for Slot[7:4]
1221 * The X, Y sample positions come in as bytes in thread payload. So, read
1222 * the positions using vstride=16, width=8, hstride=2.
1224 struct brw_reg sample_pos_reg
=
1225 stride(retype(brw_vec1_grf(payload
.sample_pos_reg
, 0),
1226 BRW_REGISTER_TYPE_B
), 16, 8, 2);
1228 if (dispatch_width
== 8) {
1229 abld
.MOV(int_sample_x
, fs_reg(sample_pos_reg
));
1231 abld
.half(0).MOV(half(int_sample_x
, 0), fs_reg(sample_pos_reg
));
1232 abld
.half(1).MOV(half(int_sample_x
, 1),
1233 fs_reg(suboffset(sample_pos_reg
, 16)));
1235 /* Compute gl_SamplePosition.x */
1236 compute_sample_position(pos
, int_sample_x
);
1237 pos
= offset(pos
, abld
, 1);
1238 if (dispatch_width
== 8) {
1239 abld
.MOV(int_sample_y
, fs_reg(suboffset(sample_pos_reg
, 1)));
1241 abld
.half(0).MOV(half(int_sample_y
, 0),
1242 fs_reg(suboffset(sample_pos_reg
, 1)));
1243 abld
.half(1).MOV(half(int_sample_y
, 1),
1244 fs_reg(suboffset(sample_pos_reg
, 17)));
1246 /* Compute gl_SamplePosition.y */
1247 compute_sample_position(pos
, int_sample_y
);
1252 fs_visitor::emit_sampleid_setup()
1254 assert(stage
== MESA_SHADER_FRAGMENT
);
1255 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1256 assert(devinfo
->gen
>= 6);
1258 const fs_builder abld
= bld
.annotate("compute sample id");
1259 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1261 if (key
->compute_sample_id
) {
1262 fs_reg t1
= vgrf(glsl_type::int_type
);
1263 fs_reg t2
= vgrf(glsl_type::int_type
);
1264 t2
.type
= BRW_REGISTER_TYPE_UW
;
1266 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1267 * 8x multisampling, subspan 0 will represent sample N (where N
1268 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1269 * 7. We can find the value of N by looking at R0.0 bits 7:6
1270 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1271 * (since samples are always delivered in pairs). That is, we
1272 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1273 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1274 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1275 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1276 * populating a temporary variable with the sequence (0, 1, 2, 3),
1277 * and then reading from it using vstride=1, width=4, hstride=0.
1278 * These computations hold good for 4x multisampling as well.
1280 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1281 * the first four slots are sample 0 of subspan 0; the next four
1282 * are sample 1 of subspan 0; the third group is sample 0 of
1283 * subspan 1, and finally sample 1 of subspan 1.
1286 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
1288 abld
.exec_all().SHR(t1
, t1
, fs_reg(5));
1290 /* This works for both SIMD8 and SIMD16 */
1292 .MOV(t2
, brw_imm_v(key
->persample_2x
? 0x1010 : 0x3210));
1294 /* This special instruction takes care of setting vstride=1,
1295 * width=4, hstride=0 of t2 during an ADD instruction.
1297 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1299 /* As per GL_ARB_sample_shading specification:
1300 * "When rendering to a non-multisample buffer, or if multisample
1301 * rasterization is disabled, gl_SampleID will always be zero."
1303 abld
.MOV(*reg
, fs_reg(0));
1310 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1312 if (!src
.abs
&& !src
.negate
)
1315 fs_reg temp
= bld
.vgrf(src
.type
);
1322 fs_visitor::emit_discard_jump()
1324 assert(((brw_wm_prog_data
*) this->prog_data
)->uses_kill
);
1326 /* For performance, after a discard, jump to the end of the
1327 * shader if all relevant channels have been discarded.
1329 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1330 discard_jump
->flag_subreg
= 1;
1332 discard_jump
->predicate
= (dispatch_width
== 8)
1333 ? BRW_PREDICATE_ALIGN1_ANY8H
1334 : BRW_PREDICATE_ALIGN1_ANY16H
;
1335 discard_jump
->predicate_inverse
= true;
1339 fs_visitor::assign_curb_setup()
1341 if (dispatch_width
== 8) {
1342 prog_data
->dispatch_grf_start_reg
= payload
.num_regs
;
1344 if (stage
== MESA_SHADER_FRAGMENT
) {
1345 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1346 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1347 } else if (stage
== MESA_SHADER_COMPUTE
) {
1348 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
1349 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1351 unreachable("Unsupported shader type!");
1355 prog_data
->curb_read_length
= ALIGN(stage_prog_data
->nr_params
, 8) / 8;
1357 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1358 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1359 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1360 if (inst
->src
[i
].file
== UNIFORM
) {
1361 int uniform_nr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1363 if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1364 constant_nr
= push_constant_loc
[uniform_nr
];
1366 /* Section 5.11 of the OpenGL 4.1 spec says:
1367 * "Out-of-bounds reads return undefined values, which include
1368 * values from other variables of the active program or zero."
1369 * Just return the first push constant.
1374 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1378 assert(inst
->src
[i
].stride
== 0);
1379 inst
->src
[i
].file
= HW_REG
;
1380 inst
->src
[i
].fixed_hw_reg
= byte_offset(
1381 retype(brw_reg
, inst
->src
[i
].type
),
1382 inst
->src
[i
].subreg_offset
);
1387 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1388 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1392 fs_visitor::calculate_urb_setup()
1394 assert(stage
== MESA_SHADER_FRAGMENT
);
1395 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1396 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1398 memset(prog_data
->urb_setup
, -1,
1399 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1402 /* Figure out where each of the incoming setup attributes lands. */
1403 if (devinfo
->gen
>= 6) {
1404 if (_mesa_bitcount_64(nir
->info
.inputs_read
&
1405 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1406 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1407 * first 16 varying inputs, so we can put them wherever we want.
1408 * Just put them in order.
1410 * This is useful because it means that (a) inputs not used by the
1411 * fragment shader won't take up valuable register space, and (b) we
1412 * won't have to recompile the fragment shader if it gets paired with
1413 * a different vertex (or geometry) shader.
1415 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1416 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1417 BITFIELD64_BIT(i
)) {
1418 prog_data
->urb_setup
[i
] = urb_next
++;
1422 /* We have enough input varyings that the SF/SBE pipeline stage can't
1423 * arbitrarily rearrange them to suit our whim; we have to put them
1424 * in an order that matches the output of the previous pipeline stage
1425 * (geometry or vertex shader).
1427 struct brw_vue_map prev_stage_vue_map
;
1428 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1429 key
->input_slots_valid
,
1430 nir
->info
.separate_shader
);
1431 int first_slot
= 2 * BRW_SF_URB_ENTRY_READ_OFFSET
;
1432 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1433 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1435 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1436 /* Note that varying == BRW_VARYING_SLOT_COUNT when a slot is
1439 if (varying
!= BRW_VARYING_SLOT_COUNT
&&
1440 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1441 BITFIELD64_BIT(varying
))) {
1442 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1445 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1448 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1449 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1450 /* Point size is packed into the header, not as a general attribute */
1451 if (i
== VARYING_SLOT_PSIZ
)
1454 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1455 /* The back color slot is skipped when the front color is
1456 * also written to. In addition, some slots can be
1457 * written in the vertex shader and not read in the
1458 * fragment shader. So the register number must always be
1459 * incremented, mapped or not.
1461 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1462 prog_data
->urb_setup
[i
] = urb_next
;
1468 * It's a FS only attribute, and we did interpolation for this attribute
1469 * in SF thread. So, count it here, too.
1471 * See compile_sf_prog() for more info.
1473 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1474 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1477 prog_data
->num_varying_inputs
= urb_next
;
1481 fs_visitor::assign_urb_setup()
1483 assert(stage
== MESA_SHADER_FRAGMENT
);
1484 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1486 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1488 /* Offset all the urb_setup[] index by the actual position of the
1489 * setup regs, now that the location of the constants has been chosen.
1491 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1492 if (inst
->opcode
== FS_OPCODE_LINTERP
) {
1493 assert(inst
->src
[1].file
== HW_REG
);
1494 inst
->src
[1].fixed_hw_reg
.nr
+= urb_start
;
1497 if (inst
->opcode
== FS_OPCODE_CINTERP
) {
1498 assert(inst
->src
[0].file
== HW_REG
);
1499 inst
->src
[0].fixed_hw_reg
.nr
+= urb_start
;
1503 /* Each attribute is 4 setup channels, each of which is half a reg. */
1504 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1508 fs_visitor::assign_vs_urb_setup()
1510 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
1512 assert(stage
== MESA_SHADER_VERTEX
);
1513 int count
= _mesa_bitcount_64(vs_prog_data
->inputs_read
);
1514 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
)
1517 /* Each attribute is 4 regs. */
1518 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attributes
;
1520 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1522 /* Rewrite all ATTR file references to the hw grf that they land in. */
1523 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1524 for (int i
= 0; i
< inst
->sources
; i
++) {
1525 if (inst
->src
[i
].file
== ATTR
) {
1526 int grf
= payload
.num_regs
+
1527 prog_data
->curb_read_length
+
1529 inst
->src
[i
].reg_offset
;
1531 inst
->src
[i
].file
= HW_REG
;
1532 inst
->src
[i
].fixed_hw_reg
=
1533 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1534 inst
->src
[i
].subreg_offset
),
1535 inst
->exec_size
* inst
->src
[i
].stride
,
1536 inst
->exec_size
, inst
->src
[i
].stride
);
1543 * Split large virtual GRFs into separate components if we can.
1545 * This is mostly duplicated with what brw_fs_vector_splitting does,
1546 * but that's really conservative because it's afraid of doing
1547 * splitting that doesn't result in real progress after the rest of
1548 * the optimization phases, which would cause infinite looping in
1549 * optimization. We can do it once here, safely. This also has the
1550 * opportunity to split interpolated values, or maybe even uniforms,
1551 * which we don't have at the IR level.
1553 * We want to split, because virtual GRFs are what we register
1554 * allocate and spill (due to contiguousness requirements for some
1555 * instructions), and they're what we naturally generate in the
1556 * codegen process, but most virtual GRFs don't actually need to be
1557 * contiguous sets of GRFs. If we split, we'll end up with reduced
1558 * live intervals and better dead code elimination and coalescing.
1561 fs_visitor::split_virtual_grfs()
1563 int num_vars
= this->alloc
.count
;
1565 /* Count the total number of registers */
1567 int vgrf_to_reg
[num_vars
];
1568 for (int i
= 0; i
< num_vars
; i
++) {
1569 vgrf_to_reg
[i
] = reg_count
;
1570 reg_count
+= alloc
.sizes
[i
];
1573 /* An array of "split points". For each register slot, this indicates
1574 * if this slot can be separated from the previous slot. Every time an
1575 * instruction uses multiple elements of a register (as a source or
1576 * destination), we mark the used slots as inseparable. Then we go
1577 * through and split the registers into the smallest pieces we can.
1579 bool split_points
[reg_count
];
1580 memset(split_points
, 0, sizeof(split_points
));
1582 /* Mark all used registers as fully splittable */
1583 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1584 if (inst
->dst
.file
== GRF
) {
1585 int reg
= vgrf_to_reg
[inst
->dst
.reg
];
1586 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.reg
]; j
++)
1587 split_points
[reg
+ j
] = true;
1590 for (int i
= 0; i
< inst
->sources
; i
++) {
1591 if (inst
->src
[i
].file
== GRF
) {
1592 int reg
= vgrf_to_reg
[inst
->src
[i
].reg
];
1593 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].reg
]; j
++)
1594 split_points
[reg
+ j
] = true;
1599 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1600 if (inst
->dst
.file
== GRF
) {
1601 int reg
= vgrf_to_reg
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
1602 for (int j
= 1; j
< inst
->regs_written
; j
++)
1603 split_points
[reg
+ j
] = false;
1605 for (int i
= 0; i
< inst
->sources
; i
++) {
1606 if (inst
->src
[i
].file
== GRF
) {
1607 int reg
= vgrf_to_reg
[inst
->src
[i
].reg
] + inst
->src
[i
].reg_offset
;
1608 for (int j
= 1; j
< inst
->regs_read(i
); j
++)
1609 split_points
[reg
+ j
] = false;
1614 int new_virtual_grf
[reg_count
];
1615 int new_reg_offset
[reg_count
];
1618 for (int i
= 0; i
< num_vars
; i
++) {
1619 /* The first one should always be 0 as a quick sanity check. */
1620 assert(split_points
[reg
] == false);
1623 new_reg_offset
[reg
] = 0;
1628 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1629 /* If this is a split point, reset the offset to 0 and allocate a
1630 * new virtual GRF for the previous offset many registers
1632 if (split_points
[reg
]) {
1633 assert(offset
<= MAX_VGRF_SIZE
);
1634 int grf
= alloc
.allocate(offset
);
1635 for (int k
= reg
- offset
; k
< reg
; k
++)
1636 new_virtual_grf
[k
] = grf
;
1639 new_reg_offset
[reg
] = offset
;
1644 /* The last one gets the original register number */
1645 assert(offset
<= MAX_VGRF_SIZE
);
1646 alloc
.sizes
[i
] = offset
;
1647 for (int k
= reg
- offset
; k
< reg
; k
++)
1648 new_virtual_grf
[k
] = i
;
1650 assert(reg
== reg_count
);
1652 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1653 if (inst
->dst
.file
== GRF
) {
1654 reg
= vgrf_to_reg
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
1655 inst
->dst
.reg
= new_virtual_grf
[reg
];
1656 inst
->dst
.reg_offset
= new_reg_offset
[reg
];
1657 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1659 for (int i
= 0; i
< inst
->sources
; i
++) {
1660 if (inst
->src
[i
].file
== GRF
) {
1661 reg
= vgrf_to_reg
[inst
->src
[i
].reg
] + inst
->src
[i
].reg_offset
;
1662 inst
->src
[i
].reg
= new_virtual_grf
[reg
];
1663 inst
->src
[i
].reg_offset
= new_reg_offset
[reg
];
1664 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1668 invalidate_live_intervals();
1672 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1674 * During code generation, we create tons of temporary variables, many of
1675 * which get immediately killed and are never used again. Yet, in later
1676 * optimization and analysis passes, such as compute_live_intervals, we need
1677 * to loop over all the virtual GRFs. Compacting them can save a lot of
1681 fs_visitor::compact_virtual_grfs()
1683 bool progress
= false;
1684 int remap_table
[this->alloc
.count
];
1685 memset(remap_table
, -1, sizeof(remap_table
));
1687 /* Mark which virtual GRFs are used. */
1688 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
1689 if (inst
->dst
.file
== GRF
)
1690 remap_table
[inst
->dst
.reg
] = 0;
1692 for (int i
= 0; i
< inst
->sources
; i
++) {
1693 if (inst
->src
[i
].file
== GRF
)
1694 remap_table
[inst
->src
[i
].reg
] = 0;
1698 /* Compact the GRF arrays. */
1700 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
1701 if (remap_table
[i
] == -1) {
1702 /* We just found an unused register. This means that we are
1703 * actually going to compact something.
1707 remap_table
[i
] = new_index
;
1708 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
1709 invalidate_live_intervals();
1714 this->alloc
.count
= new_index
;
1716 /* Patch all the instructions to use the newly renumbered registers */
1717 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1718 if (inst
->dst
.file
== GRF
)
1719 inst
->dst
.reg
= remap_table
[inst
->dst
.reg
];
1721 for (int i
= 0; i
< inst
->sources
; i
++) {
1722 if (inst
->src
[i
].file
== GRF
)
1723 inst
->src
[i
].reg
= remap_table
[inst
->src
[i
].reg
];
1727 /* Patch all the references to delta_xy, since they're used in register
1728 * allocation. If they're unused, switch them to BAD_FILE so we don't
1729 * think some random VGRF is delta_xy.
1731 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
1732 if (delta_xy
[i
].file
== GRF
) {
1733 if (remap_table
[delta_xy
[i
].reg
] != -1) {
1734 delta_xy
[i
].reg
= remap_table
[delta_xy
[i
].reg
];
1736 delta_xy
[i
].file
= BAD_FILE
;
1745 * Assign UNIFORM file registers to either push constants or pull constants.
1747 * We allow a fragment shader to have more than the specified minimum
1748 * maximum number of fragment shader uniform components (64). If
1749 * there are too many of these, they'd fill up all of register space.
1750 * So, this will push some of them out to the pull constant buffer and
1751 * update the program to load them. We also use pull constants for all
1752 * indirect constant loads because we don't support indirect accesses in
1756 fs_visitor::assign_constant_locations()
1758 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
1759 if (dispatch_width
!= 8)
1762 unsigned int num_pull_constants
= 0;
1764 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1765 memset(pull_constant_loc
, -1, sizeof(pull_constant_loc
[0]) * uniforms
);
1767 bool is_live
[uniforms
];
1768 memset(is_live
, 0, sizeof(is_live
));
1770 /* First, we walk through the instructions and do two things:
1772 * 1) Figure out which uniforms are live.
1774 * 2) Find all indirect access of uniform arrays and flag them as needing
1775 * to go into the pull constant buffer.
1777 * Note that we don't move constant-indexed accesses to arrays. No
1778 * testing has been done of the performance impact of this choice.
1780 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
1781 for (int i
= 0 ; i
< inst
->sources
; i
++) {
1782 if (inst
->src
[i
].file
!= UNIFORM
)
1785 if (inst
->src
[i
].reladdr
) {
1786 int uniform
= inst
->src
[i
].reg
;
1788 /* If this array isn't already present in the pull constant buffer,
1791 if (pull_constant_loc
[uniform
] == -1) {
1792 assert(param_size
[uniform
]);
1793 for (int j
= 0; j
< param_size
[uniform
]; j
++)
1794 pull_constant_loc
[uniform
+ j
] = num_pull_constants
++;
1797 /* Mark the the one accessed uniform as live */
1798 int constant_nr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1799 if (constant_nr
>= 0 && constant_nr
< (int) uniforms
)
1800 is_live
[constant_nr
] = true;
1805 /* Only allow 16 registers (128 uniform components) as push constants.
1807 * Just demote the end of the list. We could probably do better
1808 * here, demoting things that are rarely used in the program first.
1810 * If changing this value, note the limitation about total_regs in
1813 unsigned int max_push_components
= 16 * 8;
1814 unsigned int num_push_constants
= 0;
1816 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1818 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1819 if (!is_live
[i
] || pull_constant_loc
[i
] != -1) {
1820 /* This UNIFORM register is either dead, or has already been demoted
1821 * to a pull const. Mark it as no longer living in the param[] array.
1823 push_constant_loc
[i
] = -1;
1827 if (num_push_constants
< max_push_components
) {
1828 /* Retain as a push constant. Record the location in the params[]
1831 push_constant_loc
[i
] = num_push_constants
++;
1833 /* Demote to a pull constant. */
1834 push_constant_loc
[i
] = -1;
1835 pull_constant_loc
[i
] = num_pull_constants
++;
1839 stage_prog_data
->nr_params
= num_push_constants
;
1840 stage_prog_data
->nr_pull_params
= num_pull_constants
;
1842 /* Up until now, the param[] array has been indexed by reg + reg_offset
1843 * of UNIFORM registers. Move pull constants into pull_param[] and
1844 * condense param[] to only contain the uniforms we chose to push.
1846 * NOTE: Because we are condensing the params[] array, we know that
1847 * push_constant_loc[i] <= i and we can do it in one smooth loop without
1848 * having to make a copy.
1850 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1851 const gl_constant_value
*value
= stage_prog_data
->param
[i
];
1853 if (pull_constant_loc
[i
] != -1) {
1854 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
1855 } else if (push_constant_loc
[i
] != -1) {
1856 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
1862 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
1863 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
1866 fs_visitor::demote_pull_constants()
1868 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
1869 for (int i
= 0; i
< inst
->sources
; i
++) {
1870 if (inst
->src
[i
].file
!= UNIFORM
)
1874 unsigned location
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1875 if (location
>= uniforms
) /* Out of bounds access */
1878 pull_index
= pull_constant_loc
[location
];
1880 if (pull_index
== -1)
1883 /* Set up the annotation tracking for new generated instructions. */
1884 const fs_builder
ibld(this, block
, inst
);
1885 fs_reg
surf_index(stage_prog_data
->binding_table
.pull_constants_start
);
1886 fs_reg dst
= vgrf(glsl_type::float_type
);
1888 assert(inst
->src
[i
].stride
== 0);
1890 /* Generate a pull load into dst. */
1891 if (inst
->src
[i
].reladdr
) {
1892 VARYING_PULL_CONSTANT_LOAD(ibld
, dst
,
1894 *inst
->src
[i
].reladdr
,
1896 inst
->src
[i
].reladdr
= NULL
;
1897 inst
->src
[i
].stride
= 1;
1899 const fs_builder ubld
= ibld
.exec_all().group(8, 0);
1900 fs_reg offset
= fs_reg((unsigned)(pull_index
* 4) & ~15);
1901 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
1902 dst
, surf_index
, offset
);
1903 inst
->src
[i
].set_smear(pull_index
& 3);
1906 /* Rewrite the instruction to use the temporary VGRF. */
1907 inst
->src
[i
].file
= GRF
;
1908 inst
->src
[i
].reg
= dst
.reg
;
1909 inst
->src
[i
].reg_offset
= 0;
1912 invalidate_live_intervals();
1916 fs_visitor::opt_algebraic()
1918 bool progress
= false;
1920 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1921 switch (inst
->opcode
) {
1922 case BRW_OPCODE_MOV
:
1923 if (inst
->src
[0].file
!= IMM
)
1926 if (inst
->saturate
) {
1927 if (inst
->dst
.type
!= inst
->src
[0].type
)
1928 assert(!"unimplemented: saturate mixed types");
1930 if (brw_saturate_immediate(inst
->dst
.type
,
1931 &inst
->src
[0].fixed_hw_reg
)) {
1932 inst
->saturate
= false;
1938 case BRW_OPCODE_MUL
:
1939 if (inst
->src
[1].file
!= IMM
)
1943 if (inst
->src
[1].is_one()) {
1944 inst
->opcode
= BRW_OPCODE_MOV
;
1945 inst
->src
[1] = reg_undef
;
1951 if (inst
->src
[1].is_negative_one()) {
1952 inst
->opcode
= BRW_OPCODE_MOV
;
1953 inst
->src
[0].negate
= !inst
->src
[0].negate
;
1954 inst
->src
[1] = reg_undef
;
1960 if (inst
->src
[1].is_zero()) {
1961 inst
->opcode
= BRW_OPCODE_MOV
;
1962 inst
->src
[0] = inst
->src
[1];
1963 inst
->src
[1] = reg_undef
;
1968 if (inst
->src
[0].file
== IMM
) {
1969 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
1970 inst
->opcode
= BRW_OPCODE_MOV
;
1971 inst
->src
[0].fixed_hw_reg
.dw1
.f
*= inst
->src
[1].fixed_hw_reg
.dw1
.f
;
1972 inst
->src
[1] = reg_undef
;
1977 case BRW_OPCODE_ADD
:
1978 if (inst
->src
[1].file
!= IMM
)
1982 if (inst
->src
[1].is_zero()) {
1983 inst
->opcode
= BRW_OPCODE_MOV
;
1984 inst
->src
[1] = reg_undef
;
1989 if (inst
->src
[0].file
== IMM
) {
1990 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
1991 inst
->opcode
= BRW_OPCODE_MOV
;
1992 inst
->src
[0].fixed_hw_reg
.dw1
.f
+= inst
->src
[1].fixed_hw_reg
.dw1
.f
;
1993 inst
->src
[1] = reg_undef
;
1999 if (inst
->src
[0].equals(inst
->src
[1])) {
2000 inst
->opcode
= BRW_OPCODE_MOV
;
2001 inst
->src
[1] = reg_undef
;
2006 case BRW_OPCODE_LRP
:
2007 if (inst
->src
[1].equals(inst
->src
[2])) {
2008 inst
->opcode
= BRW_OPCODE_MOV
;
2009 inst
->src
[0] = inst
->src
[1];
2010 inst
->src
[1] = reg_undef
;
2011 inst
->src
[2] = reg_undef
;
2016 case BRW_OPCODE_CMP
:
2017 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
2019 inst
->src
[0].negate
&&
2020 inst
->src
[1].is_zero()) {
2021 inst
->src
[0].abs
= false;
2022 inst
->src
[0].negate
= false;
2023 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2028 case BRW_OPCODE_SEL
:
2029 if (inst
->src
[0].equals(inst
->src
[1])) {
2030 inst
->opcode
= BRW_OPCODE_MOV
;
2031 inst
->src
[1] = reg_undef
;
2032 inst
->predicate
= BRW_PREDICATE_NONE
;
2033 inst
->predicate_inverse
= false;
2035 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2036 switch (inst
->conditional_mod
) {
2037 case BRW_CONDITIONAL_LE
:
2038 case BRW_CONDITIONAL_L
:
2039 switch (inst
->src
[1].type
) {
2040 case BRW_REGISTER_TYPE_F
:
2041 if (inst
->src
[1].fixed_hw_reg
.dw1
.f
>= 1.0f
) {
2042 inst
->opcode
= BRW_OPCODE_MOV
;
2043 inst
->src
[1] = reg_undef
;
2044 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2052 case BRW_CONDITIONAL_GE
:
2053 case BRW_CONDITIONAL_G
:
2054 switch (inst
->src
[1].type
) {
2055 case BRW_REGISTER_TYPE_F
:
2056 if (inst
->src
[1].fixed_hw_reg
.dw1
.f
<= 0.0f
) {
2057 inst
->opcode
= BRW_OPCODE_MOV
;
2058 inst
->src
[1] = reg_undef
;
2059 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2071 case BRW_OPCODE_MAD
:
2072 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2073 inst
->opcode
= BRW_OPCODE_MOV
;
2074 inst
->src
[1] = reg_undef
;
2075 inst
->src
[2] = reg_undef
;
2077 } else if (inst
->src
[0].is_zero()) {
2078 inst
->opcode
= BRW_OPCODE_MUL
;
2079 inst
->src
[0] = inst
->src
[2];
2080 inst
->src
[2] = reg_undef
;
2082 } else if (inst
->src
[1].is_one()) {
2083 inst
->opcode
= BRW_OPCODE_ADD
;
2084 inst
->src
[1] = inst
->src
[2];
2085 inst
->src
[2] = reg_undef
;
2087 } else if (inst
->src
[2].is_one()) {
2088 inst
->opcode
= BRW_OPCODE_ADD
;
2089 inst
->src
[2] = reg_undef
;
2091 } else if (inst
->src
[1].file
== IMM
&& inst
->src
[2].file
== IMM
) {
2092 inst
->opcode
= BRW_OPCODE_ADD
;
2093 inst
->src
[1].fixed_hw_reg
.dw1
.f
*= inst
->src
[2].fixed_hw_reg
.dw1
.f
;
2094 inst
->src
[2] = reg_undef
;
2098 case SHADER_OPCODE_RCP
: {
2099 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
2100 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
2101 if (inst
->src
[0].equals(prev
->dst
)) {
2102 inst
->opcode
= SHADER_OPCODE_RSQ
;
2103 inst
->src
[0] = prev
->src
[0];
2109 case SHADER_OPCODE_BROADCAST
:
2110 if (is_uniform(inst
->src
[0])) {
2111 inst
->opcode
= BRW_OPCODE_MOV
;
2113 inst
->force_writemask_all
= true;
2115 } else if (inst
->src
[1].file
== IMM
) {
2116 inst
->opcode
= BRW_OPCODE_MOV
;
2117 inst
->src
[0] = component(inst
->src
[0],
2118 inst
->src
[1].fixed_hw_reg
.dw1
.ud
);
2120 inst
->force_writemask_all
= true;
2129 /* Swap if src[0] is immediate. */
2130 if (progress
&& inst
->is_commutative()) {
2131 if (inst
->src
[0].file
== IMM
) {
2132 fs_reg tmp
= inst
->src
[1];
2133 inst
->src
[1] = inst
->src
[0];
2142 * Optimize sample messages that have constant zero values for the trailing
2143 * texture coordinates. We can just reduce the message length for these
2144 * instructions instead of reserving a register for it. Trailing parameters
2145 * that aren't sent default to zero anyway. This will cause the dead code
2146 * eliminator to remove the MOV instruction that would otherwise be emitted to
2147 * set up the zero value.
2150 fs_visitor::opt_zero_samples()
2152 /* Gen4 infers the texturing opcode based on the message length so we can't
2155 if (devinfo
->gen
< 5)
2158 bool progress
= false;
2160 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2161 if (!inst
->is_tex())
2164 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2166 if (load_payload
->is_head_sentinel() ||
2167 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2170 /* We don't want to remove the message header or the first parameter.
2171 * Removing the first parameter is not allowed, see the Haswell PRM
2172 * volume 7, page 149:
2174 * "Parameter 0 is required except for the sampleinfo message, which
2175 * has no parameter 0"
2177 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2178 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2179 (inst
->exec_size
/ 8) +
2180 inst
->header_size
- 1].is_zero()) {
2181 inst
->mlen
-= inst
->exec_size
/ 8;
2187 invalidate_live_intervals();
2193 * Optimize sample messages which are followed by the final RT write.
2195 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2196 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2197 * final texturing results copied to the framebuffer write payload and modify
2198 * them to write to the framebuffer directly.
2201 fs_visitor::opt_sampler_eot()
2203 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2205 if (stage
!= MESA_SHADER_FRAGMENT
)
2208 if (devinfo
->gen
< 9 && !devinfo
->is_cherryview
)
2211 /* FINISHME: It should be possible to implement this optimization when there
2212 * are multiple drawbuffers.
2214 if (key
->nr_color_regions
!= 1)
2217 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2218 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2219 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2220 assert(fb_write
->eot
);
2221 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE
);
2223 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2225 /* There wasn't one; nothing to do. */
2226 if (unlikely(tex_inst
->is_head_sentinel()) || !tex_inst
->is_tex())
2229 /* This optimisation doesn't seem to work for textureGather for some
2230 * reason. I can't find any documentation or known workarounds to indicate
2231 * that this is expected, but considering that it is probably pretty
2232 * unlikely that a shader would directly write out the results from
2233 * textureGather we might as well just disable it.
2235 if (tex_inst
->opcode
== SHADER_OPCODE_TG4
||
2236 tex_inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
2239 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2240 * It's very likely to be the previous instruction.
2242 fs_inst
*load_payload
= (fs_inst
*) tex_inst
->prev
;
2243 if (load_payload
->is_head_sentinel() ||
2244 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2247 assert(!tex_inst
->eot
); /* We can't get here twice */
2248 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2250 const fs_builder
ibld(this, block
, tex_inst
);
2252 tex_inst
->offset
|= fb_write
->target
<< 24;
2253 tex_inst
->eot
= true;
2254 tex_inst
->dst
= ibld
.null_reg_ud();
2255 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2257 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2258 * to create a new LOAD_PAYLOAD command with the same sources and a space
2259 * saved for the header. Using a new destination register not only makes sure
2260 * we have enough space, but it will make sure the dead code eliminator kills
2261 * the instruction that this will replace.
2263 if (tex_inst
->header_size
!= 0)
2266 fs_reg send_header
= ibld
.vgrf(BRW_REGISTER_TYPE_F
,
2267 load_payload
->sources
+ 1);
2268 fs_reg
*new_sources
=
2269 ralloc_array(mem_ctx
, fs_reg
, load_payload
->sources
+ 1);
2271 new_sources
[0] = fs_reg();
2272 for (int i
= 0; i
< load_payload
->sources
; i
++)
2273 new_sources
[i
+1] = load_payload
->src
[i
];
2275 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2276 * requires a lot of information about the sources to appropriately figure
2277 * out the number of registers needed to be used. Given this stage in our
2278 * optimization, we may not have the appropriate GRFs required by
2279 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2280 * manually emit the instruction.
2282 fs_inst
*new_load_payload
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
,
2283 load_payload
->exec_size
,
2286 load_payload
->sources
+ 1);
2288 new_load_payload
->regs_written
= load_payload
->regs_written
+ 1;
2289 new_load_payload
->header_size
= 1;
2291 tex_inst
->header_size
= 1;
2292 tex_inst
->insert_before(cfg
->blocks
[cfg
->num_blocks
- 1], new_load_payload
);
2293 tex_inst
->src
[0] = send_header
;
2299 fs_visitor::opt_register_renaming()
2301 bool progress
= false;
2304 int remap
[alloc
.count
];
2305 memset(remap
, -1, sizeof(int) * alloc
.count
);
2307 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2308 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2310 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2311 inst
->opcode
== BRW_OPCODE_WHILE
) {
2315 /* Rewrite instruction sources. */
2316 for (int i
= 0; i
< inst
->sources
; i
++) {
2317 if (inst
->src
[i
].file
== GRF
&&
2318 remap
[inst
->src
[i
].reg
] != -1 &&
2319 remap
[inst
->src
[i
].reg
] != inst
->src
[i
].reg
) {
2320 inst
->src
[i
].reg
= remap
[inst
->src
[i
].reg
];
2325 const int dst
= inst
->dst
.reg
;
2328 inst
->dst
.file
== GRF
&&
2329 alloc
.sizes
[inst
->dst
.reg
] == inst
->exec_size
/ 8 &&
2330 !inst
->is_partial_write()) {
2331 if (remap
[dst
] == -1) {
2334 remap
[dst
] = alloc
.allocate(inst
->exec_size
/ 8);
2335 inst
->dst
.reg
= remap
[dst
];
2338 } else if (inst
->dst
.file
== GRF
&&
2340 remap
[dst
] != dst
) {
2341 inst
->dst
.reg
= remap
[dst
];
2347 invalidate_live_intervals();
2349 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2350 if (delta_xy
[i
].file
== GRF
&& remap
[delta_xy
[i
].reg
] != -1) {
2351 delta_xy
[i
].reg
= remap
[delta_xy
[i
].reg
];
2360 * Remove redundant or useless discard jumps.
2362 * For example, we can eliminate jumps in the following sequence:
2364 * discard-jump (redundant with the next jump)
2365 * discard-jump (useless; jumps to the next instruction)
2369 fs_visitor::opt_redundant_discard_jumps()
2371 bool progress
= false;
2373 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2375 fs_inst
*placeholder_halt
= NULL
;
2376 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2377 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2378 placeholder_halt
= inst
;
2383 if (!placeholder_halt
)
2386 /* Delete any HALTs immediately before the placeholder halt. */
2387 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2388 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2389 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2390 prev
->remove(last_bblock
);
2395 invalidate_live_intervals();
2401 fs_visitor::compute_to_mrf()
2403 bool progress
= false;
2406 /* No MRFs on Gen >= 7. */
2407 if (devinfo
->gen
>= 7)
2410 calculate_live_intervals();
2412 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2416 if (inst
->opcode
!= BRW_OPCODE_MOV
||
2417 inst
->is_partial_write() ||
2418 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= GRF
||
2419 inst
->dst
.type
!= inst
->src
[0].type
||
2420 inst
->src
[0].abs
|| inst
->src
[0].negate
||
2421 !inst
->src
[0].is_contiguous() ||
2422 inst
->src
[0].subreg_offset
)
2425 /* Work out which hardware MRF registers are written by this
2428 int mrf_low
= inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
2430 if (inst
->dst
.reg
& BRW_MRF_COMPR4
) {
2431 mrf_high
= mrf_low
+ 4;
2432 } else if (inst
->exec_size
== 16) {
2433 mrf_high
= mrf_low
+ 1;
2438 /* Can't compute-to-MRF this GRF if someone else was going to
2441 if (this->virtual_grf_end
[inst
->src
[0].reg
] > ip
)
2444 /* Found a move of a GRF to a MRF. Let's see if we can go
2445 * rewrite the thing that made this GRF to write into the MRF.
2447 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2448 if (scan_inst
->dst
.file
== GRF
&&
2449 scan_inst
->dst
.reg
== inst
->src
[0].reg
) {
2450 /* Found the last thing to write our reg we want to turn
2451 * into a compute-to-MRF.
2454 /* If this one instruction didn't populate all the
2455 * channels, bail. We might be able to rewrite everything
2456 * that writes that reg, but it would require smarter
2457 * tracking to delay the rewriting until complete success.
2459 if (scan_inst
->is_partial_write())
2462 /* Things returning more than one register would need us to
2463 * understand coalescing out more than one MOV at a time.
2465 if (scan_inst
->regs_written
> scan_inst
->exec_size
/ 8)
2468 /* SEND instructions can't have MRF as a destination. */
2469 if (scan_inst
->mlen
)
2472 if (devinfo
->gen
== 6) {
2473 /* gen6 math instructions must have the destination be
2474 * GRF, so no compute-to-MRF for them.
2476 if (scan_inst
->is_math()) {
2481 if (scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
2482 /* Found the creator of our MRF's source value. */
2483 scan_inst
->dst
.file
= MRF
;
2484 scan_inst
->dst
.reg
= inst
->dst
.reg
;
2485 scan_inst
->saturate
|= inst
->saturate
;
2486 inst
->remove(block
);
2492 /* We don't handle control flow here. Most computation of
2493 * values that end up in MRFs are shortly before the MRF
2496 if (block
->start() == scan_inst
)
2499 /* You can't read from an MRF, so if someone else reads our
2500 * MRF's source GRF that we wanted to rewrite, that stops us.
2502 bool interfered
= false;
2503 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
2504 if (scan_inst
->src
[i
].file
== GRF
&&
2505 scan_inst
->src
[i
].reg
== inst
->src
[0].reg
&&
2506 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
2513 if (scan_inst
->dst
.file
== MRF
) {
2514 /* If somebody else writes our MRF here, we can't
2515 * compute-to-MRF before that.
2517 int scan_mrf_low
= scan_inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
2520 if (scan_inst
->dst
.reg
& BRW_MRF_COMPR4
) {
2521 scan_mrf_high
= scan_mrf_low
+ 4;
2522 } else if (scan_inst
->exec_size
== 16) {
2523 scan_mrf_high
= scan_mrf_low
+ 1;
2525 scan_mrf_high
= scan_mrf_low
;
2528 if (mrf_low
== scan_mrf_low
||
2529 mrf_low
== scan_mrf_high
||
2530 mrf_high
== scan_mrf_low
||
2531 mrf_high
== scan_mrf_high
) {
2536 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1) {
2537 /* Found a SEND instruction, which means that there are
2538 * live values in MRFs from base_mrf to base_mrf +
2539 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2542 if (mrf_low
>= scan_inst
->base_mrf
&&
2543 mrf_low
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2546 if (mrf_high
>= scan_inst
->base_mrf
&&
2547 mrf_high
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2555 invalidate_live_intervals();
2561 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2562 * flow. We could probably do better here with some form of divergence
2566 fs_visitor::eliminate_find_live_channel()
2568 bool progress
= false;
2571 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2572 switch (inst
->opcode
) {
2578 case BRW_OPCODE_ENDIF
:
2579 case BRW_OPCODE_WHILE
:
2583 case FS_OPCODE_DISCARD_JUMP
:
2584 /* This can potentially make control flow non-uniform until the end
2589 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2591 inst
->opcode
= BRW_OPCODE_MOV
;
2592 inst
->src
[0] = fs_reg(0);
2594 inst
->force_writemask_all
= true;
2608 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2609 * instructions to FS_OPCODE_REP_FB_WRITE.
2612 fs_visitor::emit_repclear_shader()
2614 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2616 int color_mrf
= base_mrf
+ 2;
2618 fs_inst
*mov
= bld
.exec_all().MOV(vec4(brw_message_reg(color_mrf
)),
2619 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
2622 if (key
->nr_color_regions
== 1) {
2623 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2624 write
->saturate
= key
->clamp_fragment_color
;
2625 write
->base_mrf
= color_mrf
;
2627 write
->header_size
= 0;
2630 assume(key
->nr_color_regions
> 0);
2631 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
2632 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2633 write
->saturate
= key
->clamp_fragment_color
;
2634 write
->base_mrf
= base_mrf
;
2636 write
->header_size
= 2;
2644 assign_constant_locations();
2645 assign_curb_setup();
2647 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2648 assert(mov
->src
[0].file
== HW_REG
);
2649 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].fixed_hw_reg
.nr
, 0);
2653 * Walks through basic blocks, looking for repeated MRF writes and
2654 * removing the later ones.
2657 fs_visitor::remove_duplicate_mrf_writes()
2659 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
2660 bool progress
= false;
2662 /* Need to update the MRF tracking for compressed instructions. */
2663 if (dispatch_width
== 16)
2666 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2668 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2669 if (inst
->is_control_flow()) {
2670 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2673 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2674 inst
->dst
.file
== MRF
) {
2675 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.reg
];
2676 if (prev_inst
&& inst
->equals(prev_inst
)) {
2677 inst
->remove(block
);
2683 /* Clear out the last-write records for MRFs that were overwritten. */
2684 if (inst
->dst
.file
== MRF
) {
2685 last_mrf_move
[inst
->dst
.reg
] = NULL
;
2688 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
2689 /* Found a SEND instruction, which will include two or fewer
2690 * implied MRF writes. We could do better here.
2692 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
2693 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
2697 /* Clear out any MRF move records whose sources got overwritten. */
2698 if (inst
->dst
.file
== GRF
) {
2699 for (unsigned int i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
2700 if (last_mrf_move
[i
] &&
2701 last_mrf_move
[i
]->src
[0].reg
== inst
->dst
.reg
) {
2702 last_mrf_move
[i
] = NULL
;
2707 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2708 inst
->dst
.file
== MRF
&&
2709 inst
->src
[0].file
== GRF
&&
2710 !inst
->is_partial_write()) {
2711 last_mrf_move
[inst
->dst
.reg
] = inst
;
2716 invalidate_live_intervals();
2722 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
2724 /* Clear the flag for registers that actually got read (as expected). */
2725 for (int i
= 0; i
< inst
->sources
; i
++) {
2727 if (inst
->src
[i
].file
== GRF
) {
2728 grf
= inst
->src
[i
].reg
;
2729 } else if (inst
->src
[i
].file
== HW_REG
&&
2730 inst
->src
[i
].fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
) {
2731 grf
= inst
->src
[i
].fixed_hw_reg
.nr
;
2736 if (grf
>= first_grf
&&
2737 grf
< first_grf
+ grf_len
) {
2738 deps
[grf
- first_grf
] = false;
2739 if (inst
->exec_size
== 16)
2740 deps
[grf
- first_grf
+ 1] = false;
2746 * Implements this workaround for the original 965:
2748 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2749 * check for post destination dependencies on this instruction, software
2750 * must ensure that there is no destination hazard for the case of ‘write
2751 * followed by a posted write’ shown in the following example.
2754 * 2. send r3.xy <rest of send instruction>
2757 * Due to no post-destination dependency check on the ‘send’, the above
2758 * code sequence could have two instructions (1 and 2) in flight at the
2759 * same time that both consider ‘r3’ as the target of their final writes.
2762 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
2765 int write_len
= inst
->regs_written
;
2766 int first_write_grf
= inst
->dst
.reg
;
2767 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2768 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2770 memset(needs_dep
, false, sizeof(needs_dep
));
2771 memset(needs_dep
, true, write_len
);
2773 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
2775 /* Walk backwards looking for writes to registers we're writing which
2776 * aren't read since being written. If we hit the start of the program,
2777 * we assume that there are no outstanding dependencies on entry to the
2780 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2781 /* If we hit control flow, assume that there *are* outstanding
2782 * dependencies, and force their cleanup before our instruction.
2784 if (block
->start() == scan_inst
) {
2785 for (int i
= 0; i
< write_len
; i
++) {
2787 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
2788 first_write_grf
+ i
);
2793 /* We insert our reads as late as possible on the assumption that any
2794 * instruction but a MOV that might have left us an outstanding
2795 * dependency has more latency than a MOV.
2797 if (scan_inst
->dst
.file
== GRF
) {
2798 for (int i
= 0; i
< scan_inst
->regs_written
; i
++) {
2799 int reg
= scan_inst
->dst
.reg
+ i
;
2801 if (reg
>= first_write_grf
&&
2802 reg
< first_write_grf
+ write_len
&&
2803 needs_dep
[reg
- first_write_grf
]) {
2804 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
2805 needs_dep
[reg
- first_write_grf
] = false;
2806 if (scan_inst
->exec_size
== 16)
2807 needs_dep
[reg
- first_write_grf
+ 1] = false;
2812 /* Clear the flag for registers that actually got read (as expected). */
2813 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2815 /* Continue the loop only if we haven't resolved all the dependencies */
2817 for (i
= 0; i
< write_len
; i
++) {
2827 * Implements this workaround for the original 965:
2829 * "[DevBW, DevCL] Errata: A destination register from a send can not be
2830 * used as a destination register until after it has been sourced by an
2831 * instruction with a different destination register.
2834 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
2836 int write_len
= inst
->regs_written
;
2837 int first_write_grf
= inst
->dst
.reg
;
2838 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2839 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2841 memset(needs_dep
, false, sizeof(needs_dep
));
2842 memset(needs_dep
, true, write_len
);
2843 /* Walk forwards looking for writes to registers we're writing which aren't
2844 * read before being written.
2846 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2847 /* If we hit control flow, force resolve all remaining dependencies. */
2848 if (block
->end() == scan_inst
) {
2849 for (int i
= 0; i
< write_len
; i
++) {
2851 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2852 first_write_grf
+ i
);
2857 /* Clear the flag for registers that actually got read (as expected). */
2858 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2860 /* We insert our reads as late as possible since they're reading the
2861 * result of a SEND, which has massive latency.
2863 if (scan_inst
->dst
.file
== GRF
&&
2864 scan_inst
->dst
.reg
>= first_write_grf
&&
2865 scan_inst
->dst
.reg
< first_write_grf
+ write_len
&&
2866 needs_dep
[scan_inst
->dst
.reg
- first_write_grf
]) {
2867 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2868 scan_inst
->dst
.reg
);
2869 needs_dep
[scan_inst
->dst
.reg
- first_write_grf
] = false;
2872 /* Continue the loop only if we haven't resolved all the dependencies */
2874 for (i
= 0; i
< write_len
; i
++) {
2884 fs_visitor::insert_gen4_send_dependency_workarounds()
2886 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
2889 bool progress
= false;
2891 /* Note that we're done with register allocation, so GRF fs_regs always
2892 * have a .reg_offset of 0.
2895 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2896 if (inst
->mlen
!= 0 && inst
->dst
.file
== GRF
) {
2897 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
2898 insert_gen4_post_send_dependency_workarounds(block
, inst
);
2904 invalidate_live_intervals();
2908 * Turns the generic expression-style uniform pull constant load instruction
2909 * into a hardware-specific series of instructions for loading a pull
2912 * The expression style allows the CSE pass before this to optimize out
2913 * repeated loads from the same offset, and gives the pre-register-allocation
2914 * scheduling full flexibility, while the conversion to native instructions
2915 * allows the post-register-allocation scheduler the best information
2918 * Note that execution masking for setting up pull constant loads is special:
2919 * the channels that need to be written are unrelated to the current execution
2920 * mask, since a later instruction will use one of the result channels as a
2921 * source operand for all 8 or 16 of its channels.
2924 fs_visitor::lower_uniform_pull_constant_loads()
2926 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
2927 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
2930 if (devinfo
->gen
>= 7) {
2931 /* The offset arg before was a vec4-aligned byte offset. We need to
2932 * turn it into a dword offset.
2934 fs_reg const_offset_reg
= inst
->src
[1];
2935 assert(const_offset_reg
.file
== IMM
&&
2936 const_offset_reg
.type
== BRW_REGISTER_TYPE_UD
);
2937 const_offset_reg
.fixed_hw_reg
.dw1
.ud
/= 4;
2939 fs_reg payload
, offset
;
2940 if (devinfo
->gen
>= 9) {
2941 /* We have to use a message header on Skylake to get SIMD4x2
2942 * mode. Reserve space for the register.
2944 offset
= payload
= fs_reg(GRF
, alloc
.allocate(2));
2945 offset
.reg_offset
++;
2948 offset
= payload
= fs_reg(GRF
, alloc
.allocate(1));
2952 /* This is actually going to be a MOV, but since only the first dword
2953 * is accessed, we have a special opcode to do just that one. Note
2954 * that this needs to be an operation that will be considered a def
2955 * by live variable analysis, or register allocation will explode.
2957 fs_inst
*setup
= new(mem_ctx
) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET
,
2958 8, offset
, const_offset_reg
);
2959 setup
->force_writemask_all
= true;
2961 setup
->ir
= inst
->ir
;
2962 setup
->annotation
= inst
->annotation
;
2963 inst
->insert_before(block
, setup
);
2965 /* Similarly, this will only populate the first 4 channels of the
2966 * result register (since we only use smear values from 0-3), but we
2967 * don't tell the optimizer.
2969 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
2970 inst
->src
[1] = payload
;
2971 inst
->base_mrf
= -1;
2973 invalidate_live_intervals();
2975 /* Before register allocation, we didn't tell the scheduler about the
2976 * MRF we use. We know it's safe to use this MRF because nothing
2977 * else does except for register spill/unspill, which generates and
2978 * uses its MRF within a single IR instruction.
2980 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
2987 fs_visitor::lower_load_payload()
2989 bool progress
= false;
2991 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2992 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2995 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== GRF
);
2996 assert(inst
->saturate
== false);
2997 fs_reg dst
= inst
->dst
;
2999 /* Get rid of COMPR4. We'll add it back in if we need it */
3000 if (dst
.file
== MRF
)
3001 dst
.reg
= dst
.reg
& ~BRW_MRF_COMPR4
;
3003 const fs_builder
ibld(this, block
, inst
);
3004 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3006 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3007 if (inst
->src
[i
].file
!= BAD_FILE
) {
3008 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3009 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3010 hbld
.MOV(mov_dst
, mov_src
);
3012 dst
= offset(dst
, hbld
, 1);
3015 if (inst
->dst
.file
== MRF
&& (inst
->dst
.reg
& BRW_MRF_COMPR4
) &&
3016 inst
->exec_size
> 8) {
3017 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3018 * a straightforward copy. Instead, the result of the
3019 * LOAD_PAYLOAD is treated as interleaved and the first four
3020 * non-header sources are unpacked as:
3031 * This is used for gen <= 5 fb writes.
3033 assert(inst
->exec_size
== 16);
3034 assert(inst
->header_size
+ 4 <= inst
->sources
);
3035 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3036 if (inst
->src
[i
].file
!= BAD_FILE
) {
3037 if (devinfo
->has_compr4
) {
3038 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3039 compr4_dst
.reg
|= BRW_MRF_COMPR4
;
3040 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3042 /* Platform doesn't have COMPR4. We have to fake it */
3043 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3044 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3046 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3053 /* The loop above only ever incremented us through the first set
3054 * of 4 registers. However, thanks to the magic of COMPR4, we
3055 * actually wrote to the first 8 registers, so we need to take
3056 * that into account now.
3060 /* The COMPR4 code took care of the first 4 sources. We'll let
3061 * the regular path handle any remaining sources. Yes, we are
3062 * modifying the instruction but we're about to delete it so
3063 * this really doesn't hurt anything.
3065 inst
->header_size
+= 4;
3068 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3069 if (inst
->src
[i
].file
!= BAD_FILE
)
3070 ibld
.MOV(retype(dst
, inst
->src
[i
].type
), inst
->src
[i
]);
3071 dst
= offset(dst
, ibld
, 1);
3074 inst
->remove(block
);
3079 invalidate_live_intervals();
3085 fs_visitor::lower_integer_multiplication()
3087 bool progress
= false;
3089 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3090 const fs_builder
ibld(this, block
, inst
);
3092 if (inst
->opcode
== BRW_OPCODE_MUL
) {
3093 if (inst
->dst
.is_accumulator() ||
3094 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3095 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3098 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit
3099 * operation directly, but CHV/BXT cannot.
3101 if (devinfo
->gen
>= 8 &&
3102 !devinfo
->is_cherryview
&& !devinfo
->is_broxton
)
3105 if (inst
->src
[1].file
== IMM
&&
3106 inst
->src
[1].fixed_hw_reg
.dw1
.ud
< (1 << 16)) {
3107 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3108 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3111 * If multiplying by an immediate value that fits in 16-bits, do a
3112 * single MUL instruction with that value in the proper location.
3114 if (devinfo
->gen
< 7) {
3115 fs_reg
imm(GRF
, alloc
.allocate(dispatch_width
/ 8),
3117 ibld
.MOV(imm
, inst
->src
[1]);
3118 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3120 ibld
.MUL(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3123 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3124 * do 32-bit integer multiplication in one instruction, but instead
3125 * must do a sequence (which actually calculates a 64-bit result):
3127 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3128 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3129 * mov(8) g2<1>D acc0<8,8,1>D
3131 * But on Gen > 6, the ability to use second accumulator register
3132 * (acc1) for non-float data types was removed, preventing a simple
3133 * implementation in SIMD16. A 16-channel result can be calculated by
3134 * executing the three instructions twice in SIMD8, once with quarter
3135 * control of 1Q for the first eight channels and again with 2Q for
3136 * the second eight channels.
3138 * Which accumulator register is implicitly accessed (by AccWrEnable
3139 * for instance) is determined by the quarter control. Unfortunately
3140 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3141 * implicit accumulator access by an instruction with 2Q will access
3142 * acc1 regardless of whether the data type is usable in acc1.
3144 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3145 * integer data types.
3147 * Since we only want the low 32-bits of the result, we can do two
3148 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3149 * adjust the high result and add them (like the mach is doing):
3151 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3152 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3153 * shl(8) g9<1>D g8<8,8,1>D 16D
3154 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3156 * We avoid the shl instruction by realizing that we only want to add
3157 * the low 16-bits of the "high" result to the high 16-bits of the
3158 * "low" result and using proper regioning on the add:
3160 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3161 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3162 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3164 * Since it does not use the (single) accumulator register, we can
3165 * schedule multi-component multiplications much better.
3168 fs_reg orig_dst
= inst
->dst
;
3169 if (orig_dst
.is_null() || orig_dst
.file
== MRF
) {
3170 inst
->dst
= fs_reg(GRF
, alloc
.allocate(dispatch_width
/ 8),
3173 fs_reg low
= inst
->dst
;
3174 fs_reg
high(GRF
, alloc
.allocate(dispatch_width
/ 8),
3177 if (devinfo
->gen
>= 7) {
3178 fs_reg src1_0_w
= inst
->src
[1];
3179 fs_reg src1_1_w
= inst
->src
[1];
3181 if (inst
->src
[1].file
== IMM
) {
3182 src1_0_w
.fixed_hw_reg
.dw1
.ud
&= 0xffff;
3183 src1_1_w
.fixed_hw_reg
.dw1
.ud
>>= 16;
3185 src1_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3186 if (src1_0_w
.stride
!= 0) {
3187 assert(src1_0_w
.stride
== 1);
3188 src1_0_w
.stride
= 2;
3191 src1_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3192 if (src1_1_w
.stride
!= 0) {
3193 assert(src1_1_w
.stride
== 1);
3194 src1_1_w
.stride
= 2;
3196 src1_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3198 ibld
.MUL(low
, inst
->src
[0], src1_0_w
);
3199 ibld
.MUL(high
, inst
->src
[0], src1_1_w
);
3201 fs_reg src0_0_w
= inst
->src
[0];
3202 fs_reg src0_1_w
= inst
->src
[0];
3204 src0_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3205 if (src0_0_w
.stride
!= 0) {
3206 assert(src0_0_w
.stride
== 1);
3207 src0_0_w
.stride
= 2;
3210 src0_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3211 if (src0_1_w
.stride
!= 0) {
3212 assert(src0_1_w
.stride
== 1);
3213 src0_1_w
.stride
= 2;
3215 src0_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3217 ibld
.MUL(low
, src0_0_w
, inst
->src
[1]);
3218 ibld
.MUL(high
, src0_1_w
, inst
->src
[1]);
3221 fs_reg dst
= inst
->dst
;
3222 dst
.type
= BRW_REGISTER_TYPE_UW
;
3223 dst
.subreg_offset
= 2;
3226 high
.type
= BRW_REGISTER_TYPE_UW
;
3229 low
.type
= BRW_REGISTER_TYPE_UW
;
3230 low
.subreg_offset
= 2;
3233 ibld
.ADD(dst
, low
, high
);
3235 if (inst
->conditional_mod
|| orig_dst
.file
== MRF
) {
3236 set_condmod(inst
->conditional_mod
,
3237 ibld
.MOV(orig_dst
, inst
->dst
));
3241 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
3242 /* Should have been lowered to 8-wide. */
3243 assert(inst
->exec_size
<= 8);
3244 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
),
3246 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
3247 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3249 if (devinfo
->gen
>= 8) {
3250 /* Until Gen8, integer multiplies read 32-bits from one source,
3251 * and 16-bits from the other, and relying on the MACH instruction
3252 * to generate the high bits of the result.
3254 * On Gen8, the multiply instruction does a full 32x32-bit
3255 * multiply, but in order to do a 64-bit multiply we can simulate
3256 * the previous behavior and then use a MACH instruction.
3258 * FINISHME: Don't use source modifiers on src1.
3260 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
3261 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3262 mul
->src
[1].type
= (type_is_signed(mul
->src
[1].type
) ?
3263 BRW_REGISTER_TYPE_W
: BRW_REGISTER_TYPE_UW
);
3264 mul
->src
[1].stride
*= 2;
3266 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
3267 inst
->force_sechalf
) {
3268 /* Among other things the quarter control bits influence which
3269 * accumulator register is used by the hardware for instructions
3270 * that access the accumulator implicitly (e.g. MACH). A
3271 * second-half instruction would normally map to acc1, which
3272 * doesn't exist on Gen7 and up (the hardware does emulate it for
3273 * floating-point instructions *only* by taking advantage of the
3274 * extra precision of acc0 not normally used for floating point
3277 * HSW and up are careful enough not to try to access an
3278 * accumulator register that doesn't exist, but on earlier Gen7
3279 * hardware we need to make sure that the quarter control bits are
3280 * zero to avoid non-deterministic behaviour and emit an extra MOV
3281 * to get the result masked correctly according to the current
3284 mach
->force_sechalf
= false;
3285 mach
->force_writemask_all
= true;
3286 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
3287 ibld
.MOV(inst
->dst
, mach
->dst
);
3293 inst
->remove(block
);
3298 invalidate_live_intervals();
3304 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
3305 fs_reg
*dst
, fs_reg color
, unsigned components
)
3307 if (key
->clamp_fragment_color
) {
3308 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
3309 assert(color
.type
== BRW_REGISTER_TYPE_F
);
3311 for (unsigned i
= 0; i
< components
; i
++)
3313 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
3318 for (unsigned i
= 0; i
< components
; i
++)
3319 dst
[i
] = offset(color
, bld
, i
);
3323 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
3324 const brw_wm_prog_data
*prog_data
,
3325 const brw_wm_prog_key
*key
,
3326 const fs_visitor::thread_payload
&payload
)
3328 assert(inst
->src
[6].file
== IMM
);
3329 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3330 const fs_reg
&color0
= inst
->src
[0];
3331 const fs_reg
&color1
= inst
->src
[1];
3332 const fs_reg
&src0_alpha
= inst
->src
[2];
3333 const fs_reg
&src_depth
= inst
->src
[3];
3334 const fs_reg
&dst_depth
= inst
->src
[4];
3335 fs_reg sample_mask
= inst
->src
[5];
3336 const unsigned components
= inst
->src
[6].fixed_hw_reg
.dw1
.ud
;
3338 /* We can potentially have a message length of up to 15, so we have to set
3339 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3342 int header_size
= 2, payload_header_size
;
3343 unsigned length
= 0;
3345 /* From the Sandy Bridge PRM, volume 4, page 198:
3347 * "Dispatched Pixel Enables. One bit per pixel indicating
3348 * which pixels were originally enabled when the thread was
3349 * dispatched. This field is only required for the end-of-
3350 * thread message and on all dual-source messages."
3352 if (devinfo
->gen
>= 6 &&
3353 (devinfo
->is_haswell
|| devinfo
->gen
>= 8 || !prog_data
->uses_kill
) &&
3354 color1
.file
== BAD_FILE
&&
3355 key
->nr_color_regions
== 1) {
3359 if (header_size
!= 0) {
3360 assert(header_size
== 2);
3361 /* Allocate 2 registers for a header */
3365 if (payload
.aa_dest_stencil_reg
) {
3366 sources
[length
] = fs_reg(GRF
, bld
.shader
->alloc
.allocate(1));
3367 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3368 .MOV(sources
[length
],
3369 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0)));
3373 if (prog_data
->uses_omask
) {
3374 sources
[length
] = fs_reg(GRF
, bld
.shader
->alloc
.allocate(1),
3375 BRW_REGISTER_TYPE_UD
);
3377 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3378 * relevant. Since it's unsigned single words one vgrf is always
3379 * 16-wide, but only the lower or higher 8 channels will be used by the
3380 * hardware when doing a SIMD8 write depending on whether we have
3381 * selected the subspans for the first or second half respectively.
3383 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
3384 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
3385 sample_mask
.stride
*= 2;
3387 bld
.exec_all().annotate("FB write oMask")
3388 .MOV(half(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
3389 inst
->force_sechalf
),
3394 payload_header_size
= length
;
3396 if (src0_alpha
.file
!= BAD_FILE
) {
3397 /* FIXME: This is being passed at the wrong location in the payload and
3398 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3399 * It's supposed to be immediately before oMask but there seems to be no
3400 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3401 * requires header sources to form a contiguous segment at the beginning
3402 * of the message and src0_alpha has per-channel semantics.
3404 setup_color_payload(bld
, key
, &sources
[length
], src0_alpha
, 1);
3408 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
3411 if (color1
.file
!= BAD_FILE
) {
3412 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
3416 if (src_depth
.file
!= BAD_FILE
) {
3417 sources
[length
] = src_depth
;
3421 if (dst_depth
.file
!= BAD_FILE
) {
3422 sources
[length
] = dst_depth
;
3427 if (devinfo
->gen
>= 7) {
3428 /* Send from the GRF */
3429 fs_reg payload
= fs_reg(GRF
, -1, BRW_REGISTER_TYPE_F
);
3430 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
3431 payload
.reg
= bld
.shader
->alloc
.allocate(load
->regs_written
);
3432 load
->dst
= payload
;
3434 inst
->src
[0] = payload
;
3435 inst
->resize_sources(1);
3436 inst
->base_mrf
= -1;
3438 /* Send from the MRF */
3439 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3440 sources
, length
, payload_header_size
);
3442 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3443 * will do this for us if we just give it a COMPR4 destination.
3445 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
3446 load
->dst
.reg
|= BRW_MRF_COMPR4
;
3448 inst
->resize_sources(0);
3452 inst
->opcode
= FS_OPCODE_FB_WRITE
;
3453 inst
->mlen
= load
->regs_written
;
3454 inst
->header_size
= header_size
;
3458 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3459 const fs_reg
&coordinate
,
3460 const fs_reg
&shadow_c
,
3461 const fs_reg
&lod
, const fs_reg
&lod2
,
3462 const fs_reg
&sampler
,
3463 unsigned coord_components
,
3464 unsigned grad_components
)
3466 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
3467 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
3468 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
3469 fs_reg msg_end
= msg_begin
;
3472 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
3474 for (unsigned i
= 0; i
< coord_components
; i
++)
3475 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
3476 offset(coordinate
, bld
, i
));
3478 msg_end
= offset(msg_end
, bld
, coord_components
);
3480 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3481 * require all three components to be present and zero if they are unused.
3483 if (coord_components
> 0 &&
3484 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
3485 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
3486 for (unsigned i
= coord_components
; i
< 3; i
++)
3487 bld
.MOV(offset(msg_end
, bld
, i
), fs_reg(0.0f
));
3489 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
3492 if (op
== SHADER_OPCODE_TXD
) {
3493 /* TXD unsupported in SIMD16 mode. */
3494 assert(bld
.dispatch_width() == 8);
3496 /* the slots for u and v are always present, but r is optional */
3497 if (coord_components
< 2)
3498 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
3501 * dPdx = dudx, dvdx, drdx
3502 * dPdy = dudy, dvdy, drdy
3504 * 1-arg: Does not exist.
3506 * 2-arg: dudx dvdx dudy dvdy
3507 * dPdx.x dPdx.y dPdy.x dPdy.y
3510 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3511 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3512 * m5 m6 m7 m8 m9 m10
3514 for (unsigned i
= 0; i
< grad_components
; i
++)
3515 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
3517 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3519 for (unsigned i
= 0; i
< grad_components
; i
++)
3520 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
3522 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3526 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3527 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3529 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
3530 bld
.dispatch_width() == 16);
3532 const brw_reg_type type
=
3533 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
3534 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
3535 bld
.MOV(retype(msg_end
, type
), lod
);
3536 msg_end
= offset(msg_end
, bld
, 1);
3539 if (shadow_c
.file
!= BAD_FILE
) {
3540 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
3541 /* There's no plain shadow compare message, so we use shadow
3542 * compare with a bias of 0.0.
3544 bld
.MOV(msg_end
, fs_reg(0.0f
));
3545 msg_end
= offset(msg_end
, bld
, 1);
3548 bld
.MOV(msg_end
, shadow_c
);
3549 msg_end
= offset(msg_end
, bld
, 1);
3553 inst
->src
[0] = reg_undef
;
3554 inst
->src
[1] = sampler
;
3555 inst
->resize_sources(2);
3556 inst
->base_mrf
= msg_begin
.reg
;
3557 inst
->mlen
= msg_end
.reg
- msg_begin
.reg
;
3558 inst
->header_size
= 1;
3562 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3564 const fs_reg
&shadow_c
,
3565 fs_reg lod
, fs_reg lod2
,
3566 const fs_reg
&sample_index
,
3567 const fs_reg
&sampler
,
3568 const fs_reg
&offset_value
,
3569 unsigned coord_components
,
3570 unsigned grad_components
)
3572 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
3573 fs_reg msg_coords
= message
;
3574 unsigned header_size
= 0;
3576 if (offset_value
.file
!= BAD_FILE
) {
3577 /* The offsets set up by the visitor are in the m1 header, so we can't
3584 for (unsigned i
= 0; i
< coord_components
; i
++) {
3585 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
), coordinate
);
3586 coordinate
= offset(coordinate
, bld
, 1);
3588 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
3589 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
3591 if (shadow_c
.file
!= BAD_FILE
) {
3592 fs_reg msg_shadow
= msg_lod
;
3593 bld
.MOV(msg_shadow
, shadow_c
);
3594 msg_lod
= offset(msg_shadow
, bld
, 1);
3599 case SHADER_OPCODE_TXL
:
3601 bld
.MOV(msg_lod
, lod
);
3602 msg_end
= offset(msg_lod
, bld
, 1);
3604 case SHADER_OPCODE_TXD
:
3607 * dPdx = dudx, dvdx, drdx
3608 * dPdy = dudy, dvdy, drdy
3610 * Load up these values:
3611 * - dudx dudy dvdx dvdy drdx drdy
3612 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3615 for (unsigned i
= 0; i
< grad_components
; i
++) {
3616 bld
.MOV(msg_end
, lod
);
3617 lod
= offset(lod
, bld
, 1);
3618 msg_end
= offset(msg_end
, bld
, 1);
3620 bld
.MOV(msg_end
, lod2
);
3621 lod2
= offset(lod2
, bld
, 1);
3622 msg_end
= offset(msg_end
, bld
, 1);
3625 case SHADER_OPCODE_TXS
:
3626 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
3627 bld
.MOV(msg_lod
, lod
);
3628 msg_end
= offset(msg_lod
, bld
, 1);
3630 case SHADER_OPCODE_TXF
:
3631 msg_lod
= offset(msg_coords
, bld
, 3);
3632 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
3633 msg_end
= offset(msg_lod
, bld
, 1);
3635 case SHADER_OPCODE_TXF_CMS
:
3636 msg_lod
= offset(msg_coords
, bld
, 3);
3638 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u));
3640 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
3641 msg_end
= offset(msg_lod
, bld
, 2);
3648 inst
->src
[0] = reg_undef
;
3649 inst
->src
[1] = sampler
;
3650 inst
->resize_sources(2);
3651 inst
->base_mrf
= message
.reg
;
3652 inst
->mlen
= msg_end
.reg
- message
.reg
;
3653 inst
->header_size
= header_size
;
3655 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3656 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3660 is_high_sampler(const struct brw_device_info
*devinfo
, const fs_reg
&sampler
)
3662 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
3665 return sampler
.file
!= IMM
|| sampler
.fixed_hw_reg
.dw1
.ud
>= 16;
3669 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3671 const fs_reg
&shadow_c
,
3672 fs_reg lod
, fs_reg lod2
,
3673 const fs_reg
&sample_index
,
3674 const fs_reg
&mcs
, const fs_reg
&sampler
,
3675 fs_reg offset_value
,
3676 unsigned coord_components
,
3677 unsigned grad_components
)
3679 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3680 int reg_width
= bld
.dispatch_width() / 8;
3681 unsigned header_size
= 0, length
= 0;
3682 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
3683 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
3684 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
3686 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
3687 offset_value
.file
!= BAD_FILE
||
3688 is_high_sampler(devinfo
, sampler
)) {
3689 /* For general texture offsets (no txf workaround), we need a header to
3690 * put them in. Note that we're only reserving space for it in the
3691 * message payload as it will be initialized implicitly by the
3694 * TG4 needs to place its channel select in the header, for interaction
3695 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3696 * larger sampler numbers we need to offset the Sampler State Pointer in
3700 sources
[0] = fs_reg();
3704 if (shadow_c
.file
!= BAD_FILE
) {
3705 bld
.MOV(sources
[length
], shadow_c
);
3709 bool coordinate_done
= false;
3711 /* The sampler can only meaningfully compute LOD for fragment shader
3712 * messages. For all other stages, we change the opcode to TXL and
3713 * hardcode the LOD to 0.
3715 if (bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
&&
3716 op
== SHADER_OPCODE_TEX
) {
3717 op
= SHADER_OPCODE_TXL
;
3721 /* Set up the LOD info */
3724 case SHADER_OPCODE_TXL
:
3725 bld
.MOV(sources
[length
], lod
);
3728 case SHADER_OPCODE_TXD
:
3729 /* TXD should have been lowered in SIMD16 mode. */
3730 assert(bld
.dispatch_width() == 8);
3732 /* Load dPdx and the coordinate together:
3733 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
3735 for (unsigned i
= 0; i
< coord_components
; i
++) {
3736 bld
.MOV(sources
[length
], coordinate
);
3737 coordinate
= offset(coordinate
, bld
, 1);
3740 /* For cube map array, the coordinate is (u,v,r,ai) but there are
3741 * only derivatives for (u, v, r).
3743 if (i
< grad_components
) {
3744 bld
.MOV(sources
[length
], lod
);
3745 lod
= offset(lod
, bld
, 1);
3748 bld
.MOV(sources
[length
], lod2
);
3749 lod2
= offset(lod2
, bld
, 1);
3754 coordinate_done
= true;
3756 case SHADER_OPCODE_TXS
:
3757 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
3760 case SHADER_OPCODE_TXF
:
3761 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
3762 * On Gen9 they are u, v, lod, r
3764 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3765 coordinate
= offset(coordinate
, bld
, 1);
3768 if (devinfo
->gen
>= 9) {
3769 if (coord_components
>= 2) {
3770 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3771 coordinate
= offset(coordinate
, bld
, 1);
3776 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
3779 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++) {
3780 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3781 coordinate
= offset(coordinate
, bld
, 1);
3785 coordinate_done
= true;
3787 case SHADER_OPCODE_TXF_CMS
:
3788 case SHADER_OPCODE_TXF_UMS
:
3789 case SHADER_OPCODE_TXF_MCS
:
3790 if (op
== SHADER_OPCODE_TXF_UMS
|| op
== SHADER_OPCODE_TXF_CMS
) {
3791 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
3795 if (op
== SHADER_OPCODE_TXF_CMS
) {
3796 /* Data from the multisample control surface. */
3797 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
3801 /* There is no offsetting for this message; just copy in the integer
3802 * texture coordinates.
3804 for (unsigned i
= 0; i
< coord_components
; i
++) {
3805 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3806 coordinate
= offset(coordinate
, bld
, 1);
3810 coordinate_done
= true;
3812 case SHADER_OPCODE_TG4_OFFSET
:
3813 /* gather4_po_c should have been lowered in SIMD16 mode. */
3814 assert(bld
.dispatch_width() == 8 || shadow_c
.file
== BAD_FILE
);
3816 /* More crazy intermixing */
3817 for (unsigned i
= 0; i
< 2; i
++) { /* u, v */
3818 bld
.MOV(sources
[length
], coordinate
);
3819 coordinate
= offset(coordinate
, bld
, 1);
3823 for (unsigned i
= 0; i
< 2; i
++) { /* offu, offv */
3824 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
);
3825 offset_value
= offset(offset_value
, bld
, 1);
3829 if (coord_components
== 3) { /* r if present */
3830 bld
.MOV(sources
[length
], coordinate
);
3831 coordinate
= offset(coordinate
, bld
, 1);
3835 coordinate_done
= true;
3841 /* Set up the coordinate (except for cases where it was done above) */
3842 if (!coordinate_done
) {
3843 for (unsigned i
= 0; i
< coord_components
; i
++) {
3844 bld
.MOV(sources
[length
], coordinate
);
3845 coordinate
= offset(coordinate
, bld
, 1);
3852 mlen
= length
* reg_width
- header_size
;
3854 mlen
= length
* reg_width
;
3856 const fs_reg src_payload
= fs_reg(GRF
, bld
.shader
->alloc
.allocate(mlen
),
3857 BRW_REGISTER_TYPE_F
);
3858 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
3860 /* Generate the SEND. */
3862 inst
->src
[0] = src_payload
;
3863 inst
->src
[1] = sampler
;
3864 inst
->resize_sources(2);
3865 inst
->base_mrf
= -1;
3867 inst
->header_size
= header_size
;
3869 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3870 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3874 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
3876 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3877 const fs_reg
&coordinate
= inst
->src
[0];
3878 const fs_reg
&shadow_c
= inst
->src
[1];
3879 const fs_reg
&lod
= inst
->src
[2];
3880 const fs_reg
&lod2
= inst
->src
[3];
3881 const fs_reg
&sample_index
= inst
->src
[4];
3882 const fs_reg
&mcs
= inst
->src
[5];
3883 const fs_reg
&sampler
= inst
->src
[6];
3884 const fs_reg
&offset_value
= inst
->src
[7];
3885 assert(inst
->src
[8].file
== IMM
&& inst
->src
[9].file
== IMM
);
3886 const unsigned coord_components
= inst
->src
[8].fixed_hw_reg
.dw1
.ud
;
3887 const unsigned grad_components
= inst
->src
[9].fixed_hw_reg
.dw1
.ud
;
3889 if (devinfo
->gen
>= 7) {
3890 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
3891 shadow_c
, lod
, lod2
, sample_index
,
3892 mcs
, sampler
, offset_value
,
3893 coord_components
, grad_components
);
3894 } else if (devinfo
->gen
>= 5) {
3895 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
3896 shadow_c
, lod
, lod2
, sample_index
,
3897 sampler
, offset_value
,
3898 coord_components
, grad_components
);
3900 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
3901 shadow_c
, lod
, lod2
, sampler
,
3902 coord_components
, grad_components
);
3907 * Initialize the header present in some typed and untyped surface
3911 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
3913 fs_builder ubld
= bld
.exec_all().group(8, 0);
3914 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
3915 ubld
.MOV(dst
, fs_reg(0));
3916 ubld
.MOV(component(dst
, 7), sample_mask
);
3921 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3922 const fs_reg
&sample_mask
)
3924 /* Get the logical send arguments. */
3925 const fs_reg
&addr
= inst
->src
[0];
3926 const fs_reg
&src
= inst
->src
[1];
3927 const fs_reg
&surface
= inst
->src
[2];
3928 const UNUSED fs_reg
&dims
= inst
->src
[3];
3929 const fs_reg
&arg
= inst
->src
[4];
3931 /* Calculate the total number of components of the payload. */
3932 const unsigned addr_sz
= inst
->components_read(0);
3933 const unsigned src_sz
= inst
->components_read(1);
3934 const unsigned header_sz
= (sample_mask
.file
== BAD_FILE
? 0 : 1);
3935 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
3937 /* Allocate space for the payload. */
3938 fs_reg
*const components
= new fs_reg
[sz
];
3939 const fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
3942 /* Construct the payload. */
3944 components
[n
++] = emit_surface_header(bld
, sample_mask
);
3946 for (unsigned i
= 0; i
< addr_sz
; i
++)
3947 components
[n
++] = offset(addr
, bld
, i
);
3949 for (unsigned i
= 0; i
< src_sz
; i
++)
3950 components
[n
++] = offset(src
, bld
, i
);
3952 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
3954 /* Update the original instruction. */
3956 inst
->mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
3957 inst
->header_size
= header_sz
;
3959 inst
->src
[0] = payload
;
3960 inst
->src
[1] = surface
;
3962 inst
->resize_sources(3);
3964 delete[] components
;
3968 fs_visitor::lower_logical_sends()
3970 bool progress
= false;
3972 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3973 const fs_builder
ibld(this, block
, inst
);
3975 switch (inst
->opcode
) {
3976 case FS_OPCODE_FB_WRITE_LOGICAL
:
3977 assert(stage
== MESA_SHADER_FRAGMENT
);
3978 lower_fb_write_logical_send(ibld
, inst
,
3979 (const brw_wm_prog_data
*)prog_data
,
3980 (const brw_wm_prog_key
*)key
,
3984 case SHADER_OPCODE_TEX_LOGICAL
:
3985 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
3988 case SHADER_OPCODE_TXD_LOGICAL
:
3989 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
3992 case SHADER_OPCODE_TXF_LOGICAL
:
3993 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
3996 case SHADER_OPCODE_TXL_LOGICAL
:
3997 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
4000 case SHADER_OPCODE_TXS_LOGICAL
:
4001 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
4004 case FS_OPCODE_TXB_LOGICAL
:
4005 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
4008 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
4009 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
4012 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
4013 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
4016 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
4017 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
4020 case SHADER_OPCODE_LOD_LOGICAL
:
4021 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
4024 case SHADER_OPCODE_TG4_LOGICAL
:
4025 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
4028 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
4029 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
4032 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
4033 lower_surface_logical_send(ibld
, inst
,
4034 SHADER_OPCODE_UNTYPED_SURFACE_READ
,
4038 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
4039 lower_surface_logical_send(ibld
, inst
,
4040 SHADER_OPCODE_UNTYPED_SURFACE_WRITE
,
4041 ibld
.sample_mask_reg());
4044 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
4045 lower_surface_logical_send(ibld
, inst
,
4046 SHADER_OPCODE_UNTYPED_ATOMIC
,
4047 ibld
.sample_mask_reg());
4050 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4051 lower_surface_logical_send(ibld
, inst
,
4052 SHADER_OPCODE_TYPED_SURFACE_READ
,
4056 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4057 lower_surface_logical_send(ibld
, inst
,
4058 SHADER_OPCODE_TYPED_SURFACE_WRITE
,
4059 ibld
.sample_mask_reg());
4062 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4063 lower_surface_logical_send(ibld
, inst
,
4064 SHADER_OPCODE_TYPED_ATOMIC
,
4065 ibld
.sample_mask_reg());
4076 invalidate_live_intervals();
4082 * Get the closest native SIMD width supported by the hardware for instruction
4083 * \p inst. The instruction will be left untouched by
4084 * fs_visitor::lower_simd_width() if the returned value is equal to the
4085 * original execution size.
4088 get_lowered_simd_width(const struct brw_device_info
*devinfo
,
4089 const fs_inst
*inst
)
4091 switch (inst
->opcode
) {
4092 case BRW_OPCODE_MOV
:
4093 case BRW_OPCODE_SEL
:
4094 case BRW_OPCODE_NOT
:
4095 case BRW_OPCODE_AND
:
4097 case BRW_OPCODE_XOR
:
4098 case BRW_OPCODE_SHR
:
4099 case BRW_OPCODE_SHL
:
4100 case BRW_OPCODE_ASR
:
4101 case BRW_OPCODE_CMP
:
4102 case BRW_OPCODE_CMPN
:
4103 case BRW_OPCODE_CSEL
:
4104 case BRW_OPCODE_F32TO16
:
4105 case BRW_OPCODE_F16TO32
:
4106 case BRW_OPCODE_BFREV
:
4107 case BRW_OPCODE_BFE
:
4108 case BRW_OPCODE_BFI1
:
4109 case BRW_OPCODE_BFI2
:
4110 case BRW_OPCODE_ADD
:
4111 case BRW_OPCODE_MUL
:
4112 case BRW_OPCODE_AVG
:
4113 case BRW_OPCODE_FRC
:
4114 case BRW_OPCODE_RNDU
:
4115 case BRW_OPCODE_RNDD
:
4116 case BRW_OPCODE_RNDE
:
4117 case BRW_OPCODE_RNDZ
:
4118 case BRW_OPCODE_LZD
:
4119 case BRW_OPCODE_FBH
:
4120 case BRW_OPCODE_FBL
:
4121 case BRW_OPCODE_CBIT
:
4122 case BRW_OPCODE_SAD2
:
4123 case BRW_OPCODE_MAD
:
4124 case BRW_OPCODE_LRP
:
4125 case SHADER_OPCODE_RCP
:
4126 case SHADER_OPCODE_RSQ
:
4127 case SHADER_OPCODE_SQRT
:
4128 case SHADER_OPCODE_EXP2
:
4129 case SHADER_OPCODE_LOG2
:
4130 case SHADER_OPCODE_POW
:
4131 case SHADER_OPCODE_INT_QUOTIENT
:
4132 case SHADER_OPCODE_INT_REMAINDER
:
4133 case SHADER_OPCODE_SIN
:
4134 case SHADER_OPCODE_COS
: {
4135 /* According to the PRMs:
4136 * "A. In Direct Addressing mode, a source cannot span more than 2
4137 * adjacent GRF registers.
4138 * B. A destination cannot span more than 2 adjacent GRF registers."
4140 * Look for the source or destination with the largest register region
4141 * which is the one that is going to limit the overal execution size of
4142 * the instruction due to this rule.
4144 unsigned reg_count
= inst
->regs_written
;
4146 for (unsigned i
= 0; i
< inst
->sources
; i
++)
4147 reg_count
= MAX2(reg_count
, (unsigned)inst
->regs_read(i
));
4149 /* Calculate the maximum execution size of the instruction based on the
4150 * factor by which it goes over the hardware limit of 2 GRFs.
4152 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
4154 case SHADER_OPCODE_MULH
:
4155 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
4156 * is 8-wide on Gen7+.
4158 return (devinfo
->gen
>= 7 ? 8 : inst
->exec_size
);
4160 case FS_OPCODE_FB_WRITE_LOGICAL
:
4161 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4164 assert(devinfo
->gen
!= 6 || inst
->src
[3].file
== BAD_FILE
||
4165 inst
->exec_size
== 8);
4166 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4167 return (inst
->src
[1].file
!= BAD_FILE
? 8 : inst
->exec_size
);
4169 case SHADER_OPCODE_TXD_LOGICAL
:
4170 /* TXD is unsupported in SIMD16 mode. */
4173 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
: {
4174 /* gather4_po_c is unsupported in SIMD16 mode. */
4175 const fs_reg
&shadow_c
= inst
->src
[1];
4176 return (shadow_c
.file
!= BAD_FILE
? 8 : inst
->exec_size
);
4178 case SHADER_OPCODE_TXL_LOGICAL
:
4179 case FS_OPCODE_TXB_LOGICAL
: {
4180 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4181 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4182 * mode because the message exceeds the maximum length of 11.
4184 const fs_reg
&shadow_c
= inst
->src
[1];
4185 if (devinfo
->gen
== 4 && shadow_c
.file
== BAD_FILE
)
4187 else if (devinfo
->gen
< 7 && shadow_c
.file
!= BAD_FILE
)
4190 return inst
->exec_size
;
4192 case SHADER_OPCODE_TXF_LOGICAL
:
4193 case SHADER_OPCODE_TXS_LOGICAL
:
4194 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4195 * messages. Use SIMD16 instead.
4197 if (devinfo
->gen
== 4)
4200 return inst
->exec_size
;
4202 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4203 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4204 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4208 return inst
->exec_size
;
4213 * The \p rows array of registers represents a \p num_rows by \p num_columns
4214 * matrix in row-major order, write it in column-major order into the register
4215 * passed as destination. \p stride gives the separation between matrix
4216 * elements in the input in fs_builder::dispatch_width() units.
4219 emit_transpose(const fs_builder
&bld
,
4220 const fs_reg
&dst
, const fs_reg
*rows
,
4221 unsigned num_rows
, unsigned num_columns
, unsigned stride
)
4223 fs_reg
*const components
= new fs_reg
[num_rows
* num_columns
];
4225 for (unsigned i
= 0; i
< num_columns
; ++i
) {
4226 for (unsigned j
= 0; j
< num_rows
; ++j
)
4227 components
[num_rows
* i
+ j
] = offset(rows
[j
], bld
, stride
* i
);
4230 bld
.LOAD_PAYLOAD(dst
, components
, num_rows
* num_columns
, 0);
4232 delete[] components
;
4236 fs_visitor::lower_simd_width()
4238 bool progress
= false;
4240 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4241 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
4243 if (lower_width
!= inst
->exec_size
) {
4244 /* Builder matching the original instruction. We may also need to
4245 * emit an instruction of width larger than the original, set the
4246 * execution size of the builder to the highest of both for now so
4247 * we're sure that both cases can be handled.
4249 const fs_builder ibld
= bld
.at(block
, inst
)
4250 .exec_all(inst
->force_writemask_all
)
4251 .group(MAX2(inst
->exec_size
, lower_width
),
4252 inst
->force_sechalf
);
4254 /* Split the copies in chunks of the execution width of either the
4255 * original or the lowered instruction, whichever is lower.
4257 const unsigned copy_width
= MIN2(lower_width
, inst
->exec_size
);
4258 const unsigned n
= inst
->exec_size
/ copy_width
;
4259 const unsigned dst_size
= inst
->regs_written
* REG_SIZE
/
4260 inst
->dst
.component_size(inst
->exec_size
);
4263 assert(n
> 0 && n
<= ARRAY_SIZE(dsts
) &&
4264 !inst
->writes_accumulator
&& !inst
->mlen
);
4266 for (unsigned i
= 0; i
< n
; i
++) {
4267 /* Emit a copy of the original instruction with the lowered width.
4268 * If the EOT flag was set throw it away except for the last
4269 * instruction to avoid killing the thread prematurely.
4271 fs_inst split_inst
= *inst
;
4272 split_inst
.exec_size
= lower_width
;
4273 split_inst
.eot
= inst
->eot
&& i
== n
- 1;
4275 /* Select the correct channel enables for the i-th group, then
4276 * transform the sources and destination and emit the lowered
4279 const fs_builder lbld
= ibld
.group(lower_width
, i
);
4281 for (unsigned j
= 0; j
< inst
->sources
; j
++) {
4282 if (inst
->src
[j
].file
!= BAD_FILE
&&
4283 !is_uniform(inst
->src
[j
])) {
4284 /* Get the i-th copy_width-wide chunk of the source. */
4285 const fs_reg src
= horiz_offset(inst
->src
[j
], copy_width
* i
);
4286 const unsigned src_size
= inst
->components_read(j
);
4288 /* Use a trivial transposition to copy one every n
4289 * copy_width-wide components of the register into a
4290 * temporary passed as source to the lowered instruction.
4292 split_inst
.src
[j
] = lbld
.vgrf(inst
->src
[j
].type
, src_size
);
4293 emit_transpose(lbld
.group(copy_width
, 0),
4294 split_inst
.src
[j
], &src
, 1, src_size
, n
);
4298 if (inst
->regs_written
) {
4299 /* Allocate enough space to hold the result of the lowered
4300 * instruction and fix up the number of registers written.
4302 split_inst
.dst
= dsts
[i
] =
4303 lbld
.vgrf(inst
->dst
.type
, dst_size
);
4304 split_inst
.regs_written
=
4305 DIV_ROUND_UP(inst
->regs_written
* lower_width
,
4309 lbld
.emit(split_inst
);
4312 if (inst
->regs_written
) {
4313 /* Distance between useful channels in the temporaries, skipping
4314 * garbage if the lowered instruction is wider than the original.
4316 const unsigned m
= lower_width
/ copy_width
;
4318 /* Interleave the components of the result from the lowered
4319 * instructions. We need to set exec_all() when copying more than
4320 * one half per component, because LOAD_PAYLOAD (in terms of which
4321 * emit_transpose is implemented) can only use the same channel
4322 * enable signals for all of its non-header sources.
4324 emit_transpose(ibld
.exec_all(inst
->exec_size
> copy_width
)
4325 .group(copy_width
, 0),
4326 inst
->dst
, dsts
, n
, dst_size
, m
);
4329 inst
->remove(block
);
4335 invalidate_live_intervals();
4341 fs_visitor::dump_instructions()
4343 dump_instructions(NULL
);
4347 fs_visitor::dump_instructions(const char *name
)
4349 FILE *file
= stderr
;
4350 if (name
&& geteuid() != 0) {
4351 file
= fopen(name
, "w");
4357 calculate_register_pressure();
4358 int ip
= 0, max_pressure
= 0;
4359 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
4360 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
4361 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
4362 dump_instruction(inst
, file
);
4365 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
4368 foreach_in_list(backend_instruction
, inst
, &instructions
) {
4369 fprintf(file
, "%4d: ", ip
++);
4370 dump_instruction(inst
, file
);
4374 if (file
!= stderr
) {
4380 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
4382 dump_instruction(be_inst
, stderr
);
4386 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
4388 fs_inst
*inst
= (fs_inst
*)be_inst
;
4390 if (inst
->predicate
) {
4391 fprintf(file
, "(%cf0.%d) ",
4392 inst
->predicate_inverse
? '-' : '+',
4396 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
4398 fprintf(file
, ".sat");
4399 if (inst
->conditional_mod
) {
4400 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
4401 if (!inst
->predicate
&&
4402 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
4403 inst
->opcode
!= BRW_OPCODE_IF
&&
4404 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
4405 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
4408 fprintf(file
, "(%d) ", inst
->exec_size
);
4411 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
4414 switch (inst
->dst
.file
) {
4416 fprintf(file
, "vgrf%d", inst
->dst
.reg
);
4417 if (alloc
.sizes
[inst
->dst
.reg
] != inst
->regs_written
||
4418 inst
->dst
.subreg_offset
)
4419 fprintf(file
, "+%d.%d",
4420 inst
->dst
.reg_offset
, inst
->dst
.subreg_offset
);
4423 fprintf(file
, "m%d", inst
->dst
.reg
);
4426 fprintf(file
, "(null)");
4429 fprintf(file
, "***u%d***", inst
->dst
.reg
+ inst
->dst
.reg_offset
);
4432 fprintf(file
, "***attr%d***", inst
->dst
.reg
+ inst
->dst
.reg_offset
);
4435 if (inst
->dst
.fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
4436 switch (inst
->dst
.fixed_hw_reg
.nr
) {
4438 fprintf(file
, "null");
4440 case BRW_ARF_ADDRESS
:
4441 fprintf(file
, "a0.%d", inst
->dst
.fixed_hw_reg
.subnr
);
4443 case BRW_ARF_ACCUMULATOR
:
4444 fprintf(file
, "acc%d", inst
->dst
.fixed_hw_reg
.subnr
);
4447 fprintf(file
, "f%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
4448 inst
->dst
.fixed_hw_reg
.subnr
);
4451 fprintf(file
, "arf%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
4452 inst
->dst
.fixed_hw_reg
.subnr
);
4456 fprintf(file
, "hw_reg%d", inst
->dst
.fixed_hw_reg
.nr
);
4458 if (inst
->dst
.fixed_hw_reg
.subnr
)
4459 fprintf(file
, "+%d", inst
->dst
.fixed_hw_reg
.subnr
);
4462 fprintf(file
, "???");
4465 fprintf(file
, ":%s, ", brw_reg_type_letters(inst
->dst
.type
));
4467 for (int i
= 0; i
< inst
->sources
; i
++) {
4468 if (inst
->src
[i
].negate
)
4470 if (inst
->src
[i
].abs
)
4472 switch (inst
->src
[i
].file
) {
4474 fprintf(file
, "vgrf%d", inst
->src
[i
].reg
);
4475 if (alloc
.sizes
[inst
->src
[i
].reg
] != (unsigned)inst
->regs_read(i
) ||
4476 inst
->src
[i
].subreg_offset
)
4477 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4478 inst
->src
[i
].subreg_offset
);
4481 fprintf(file
, "***m%d***", inst
->src
[i
].reg
);
4484 fprintf(file
, "attr%d+%d", inst
->src
[i
].reg
, inst
->src
[i
].reg_offset
);
4487 fprintf(file
, "u%d", inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
);
4488 if (inst
->src
[i
].reladdr
) {
4489 fprintf(file
, "+reladdr");
4490 } else if (inst
->src
[i
].subreg_offset
) {
4491 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4492 inst
->src
[i
].subreg_offset
);
4496 fprintf(file
, "(null)");
4499 switch (inst
->src
[i
].type
) {
4500 case BRW_REGISTER_TYPE_F
:
4501 fprintf(file
, "%ff", inst
->src
[i
].fixed_hw_reg
.dw1
.f
);
4503 case BRW_REGISTER_TYPE_W
:
4504 case BRW_REGISTER_TYPE_D
:
4505 fprintf(file
, "%dd", inst
->src
[i
].fixed_hw_reg
.dw1
.d
);
4507 case BRW_REGISTER_TYPE_UW
:
4508 case BRW_REGISTER_TYPE_UD
:
4509 fprintf(file
, "%uu", inst
->src
[i
].fixed_hw_reg
.dw1
.ud
);
4511 case BRW_REGISTER_TYPE_VF
:
4512 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
4513 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 0) & 0xff),
4514 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 8) & 0xff),
4515 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 16) & 0xff),
4516 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 24) & 0xff));
4519 fprintf(file
, "???");
4524 if (inst
->src
[i
].fixed_hw_reg
.negate
)
4526 if (inst
->src
[i
].fixed_hw_reg
.abs
)
4528 if (inst
->src
[i
].fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
4529 switch (inst
->src
[i
].fixed_hw_reg
.nr
) {
4531 fprintf(file
, "null");
4533 case BRW_ARF_ADDRESS
:
4534 fprintf(file
, "a0.%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
4536 case BRW_ARF_ACCUMULATOR
:
4537 fprintf(file
, "acc%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
4540 fprintf(file
, "f%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
4541 inst
->src
[i
].fixed_hw_reg
.subnr
);
4544 fprintf(file
, "arf%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
4545 inst
->src
[i
].fixed_hw_reg
.subnr
);
4549 fprintf(file
, "hw_reg%d", inst
->src
[i
].fixed_hw_reg
.nr
);
4551 if (inst
->src
[i
].fixed_hw_reg
.subnr
)
4552 fprintf(file
, "+%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
4553 if (inst
->src
[i
].fixed_hw_reg
.abs
)
4557 fprintf(file
, "???");
4560 if (inst
->src
[i
].abs
)
4563 if (inst
->src
[i
].file
!= IMM
) {
4564 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
4567 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
4568 fprintf(file
, ", ");
4573 if (dispatch_width
== 16 && inst
->exec_size
== 8) {
4574 if (inst
->force_sechalf
)
4575 fprintf(file
, "2ndhalf ");
4577 fprintf(file
, "1sthalf ");
4580 fprintf(file
, "\n");
4584 * Possibly returns an instruction that set up @param reg.
4586 * Sometimes we want to take the result of some expression/variable
4587 * dereference tree and rewrite the instruction generating the result
4588 * of the tree. When processing the tree, we know that the
4589 * instructions generated are all writing temporaries that are dead
4590 * outside of this tree. So, if we have some instructions that write
4591 * a temporary, we're free to point that temp write somewhere else.
4593 * Note that this doesn't guarantee that the instruction generated
4594 * only reg -- it might be the size=4 destination of a texture instruction.
4597 fs_visitor::get_instruction_generating_reg(fs_inst
*start
,
4602 end
->is_partial_write() ||
4604 !reg
.equals(end
->dst
)) {
4612 fs_visitor::setup_payload_gen6()
4615 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
4616 unsigned barycentric_interp_modes
=
4617 (stage
== MESA_SHADER_FRAGMENT
) ?
4618 ((brw_wm_prog_data
*) this->prog_data
)->barycentric_interp_modes
: 0;
4620 assert(devinfo
->gen
>= 6);
4622 /* R0-1: masks, pixel X/Y coordinates. */
4623 payload
.num_regs
= 2;
4624 /* R2: only for 32-pixel dispatch.*/
4626 /* R3-26: barycentric interpolation coordinates. These appear in the
4627 * same order that they appear in the brw_wm_barycentric_interp_mode
4628 * enum. Each set of coordinates occupies 2 registers if dispatch width
4629 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4630 * appear if they were enabled using the "Barycentric Interpolation
4631 * Mode" bits in WM_STATE.
4633 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
4634 if (barycentric_interp_modes
& (1 << i
)) {
4635 payload
.barycentric_coord_reg
[i
] = payload
.num_regs
;
4636 payload
.num_regs
+= 2;
4637 if (dispatch_width
== 16) {
4638 payload
.num_regs
+= 2;
4643 /* R27: interpolated depth if uses source depth */
4645 payload
.source_depth_reg
= payload
.num_regs
;
4647 if (dispatch_width
== 16) {
4648 /* R28: interpolated depth if not SIMD8. */
4652 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4654 payload
.source_w_reg
= payload
.num_regs
;
4656 if (dispatch_width
== 16) {
4657 /* R30: interpolated W if not SIMD8. */
4662 if (stage
== MESA_SHADER_FRAGMENT
) {
4663 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4664 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
4665 prog_data
->uses_pos_offset
= key
->compute_pos_offset
;
4666 /* R31: MSAA position offsets. */
4667 if (prog_data
->uses_pos_offset
) {
4668 payload
.sample_pos_reg
= payload
.num_regs
;
4673 /* R32: MSAA input coverage mask */
4674 if (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
) {
4675 assert(devinfo
->gen
>= 7);
4676 payload
.sample_mask_in_reg
= payload
.num_regs
;
4678 if (dispatch_width
== 16) {
4679 /* R33: input coverage mask if not SIMD8. */
4684 /* R34-: bary for 32-pixel. */
4685 /* R58-59: interp W for 32-pixel. */
4687 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
4688 source_depth_to_render_target
= true;
4693 fs_visitor::setup_vs_payload()
4695 /* R0: thread header, R1: urb handles */
4696 payload
.num_regs
= 2;
4700 * We are building the local ID push constant data using the simplest possible
4701 * method. We simply push the local IDs directly as they should appear in the
4702 * registers for the uvec3 gl_LocalInvocationID variable.
4704 * Therefore, for SIMD8, we use 3 full registers, and for SIMD16 we use 6
4705 * registers worth of push constant space.
4707 * Note: Any updates to brw_cs_prog_local_id_payload_dwords,
4708 * fill_local_id_payload or fs_visitor::emit_cs_local_invocation_id_setup need
4711 * FINISHME: There are a few easy optimizations to consider.
4713 * 1. If gl_WorkGroupSize x, y or z is 1, we can just use zero, and there is
4714 * no need for using push constant space for that dimension.
4716 * 2. Since GL_MAX_COMPUTE_WORK_GROUP_SIZE is currently 1024 or less, we can
4717 * easily use 16-bit words rather than 32-bit dwords in the push constant
4720 * 3. If gl_WorkGroupSize x, y or z is small, then we can use bytes for
4721 * conveying the data, and thereby reduce push constant usage.
4725 fs_visitor::setup_cs_payload()
4727 assert(devinfo
->gen
>= 7);
4728 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
4730 payload
.num_regs
= 1;
4732 if (nir
->info
.system_values_read
& SYSTEM_BIT_LOCAL_INVOCATION_ID
) {
4733 prog_data
->local_invocation_id_regs
= dispatch_width
* 3 / 8;
4734 payload
.local_invocation_id_reg
= payload
.num_regs
;
4735 payload
.num_regs
+= prog_data
->local_invocation_id_regs
;
4740 fs_visitor::calculate_register_pressure()
4742 invalidate_live_intervals();
4743 calculate_live_intervals();
4745 unsigned num_instructions
= 0;
4746 foreach_block(block
, cfg
)
4747 num_instructions
+= block
->instructions
.length();
4749 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
4751 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
4752 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
4753 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
4758 fs_visitor::optimize()
4760 /* Start by validating the shader we currently have. */
4763 /* bld is the common builder object pointing at the end of the program we
4764 * used to translate it into i965 IR. For the optimization and lowering
4765 * passes coming next, any code added after the end of the program without
4766 * having explicitly called fs_builder::at() clearly points at a mistake.
4767 * Ideally optimization passes wouldn't be part of the visitor so they
4768 * wouldn't have access to bld at all, but they do, so just in case some
4769 * pass forgets to ask for a location explicitly set it to NULL here to
4770 * make it trip. The dispatch width is initialized to a bogus value to
4771 * make sure that optimizations set the execution controls explicitly to
4772 * match the code they are manipulating instead of relying on the defaults.
4774 bld
= fs_builder(this, 64);
4776 assign_constant_locations();
4777 demote_pull_constants();
4781 split_virtual_grfs();
4784 #define OPT(pass, args...) ({ \
4786 bool this_progress = pass(args); \
4788 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
4789 char filename[64]; \
4790 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
4791 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
4793 backend_shader::dump_instructions(filename); \
4798 progress = progress || this_progress; \
4802 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
4804 snprintf(filename
, 64, "%s%d-%s-00-start",
4805 stage_abbrev
, dispatch_width
, nir
->info
.name
);
4807 backend_shader::dump_instructions(filename
);
4810 bool progress
= false;
4814 OPT(lower_simd_width
);
4815 OPT(lower_logical_sends
);
4822 OPT(remove_duplicate_mrf_writes
);
4826 OPT(opt_copy_propagate
);
4827 OPT(opt_predicated_break
, this);
4828 OPT(opt_cmod_propagation
);
4829 OPT(dead_code_eliminate
);
4830 OPT(opt_peephole_sel
);
4831 OPT(dead_control_flow_eliminate
, this);
4832 OPT(opt_register_renaming
);
4833 OPT(opt_redundant_discard_jumps
);
4834 OPT(opt_saturate_propagation
);
4835 OPT(opt_zero_samples
);
4836 OPT(register_coalesce
);
4837 OPT(compute_to_mrf
);
4838 OPT(eliminate_find_live_channel
);
4840 OPT(compact_virtual_grfs
);
4845 OPT(opt_sampler_eot
);
4847 if (OPT(lower_load_payload
)) {
4848 split_virtual_grfs();
4849 OPT(register_coalesce
);
4850 OPT(compute_to_mrf
);
4851 OPT(dead_code_eliminate
);
4854 OPT(opt_combine_constants
);
4855 OPT(lower_integer_multiplication
);
4857 lower_uniform_pull_constant_loads();
4863 * Three source instruction must have a GRF/MRF destination register.
4864 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
4867 fs_visitor::fixup_3src_null_dest()
4869 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
4870 if (inst
->is_3src() && inst
->dst
.is_null()) {
4871 inst
->dst
= fs_reg(GRF
, alloc
.allocate(dispatch_width
/ 8),
4878 fs_visitor::allocate_registers()
4880 bool allocated_without_spills
;
4882 static const enum instruction_scheduler_mode pre_modes
[] = {
4884 SCHEDULE_PRE_NON_LIFO
,
4888 /* Try each scheduling heuristic to see if it can successfully register
4889 * allocate without spilling. They should be ordered by decreasing
4890 * performance but increasing likelihood of allocating.
4892 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
4893 schedule_instructions(pre_modes
[i
]);
4896 assign_regs_trivial();
4897 allocated_without_spills
= true;
4899 allocated_without_spills
= assign_regs(false);
4901 if (allocated_without_spills
)
4905 if (!allocated_without_spills
) {
4906 /* We assume that any spilling is worse than just dropping back to
4907 * SIMD8. There's probably actually some intermediate point where
4908 * SIMD16 with a couple of spills is still better.
4910 if (dispatch_width
== 16) {
4911 fail("Failure to register allocate. Reduce number of "
4912 "live scalar values to avoid this.");
4914 compiler
->shader_perf_log(log_data
,
4915 "%s shader triggered register spilling. "
4916 "Try reducing the number of live scalar "
4917 "values to improve performance.\n",
4921 /* Since we're out of heuristics, just go spill registers until we
4922 * get an allocation.
4924 while (!assign_regs(true)) {
4930 /* This must come after all optimization and register allocation, since
4931 * it inserts dead code that happens to have side effects, and it does
4932 * so based on the actual physical registers in use.
4934 insert_gen4_send_dependency_workarounds();
4939 if (!allocated_without_spills
)
4940 schedule_instructions(SCHEDULE_POST
);
4942 if (last_scratch
> 0)
4943 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
4947 fs_visitor::run_vs(gl_clip_plane
*clip_planes
)
4949 assert(stage
== MESA_SHADER_VERTEX
);
4953 if (shader_time_index
>= 0)
4954 emit_shader_time_begin();
4961 compute_clip_distance(clip_planes
);
4965 if (shader_time_index
>= 0)
4966 emit_shader_time_end();
4972 assign_curb_setup();
4973 assign_vs_urb_setup();
4975 fixup_3src_null_dest();
4976 allocate_registers();
4982 fs_visitor::run_fs(bool do_rep_send
)
4984 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4985 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
4987 assert(stage
== MESA_SHADER_FRAGMENT
);
4989 if (devinfo
->gen
>= 6)
4990 setup_payload_gen6();
4992 setup_payload_gen4();
4996 } else if (do_rep_send
) {
4997 assert(dispatch_width
== 16);
4998 emit_repclear_shader();
5000 if (shader_time_index
>= 0)
5001 emit_shader_time_begin();
5003 calculate_urb_setup();
5004 if (nir
->info
.inputs_read
> 0) {
5005 if (devinfo
->gen
< 6)
5006 emit_interpolation_setup_gen4();
5008 emit_interpolation_setup_gen6();
5011 /* We handle discards by keeping track of the still-live pixels in f0.1.
5012 * Initialize it with the dispatched pixels.
5014 if (wm_prog_data
->uses_kill
) {
5015 fs_inst
*discard_init
= bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
5016 discard_init
->flag_subreg
= 1;
5019 /* Generate FS IR for main(). (the visitor only descends into
5020 * functions called "main").
5027 if (wm_prog_data
->uses_kill
)
5028 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
5030 if (wm_key
->alpha_test_func
)
5035 if (shader_time_index
>= 0)
5036 emit_shader_time_end();
5042 assign_curb_setup();
5045 fixup_3src_null_dest();
5046 allocate_registers();
5052 if (dispatch_width
== 8)
5053 wm_prog_data
->reg_blocks
= brw_register_blocks(grf_used
);
5055 wm_prog_data
->reg_blocks_16
= brw_register_blocks(grf_used
);
5061 fs_visitor::run_cs()
5063 assert(stage
== MESA_SHADER_COMPUTE
);
5067 if (shader_time_index
>= 0)
5068 emit_shader_time_begin();
5075 emit_cs_terminate();
5077 if (shader_time_index
>= 0)
5078 emit_shader_time_end();
5084 assign_curb_setup();
5086 fixup_3src_null_dest();
5087 allocate_registers();
5096 brw_wm_fs_emit(const struct brw_compiler
*compiler
, void *log_data
,
5098 const struct brw_wm_prog_key
*key
,
5099 struct brw_wm_prog_data
*prog_data
,
5100 const nir_shader
*shader
,
5101 struct gl_program
*prog
,
5102 int shader_time_index8
, int shader_time_index16
,
5104 unsigned *final_assembly_size
,
5107 fs_visitor
v(compiler
, log_data
, mem_ctx
, key
,
5108 &prog_data
->base
, prog
, shader
, 8,
5109 shader_time_index8
);
5110 if (!v
.run_fs(false /* do_rep_send */)) {
5112 *error_str
= ralloc_strdup(mem_ctx
, v
.fail_msg
);
5117 cfg_t
*simd16_cfg
= NULL
;
5118 fs_visitor
v2(compiler
, log_data
, mem_ctx
, key
,
5119 &prog_data
->base
, prog
, shader
, 16,
5120 shader_time_index16
);
5121 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
5122 if (!v
.simd16_unsupported
) {
5123 /* Try a SIMD16 compile */
5124 v2
.import_uniforms(&v
);
5125 if (!v2
.run_fs(use_rep_send
)) {
5126 compiler
->shader_perf_log(log_data
,
5127 "SIMD16 shader failed to compile: %s",
5130 simd16_cfg
= v2
.cfg
;
5136 int no_simd8
= (INTEL_DEBUG
& DEBUG_NO8
) || use_rep_send
;
5137 if ((no_simd8
|| compiler
->devinfo
->gen
< 5) && simd16_cfg
) {
5139 prog_data
->no_8
= true;
5142 prog_data
->no_8
= false;
5145 fs_generator
g(compiler
, log_data
, mem_ctx
, (void *) key
, &prog_data
->base
,
5146 v
.promoted_constants
, v
.runtime_check_aads_emit
, "FS");
5148 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
5149 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
5150 shader
->info
.label
? shader
->info
.label
:
5152 shader
->info
.name
));
5156 g
.generate_code(simd8_cfg
, 8);
5158 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
5160 return g
.get_assembly(final_assembly_size
);
5164 brw_cs_fill_local_id_payload(const struct brw_cs_prog_data
*prog_data
,
5165 void *buffer
, uint32_t threads
, uint32_t stride
)
5167 if (prog_data
->local_invocation_id_regs
== 0)
5170 /* 'stride' should be an integer number of registers, that is, a multiple
5173 assert(stride
% 32 == 0);
5175 unsigned x
= 0, y
= 0, z
= 0;
5176 for (unsigned t
= 0; t
< threads
; t
++) {
5177 uint32_t *param
= (uint32_t *) buffer
+ stride
* t
/ 4;
5179 for (unsigned i
= 0; i
< prog_data
->simd_size
; i
++) {
5180 param
[0 * prog_data
->simd_size
+ i
] = x
;
5181 param
[1 * prog_data
->simd_size
+ i
] = y
;
5182 param
[2 * prog_data
->simd_size
+ i
] = z
;
5185 if (x
== prog_data
->local_size
[0]) {
5188 if (y
== prog_data
->local_size
[1]) {
5191 if (z
== prog_data
->local_size
[2])
5200 fs_visitor::emit_cs_local_invocation_id_setup()
5202 assert(stage
== MESA_SHADER_COMPUTE
);
5204 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5206 struct brw_reg src
=
5207 brw_vec8_grf(payload
.local_invocation_id_reg
, 0);
5208 src
= retype(src
, BRW_REGISTER_TYPE_UD
);
5210 src
.nr
+= dispatch_width
/ 8;
5211 bld
.MOV(offset(*reg
, bld
, 1), src
);
5212 src
.nr
+= dispatch_width
/ 8;
5213 bld
.MOV(offset(*reg
, bld
, 2), src
);
5219 fs_visitor::emit_cs_work_group_id_setup()
5221 assert(stage
== MESA_SHADER_COMPUTE
);
5223 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5225 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
5226 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
5227 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
5229 bld
.MOV(*reg
, r0_1
);
5230 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
5231 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
5237 brw_cs_emit(const struct brw_compiler
*compiler
, void *log_data
,
5239 const struct brw_cs_prog_key
*key
,
5240 struct brw_cs_prog_data
*prog_data
,
5241 const nir_shader
*shader
,
5242 int shader_time_index
,
5243 unsigned *final_assembly_size
,
5246 prog_data
->local_size
[0] = shader
->info
.cs
.local_size
[0];
5247 prog_data
->local_size
[1] = shader
->info
.cs
.local_size
[1];
5248 prog_data
->local_size
[2] = shader
->info
.cs
.local_size
[2];
5249 unsigned local_workgroup_size
=
5250 shader
->info
.cs
.local_size
[0] * shader
->info
.cs
.local_size
[1] *
5251 shader
->info
.cs
.local_size
[2];
5253 unsigned max_cs_threads
= compiler
->devinfo
->max_cs_threads
;
5256 const char *fail_msg
= NULL
;
5258 /* Now the main event: Visit the shader IR and generate our CS IR for it.
5260 fs_visitor
v8(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5261 NULL
, /* Never used in core profile */
5262 shader
, 8, shader_time_index
);
5264 fail_msg
= v8
.fail_msg
;
5265 } else if (local_workgroup_size
<= 8 * max_cs_threads
) {
5267 prog_data
->simd_size
= 8;
5270 fs_visitor
v16(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5271 NULL
, /* Never used in core profile */
5272 shader
, 16, shader_time_index
);
5273 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
5274 !fail_msg
&& !v8
.simd16_unsupported
&&
5275 local_workgroup_size
<= 16 * max_cs_threads
) {
5276 /* Try a SIMD16 compile */
5277 v16
.import_uniforms(&v8
);
5278 if (!v16
.run_cs()) {
5279 compiler
->shader_perf_log(log_data
,
5280 "SIMD16 shader failed to compile: %s",
5284 "Couldn't generate SIMD16 program and not "
5285 "enough threads for SIMD8";
5289 prog_data
->simd_size
= 16;
5293 if (unlikely(cfg
== NULL
)) {
5296 *error_str
= ralloc_strdup(mem_ctx
, fail_msg
);
5301 fs_generator
g(compiler
, log_data
, mem_ctx
, (void*) key
, &prog_data
->base
,
5302 v8
.promoted_constants
, v8
.runtime_check_aads_emit
, "CS");
5303 if (INTEL_DEBUG
& DEBUG_CS
) {
5304 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
5305 shader
->info
.label
? shader
->info
.label
:
5308 g
.enable_debug(name
);
5311 g
.generate_code(cfg
, prog_data
->simd_size
);
5313 return g
.get_assembly(final_assembly_size
);