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 "main/macros.h"
32 #include "brw_context.h"
37 #include "brw_vec4_gs_visitor.h"
39 #include "brw_program.h"
40 #include "brw_dead_control_flow.h"
41 #include "compiler/glsl_types.h"
46 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
47 const fs_reg
*src
, unsigned sources
)
49 memset(this, 0, sizeof(*this));
51 this->src
= new fs_reg
[MAX2(sources
, 3)];
52 for (unsigned i
= 0; i
< sources
; i
++)
53 this->src
[i
] = src
[i
];
55 this->opcode
= opcode
;
57 this->sources
= sources
;
58 this->exec_size
= exec_size
;
60 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
62 assert(this->exec_size
!= 0);
64 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
66 /* This will be the case for almost all instructions. */
73 this->regs_written
= DIV_ROUND_UP(dst
.component_size(exec_size
),
77 this->regs_written
= 0;
81 unreachable("Invalid destination register file");
84 this->writes_accumulator
= false;
89 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
92 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
94 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
97 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
99 init(opcode
, exec_size
, dst
, NULL
, 0);
102 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
105 const fs_reg src
[1] = { src0
};
106 init(opcode
, exec_size
, dst
, src
, 1);
109 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
110 const fs_reg
&src0
, const fs_reg
&src1
)
112 const fs_reg src
[2] = { src0
, src1
};
113 init(opcode
, exec_size
, dst
, src
, 2);
116 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
117 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
119 const fs_reg src
[3] = { src0
, src1
, src2
};
120 init(opcode
, exec_size
, dst
, src
, 3);
123 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
124 const fs_reg src
[], unsigned sources
)
126 init(opcode
, exec_width
, dst
, src
, sources
);
129 fs_inst::fs_inst(const fs_inst
&that
)
131 memcpy(this, &that
, sizeof(that
));
133 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
135 for (unsigned i
= 0; i
< that
.sources
; i
++)
136 this->src
[i
] = that
.src
[i
];
145 fs_inst::resize_sources(uint8_t num_sources
)
147 if (this->sources
!= num_sources
) {
148 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
150 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
151 src
[i
] = this->src
[i
];
155 this->sources
= num_sources
;
160 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
162 const fs_reg
&surf_index
,
163 const fs_reg
&varying_offset
,
164 uint32_t const_offset
)
166 /* We have our constant surface use a pitch of 4 bytes, so our index can
167 * be any component of a vector, and then we load 4 contiguous
168 * components starting from that.
170 * We break down the const_offset to a portion added to the variable
171 * offset and a portion done using reg_offset, which means that if you
172 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
173 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
174 * CSE can later notice that those loads are all the same and eliminate
175 * the redundant ones.
177 fs_reg vec4_offset
= vgrf(glsl_type::uint_type
);
178 bld
.ADD(vec4_offset
, varying_offset
, brw_imm_ud(const_offset
& ~0xf));
181 if (devinfo
->gen
== 4 && bld
.dispatch_width() == 8) {
182 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
183 * u, v, r) as parameters, or we can just use the SIMD16 message
184 * consisting of (header, u). We choose the second, at the cost of a
185 * longer return length.
191 if (devinfo
->gen
>= 7)
192 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
;
194 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
;
196 int regs_written
= 4 * (bld
.dispatch_width() / 8) * scale
;
197 fs_reg vec4_result
= fs_reg(VGRF
, alloc
.allocate(regs_written
), dst
.type
);
198 fs_inst
*inst
= bld
.emit(op
, vec4_result
, surf_index
, vec4_offset
);
199 inst
->regs_written
= regs_written
;
201 if (devinfo
->gen
< 7) {
202 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
);
203 inst
->header_size
= 1;
204 if (devinfo
->gen
== 4)
207 inst
->mlen
= 1 + bld
.dispatch_width() / 8;
210 bld
.MOV(dst
, offset(vec4_result
, bld
, ((const_offset
& 0xf) / 4) * scale
));
214 * A helper for MOV generation for fixing up broken hardware SEND dependency
218 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
220 /* The caller always wants uncompressed to emit the minimal extra
221 * dependencies, and to avoid having to deal with aligning its regs to 2.
223 const fs_builder ubld
= bld
.annotate("send dependency resolve")
226 ubld
.MOV(ubld
.null_reg_f(), fs_reg(VGRF
, grf
, BRW_REGISTER_TYPE_F
));
230 fs_inst::equals(fs_inst
*inst
) const
232 return (opcode
== inst
->opcode
&&
233 dst
.equals(inst
->dst
) &&
234 src
[0].equals(inst
->src
[0]) &&
235 src
[1].equals(inst
->src
[1]) &&
236 src
[2].equals(inst
->src
[2]) &&
237 saturate
== inst
->saturate
&&
238 predicate
== inst
->predicate
&&
239 conditional_mod
== inst
->conditional_mod
&&
240 mlen
== inst
->mlen
&&
241 base_mrf
== inst
->base_mrf
&&
242 target
== inst
->target
&&
244 header_size
== inst
->header_size
&&
245 shadow_compare
== inst
->shadow_compare
&&
246 exec_size
== inst
->exec_size
&&
247 offset
== inst
->offset
);
251 fs_inst::overwrites_reg(const fs_reg
®
) const
253 return reg
.in_range(dst
, regs_written
);
257 fs_inst::is_send_from_grf() const
260 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
261 case SHADER_OPCODE_SHADER_TIME_ADD
:
262 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
263 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
264 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
265 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
266 case SHADER_OPCODE_UNTYPED_ATOMIC
:
267 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
268 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
269 case SHADER_OPCODE_TYPED_ATOMIC
:
270 case SHADER_OPCODE_TYPED_SURFACE_READ
:
271 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
272 case SHADER_OPCODE_URB_WRITE_SIMD8
:
273 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
274 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
275 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
276 case SHADER_OPCODE_URB_READ_SIMD8
:
277 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
279 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
280 return src
[1].file
== VGRF
;
281 case FS_OPCODE_FB_WRITE
:
282 return src
[0].file
== VGRF
;
285 return src
[0].file
== VGRF
;
292 * Returns true if this instruction's sources and destinations cannot
293 * safely be the same register.
295 * In most cases, a register can be written over safely by the same
296 * instruction that is its last use. For a single instruction, the
297 * sources are dereferenced before writing of the destination starts
300 * However, there are a few cases where this can be problematic:
302 * - Virtual opcodes that translate to multiple instructions in the
303 * code generator: if src == dst and one instruction writes the
304 * destination before a later instruction reads the source, then
305 * src will have been clobbered.
307 * - SIMD16 compressed instructions with certain regioning (see below).
309 * The register allocator uses this information to set up conflicts between
310 * GRF sources and the destination.
313 fs_inst::has_source_and_destination_hazard() const
316 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
317 /* Multiple partial writes to the destination */
320 /* The SIMD16 compressed instruction
322 * add(16) g4<1>F g4<8,8,1>F g6<8,8,1>F
324 * is actually decoded in hardware as:
326 * add(8) g4<1>F g4<8,8,1>F g6<8,8,1>F
327 * add(8) g5<1>F g5<8,8,1>F g7<8,8,1>F
329 * Which is safe. However, if we have uniform accesses
330 * happening, we get into trouble:
332 * add(8) g4<1>F g4<0,1,0>F g6<8,8,1>F
333 * add(8) g5<1>F g4<0,1,0>F g7<8,8,1>F
335 * Now our destination for the first instruction overwrote the
336 * second instruction's src0, and we get garbage for those 8
337 * pixels. There's a similar issue for the pre-gen6
338 * pixel_x/pixel_y, which are registers of 16-bit values and thus
339 * would get stomped by the first decode as well.
341 if (exec_size
== 16) {
342 for (int i
= 0; i
< sources
; i
++) {
343 if (src
[i
].file
== VGRF
&& (src
[i
].stride
== 0 ||
344 src
[i
].type
== BRW_REGISTER_TYPE_UW
||
345 src
[i
].type
== BRW_REGISTER_TYPE_W
||
346 src
[i
].type
== BRW_REGISTER_TYPE_UB
||
347 src
[i
].type
== BRW_REGISTER_TYPE_B
)) {
357 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
359 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
362 fs_reg reg
= this->src
[0];
363 if (reg
.file
!= VGRF
|| reg
.reg_offset
!= 0 || reg
.stride
== 0)
366 if (grf_alloc
.sizes
[reg
.nr
] != this->regs_written
)
369 for (int i
= 0; i
< this->sources
; i
++) {
370 reg
.type
= this->src
[i
].type
;
371 if (!this->src
[i
].equals(reg
))
374 if (i
< this->header_size
) {
377 reg
.reg_offset
+= this->exec_size
/ 8;
385 fs_inst::can_do_source_mods(const struct brw_device_info
*devinfo
)
387 if (devinfo
->gen
== 6 && is_math())
390 if (is_send_from_grf())
393 if (!backend_instruction::can_do_source_mods())
400 fs_inst::can_change_types() const
402 return dst
.type
== src
[0].type
&&
403 !src
[0].abs
&& !src
[0].negate
&& !saturate
&&
404 (opcode
== BRW_OPCODE_MOV
||
405 (opcode
== BRW_OPCODE_SEL
&&
406 dst
.type
== src
[1].type
&&
407 predicate
!= BRW_PREDICATE_NONE
&&
408 !src
[1].abs
&& !src
[1].negate
));
412 fs_inst::has_side_effects() const
414 return this->eot
|| backend_instruction::has_side_effects();
420 memset(this, 0, sizeof(*this));
424 /** Generic unset register constructor. */
428 this->file
= BAD_FILE
;
431 fs_reg::fs_reg(struct ::brw_reg reg
) :
434 this->reg_offset
= 0;
435 this->subreg_offset
= 0;
437 if (this->file
== IMM
&&
438 (this->type
!= BRW_REGISTER_TYPE_V
&&
439 this->type
!= BRW_REGISTER_TYPE_UV
&&
440 this->type
!= BRW_REGISTER_TYPE_VF
)) {
446 fs_reg::equals(const fs_reg
&r
) const
448 return (this->backend_reg::equals(r
) &&
449 subreg_offset
== r
.subreg_offset
&&
454 fs_reg::set_smear(unsigned subreg
)
456 assert(file
!= ARF
&& file
!= FIXED_GRF
&& file
!= IMM
);
457 subreg_offset
= subreg
* type_sz(type
);
463 fs_reg::is_contiguous() const
469 fs_reg::component_size(unsigned width
) const
471 const unsigned stride
= ((file
!= ARF
&& file
!= FIXED_GRF
) ? this->stride
:
474 return MAX2(width
* stride
, 1) * type_sz(type
);
478 type_size_scalar(const struct glsl_type
*type
)
480 unsigned int size
, i
;
482 switch (type
->base_type
) {
485 case GLSL_TYPE_FLOAT
:
487 return type
->components();
488 case GLSL_TYPE_ARRAY
:
489 return type_size_scalar(type
->fields
.array
) * type
->length
;
490 case GLSL_TYPE_STRUCT
:
492 for (i
= 0; i
< type
->length
; i
++) {
493 size
+= type_size_scalar(type
->fields
.structure
[i
].type
);
496 case GLSL_TYPE_SAMPLER
:
497 /* Samplers take up no register space, since they're baked in at
501 case GLSL_TYPE_ATOMIC_UINT
:
503 case GLSL_TYPE_SUBROUTINE
:
505 case GLSL_TYPE_IMAGE
:
506 return BRW_IMAGE_PARAM_SIZE
;
508 case GLSL_TYPE_ERROR
:
509 case GLSL_TYPE_INTERFACE
:
510 case GLSL_TYPE_DOUBLE
:
511 case GLSL_TYPE_FUNCTION
:
512 unreachable("not reached");
519 * Returns the number of scalar components needed to store type, assuming
520 * that vectors are padded out to vec4.
522 * This has the packing rules of type_size_vec4(), but counts components
523 * similar to type_size_scalar().
526 type_size_vec4_times_4(const struct glsl_type
*type
)
528 return 4 * type_size_vec4(type
);
532 * Create a MOV to read the timestamp register.
534 * The caller is responsible for emitting the MOV. The return value is
535 * the destination of the MOV, with extra parameters set.
538 fs_visitor::get_timestamp(const fs_builder
&bld
)
540 assert(devinfo
->gen
>= 7);
542 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
545 BRW_REGISTER_TYPE_UD
));
547 fs_reg dst
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
549 /* We want to read the 3 fields we care about even if it's not enabled in
552 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
558 fs_visitor::emit_shader_time_begin()
560 shader_start_time
= get_timestamp(bld
.annotate("shader time start"));
562 /* We want only the low 32 bits of the timestamp. Since it's running
563 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
564 * which is plenty of time for our purposes. It is identical across the
565 * EUs, but since it's tracking GPU core speed it will increment at a
566 * varying rate as render P-states change.
568 shader_start_time
.set_smear(0);
572 fs_visitor::emit_shader_time_end()
574 /* Insert our code just before the final SEND with EOT. */
575 exec_node
*end
= this->instructions
.get_tail();
576 assert(end
&& ((fs_inst
*) end
)->eot
);
577 const fs_builder ibld
= bld
.annotate("shader time end")
578 .exec_all().at(NULL
, end
);
580 fs_reg shader_end_time
= get_timestamp(ibld
);
582 /* We only use the low 32 bits of the timestamp - see
583 * emit_shader_time_begin()).
585 * We could also check if render P-states have changed (or anything
586 * else that might disrupt timing) by setting smear to 2 and checking if
587 * that field is != 0.
589 shader_end_time
.set_smear(0);
591 /* Check that there weren't any timestamp reset events (assuming these
592 * were the only two timestamp reads that happened).
594 fs_reg reset
= shader_end_time
;
596 set_condmod(BRW_CONDITIONAL_Z
,
597 ibld
.AND(ibld
.null_reg_ud(), reset
, brw_imm_ud(1u)));
598 ibld
.IF(BRW_PREDICATE_NORMAL
);
600 fs_reg start
= shader_start_time
;
602 fs_reg diff
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
605 const fs_builder cbld
= ibld
.group(1, 0);
606 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
608 /* If there were no instructions between the two timestamp gets, the diff
609 * is 2 cycles. Remove that overhead, so I can forget about that when
610 * trying to determine the time taken for single instructions.
612 cbld
.ADD(diff
, diff
, brw_imm_ud(-2u));
613 SHADER_TIME_ADD(cbld
, 0, diff
);
614 SHADER_TIME_ADD(cbld
, 1, brw_imm_ud(1u));
615 ibld
.emit(BRW_OPCODE_ELSE
);
616 SHADER_TIME_ADD(cbld
, 2, brw_imm_ud(1u));
617 ibld
.emit(BRW_OPCODE_ENDIF
);
621 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
622 int shader_time_subindex
,
625 int index
= shader_time_index
* 3 + shader_time_subindex
;
626 struct brw_reg offset
= brw_imm_d(index
* SHADER_TIME_STRIDE
);
629 if (dispatch_width
== 8)
630 payload
= vgrf(glsl_type::uvec2_type
);
632 payload
= vgrf(glsl_type::uint_type
);
634 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
638 fs_visitor::vfail(const char *format
, va_list va
)
647 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
648 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
650 this->fail_msg
= msg
;
653 fprintf(stderr
, "%s", msg
);
658 fs_visitor::fail(const char *format
, ...)
662 va_start(va
, format
);
668 * Mark this program as impossible to compile in SIMD16 mode.
670 * During the SIMD8 compile (which happens first), we can detect and flag
671 * things that are unsupported in SIMD16 mode, so the compiler can skip
672 * the SIMD16 compile altogether.
674 * During a SIMD16 compile (if one happens anyway), this just calls fail().
677 fs_visitor::no16(const char *msg
)
679 if (dispatch_width
== 16) {
682 simd16_unsupported
= true;
684 compiler
->shader_perf_log(log_data
,
685 "SIMD16 shader failed to compile: %s", msg
);
690 * Returns true if the instruction has a flag that means it won't
691 * update an entire destination register.
693 * For example, dead code elimination and live variable analysis want to know
694 * when a write to a variable screens off any preceding values that were in
698 fs_inst::is_partial_write() const
700 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
701 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
702 !this->dst
.is_contiguous());
706 fs_inst::components_read(unsigned i
) const
709 case FS_OPCODE_LINTERP
:
715 case FS_OPCODE_PIXEL_X
:
716 case FS_OPCODE_PIXEL_Y
:
720 case FS_OPCODE_FB_WRITE_LOGICAL
:
721 assert(src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
722 /* First/second FB write color. */
724 return src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
728 case SHADER_OPCODE_TEX_LOGICAL
:
729 case SHADER_OPCODE_TXD_LOGICAL
:
730 case SHADER_OPCODE_TXF_LOGICAL
:
731 case SHADER_OPCODE_TXL_LOGICAL
:
732 case SHADER_OPCODE_TXS_LOGICAL
:
733 case FS_OPCODE_TXB_LOGICAL
:
734 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
735 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
736 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
737 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
738 case SHADER_OPCODE_LOD_LOGICAL
:
739 case SHADER_OPCODE_TG4_LOGICAL
:
740 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
741 assert(src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
&&
742 src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
743 /* Texture coordinates. */
744 if (i
== TEX_LOGICAL_SRC_COORDINATE
)
745 return src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
746 /* Texture derivatives. */
747 else if ((i
== TEX_LOGICAL_SRC_LOD
|| i
== TEX_LOGICAL_SRC_LOD2
) &&
748 opcode
== SHADER_OPCODE_TXD_LOGICAL
)
749 return src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
750 /* Texture offset. */
751 else if (i
== TEX_LOGICAL_SRC_OFFSET_VALUE
)
754 else if (i
== TEX_LOGICAL_SRC_MCS
&& opcode
== SHADER_OPCODE_TXF_CMS_W_LOGICAL
)
759 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
760 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
761 assert(src
[3].file
== IMM
);
762 /* Surface coordinates. */
765 /* Surface operation source (ignored for reads). */
771 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
772 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
773 assert(src
[3].file
== IMM
&&
775 /* Surface coordinates. */
778 /* Surface operation source. */
784 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
785 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
786 assert(src
[3].file
== IMM
&&
788 const unsigned op
= src
[4].ud
;
789 /* Surface coordinates. */
792 /* Surface operation source. */
793 else if (i
== 1 && op
== BRW_AOP_CMPWR
)
795 else if (i
== 1 && (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
||
796 op
== BRW_AOP_PREDEC
))
808 fs_inst::regs_read(int arg
) const
811 case FS_OPCODE_FB_WRITE
:
812 case SHADER_OPCODE_URB_WRITE_SIMD8
:
813 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
814 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
815 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
816 case SHADER_OPCODE_URB_READ_SIMD8
:
817 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
818 case SHADER_OPCODE_UNTYPED_ATOMIC
:
819 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
820 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
821 case SHADER_OPCODE_TYPED_ATOMIC
:
822 case SHADER_OPCODE_TYPED_SURFACE_READ
:
823 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
824 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
829 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
830 /* The payload is actually stored in src1 */
835 case FS_OPCODE_LINTERP
:
840 case SHADER_OPCODE_LOAD_PAYLOAD
:
841 if (arg
< this->header_size
)
845 case CS_OPCODE_CS_TERMINATE
:
846 case SHADER_OPCODE_BARRIER
:
849 case SHADER_OPCODE_MOV_INDIRECT
:
851 assert(src
[2].file
== IMM
);
852 unsigned region_length
= src
[2].ud
;
854 if (src
[0].file
== UNIFORM
) {
855 assert(region_length
% 4 == 0);
856 return region_length
/ 4;
857 } else if (src
[0].file
== FIXED_GRF
) {
858 /* If the start of the region is not register aligned, then
859 * there's some portion of the register that's technically
860 * unread at the beginning.
862 * However, the register allocator works in terms of whole
863 * registers, and does not use subnr. It assumes that the
864 * read starts at the beginning of the register, and extends
865 * regs_read() whole registers beyond that.
867 * To compensate, we extend the region length to include this
868 * unread portion at the beginning.
871 region_length
+= src
[0].subnr
;
873 return DIV_ROUND_UP(region_length
, REG_SIZE
);
875 assert(!"Invalid register file");
881 if (is_tex() && arg
== 0 && src
[0].file
== VGRF
)
886 switch (src
[arg
].file
) {
896 return DIV_ROUND_UP(components_read(arg
) *
897 src
[arg
].component_size(exec_size
),
900 unreachable("MRF registers are not allowed as sources");
906 fs_inst::reads_flag() const
912 fs_inst::writes_flag() const
914 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
915 opcode
!= BRW_OPCODE_IF
&&
916 opcode
!= BRW_OPCODE_WHILE
)) ||
917 opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
;
921 * Returns how many MRFs an FS opcode will write over.
923 * Note that this is not the 0 or 1 implied writes in an actual gen
924 * instruction -- the FS opcodes often generate MOVs in addition.
927 fs_visitor::implied_mrf_writes(fs_inst
*inst
)
932 if (inst
->base_mrf
== -1)
935 switch (inst
->opcode
) {
936 case SHADER_OPCODE_RCP
:
937 case SHADER_OPCODE_RSQ
:
938 case SHADER_OPCODE_SQRT
:
939 case SHADER_OPCODE_EXP2
:
940 case SHADER_OPCODE_LOG2
:
941 case SHADER_OPCODE_SIN
:
942 case SHADER_OPCODE_COS
:
943 return 1 * dispatch_width
/ 8;
944 case SHADER_OPCODE_POW
:
945 case SHADER_OPCODE_INT_QUOTIENT
:
946 case SHADER_OPCODE_INT_REMAINDER
:
947 return 2 * dispatch_width
/ 8;
948 case SHADER_OPCODE_TEX
:
950 case SHADER_OPCODE_TXD
:
951 case SHADER_OPCODE_TXF
:
952 case SHADER_OPCODE_TXF_CMS
:
953 case SHADER_OPCODE_TXF_CMS_W
:
954 case SHADER_OPCODE_TXF_MCS
:
955 case SHADER_OPCODE_TG4
:
956 case SHADER_OPCODE_TG4_OFFSET
:
957 case SHADER_OPCODE_TXL
:
958 case SHADER_OPCODE_TXS
:
959 case SHADER_OPCODE_LOD
:
960 case SHADER_OPCODE_SAMPLEINFO
:
962 case FS_OPCODE_FB_WRITE
:
964 case FS_OPCODE_GET_BUFFER_SIZE
:
965 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
966 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
968 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
970 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
972 case SHADER_OPCODE_UNTYPED_ATOMIC
:
973 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
974 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
975 case SHADER_OPCODE_TYPED_ATOMIC
:
976 case SHADER_OPCODE_TYPED_SURFACE_READ
:
977 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
978 case SHADER_OPCODE_URB_WRITE_SIMD8
:
979 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
980 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
981 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
982 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
983 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
984 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
985 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
988 unreachable("not reached");
993 fs_visitor::vgrf(const glsl_type
*const type
)
995 int reg_width
= dispatch_width
/ 8;
996 return fs_reg(VGRF
, alloc
.allocate(type_size_scalar(type
) * reg_width
),
997 brw_type_for_base_type(type
));
1000 fs_reg::fs_reg(enum brw_reg_file file
, int nr
)
1005 this->type
= BRW_REGISTER_TYPE_F
;
1006 this->stride
= (file
== UNIFORM
? 0 : 1);
1009 fs_reg::fs_reg(enum brw_reg_file file
, int nr
, enum brw_reg_type type
)
1015 this->stride
= (file
== UNIFORM
? 0 : 1);
1018 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
1019 * This brings in those uniform definitions
1022 fs_visitor::import_uniforms(fs_visitor
*v
)
1024 this->push_constant_loc
= v
->push_constant_loc
;
1025 this->pull_constant_loc
= v
->pull_constant_loc
;
1026 this->uniforms
= v
->uniforms
;
1030 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer
,
1031 bool origin_upper_left
)
1033 assert(stage
== MESA_SHADER_FRAGMENT
);
1034 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1035 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec4_type
));
1037 bool flip
= !origin_upper_left
^ key
->render_to_fbo
;
1039 /* gl_FragCoord.x */
1040 if (pixel_center_integer
) {
1041 bld
.MOV(wpos
, this->pixel_x
);
1043 bld
.ADD(wpos
, this->pixel_x
, brw_imm_f(0.5f
));
1045 wpos
= offset(wpos
, bld
, 1);
1047 /* gl_FragCoord.y */
1048 if (!flip
&& pixel_center_integer
) {
1049 bld
.MOV(wpos
, this->pixel_y
);
1051 fs_reg pixel_y
= this->pixel_y
;
1052 float offset
= (pixel_center_integer
? 0.0f
: 0.5f
);
1055 pixel_y
.negate
= true;
1056 offset
+= key
->drawable_height
- 1.0f
;
1059 bld
.ADD(wpos
, pixel_y
, brw_imm_f(offset
));
1061 wpos
= offset(wpos
, bld
, 1);
1063 /* gl_FragCoord.z */
1064 if (devinfo
->gen
>= 6) {
1065 bld
.MOV(wpos
, fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0)));
1067 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
1068 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
1069 interp_reg(VARYING_SLOT_POS
, 2));
1071 wpos
= offset(wpos
, bld
, 1);
1073 /* gl_FragCoord.w: Already set up in emit_interpolation */
1074 bld
.MOV(wpos
, this->wpos_w
);
1080 fs_visitor::emit_linterp(const fs_reg
&attr
, const fs_reg
&interp
,
1081 glsl_interp_qualifier interpolation_mode
,
1082 bool is_centroid
, bool is_sample
)
1084 brw_wm_barycentric_interp_mode barycoord_mode
;
1085 if (devinfo
->gen
>= 6) {
1087 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1088 barycoord_mode
= BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
1090 barycoord_mode
= BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
1091 } else if (is_sample
) {
1092 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1093 barycoord_mode
= BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
1095 barycoord_mode
= BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
1097 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1098 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1100 barycoord_mode
= BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
1103 /* On Ironlake and below, there is only one interpolation mode.
1104 * Centroid interpolation doesn't mean anything on this hardware --
1105 * there is no multisampling.
1107 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1109 return bld
.emit(FS_OPCODE_LINTERP
, attr
,
1110 this->delta_xy
[barycoord_mode
], interp
);
1114 fs_visitor::emit_general_interpolation(fs_reg
*attr
, const char *name
,
1115 const glsl_type
*type
,
1116 glsl_interp_qualifier interpolation_mode
,
1117 int *location
, bool mod_centroid
,
1120 assert(stage
== MESA_SHADER_FRAGMENT
);
1121 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1122 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1124 if (interpolation_mode
== INTERP_QUALIFIER_NONE
) {
1126 *location
== VARYING_SLOT_COL0
|| *location
== VARYING_SLOT_COL1
;
1127 if (key
->flat_shade
&& is_gl_Color
) {
1128 interpolation_mode
= INTERP_QUALIFIER_FLAT
;
1130 interpolation_mode
= INTERP_QUALIFIER_SMOOTH
;
1134 if (type
->is_array() || type
->is_matrix()) {
1135 const glsl_type
*elem_type
= glsl_get_array_element(type
);
1136 const unsigned length
= glsl_get_length(type
);
1138 for (unsigned i
= 0; i
< length
; i
++) {
1139 emit_general_interpolation(attr
, name
, elem_type
, interpolation_mode
,
1140 location
, mod_centroid
, mod_sample
);
1142 } else if (type
->is_record()) {
1143 for (unsigned i
= 0; i
< type
->length
; i
++) {
1144 const glsl_type
*field_type
= type
->fields
.structure
[i
].type
;
1145 emit_general_interpolation(attr
, name
, field_type
, interpolation_mode
,
1146 location
, mod_centroid
, mod_sample
);
1149 assert(type
->is_scalar() || type
->is_vector());
1151 if (prog_data
->urb_setup
[*location
] == -1) {
1152 /* If there's no incoming setup data for this slot, don't
1153 * emit interpolation for it.
1155 *attr
= offset(*attr
, bld
, type
->vector_elements
);
1160 attr
->type
= brw_type_for_base_type(type
->get_scalar_type());
1162 if (interpolation_mode
== INTERP_QUALIFIER_FLAT
) {
1163 /* Constant interpolation (flat shading) case. The SF has
1164 * handed us defined values in only the constant offset
1165 * field of the setup reg.
1167 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
1168 struct brw_reg interp
= interp_reg(*location
, i
);
1169 interp
= suboffset(interp
, 3);
1170 interp
.type
= attr
->type
;
1171 bld
.emit(FS_OPCODE_CINTERP
, *attr
, fs_reg(interp
));
1172 *attr
= offset(*attr
, bld
, 1);
1175 /* Smooth/noperspective interpolation case. */
1176 for (unsigned int i
= 0; i
< type
->vector_elements
; i
++) {
1177 struct brw_reg interp
= interp_reg(*location
, i
);
1178 if (devinfo
->needs_unlit_centroid_workaround
&& mod_centroid
) {
1179 /* Get the pixel/sample mask into f0 so that we know
1180 * which pixels are lit. Then, for each channel that is
1181 * unlit, replace the centroid data with non-centroid
1184 bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
1187 inst
= emit_linterp(*attr
, fs_reg(interp
), interpolation_mode
,
1189 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1190 inst
->predicate_inverse
= true;
1191 if (devinfo
->has_pln
)
1192 inst
->no_dd_clear
= true;
1194 inst
= emit_linterp(*attr
, fs_reg(interp
), interpolation_mode
,
1195 mod_centroid
&& !key
->persample_shading
,
1196 mod_sample
|| key
->persample_shading
);
1197 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1198 inst
->predicate_inverse
= false;
1199 if (devinfo
->has_pln
)
1200 inst
->no_dd_check
= true;
1203 emit_linterp(*attr
, fs_reg(interp
), interpolation_mode
,
1204 mod_centroid
&& !key
->persample_shading
,
1205 mod_sample
|| key
->persample_shading
);
1207 if (devinfo
->gen
< 6 && interpolation_mode
== INTERP_QUALIFIER_SMOOTH
) {
1208 bld
.MUL(*attr
, *attr
, this->pixel_w
);
1210 *attr
= offset(*attr
, bld
, 1);
1218 fs_visitor::emit_frontfacing_interpolation()
1220 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1222 if (devinfo
->gen
>= 6) {
1223 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1224 * a boolean result from this (~0/true or 0/false).
1226 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1227 * this task in only one instruction:
1228 * - a negation source modifier will flip the bit; and
1229 * - a W -> D type conversion will sign extend the bit into the high
1230 * word of the destination.
1232 * An ASR 15 fills the low word of the destination.
1234 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1237 bld
.ASR(*reg
, g0
, brw_imm_d(15));
1239 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1240 * a boolean result from this (1/true or 0/false).
1242 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1243 * the negation source modifier to flip it. Unfortunately the SHR
1244 * instruction only operates on UD (or D with an abs source modifier)
1245 * sources without negation.
1247 * Instead, use ASR (which will give ~0/true or 0/false).
1249 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1252 bld
.ASR(*reg
, g1_6
, brw_imm_d(31));
1259 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1261 assert(stage
== MESA_SHADER_FRAGMENT
);
1262 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1263 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1265 if (key
->compute_pos_offset
) {
1266 /* Convert int_sample_pos to floating point */
1267 bld
.MOV(dst
, int_sample_pos
);
1268 /* Scale to the range [0, 1] */
1269 bld
.MUL(dst
, dst
, brw_imm_f(1 / 16.0f
));
1272 /* From ARB_sample_shading specification:
1273 * "When rendering to a non-multisample buffer, or if multisample
1274 * rasterization is disabled, gl_SamplePosition will always be
1277 bld
.MOV(dst
, brw_imm_f(0.5f
));
1282 fs_visitor::emit_samplepos_setup()
1284 assert(devinfo
->gen
>= 6);
1286 const fs_builder abld
= bld
.annotate("compute sample position");
1287 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1289 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1290 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1292 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1293 * mode will be enabled.
1295 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1296 * R31.1:0 Position Offset X/Y for Slot[3:0]
1297 * R31.3:2 Position Offset X/Y for Slot[7:4]
1300 * The X, Y sample positions come in as bytes in thread payload. So, read
1301 * the positions using vstride=16, width=8, hstride=2.
1303 struct brw_reg sample_pos_reg
=
1304 stride(retype(brw_vec1_grf(payload
.sample_pos_reg
, 0),
1305 BRW_REGISTER_TYPE_B
), 16, 8, 2);
1307 if (dispatch_width
== 8) {
1308 abld
.MOV(int_sample_x
, fs_reg(sample_pos_reg
));
1310 abld
.half(0).MOV(half(int_sample_x
, 0), fs_reg(sample_pos_reg
));
1311 abld
.half(1).MOV(half(int_sample_x
, 1),
1312 fs_reg(suboffset(sample_pos_reg
, 16)));
1314 /* Compute gl_SamplePosition.x */
1315 compute_sample_position(pos
, int_sample_x
);
1316 pos
= offset(pos
, abld
, 1);
1317 if (dispatch_width
== 8) {
1318 abld
.MOV(int_sample_y
, fs_reg(suboffset(sample_pos_reg
, 1)));
1320 abld
.half(0).MOV(half(int_sample_y
, 0),
1321 fs_reg(suboffset(sample_pos_reg
, 1)));
1322 abld
.half(1).MOV(half(int_sample_y
, 1),
1323 fs_reg(suboffset(sample_pos_reg
, 17)));
1325 /* Compute gl_SamplePosition.y */
1326 compute_sample_position(pos
, int_sample_y
);
1331 fs_visitor::emit_sampleid_setup()
1333 assert(stage
== MESA_SHADER_FRAGMENT
);
1334 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1335 assert(devinfo
->gen
>= 6);
1337 const fs_builder abld
= bld
.annotate("compute sample id");
1338 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1340 if (key
->compute_sample_id
) {
1341 fs_reg
t1(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_D
);
1343 fs_reg
t2(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_W
);
1345 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1346 * 8x multisampling, subspan 0 will represent sample N (where N
1347 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1348 * 7. We can find the value of N by looking at R0.0 bits 7:6
1349 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1350 * (since samples are always delivered in pairs). That is, we
1351 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1352 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1353 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1354 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1355 * populating a temporary variable with the sequence (0, 1, 2, 3),
1356 * and then reading from it using vstride=1, width=4, hstride=0.
1357 * These computations hold good for 4x multisampling as well.
1359 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1360 * the first four slots are sample 0 of subspan 0; the next four
1361 * are sample 1 of subspan 0; the third group is sample 0 of
1362 * subspan 1, and finally sample 1 of subspan 1.
1365 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1366 * accomodate 16x MSAA.
1368 unsigned sspi_mask
= devinfo
->gen
>= 9 ? 0x1c0 : 0xc0;
1370 abld
.exec_all().group(1, 0)
1371 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_D
)),
1372 brw_imm_ud(sspi_mask
));
1373 abld
.exec_all().group(1, 0).SHR(t1
, t1
, brw_imm_d(5));
1375 /* This works for both SIMD8 and SIMD16 */
1376 abld
.exec_all().group(4, 0)
1377 .MOV(t2
, brw_imm_v(key
->persample_2x
? 0x1010 : 0x3210));
1379 /* This special instruction takes care of setting vstride=1,
1380 * width=4, hstride=0 of t2 during an ADD instruction.
1382 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1384 /* As per GL_ARB_sample_shading specification:
1385 * "When rendering to a non-multisample buffer, or if multisample
1386 * rasterization is disabled, gl_SampleID will always be zero."
1388 abld
.MOV(*reg
, brw_imm_d(0));
1395 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1397 if (!src
.abs
&& !src
.negate
)
1400 fs_reg temp
= bld
.vgrf(src
.type
);
1407 fs_visitor::emit_discard_jump()
1409 assert(((brw_wm_prog_data
*) this->prog_data
)->uses_kill
);
1411 /* For performance, after a discard, jump to the end of the
1412 * shader if all relevant channels have been discarded.
1414 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1415 discard_jump
->flag_subreg
= 1;
1417 discard_jump
->predicate
= (dispatch_width
== 8)
1418 ? BRW_PREDICATE_ALIGN1_ANY8H
1419 : BRW_PREDICATE_ALIGN1_ANY16H
;
1420 discard_jump
->predicate_inverse
= true;
1424 fs_visitor::emit_gs_thread_end()
1426 assert(stage
== MESA_SHADER_GEOMETRY
);
1428 struct brw_gs_prog_data
*gs_prog_data
=
1429 (struct brw_gs_prog_data
*) prog_data
;
1431 if (gs_compile
->control_data_header_size_bits
> 0) {
1432 emit_gs_control_data_bits(this->final_gs_vertex_count
);
1435 const fs_builder abld
= bld
.annotate("thread end");
1438 if (gs_prog_data
->static_vertex_count
!= -1) {
1439 foreach_in_list_reverse(fs_inst
, prev
, &this->instructions
) {
1440 if (prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
||
1441 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
1442 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
1443 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
) {
1446 /* Delete now dead instructions. */
1447 foreach_in_list_reverse_safe(exec_node
, dead
, &this->instructions
) {
1453 } else if (prev
->is_control_flow() || prev
->has_side_effects()) {
1457 fs_reg hdr
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1458 abld
.MOV(hdr
, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
)));
1459 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, hdr
);
1462 fs_reg payload
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1463 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
1464 sources
[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1465 sources
[1] = this->final_gs_vertex_count
;
1466 abld
.LOAD_PAYLOAD(payload
, sources
, 2, 2);
1467 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1475 fs_visitor::assign_curb_setup()
1477 if (dispatch_width
== 8) {
1478 prog_data
->dispatch_grf_start_reg
= payload
.num_regs
;
1480 if (stage
== MESA_SHADER_FRAGMENT
) {
1481 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1482 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1483 } else if (stage
== MESA_SHADER_COMPUTE
) {
1484 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
1485 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1487 unreachable("Unsupported shader type!");
1491 prog_data
->curb_read_length
= ALIGN(stage_prog_data
->nr_params
, 8) / 8;
1493 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1494 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1495 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1496 if (inst
->src
[i
].file
== UNIFORM
) {
1497 int uniform_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1499 if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1500 constant_nr
= push_constant_loc
[uniform_nr
];
1502 /* Section 5.11 of the OpenGL 4.1 spec says:
1503 * "Out-of-bounds reads return undefined values, which include
1504 * values from other variables of the active program or zero."
1505 * Just return the first push constant.
1510 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1513 brw_reg
.abs
= inst
->src
[i
].abs
;
1514 brw_reg
.negate
= inst
->src
[i
].negate
;
1516 assert(inst
->src
[i
].stride
== 0);
1517 inst
->src
[i
] = byte_offset(
1518 retype(brw_reg
, inst
->src
[i
].type
),
1519 inst
->src
[i
].subreg_offset
);
1524 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1525 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1529 fs_visitor::calculate_urb_setup()
1531 assert(stage
== MESA_SHADER_FRAGMENT
);
1532 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1533 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1535 memset(prog_data
->urb_setup
, -1,
1536 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1539 /* Figure out where each of the incoming setup attributes lands. */
1540 if (devinfo
->gen
>= 6) {
1541 if (_mesa_bitcount_64(nir
->info
.inputs_read
&
1542 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1543 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1544 * first 16 varying inputs, so we can put them wherever we want.
1545 * Just put them in order.
1547 * This is useful because it means that (a) inputs not used by the
1548 * fragment shader won't take up valuable register space, and (b) we
1549 * won't have to recompile the fragment shader if it gets paired with
1550 * a different vertex (or geometry) shader.
1552 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1553 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1554 BITFIELD64_BIT(i
)) {
1555 prog_data
->urb_setup
[i
] = urb_next
++;
1559 bool include_vue_header
=
1560 nir
->info
.inputs_read
& (VARYING_BIT_LAYER
| VARYING_BIT_VIEWPORT
);
1562 /* We have enough input varyings that the SF/SBE pipeline stage can't
1563 * arbitrarily rearrange them to suit our whim; we have to put them
1564 * in an order that matches the output of the previous pipeline stage
1565 * (geometry or vertex shader).
1567 struct brw_vue_map prev_stage_vue_map
;
1568 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1569 key
->input_slots_valid
,
1570 nir
->info
.separate_shader
);
1572 include_vue_header
? 0 : 2 * BRW_SF_URB_ENTRY_READ_OFFSET
;
1574 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1575 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1577 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1578 if (varying
!= BRW_VARYING_SLOT_PAD
&&
1579 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1580 BITFIELD64_BIT(varying
))) {
1581 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1584 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1587 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1588 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1589 /* Point size is packed into the header, not as a general attribute */
1590 if (i
== VARYING_SLOT_PSIZ
)
1593 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1594 /* The back color slot is skipped when the front color is
1595 * also written to. In addition, some slots can be
1596 * written in the vertex shader and not read in the
1597 * fragment shader. So the register number must always be
1598 * incremented, mapped or not.
1600 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1601 prog_data
->urb_setup
[i
] = urb_next
;
1607 * It's a FS only attribute, and we did interpolation for this attribute
1608 * in SF thread. So, count it here, too.
1610 * See compile_sf_prog() for more info.
1612 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1613 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1616 prog_data
->num_varying_inputs
= urb_next
;
1620 fs_visitor::assign_urb_setup()
1622 assert(stage
== MESA_SHADER_FRAGMENT
);
1623 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1625 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1627 /* Offset all the urb_setup[] index by the actual position of the
1628 * setup regs, now that the location of the constants has been chosen.
1630 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1631 if (inst
->opcode
== FS_OPCODE_LINTERP
) {
1632 assert(inst
->src
[1].file
== FIXED_GRF
);
1633 inst
->src
[1].nr
+= urb_start
;
1636 if (inst
->opcode
== FS_OPCODE_CINTERP
) {
1637 assert(inst
->src
[0].file
== FIXED_GRF
);
1638 inst
->src
[0].nr
+= urb_start
;
1642 /* Each attribute is 4 setup channels, each of which is half a reg. */
1643 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1647 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst
*inst
)
1649 for (int i
= 0; i
< inst
->sources
; i
++) {
1650 if (inst
->src
[i
].file
== ATTR
) {
1651 int grf
= payload
.num_regs
+
1652 prog_data
->curb_read_length
+
1654 inst
->src
[i
].reg_offset
;
1656 unsigned width
= inst
->src
[i
].stride
== 0 ? 1 : inst
->exec_size
;
1657 struct brw_reg reg
=
1658 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1659 inst
->src
[i
].subreg_offset
),
1660 inst
->exec_size
* inst
->src
[i
].stride
,
1661 width
, inst
->src
[i
].stride
);
1662 reg
.abs
= inst
->src
[i
].abs
;
1663 reg
.negate
= inst
->src
[i
].negate
;
1671 fs_visitor::assign_vs_urb_setup()
1673 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
1675 assert(stage
== MESA_SHADER_VERTEX
);
1677 /* Each attribute is 4 regs. */
1678 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attributes
;
1680 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1682 /* Rewrite all ATTR file references to the hw grf that they land in. */
1683 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1684 convert_attr_sources_to_hw_regs(inst
);
1689 fs_visitor::assign_tes_urb_setup()
1691 assert(stage
== MESA_SHADER_TESS_EVAL
);
1693 brw_vue_prog_data
*vue_prog_data
= (brw_vue_prog_data
*) prog_data
;
1695 first_non_payload_grf
+= 8 * vue_prog_data
->urb_read_length
;
1697 /* Rewrite all ATTR file references to HW_REGs. */
1698 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1699 convert_attr_sources_to_hw_regs(inst
);
1704 fs_visitor::assign_gs_urb_setup()
1706 assert(stage
== MESA_SHADER_GEOMETRY
);
1708 brw_vue_prog_data
*vue_prog_data
= (brw_vue_prog_data
*) prog_data
;
1710 first_non_payload_grf
+=
1711 8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
;
1713 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1714 /* Rewrite all ATTR file references to GRFs. */
1715 convert_attr_sources_to_hw_regs(inst
);
1721 * Split large virtual GRFs into separate components if we can.
1723 * This is mostly duplicated with what brw_fs_vector_splitting does,
1724 * but that's really conservative because it's afraid of doing
1725 * splitting that doesn't result in real progress after the rest of
1726 * the optimization phases, which would cause infinite looping in
1727 * optimization. We can do it once here, safely. This also has the
1728 * opportunity to split interpolated values, or maybe even uniforms,
1729 * which we don't have at the IR level.
1731 * We want to split, because virtual GRFs are what we register
1732 * allocate and spill (due to contiguousness requirements for some
1733 * instructions), and they're what we naturally generate in the
1734 * codegen process, but most virtual GRFs don't actually need to be
1735 * contiguous sets of GRFs. If we split, we'll end up with reduced
1736 * live intervals and better dead code elimination and coalescing.
1739 fs_visitor::split_virtual_grfs()
1741 int num_vars
= this->alloc
.count
;
1743 /* Count the total number of registers */
1745 int vgrf_to_reg
[num_vars
];
1746 for (int i
= 0; i
< num_vars
; i
++) {
1747 vgrf_to_reg
[i
] = reg_count
;
1748 reg_count
+= alloc
.sizes
[i
];
1751 /* An array of "split points". For each register slot, this indicates
1752 * if this slot can be separated from the previous slot. Every time an
1753 * instruction uses multiple elements of a register (as a source or
1754 * destination), we mark the used slots as inseparable. Then we go
1755 * through and split the registers into the smallest pieces we can.
1757 bool split_points
[reg_count
];
1758 memset(split_points
, 0, sizeof(split_points
));
1760 /* Mark all used registers as fully splittable */
1761 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1762 if (inst
->dst
.file
== VGRF
) {
1763 int reg
= vgrf_to_reg
[inst
->dst
.nr
];
1764 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.nr
]; j
++)
1765 split_points
[reg
+ j
] = true;
1768 for (int i
= 0; i
< inst
->sources
; i
++) {
1769 if (inst
->src
[i
].file
== VGRF
) {
1770 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
];
1771 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].nr
]; j
++)
1772 split_points
[reg
+ j
] = true;
1777 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1778 if (inst
->dst
.file
== VGRF
) {
1779 int reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.reg_offset
;
1780 for (int j
= 1; j
< inst
->regs_written
; j
++)
1781 split_points
[reg
+ j
] = false;
1783 for (int i
= 0; i
< inst
->sources
; i
++) {
1784 if (inst
->src
[i
].file
== VGRF
) {
1785 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].reg_offset
;
1786 for (int j
= 1; j
< inst
->regs_read(i
); j
++)
1787 split_points
[reg
+ j
] = false;
1792 int new_virtual_grf
[reg_count
];
1793 int new_reg_offset
[reg_count
];
1796 for (int i
= 0; i
< num_vars
; i
++) {
1797 /* The first one should always be 0 as a quick sanity check. */
1798 assert(split_points
[reg
] == false);
1801 new_reg_offset
[reg
] = 0;
1806 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1807 /* If this is a split point, reset the offset to 0 and allocate a
1808 * new virtual GRF for the previous offset many registers
1810 if (split_points
[reg
]) {
1811 assert(offset
<= MAX_VGRF_SIZE
);
1812 int grf
= alloc
.allocate(offset
);
1813 for (int k
= reg
- offset
; k
< reg
; k
++)
1814 new_virtual_grf
[k
] = grf
;
1817 new_reg_offset
[reg
] = offset
;
1822 /* The last one gets the original register number */
1823 assert(offset
<= MAX_VGRF_SIZE
);
1824 alloc
.sizes
[i
] = offset
;
1825 for (int k
= reg
- offset
; k
< reg
; k
++)
1826 new_virtual_grf
[k
] = i
;
1828 assert(reg
== reg_count
);
1830 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1831 if (inst
->dst
.file
== VGRF
) {
1832 reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.reg_offset
;
1833 inst
->dst
.nr
= new_virtual_grf
[reg
];
1834 inst
->dst
.reg_offset
= new_reg_offset
[reg
];
1835 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1837 for (int i
= 0; i
< inst
->sources
; i
++) {
1838 if (inst
->src
[i
].file
== VGRF
) {
1839 reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].reg_offset
;
1840 inst
->src
[i
].nr
= new_virtual_grf
[reg
];
1841 inst
->src
[i
].reg_offset
= new_reg_offset
[reg
];
1842 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1846 invalidate_live_intervals();
1850 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1852 * During code generation, we create tons of temporary variables, many of
1853 * which get immediately killed and are never used again. Yet, in later
1854 * optimization and analysis passes, such as compute_live_intervals, we need
1855 * to loop over all the virtual GRFs. Compacting them can save a lot of
1859 fs_visitor::compact_virtual_grfs()
1861 bool progress
= false;
1862 int remap_table
[this->alloc
.count
];
1863 memset(remap_table
, -1, sizeof(remap_table
));
1865 /* Mark which virtual GRFs are used. */
1866 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
1867 if (inst
->dst
.file
== VGRF
)
1868 remap_table
[inst
->dst
.nr
] = 0;
1870 for (int i
= 0; i
< inst
->sources
; i
++) {
1871 if (inst
->src
[i
].file
== VGRF
)
1872 remap_table
[inst
->src
[i
].nr
] = 0;
1876 /* Compact the GRF arrays. */
1878 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
1879 if (remap_table
[i
] == -1) {
1880 /* We just found an unused register. This means that we are
1881 * actually going to compact something.
1885 remap_table
[i
] = new_index
;
1886 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
1887 invalidate_live_intervals();
1892 this->alloc
.count
= new_index
;
1894 /* Patch all the instructions to use the newly renumbered registers */
1895 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1896 if (inst
->dst
.file
== VGRF
)
1897 inst
->dst
.nr
= remap_table
[inst
->dst
.nr
];
1899 for (int i
= 0; i
< inst
->sources
; i
++) {
1900 if (inst
->src
[i
].file
== VGRF
)
1901 inst
->src
[i
].nr
= remap_table
[inst
->src
[i
].nr
];
1905 /* Patch all the references to delta_xy, since they're used in register
1906 * allocation. If they're unused, switch them to BAD_FILE so we don't
1907 * think some random VGRF is delta_xy.
1909 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
1910 if (delta_xy
[i
].file
== VGRF
) {
1911 if (remap_table
[delta_xy
[i
].nr
] != -1) {
1912 delta_xy
[i
].nr
= remap_table
[delta_xy
[i
].nr
];
1914 delta_xy
[i
].file
= BAD_FILE
;
1923 * Assign UNIFORM file registers to either push constants or pull constants.
1925 * We allow a fragment shader to have more than the specified minimum
1926 * maximum number of fragment shader uniform components (64). If
1927 * there are too many of these, they'd fill up all of register space.
1928 * So, this will push some of them out to the pull constant buffer and
1929 * update the program to load them.
1932 fs_visitor::assign_constant_locations()
1934 /* Only the first compile gets to decide on locations. */
1935 if (dispatch_width
!= min_dispatch_width
)
1938 bool is_live
[uniforms
];
1939 memset(is_live
, 0, sizeof(is_live
));
1941 /* For each uniform slot, a value of true indicates that the given slot and
1942 * the next slot must remain contiguous. This is used to keep us from
1943 * splitting arrays apart.
1945 bool contiguous
[uniforms
];
1946 memset(contiguous
, 0, sizeof(contiguous
));
1948 /* First, we walk through the instructions and do two things:
1950 * 1) Figure out which uniforms are live.
1952 * 2) Mark any indirectly used ranges of registers as contiguous.
1954 * Note that we don't move constant-indexed accesses to arrays. No
1955 * testing has been done of the performance impact of this choice.
1957 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
1958 for (int i
= 0 ; i
< inst
->sources
; i
++) {
1959 if (inst
->src
[i
].file
!= UNIFORM
)
1962 int constant_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1964 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0) {
1965 assert(inst
->src
[2].ud
% 4 == 0);
1966 unsigned last
= constant_nr
+ (inst
->src
[2].ud
/ 4) - 1;
1967 assert(last
< uniforms
);
1969 for (unsigned j
= constant_nr
; j
< last
; j
++) {
1971 contiguous
[j
] = true;
1973 is_live
[last
] = true;
1975 if (constant_nr
>= 0 && constant_nr
< (int) uniforms
)
1976 is_live
[constant_nr
] = true;
1981 /* Only allow 16 registers (128 uniform components) as push constants.
1983 * Just demote the end of the list. We could probably do better
1984 * here, demoting things that are rarely used in the program first.
1986 * If changing this value, note the limitation about total_regs in
1989 const unsigned int max_push_components
= 16 * 8;
1991 /* For vulkan we don't limit the max_chunk_size. We set it to 32 float =
1992 * 128 bytes, which is the maximum vulkan push constant size.
1994 const unsigned int max_chunk_size
= 32;
1996 unsigned int num_push_constants
= 0;
1997 unsigned int num_pull_constants
= 0;
1999 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2000 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2002 int chunk_start
= -1;
2003 for (unsigned u
= 0; u
< uniforms
; u
++) {
2004 push_constant_loc
[u
] = -1;
2005 pull_constant_loc
[u
] = -1;
2010 /* This is the first live uniform in the chunk */
2011 if (chunk_start
< 0)
2014 /* If this element does not need to be contiguous with the next, we
2015 * split at this point and everthing between chunk_start and u forms a
2018 if (!contiguous
[u
]) {
2019 unsigned chunk_size
= u
- chunk_start
+ 1;
2021 if (num_push_constants
+ chunk_size
<= max_push_components
&&
2022 chunk_size
<= max_chunk_size
) {
2023 for (unsigned j
= chunk_start
; j
<= u
; j
++)
2024 push_constant_loc
[j
] = num_push_constants
++;
2026 for (unsigned j
= chunk_start
; j
<= u
; j
++)
2027 pull_constant_loc
[j
] = num_pull_constants
++;
2034 stage_prog_data
->nr_params
= num_push_constants
;
2035 stage_prog_data
->nr_pull_params
= num_pull_constants
;
2037 /* Up until now, the param[] array has been indexed by reg + reg_offset
2038 * of UNIFORM registers. Move pull constants into pull_param[] and
2039 * condense param[] to only contain the uniforms we chose to push.
2041 * NOTE: Because we are condensing the params[] array, we know that
2042 * push_constant_loc[i] <= i and we can do it in one smooth loop without
2043 * having to make a copy.
2045 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2046 const gl_constant_value
*value
= stage_prog_data
->param
[i
];
2048 if (pull_constant_loc
[i
] != -1) {
2049 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
2050 } else if (push_constant_loc
[i
] != -1) {
2051 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
2057 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2058 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2061 fs_visitor::lower_constant_loads()
2063 const unsigned index
= stage_prog_data
->binding_table
.pull_constants_start
;
2065 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2066 /* Set up the annotation tracking for new generated instructions. */
2067 const fs_builder
ibld(this, block
, inst
);
2069 for (int i
= 0; i
< inst
->sources
; i
++) {
2070 if (inst
->src
[i
].file
!= UNIFORM
)
2073 /* We'll handle this case later */
2074 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0)
2077 unsigned location
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
2078 if (location
>= uniforms
)
2079 continue; /* Out of bounds access */
2081 int pull_index
= pull_constant_loc
[location
];
2083 if (pull_index
== -1)
2086 assert(inst
->src
[i
].stride
== 0);
2088 fs_reg dst
= vgrf(glsl_type::float_type
);
2089 const fs_builder ubld
= ibld
.exec_all().group(8, 0);
2090 struct brw_reg offset
= brw_imm_ud((unsigned)(pull_index
* 4) & ~15);
2091 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
2092 dst
, brw_imm_ud(index
), offset
);
2094 /* Rewrite the instruction to use the temporary VGRF. */
2095 inst
->src
[i
].file
= VGRF
;
2096 inst
->src
[i
].nr
= dst
.nr
;
2097 inst
->src
[i
].reg_offset
= 0;
2098 inst
->src
[i
].set_smear(pull_index
& 3);
2100 brw_mark_surface_used(prog_data
, index
);
2103 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&&
2104 inst
->src
[0].file
== UNIFORM
) {
2106 unsigned location
= inst
->src
[0].nr
+ inst
->src
[0].reg_offset
;
2107 if (location
>= uniforms
)
2108 continue; /* Out of bounds access */
2110 int pull_index
= pull_constant_loc
[location
];
2112 if (pull_index
== -1)
2115 VARYING_PULL_CONSTANT_LOAD(ibld
, inst
->dst
,
2119 inst
->remove(block
);
2121 brw_mark_surface_used(prog_data
, index
);
2124 invalidate_live_intervals();
2128 fs_visitor::opt_algebraic()
2130 bool progress
= false;
2132 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2133 switch (inst
->opcode
) {
2134 case BRW_OPCODE_MOV
:
2135 if (inst
->src
[0].file
!= IMM
)
2138 if (inst
->saturate
) {
2139 if (inst
->dst
.type
!= inst
->src
[0].type
)
2140 assert(!"unimplemented: saturate mixed types");
2142 if (brw_saturate_immediate(inst
->dst
.type
,
2143 &inst
->src
[0].as_brw_reg())) {
2144 inst
->saturate
= false;
2150 case BRW_OPCODE_MUL
:
2151 if (inst
->src
[1].file
!= IMM
)
2155 if (inst
->src
[1].is_one()) {
2156 inst
->opcode
= BRW_OPCODE_MOV
;
2157 inst
->src
[1] = reg_undef
;
2163 if (inst
->src
[1].is_negative_one()) {
2164 inst
->opcode
= BRW_OPCODE_MOV
;
2165 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2166 inst
->src
[1] = reg_undef
;
2172 if (inst
->src
[1].is_zero()) {
2173 inst
->opcode
= BRW_OPCODE_MOV
;
2174 inst
->src
[0] = inst
->src
[1];
2175 inst
->src
[1] = reg_undef
;
2180 if (inst
->src
[0].file
== IMM
) {
2181 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2182 inst
->opcode
= BRW_OPCODE_MOV
;
2183 inst
->src
[0].f
*= inst
->src
[1].f
;
2184 inst
->src
[1] = reg_undef
;
2189 case BRW_OPCODE_ADD
:
2190 if (inst
->src
[1].file
!= IMM
)
2194 if (inst
->src
[1].is_zero()) {
2195 inst
->opcode
= BRW_OPCODE_MOV
;
2196 inst
->src
[1] = reg_undef
;
2201 if (inst
->src
[0].file
== IMM
) {
2202 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2203 inst
->opcode
= BRW_OPCODE_MOV
;
2204 inst
->src
[0].f
+= inst
->src
[1].f
;
2205 inst
->src
[1] = reg_undef
;
2211 if (inst
->src
[0].equals(inst
->src
[1])) {
2212 inst
->opcode
= BRW_OPCODE_MOV
;
2213 inst
->src
[1] = reg_undef
;
2218 case BRW_OPCODE_LRP
:
2219 if (inst
->src
[1].equals(inst
->src
[2])) {
2220 inst
->opcode
= BRW_OPCODE_MOV
;
2221 inst
->src
[0] = inst
->src
[1];
2222 inst
->src
[1] = reg_undef
;
2223 inst
->src
[2] = reg_undef
;
2228 case BRW_OPCODE_CMP
:
2229 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
2231 inst
->src
[0].negate
&&
2232 inst
->src
[1].is_zero()) {
2233 inst
->src
[0].abs
= false;
2234 inst
->src
[0].negate
= false;
2235 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2240 case BRW_OPCODE_SEL
:
2241 if (inst
->src
[0].equals(inst
->src
[1])) {
2242 inst
->opcode
= BRW_OPCODE_MOV
;
2243 inst
->src
[1] = reg_undef
;
2244 inst
->predicate
= BRW_PREDICATE_NONE
;
2245 inst
->predicate_inverse
= false;
2247 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2248 switch (inst
->conditional_mod
) {
2249 case BRW_CONDITIONAL_LE
:
2250 case BRW_CONDITIONAL_L
:
2251 switch (inst
->src
[1].type
) {
2252 case BRW_REGISTER_TYPE_F
:
2253 if (inst
->src
[1].f
>= 1.0f
) {
2254 inst
->opcode
= BRW_OPCODE_MOV
;
2255 inst
->src
[1] = reg_undef
;
2256 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2264 case BRW_CONDITIONAL_GE
:
2265 case BRW_CONDITIONAL_G
:
2266 switch (inst
->src
[1].type
) {
2267 case BRW_REGISTER_TYPE_F
:
2268 if (inst
->src
[1].f
<= 0.0f
) {
2269 inst
->opcode
= BRW_OPCODE_MOV
;
2270 inst
->src
[1] = reg_undef
;
2271 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2283 case BRW_OPCODE_MAD
:
2284 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2285 inst
->opcode
= BRW_OPCODE_MOV
;
2286 inst
->src
[1] = reg_undef
;
2287 inst
->src
[2] = reg_undef
;
2289 } else if (inst
->src
[0].is_zero()) {
2290 inst
->opcode
= BRW_OPCODE_MUL
;
2291 inst
->src
[0] = inst
->src
[2];
2292 inst
->src
[2] = reg_undef
;
2294 } else if (inst
->src
[1].is_one()) {
2295 inst
->opcode
= BRW_OPCODE_ADD
;
2296 inst
->src
[1] = inst
->src
[2];
2297 inst
->src
[2] = reg_undef
;
2299 } else if (inst
->src
[2].is_one()) {
2300 inst
->opcode
= BRW_OPCODE_ADD
;
2301 inst
->src
[2] = reg_undef
;
2303 } else if (inst
->src
[1].file
== IMM
&& inst
->src
[2].file
== IMM
) {
2304 inst
->opcode
= BRW_OPCODE_ADD
;
2305 inst
->src
[1].f
*= inst
->src
[2].f
;
2306 inst
->src
[2] = reg_undef
;
2310 case SHADER_OPCODE_BROADCAST
:
2311 if (is_uniform(inst
->src
[0])) {
2312 inst
->opcode
= BRW_OPCODE_MOV
;
2314 inst
->force_writemask_all
= true;
2316 } else if (inst
->src
[1].file
== IMM
) {
2317 inst
->opcode
= BRW_OPCODE_MOV
;
2318 inst
->src
[0] = component(inst
->src
[0],
2321 inst
->force_writemask_all
= true;
2330 /* Swap if src[0] is immediate. */
2331 if (progress
&& inst
->is_commutative()) {
2332 if (inst
->src
[0].file
== IMM
) {
2333 fs_reg tmp
= inst
->src
[1];
2334 inst
->src
[1] = inst
->src
[0];
2343 * Optimize sample messages that have constant zero values for the trailing
2344 * texture coordinates. We can just reduce the message length for these
2345 * instructions instead of reserving a register for it. Trailing parameters
2346 * that aren't sent default to zero anyway. This will cause the dead code
2347 * eliminator to remove the MOV instruction that would otherwise be emitted to
2348 * set up the zero value.
2351 fs_visitor::opt_zero_samples()
2353 /* Gen4 infers the texturing opcode based on the message length so we can't
2356 if (devinfo
->gen
< 5)
2359 bool progress
= false;
2361 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2362 if (!inst
->is_tex())
2365 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2367 if (load_payload
->is_head_sentinel() ||
2368 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2371 /* We don't want to remove the message header or the first parameter.
2372 * Removing the first parameter is not allowed, see the Haswell PRM
2373 * volume 7, page 149:
2375 * "Parameter 0 is required except for the sampleinfo message, which
2376 * has no parameter 0"
2378 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2379 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2380 (inst
->exec_size
/ 8) +
2381 inst
->header_size
- 1].is_zero()) {
2382 inst
->mlen
-= inst
->exec_size
/ 8;
2388 invalidate_live_intervals();
2394 * Optimize sample messages which are followed by the final RT write.
2396 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2397 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2398 * final texturing results copied to the framebuffer write payload and modify
2399 * them to write to the framebuffer directly.
2402 fs_visitor::opt_sampler_eot()
2404 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2406 if (stage
!= MESA_SHADER_FRAGMENT
)
2409 if (devinfo
->gen
< 9 && !devinfo
->is_cherryview
)
2412 /* FINISHME: It should be possible to implement this optimization when there
2413 * are multiple drawbuffers.
2415 if (key
->nr_color_regions
!= 1)
2418 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2419 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2420 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2421 assert(fb_write
->eot
);
2422 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE
);
2424 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2426 /* There wasn't one; nothing to do. */
2427 if (unlikely(tex_inst
->is_head_sentinel()) || !tex_inst
->is_tex())
2430 /* 3D Sampler » Messages » Message Format
2432 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2433 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2435 if (tex_inst
->opcode
== SHADER_OPCODE_TXS
||
2436 tex_inst
->opcode
== SHADER_OPCODE_SAMPLEINFO
||
2437 tex_inst
->opcode
== SHADER_OPCODE_LOD
||
2438 tex_inst
->opcode
== SHADER_OPCODE_TG4
||
2439 tex_inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
2442 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2443 * It's very likely to be the previous instruction.
2445 fs_inst
*load_payload
= (fs_inst
*) tex_inst
->prev
;
2446 if (load_payload
->is_head_sentinel() ||
2447 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2450 assert(!tex_inst
->eot
); /* We can't get here twice */
2451 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2453 const fs_builder
ibld(this, block
, tex_inst
);
2455 tex_inst
->offset
|= fb_write
->target
<< 24;
2456 tex_inst
->eot
= true;
2457 tex_inst
->dst
= ibld
.null_reg_ud();
2458 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2460 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2461 * to create a new LOAD_PAYLOAD command with the same sources and a space
2462 * saved for the header. Using a new destination register not only makes sure
2463 * we have enough space, but it will make sure the dead code eliminator kills
2464 * the instruction that this will replace.
2466 if (tex_inst
->header_size
!= 0) {
2467 invalidate_live_intervals();
2471 fs_reg send_header
= ibld
.vgrf(BRW_REGISTER_TYPE_F
,
2472 load_payload
->sources
+ 1);
2473 fs_reg
*new_sources
=
2474 ralloc_array(mem_ctx
, fs_reg
, load_payload
->sources
+ 1);
2476 new_sources
[0] = fs_reg();
2477 for (int i
= 0; i
< load_payload
->sources
; i
++)
2478 new_sources
[i
+1] = load_payload
->src
[i
];
2480 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2481 * requires a lot of information about the sources to appropriately figure
2482 * out the number of registers needed to be used. Given this stage in our
2483 * optimization, we may not have the appropriate GRFs required by
2484 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2485 * manually emit the instruction.
2487 fs_inst
*new_load_payload
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
,
2488 load_payload
->exec_size
,
2491 load_payload
->sources
+ 1);
2493 new_load_payload
->regs_written
= load_payload
->regs_written
+ 1;
2494 new_load_payload
->header_size
= 1;
2496 tex_inst
->header_size
= 1;
2497 tex_inst
->insert_before(cfg
->blocks
[cfg
->num_blocks
- 1], new_load_payload
);
2498 tex_inst
->src
[0] = send_header
;
2500 invalidate_live_intervals();
2505 fs_visitor::opt_register_renaming()
2507 bool progress
= false;
2510 int remap
[alloc
.count
];
2511 memset(remap
, -1, sizeof(int) * alloc
.count
);
2513 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2514 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2516 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2517 inst
->opcode
== BRW_OPCODE_WHILE
) {
2521 /* Rewrite instruction sources. */
2522 for (int i
= 0; i
< inst
->sources
; i
++) {
2523 if (inst
->src
[i
].file
== VGRF
&&
2524 remap
[inst
->src
[i
].nr
] != -1 &&
2525 remap
[inst
->src
[i
].nr
] != inst
->src
[i
].nr
) {
2526 inst
->src
[i
].nr
= remap
[inst
->src
[i
].nr
];
2531 const int dst
= inst
->dst
.nr
;
2534 inst
->dst
.file
== VGRF
&&
2535 alloc
.sizes
[inst
->dst
.nr
] == inst
->exec_size
/ 8 &&
2536 !inst
->is_partial_write()) {
2537 if (remap
[dst
] == -1) {
2540 remap
[dst
] = alloc
.allocate(inst
->exec_size
/ 8);
2541 inst
->dst
.nr
= remap
[dst
];
2544 } else if (inst
->dst
.file
== VGRF
&&
2546 remap
[dst
] != dst
) {
2547 inst
->dst
.nr
= remap
[dst
];
2553 invalidate_live_intervals();
2555 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2556 if (delta_xy
[i
].file
== VGRF
&& remap
[delta_xy
[i
].nr
] != -1) {
2557 delta_xy
[i
].nr
= remap
[delta_xy
[i
].nr
];
2566 * Remove redundant or useless discard jumps.
2568 * For example, we can eliminate jumps in the following sequence:
2570 * discard-jump (redundant with the next jump)
2571 * discard-jump (useless; jumps to the next instruction)
2575 fs_visitor::opt_redundant_discard_jumps()
2577 bool progress
= false;
2579 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2581 fs_inst
*placeholder_halt
= NULL
;
2582 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2583 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2584 placeholder_halt
= inst
;
2589 if (!placeholder_halt
)
2592 /* Delete any HALTs immediately before the placeholder halt. */
2593 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2594 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2595 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2596 prev
->remove(last_bblock
);
2601 invalidate_live_intervals();
2607 fs_visitor::compute_to_mrf()
2609 bool progress
= false;
2612 /* No MRFs on Gen >= 7. */
2613 if (devinfo
->gen
>= 7)
2616 calculate_live_intervals();
2618 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2622 if (inst
->opcode
!= BRW_OPCODE_MOV
||
2623 inst
->is_partial_write() ||
2624 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= VGRF
||
2625 inst
->dst
.type
!= inst
->src
[0].type
||
2626 inst
->src
[0].abs
|| inst
->src
[0].negate
||
2627 !inst
->src
[0].is_contiguous() ||
2628 inst
->src
[0].subreg_offset
)
2631 /* Work out which hardware MRF registers are written by this
2634 int mrf_low
= inst
->dst
.nr
& ~BRW_MRF_COMPR4
;
2636 if (inst
->dst
.nr
& BRW_MRF_COMPR4
) {
2637 mrf_high
= mrf_low
+ 4;
2638 } else if (inst
->exec_size
== 16) {
2639 mrf_high
= mrf_low
+ 1;
2644 /* Can't compute-to-MRF this GRF if someone else was going to
2647 if (this->virtual_grf_end
[inst
->src
[0].nr
] > ip
)
2650 /* Found a move of a GRF to a MRF. Let's see if we can go
2651 * rewrite the thing that made this GRF to write into the MRF.
2653 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
2654 if (scan_inst
->dst
.file
== VGRF
&&
2655 scan_inst
->dst
.nr
== inst
->src
[0].nr
) {
2656 /* Found the last thing to write our reg we want to turn
2657 * into a compute-to-MRF.
2660 /* If this one instruction didn't populate all the
2661 * channels, bail. We might be able to rewrite everything
2662 * that writes that reg, but it would require smarter
2663 * tracking to delay the rewriting until complete success.
2665 if (scan_inst
->is_partial_write())
2668 /* Things returning more than one register would need us to
2669 * understand coalescing out more than one MOV at a time.
2671 if (scan_inst
->regs_written
> scan_inst
->exec_size
/ 8)
2674 /* SEND instructions can't have MRF as a destination. */
2675 if (scan_inst
->mlen
)
2678 if (devinfo
->gen
== 6) {
2679 /* gen6 math instructions must have the destination be
2680 * GRF, so no compute-to-MRF for them.
2682 if (scan_inst
->is_math()) {
2687 if (scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
2688 /* Found the creator of our MRF's source value. */
2689 scan_inst
->dst
.file
= MRF
;
2690 scan_inst
->dst
.nr
= inst
->dst
.nr
;
2691 scan_inst
->saturate
|= inst
->saturate
;
2692 inst
->remove(block
);
2698 /* We don't handle control flow here. Most computation of
2699 * values that end up in MRFs are shortly before the MRF
2702 if (block
->start() == scan_inst
)
2705 /* You can't read from an MRF, so if someone else reads our
2706 * MRF's source GRF that we wanted to rewrite, that stops us.
2708 bool interfered
= false;
2709 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
2710 if (scan_inst
->src
[i
].file
== VGRF
&&
2711 scan_inst
->src
[i
].nr
== inst
->src
[0].nr
&&
2712 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
2719 if (scan_inst
->dst
.file
== MRF
) {
2720 /* If somebody else writes our MRF here, we can't
2721 * compute-to-MRF before that.
2723 int scan_mrf_low
= scan_inst
->dst
.nr
& ~BRW_MRF_COMPR4
;
2726 if (scan_inst
->dst
.nr
& BRW_MRF_COMPR4
) {
2727 scan_mrf_high
= scan_mrf_low
+ 4;
2728 } else if (scan_inst
->exec_size
== 16) {
2729 scan_mrf_high
= scan_mrf_low
+ 1;
2731 scan_mrf_high
= scan_mrf_low
;
2734 if (mrf_low
== scan_mrf_low
||
2735 mrf_low
== scan_mrf_high
||
2736 mrf_high
== scan_mrf_low
||
2737 mrf_high
== scan_mrf_high
) {
2742 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1) {
2743 /* Found a SEND instruction, which means that there are
2744 * live values in MRFs from base_mrf to base_mrf +
2745 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2748 if (mrf_low
>= scan_inst
->base_mrf
&&
2749 mrf_low
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2752 if (mrf_high
>= scan_inst
->base_mrf
&&
2753 mrf_high
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2761 invalidate_live_intervals();
2767 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2768 * flow. We could probably do better here with some form of divergence
2772 fs_visitor::eliminate_find_live_channel()
2774 bool progress
= false;
2777 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2778 switch (inst
->opcode
) {
2784 case BRW_OPCODE_ENDIF
:
2785 case BRW_OPCODE_WHILE
:
2789 case FS_OPCODE_DISCARD_JUMP
:
2790 /* This can potentially make control flow non-uniform until the end
2795 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2797 inst
->opcode
= BRW_OPCODE_MOV
;
2798 inst
->src
[0] = brw_imm_ud(0u);
2800 inst
->force_writemask_all
= true;
2814 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2815 * instructions to FS_OPCODE_REP_FB_WRITE.
2818 fs_visitor::emit_repclear_shader()
2820 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2822 int color_mrf
= base_mrf
+ 2;
2825 if (uniforms
== 1) {
2826 mov
= bld
.exec_all().group(4, 0)
2827 .MOV(brw_message_reg(color_mrf
),
2828 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
2830 struct brw_reg reg
=
2831 brw_reg(BRW_GENERAL_REGISTER_FILE
,
2832 2, 3, 0, 0, BRW_REGISTER_TYPE_F
,
2833 BRW_VERTICAL_STRIDE_8
,
2835 BRW_HORIZONTAL_STRIDE_4
, BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
2837 mov
= bld
.exec_all().group(4, 0)
2838 .MOV(vec4(brw_message_reg(color_mrf
)), fs_reg(reg
));
2842 if (key
->nr_color_regions
== 1) {
2843 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2844 write
->saturate
= key
->clamp_fragment_color
;
2845 write
->base_mrf
= color_mrf
;
2847 write
->header_size
= 0;
2850 assume(key
->nr_color_regions
> 0);
2851 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
2852 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2853 write
->saturate
= key
->clamp_fragment_color
;
2854 write
->base_mrf
= base_mrf
;
2856 write
->header_size
= 2;
2864 assign_constant_locations();
2865 assign_curb_setup();
2867 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2868 if (uniforms
== 1) {
2869 assert(mov
->src
[0].file
== FIXED_GRF
);
2870 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].nr
, 0);
2875 * Walks through basic blocks, looking for repeated MRF writes and
2876 * removing the later ones.
2879 fs_visitor::remove_duplicate_mrf_writes()
2881 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
2882 bool progress
= false;
2884 /* Need to update the MRF tracking for compressed instructions. */
2885 if (dispatch_width
== 16)
2888 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2890 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2891 if (inst
->is_control_flow()) {
2892 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2895 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2896 inst
->dst
.file
== MRF
) {
2897 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.nr
];
2898 if (prev_inst
&& inst
->equals(prev_inst
)) {
2899 inst
->remove(block
);
2905 /* Clear out the last-write records for MRFs that were overwritten. */
2906 if (inst
->dst
.file
== MRF
) {
2907 last_mrf_move
[inst
->dst
.nr
] = NULL
;
2910 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
2911 /* Found a SEND instruction, which will include two or fewer
2912 * implied MRF writes. We could do better here.
2914 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
2915 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
2919 /* Clear out any MRF move records whose sources got overwritten. */
2920 if (inst
->dst
.file
== VGRF
) {
2921 for (unsigned int i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
2922 if (last_mrf_move
[i
] &&
2923 last_mrf_move
[i
]->src
[0].nr
== inst
->dst
.nr
) {
2924 last_mrf_move
[i
] = NULL
;
2929 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2930 inst
->dst
.file
== MRF
&&
2931 inst
->src
[0].file
== VGRF
&&
2932 !inst
->is_partial_write()) {
2933 last_mrf_move
[inst
->dst
.nr
] = inst
;
2938 invalidate_live_intervals();
2944 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
2946 /* Clear the flag for registers that actually got read (as expected). */
2947 for (int i
= 0; i
< inst
->sources
; i
++) {
2949 if (inst
->src
[i
].file
== VGRF
|| inst
->src
[i
].file
== FIXED_GRF
) {
2950 grf
= inst
->src
[i
].nr
;
2955 if (grf
>= first_grf
&&
2956 grf
< first_grf
+ grf_len
) {
2957 deps
[grf
- first_grf
] = false;
2958 if (inst
->exec_size
== 16)
2959 deps
[grf
- first_grf
+ 1] = false;
2965 * Implements this workaround for the original 965:
2967 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2968 * check for post destination dependencies on this instruction, software
2969 * must ensure that there is no destination hazard for the case of ‘write
2970 * followed by a posted write’ shown in the following example.
2973 * 2. send r3.xy <rest of send instruction>
2976 * Due to no post-destination dependency check on the ‘send’, the above
2977 * code sequence could have two instructions (1 and 2) in flight at the
2978 * same time that both consider ‘r3’ as the target of their final writes.
2981 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
2984 int write_len
= inst
->regs_written
;
2985 int first_write_grf
= inst
->dst
.nr
;
2986 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2987 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2989 memset(needs_dep
, false, sizeof(needs_dep
));
2990 memset(needs_dep
, true, write_len
);
2992 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
2994 /* Walk backwards looking for writes to registers we're writing which
2995 * aren't read since being written. If we hit the start of the program,
2996 * we assume that there are no outstanding dependencies on entry to the
2999 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3000 /* If we hit control flow, assume that there *are* outstanding
3001 * dependencies, and force their cleanup before our instruction.
3003 if (block
->start() == scan_inst
) {
3004 for (int i
= 0; i
< write_len
; i
++) {
3006 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
3007 first_write_grf
+ i
);
3012 /* We insert our reads as late as possible on the assumption that any
3013 * instruction but a MOV that might have left us an outstanding
3014 * dependency has more latency than a MOV.
3016 if (scan_inst
->dst
.file
== VGRF
) {
3017 for (int i
= 0; i
< scan_inst
->regs_written
; i
++) {
3018 int reg
= scan_inst
->dst
.nr
+ i
;
3020 if (reg
>= first_write_grf
&&
3021 reg
< first_write_grf
+ write_len
&&
3022 needs_dep
[reg
- first_write_grf
]) {
3023 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
3024 needs_dep
[reg
- first_write_grf
] = false;
3025 if (scan_inst
->exec_size
== 16)
3026 needs_dep
[reg
- first_write_grf
+ 1] = false;
3031 /* Clear the flag for registers that actually got read (as expected). */
3032 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3034 /* Continue the loop only if we haven't resolved all the dependencies */
3036 for (i
= 0; i
< write_len
; i
++) {
3046 * Implements this workaround for the original 965:
3048 * "[DevBW, DevCL] Errata: A destination register from a send can not be
3049 * used as a destination register until after it has been sourced by an
3050 * instruction with a different destination register.
3053 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
3055 int write_len
= inst
->regs_written
;
3056 int first_write_grf
= inst
->dst
.nr
;
3057 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3058 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3060 memset(needs_dep
, false, sizeof(needs_dep
));
3061 memset(needs_dep
, true, write_len
);
3062 /* Walk forwards looking for writes to registers we're writing which aren't
3063 * read before being written.
3065 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
) {
3066 /* If we hit control flow, force resolve all remaining dependencies. */
3067 if (block
->end() == scan_inst
) {
3068 for (int i
= 0; i
< write_len
; i
++) {
3070 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3071 first_write_grf
+ i
);
3076 /* Clear the flag for registers that actually got read (as expected). */
3077 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3079 /* We insert our reads as late as possible since they're reading the
3080 * result of a SEND, which has massive latency.
3082 if (scan_inst
->dst
.file
== VGRF
&&
3083 scan_inst
->dst
.nr
>= first_write_grf
&&
3084 scan_inst
->dst
.nr
< first_write_grf
+ write_len
&&
3085 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
]) {
3086 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3088 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
] = false;
3091 /* Continue the loop only if we haven't resolved all the dependencies */
3093 for (i
= 0; i
< write_len
; i
++) {
3103 fs_visitor::insert_gen4_send_dependency_workarounds()
3105 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
3108 bool progress
= false;
3110 /* Note that we're done with register allocation, so GRF fs_regs always
3111 * have a .reg_offset of 0.
3114 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3115 if (inst
->mlen
!= 0 && inst
->dst
.file
== VGRF
) {
3116 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3117 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3123 invalidate_live_intervals();
3127 * Turns the generic expression-style uniform pull constant load instruction
3128 * into a hardware-specific series of instructions for loading a pull
3131 * The expression style allows the CSE pass before this to optimize out
3132 * repeated loads from the same offset, and gives the pre-register-allocation
3133 * scheduling full flexibility, while the conversion to native instructions
3134 * allows the post-register-allocation scheduler the best information
3137 * Note that execution masking for setting up pull constant loads is special:
3138 * the channels that need to be written are unrelated to the current execution
3139 * mask, since a later instruction will use one of the result channels as a
3140 * source operand for all 8 or 16 of its channels.
3143 fs_visitor::lower_uniform_pull_constant_loads()
3145 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3146 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3149 if (devinfo
->gen
>= 7) {
3150 /* The offset arg is a vec4-aligned immediate byte offset. */
3151 fs_reg const_offset_reg
= inst
->src
[1];
3152 assert(const_offset_reg
.file
== IMM
&&
3153 const_offset_reg
.type
== BRW_REGISTER_TYPE_UD
);
3154 assert(const_offset_reg
.ud
% 16 == 0);
3156 fs_reg payload
, offset
;
3157 if (devinfo
->gen
>= 9) {
3158 /* We have to use a message header on Skylake to get SIMD4x2
3159 * mode. Reserve space for the register.
3161 offset
= payload
= fs_reg(VGRF
, alloc
.allocate(2));
3162 offset
.reg_offset
++;
3165 offset
= payload
= fs_reg(VGRF
, alloc
.allocate(1));
3169 /* This is actually going to be a MOV, but since only the first dword
3170 * is accessed, we have a special opcode to do just that one. Note
3171 * that this needs to be an operation that will be considered a def
3172 * by live variable analysis, or register allocation will explode.
3174 fs_inst
*setup
= new(mem_ctx
) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET
,
3175 8, offset
, const_offset_reg
);
3176 setup
->force_writemask_all
= true;
3178 setup
->ir
= inst
->ir
;
3179 setup
->annotation
= inst
->annotation
;
3180 inst
->insert_before(block
, setup
);
3182 /* Similarly, this will only populate the first 4 channels of the
3183 * result register (since we only use smear values from 0-3), but we
3184 * don't tell the optimizer.
3186 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3187 inst
->src
[1] = payload
;
3188 inst
->base_mrf
= -1;
3190 invalidate_live_intervals();
3192 /* Before register allocation, we didn't tell the scheduler about the
3193 * MRF we use. We know it's safe to use this MRF because nothing
3194 * else does except for register spill/unspill, which generates and
3195 * uses its MRF within a single IR instruction.
3197 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
3204 fs_visitor::lower_load_payload()
3206 bool progress
= false;
3208 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3209 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3212 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== VGRF
);
3213 assert(inst
->saturate
== false);
3214 fs_reg dst
= inst
->dst
;
3216 /* Get rid of COMPR4. We'll add it back in if we need it */
3217 if (dst
.file
== MRF
)
3218 dst
.nr
= dst
.nr
& ~BRW_MRF_COMPR4
;
3220 const fs_builder
ibld(this, block
, inst
);
3221 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3223 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3224 if (inst
->src
[i
].file
!= BAD_FILE
) {
3225 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3226 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3227 hbld
.MOV(mov_dst
, mov_src
);
3229 dst
= offset(dst
, hbld
, 1);
3232 if (inst
->dst
.file
== MRF
&& (inst
->dst
.nr
& BRW_MRF_COMPR4
) &&
3233 inst
->exec_size
> 8) {
3234 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3235 * a straightforward copy. Instead, the result of the
3236 * LOAD_PAYLOAD is treated as interleaved and the first four
3237 * non-header sources are unpacked as:
3248 * This is used for gen <= 5 fb writes.
3250 assert(inst
->exec_size
== 16);
3251 assert(inst
->header_size
+ 4 <= inst
->sources
);
3252 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3253 if (inst
->src
[i
].file
!= BAD_FILE
) {
3254 if (devinfo
->has_compr4
) {
3255 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3256 compr4_dst
.nr
|= BRW_MRF_COMPR4
;
3257 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3259 /* Platform doesn't have COMPR4. We have to fake it */
3260 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3261 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3263 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3270 /* The loop above only ever incremented us through the first set
3271 * of 4 registers. However, thanks to the magic of COMPR4, we
3272 * actually wrote to the first 8 registers, so we need to take
3273 * that into account now.
3277 /* The COMPR4 code took care of the first 4 sources. We'll let
3278 * the regular path handle any remaining sources. Yes, we are
3279 * modifying the instruction but we're about to delete it so
3280 * this really doesn't hurt anything.
3282 inst
->header_size
+= 4;
3285 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3286 if (inst
->src
[i
].file
!= BAD_FILE
)
3287 ibld
.MOV(retype(dst
, inst
->src
[i
].type
), inst
->src
[i
]);
3288 dst
= offset(dst
, ibld
, 1);
3291 inst
->remove(block
);
3296 invalidate_live_intervals();
3302 fs_visitor::lower_integer_multiplication()
3304 bool progress
= false;
3306 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3307 const fs_builder
ibld(this, block
, inst
);
3309 if (inst
->opcode
== BRW_OPCODE_MUL
) {
3310 if (inst
->dst
.is_accumulator() ||
3311 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3312 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3315 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit
3316 * operation directly, but CHV/BXT cannot.
3318 if (devinfo
->gen
>= 8 &&
3319 !devinfo
->is_cherryview
&& !devinfo
->is_broxton
)
3322 if (inst
->src
[1].file
== IMM
&&
3323 inst
->src
[1].ud
< (1 << 16)) {
3324 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3325 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3328 * If multiplying by an immediate value that fits in 16-bits, do a
3329 * single MUL instruction with that value in the proper location.
3331 if (devinfo
->gen
< 7) {
3332 fs_reg
imm(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3334 ibld
.MOV(imm
, inst
->src
[1]);
3335 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3337 ibld
.MUL(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3340 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3341 * do 32-bit integer multiplication in one instruction, but instead
3342 * must do a sequence (which actually calculates a 64-bit result):
3344 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3345 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3346 * mov(8) g2<1>D acc0<8,8,1>D
3348 * But on Gen > 6, the ability to use second accumulator register
3349 * (acc1) for non-float data types was removed, preventing a simple
3350 * implementation in SIMD16. A 16-channel result can be calculated by
3351 * executing the three instructions twice in SIMD8, once with quarter
3352 * control of 1Q for the first eight channels and again with 2Q for
3353 * the second eight channels.
3355 * Which accumulator register is implicitly accessed (by AccWrEnable
3356 * for instance) is determined by the quarter control. Unfortunately
3357 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3358 * implicit accumulator access by an instruction with 2Q will access
3359 * acc1 regardless of whether the data type is usable in acc1.
3361 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3362 * integer data types.
3364 * Since we only want the low 32-bits of the result, we can do two
3365 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3366 * adjust the high result and add them (like the mach is doing):
3368 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3369 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3370 * shl(8) g9<1>D g8<8,8,1>D 16D
3371 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3373 * We avoid the shl instruction by realizing that we only want to add
3374 * the low 16-bits of the "high" result to the high 16-bits of the
3375 * "low" result and using proper regioning on the add:
3377 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3378 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3379 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3381 * Since it does not use the (single) accumulator register, we can
3382 * schedule multi-component multiplications much better.
3385 fs_reg orig_dst
= inst
->dst
;
3386 if (orig_dst
.is_null() || orig_dst
.file
== MRF
) {
3387 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3390 fs_reg low
= inst
->dst
;
3391 fs_reg
high(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3394 if (devinfo
->gen
>= 7) {
3395 fs_reg src1_0_w
= inst
->src
[1];
3396 fs_reg src1_1_w
= inst
->src
[1];
3398 if (inst
->src
[1].file
== IMM
) {
3399 src1_0_w
.ud
&= 0xffff;
3402 src1_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3403 if (src1_0_w
.stride
!= 0) {
3404 assert(src1_0_w
.stride
== 1);
3405 src1_0_w
.stride
= 2;
3408 src1_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3409 if (src1_1_w
.stride
!= 0) {
3410 assert(src1_1_w
.stride
== 1);
3411 src1_1_w
.stride
= 2;
3413 src1_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3415 ibld
.MUL(low
, inst
->src
[0], src1_0_w
);
3416 ibld
.MUL(high
, inst
->src
[0], src1_1_w
);
3418 fs_reg src0_0_w
= inst
->src
[0];
3419 fs_reg src0_1_w
= inst
->src
[0];
3421 src0_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3422 if (src0_0_w
.stride
!= 0) {
3423 assert(src0_0_w
.stride
== 1);
3424 src0_0_w
.stride
= 2;
3427 src0_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3428 if (src0_1_w
.stride
!= 0) {
3429 assert(src0_1_w
.stride
== 1);
3430 src0_1_w
.stride
= 2;
3432 src0_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3434 ibld
.MUL(low
, src0_0_w
, inst
->src
[1]);
3435 ibld
.MUL(high
, src0_1_w
, inst
->src
[1]);
3438 fs_reg dst
= inst
->dst
;
3439 dst
.type
= BRW_REGISTER_TYPE_UW
;
3440 dst
.subreg_offset
= 2;
3443 high
.type
= BRW_REGISTER_TYPE_UW
;
3446 low
.type
= BRW_REGISTER_TYPE_UW
;
3447 low
.subreg_offset
= 2;
3450 ibld
.ADD(dst
, low
, high
);
3452 if (inst
->conditional_mod
|| orig_dst
.file
== MRF
) {
3453 set_condmod(inst
->conditional_mod
,
3454 ibld
.MOV(orig_dst
, inst
->dst
));
3458 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
3459 /* Should have been lowered to 8-wide. */
3460 assert(inst
->exec_size
<= 8);
3461 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
),
3463 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
3464 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3466 if (devinfo
->gen
>= 8) {
3467 /* Until Gen8, integer multiplies read 32-bits from one source,
3468 * and 16-bits from the other, and relying on the MACH instruction
3469 * to generate the high bits of the result.
3471 * On Gen8, the multiply instruction does a full 32x32-bit
3472 * multiply, but in order to do a 64-bit multiply we can simulate
3473 * the previous behavior and then use a MACH instruction.
3475 * FINISHME: Don't use source modifiers on src1.
3477 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
3478 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3479 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
3480 mul
->src
[1].stride
*= 2;
3482 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
3483 inst
->force_sechalf
) {
3484 /* Among other things the quarter control bits influence which
3485 * accumulator register is used by the hardware for instructions
3486 * that access the accumulator implicitly (e.g. MACH). A
3487 * second-half instruction would normally map to acc1, which
3488 * doesn't exist on Gen7 and up (the hardware does emulate it for
3489 * floating-point instructions *only* by taking advantage of the
3490 * extra precision of acc0 not normally used for floating point
3493 * HSW and up are careful enough not to try to access an
3494 * accumulator register that doesn't exist, but on earlier Gen7
3495 * hardware we need to make sure that the quarter control bits are
3496 * zero to avoid non-deterministic behaviour and emit an extra MOV
3497 * to get the result masked correctly according to the current
3500 mach
->force_sechalf
= false;
3501 mach
->force_writemask_all
= true;
3502 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
3503 ibld
.MOV(inst
->dst
, mach
->dst
);
3509 inst
->remove(block
);
3514 invalidate_live_intervals();
3520 fs_visitor::lower_minmax()
3522 assert(devinfo
->gen
< 6);
3524 bool progress
= false;
3526 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3527 const fs_builder
ibld(this, block
, inst
);
3529 if (inst
->opcode
== BRW_OPCODE_SEL
&&
3530 inst
->predicate
== BRW_PREDICATE_NONE
) {
3531 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
3532 * the original SEL.L/GE instruction
3534 ibld
.CMP(ibld
.null_reg_d(), inst
->src
[0], inst
->src
[1],
3535 inst
->conditional_mod
);
3536 inst
->predicate
= BRW_PREDICATE_NORMAL
;
3537 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
3544 invalidate_live_intervals();
3550 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
3551 fs_reg
*dst
, fs_reg color
, unsigned components
)
3553 if (key
->clamp_fragment_color
) {
3554 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
3555 assert(color
.type
== BRW_REGISTER_TYPE_F
);
3557 for (unsigned i
= 0; i
< components
; i
++)
3559 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
3564 for (unsigned i
= 0; i
< components
; i
++)
3565 dst
[i
] = offset(color
, bld
, i
);
3569 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
3570 const brw_wm_prog_data
*prog_data
,
3571 const brw_wm_prog_key
*key
,
3572 const fs_visitor::thread_payload
&payload
)
3574 assert(inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
3575 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3576 const fs_reg
&color0
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR0
];
3577 const fs_reg
&color1
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
];
3578 const fs_reg
&src0_alpha
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA
];
3579 const fs_reg
&src_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
];
3580 const fs_reg
&dst_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_DST_DEPTH
];
3581 const fs_reg
&src_stencil
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_STENCIL
];
3582 fs_reg sample_mask
= inst
->src
[FB_WRITE_LOGICAL_SRC_OMASK
];
3583 const unsigned components
=
3584 inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
3586 /* We can potentially have a message length of up to 15, so we have to set
3587 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3590 int header_size
= 2, payload_header_size
;
3591 unsigned length
= 0;
3593 /* From the Sandy Bridge PRM, volume 4, page 198:
3595 * "Dispatched Pixel Enables. One bit per pixel indicating
3596 * which pixels were originally enabled when the thread was
3597 * dispatched. This field is only required for the end-of-
3598 * thread message and on all dual-source messages."
3600 if (devinfo
->gen
>= 6 &&
3601 (devinfo
->is_haswell
|| devinfo
->gen
>= 8 || !prog_data
->uses_kill
) &&
3602 color1
.file
== BAD_FILE
&&
3603 key
->nr_color_regions
== 1) {
3607 if (header_size
!= 0) {
3608 assert(header_size
== 2);
3609 /* Allocate 2 registers for a header */
3613 if (payload
.aa_dest_stencil_reg
) {
3614 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1));
3615 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3616 .MOV(sources
[length
],
3617 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0)));
3621 if (prog_data
->uses_omask
) {
3622 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1),
3623 BRW_REGISTER_TYPE_UD
);
3625 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3626 * relevant. Since it's unsigned single words one vgrf is always
3627 * 16-wide, but only the lower or higher 8 channels will be used by the
3628 * hardware when doing a SIMD8 write depending on whether we have
3629 * selected the subspans for the first or second half respectively.
3631 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
3632 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
3633 sample_mask
.stride
*= 2;
3635 bld
.exec_all().annotate("FB write oMask")
3636 .MOV(half(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
3637 inst
->force_sechalf
),
3642 payload_header_size
= length
;
3644 if (src0_alpha
.file
!= BAD_FILE
) {
3645 /* FIXME: This is being passed at the wrong location in the payload and
3646 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3647 * It's supposed to be immediately before oMask but there seems to be no
3648 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3649 * requires header sources to form a contiguous segment at the beginning
3650 * of the message and src0_alpha has per-channel semantics.
3652 setup_color_payload(bld
, key
, &sources
[length
], src0_alpha
, 1);
3656 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
3659 if (color1
.file
!= BAD_FILE
) {
3660 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
3664 if (src_depth
.file
!= BAD_FILE
) {
3665 sources
[length
] = src_depth
;
3669 if (dst_depth
.file
!= BAD_FILE
) {
3670 sources
[length
] = dst_depth
;
3674 if (src_stencil
.file
!= BAD_FILE
) {
3675 assert(devinfo
->gen
>= 9);
3676 assert(bld
.dispatch_width() != 16);
3678 /* XXX: src_stencil is only available on gen9+. dst_depth is never
3679 * available on gen9+. As such it's impossible to have both enabled at the
3680 * same time and therefore length cannot overrun the array.
3682 assert(length
< 15);
3684 sources
[length
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3685 bld
.exec_all().annotate("FB write OS")
3686 .emit(FS_OPCODE_PACK_STENCIL_REF
, sources
[length
],
3687 retype(src_stencil
, BRW_REGISTER_TYPE_UB
));
3692 if (devinfo
->gen
>= 7) {
3693 /* Send from the GRF */
3694 fs_reg payload
= fs_reg(VGRF
, -1, BRW_REGISTER_TYPE_F
);
3695 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
3696 payload
.nr
= bld
.shader
->alloc
.allocate(load
->regs_written
);
3697 load
->dst
= payload
;
3699 inst
->src
[0] = payload
;
3700 inst
->resize_sources(1);
3701 inst
->base_mrf
= -1;
3703 /* Send from the MRF */
3704 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3705 sources
, length
, payload_header_size
);
3707 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3708 * will do this for us if we just give it a COMPR4 destination.
3710 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
3711 load
->dst
.nr
|= BRW_MRF_COMPR4
;
3713 inst
->resize_sources(0);
3717 inst
->opcode
= FS_OPCODE_FB_WRITE
;
3718 inst
->mlen
= load
->regs_written
;
3719 inst
->header_size
= header_size
;
3723 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3724 const fs_reg
&coordinate
,
3725 const fs_reg
&shadow_c
,
3726 const fs_reg
&lod
, const fs_reg
&lod2
,
3727 const fs_reg
&surface
,
3728 const fs_reg
&sampler
,
3729 unsigned coord_components
,
3730 unsigned grad_components
)
3732 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
3733 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
3734 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
3735 fs_reg msg_end
= msg_begin
;
3738 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
3740 for (unsigned i
= 0; i
< coord_components
; i
++)
3741 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
3742 offset(coordinate
, bld
, i
));
3744 msg_end
= offset(msg_end
, bld
, coord_components
);
3746 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3747 * require all three components to be present and zero if they are unused.
3749 if (coord_components
> 0 &&
3750 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
3751 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
3752 for (unsigned i
= coord_components
; i
< 3; i
++)
3753 bld
.MOV(offset(msg_end
, bld
, i
), brw_imm_f(0.0f
));
3755 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
3758 if (op
== SHADER_OPCODE_TXD
) {
3759 /* TXD unsupported in SIMD16 mode. */
3760 assert(bld
.dispatch_width() == 8);
3762 /* the slots for u and v are always present, but r is optional */
3763 if (coord_components
< 2)
3764 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
3767 * dPdx = dudx, dvdx, drdx
3768 * dPdy = dudy, dvdy, drdy
3770 * 1-arg: Does not exist.
3772 * 2-arg: dudx dvdx dudy dvdy
3773 * dPdx.x dPdx.y dPdy.x dPdy.y
3776 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3777 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3778 * m5 m6 m7 m8 m9 m10
3780 for (unsigned i
= 0; i
< grad_components
; i
++)
3781 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
3783 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3785 for (unsigned i
= 0; i
< grad_components
; i
++)
3786 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
3788 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3792 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3793 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3795 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
3796 bld
.dispatch_width() == 16);
3798 const brw_reg_type type
=
3799 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
3800 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
3801 bld
.MOV(retype(msg_end
, type
), lod
);
3802 msg_end
= offset(msg_end
, bld
, 1);
3805 if (shadow_c
.file
!= BAD_FILE
) {
3806 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
3807 /* There's no plain shadow compare message, so we use shadow
3808 * compare with a bias of 0.0.
3810 bld
.MOV(msg_end
, brw_imm_f(0.0f
));
3811 msg_end
= offset(msg_end
, bld
, 1);
3814 bld
.MOV(msg_end
, shadow_c
);
3815 msg_end
= offset(msg_end
, bld
, 1);
3819 inst
->src
[0] = reg_undef
;
3820 inst
->src
[1] = surface
;
3821 inst
->src
[2] = sampler
;
3822 inst
->resize_sources(3);
3823 inst
->base_mrf
= msg_begin
.nr
;
3824 inst
->mlen
= msg_end
.nr
- msg_begin
.nr
;
3825 inst
->header_size
= 1;
3829 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3831 const fs_reg
&shadow_c
,
3832 fs_reg lod
, fs_reg lod2
,
3833 const fs_reg
&sample_index
,
3834 const fs_reg
&surface
,
3835 const fs_reg
&sampler
,
3836 const fs_reg
&offset_value
,
3837 unsigned coord_components
,
3838 unsigned grad_components
)
3840 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
3841 fs_reg msg_coords
= message
;
3842 unsigned header_size
= 0;
3844 if (offset_value
.file
!= BAD_FILE
) {
3845 /* The offsets set up by the visitor are in the m1 header, so we can't
3852 for (unsigned i
= 0; i
< coord_components
; i
++) {
3853 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
), coordinate
);
3854 coordinate
= offset(coordinate
, bld
, 1);
3856 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
3857 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
3859 if (shadow_c
.file
!= BAD_FILE
) {
3860 fs_reg msg_shadow
= msg_lod
;
3861 bld
.MOV(msg_shadow
, shadow_c
);
3862 msg_lod
= offset(msg_shadow
, bld
, 1);
3867 case SHADER_OPCODE_TXL
:
3869 bld
.MOV(msg_lod
, lod
);
3870 msg_end
= offset(msg_lod
, bld
, 1);
3872 case SHADER_OPCODE_TXD
:
3875 * dPdx = dudx, dvdx, drdx
3876 * dPdy = dudy, dvdy, drdy
3878 * Load up these values:
3879 * - dudx dudy dvdx dvdy drdx drdy
3880 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3883 for (unsigned i
= 0; i
< grad_components
; i
++) {
3884 bld
.MOV(msg_end
, lod
);
3885 lod
= offset(lod
, bld
, 1);
3886 msg_end
= offset(msg_end
, bld
, 1);
3888 bld
.MOV(msg_end
, lod2
);
3889 lod2
= offset(lod2
, bld
, 1);
3890 msg_end
= offset(msg_end
, bld
, 1);
3893 case SHADER_OPCODE_TXS
:
3894 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
3895 bld
.MOV(msg_lod
, lod
);
3896 msg_end
= offset(msg_lod
, bld
, 1);
3898 case SHADER_OPCODE_TXF
:
3899 msg_lod
= offset(msg_coords
, bld
, 3);
3900 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
3901 msg_end
= offset(msg_lod
, bld
, 1);
3903 case SHADER_OPCODE_TXF_CMS
:
3904 msg_lod
= offset(msg_coords
, bld
, 3);
3906 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), brw_imm_ud(0u));
3908 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
3909 msg_end
= offset(msg_lod
, bld
, 2);
3916 inst
->src
[0] = reg_undef
;
3917 inst
->src
[1] = surface
;
3918 inst
->src
[2] = sampler
;
3919 inst
->resize_sources(3);
3920 inst
->base_mrf
= message
.nr
;
3921 inst
->mlen
= msg_end
.nr
- message
.nr
;
3922 inst
->header_size
= header_size
;
3924 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3925 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3929 is_high_sampler(const struct brw_device_info
*devinfo
, const fs_reg
&sampler
)
3931 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
3934 return sampler
.file
!= IMM
|| sampler
.ud
>= 16;
3938 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3940 const fs_reg
&shadow_c
,
3941 fs_reg lod
, fs_reg lod2
,
3942 const fs_reg
&sample_index
,
3944 const fs_reg
&surface
,
3945 const fs_reg
&sampler
,
3946 fs_reg offset_value
,
3947 unsigned coord_components
,
3948 unsigned grad_components
)
3950 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3951 int reg_width
= bld
.dispatch_width() / 8;
3952 unsigned header_size
= 0, length
= 0;
3953 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
3954 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
3955 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
3957 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
3958 offset_value
.file
!= BAD_FILE
||
3959 is_high_sampler(devinfo
, sampler
)) {
3960 /* For general texture offsets (no txf workaround), we need a header to
3961 * put them in. Note that we're only reserving space for it in the
3962 * message payload as it will be initialized implicitly by the
3965 * TG4 needs to place its channel select in the header, for interaction
3966 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3967 * larger sampler numbers we need to offset the Sampler State Pointer in
3971 sources
[0] = fs_reg();
3975 if (shadow_c
.file
!= BAD_FILE
) {
3976 bld
.MOV(sources
[length
], shadow_c
);
3980 bool coordinate_done
= false;
3982 /* The sampler can only meaningfully compute LOD for fragment shader
3983 * messages. For all other stages, we change the opcode to TXL and
3984 * hardcode the LOD to 0.
3986 if (bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
&&
3987 op
== SHADER_OPCODE_TEX
) {
3988 op
= SHADER_OPCODE_TXL
;
3989 lod
= brw_imm_f(0.0f
);
3992 /* Set up the LOD info */
3995 case SHADER_OPCODE_TXL
:
3996 bld
.MOV(sources
[length
], lod
);
3999 case SHADER_OPCODE_TXD
:
4000 /* TXD should have been lowered in SIMD16 mode. */
4001 assert(bld
.dispatch_width() == 8);
4003 /* Load dPdx and the coordinate together:
4004 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
4006 for (unsigned i
= 0; i
< coord_components
; i
++) {
4007 bld
.MOV(sources
[length
], coordinate
);
4008 coordinate
= offset(coordinate
, bld
, 1);
4011 /* For cube map array, the coordinate is (u,v,r,ai) but there are
4012 * only derivatives for (u, v, r).
4014 if (i
< grad_components
) {
4015 bld
.MOV(sources
[length
], lod
);
4016 lod
= offset(lod
, bld
, 1);
4019 bld
.MOV(sources
[length
], lod2
);
4020 lod2
= offset(lod2
, bld
, 1);
4025 coordinate_done
= true;
4027 case SHADER_OPCODE_TXS
:
4028 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
4031 case SHADER_OPCODE_TXF
:
4032 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
4033 * On Gen9 they are u, v, lod, r
4035 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
4036 coordinate
= offset(coordinate
, bld
, 1);
4039 if (devinfo
->gen
>= 9) {
4040 if (coord_components
>= 2) {
4041 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
4042 coordinate
= offset(coordinate
, bld
, 1);
4047 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
4050 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++) {
4051 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
4052 coordinate
= offset(coordinate
, bld
, 1);
4056 coordinate_done
= true;
4058 case SHADER_OPCODE_TXF_CMS
:
4059 case SHADER_OPCODE_TXF_CMS_W
:
4060 case SHADER_OPCODE_TXF_UMS
:
4061 case SHADER_OPCODE_TXF_MCS
:
4062 if (op
== SHADER_OPCODE_TXF_UMS
||
4063 op
== SHADER_OPCODE_TXF_CMS
||
4064 op
== SHADER_OPCODE_TXF_CMS_W
) {
4065 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
4069 if (op
== SHADER_OPCODE_TXF_CMS
|| op
== SHADER_OPCODE_TXF_CMS_W
) {
4070 /* Data from the multisample control surface. */
4071 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
4074 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
4077 if (op
== SHADER_OPCODE_TXF_CMS_W
) {
4078 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
),
4081 offset(mcs
, bld
, 1));
4086 /* There is no offsetting for this message; just copy in the integer
4087 * texture coordinates.
4089 for (unsigned i
= 0; i
< coord_components
; i
++) {
4090 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
4091 coordinate
= offset(coordinate
, bld
, 1);
4095 coordinate_done
= true;
4097 case SHADER_OPCODE_TG4_OFFSET
:
4098 /* gather4_po_c should have been lowered in SIMD16 mode. */
4099 assert(bld
.dispatch_width() == 8 || shadow_c
.file
== BAD_FILE
);
4101 /* More crazy intermixing */
4102 for (unsigned i
= 0; i
< 2; i
++) { /* u, v */
4103 bld
.MOV(sources
[length
], coordinate
);
4104 coordinate
= offset(coordinate
, bld
, 1);
4108 for (unsigned i
= 0; i
< 2; i
++) { /* offu, offv */
4109 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
);
4110 offset_value
= offset(offset_value
, bld
, 1);
4114 if (coord_components
== 3) { /* r if present */
4115 bld
.MOV(sources
[length
], coordinate
);
4116 coordinate
= offset(coordinate
, bld
, 1);
4120 coordinate_done
= true;
4126 /* Set up the coordinate (except for cases where it was done above) */
4127 if (!coordinate_done
) {
4128 for (unsigned i
= 0; i
< coord_components
; i
++) {
4129 bld
.MOV(sources
[length
], coordinate
);
4130 coordinate
= offset(coordinate
, bld
, 1);
4137 mlen
= length
* reg_width
- header_size
;
4139 mlen
= length
* reg_width
;
4141 const fs_reg src_payload
= fs_reg(VGRF
, bld
.shader
->alloc
.allocate(mlen
),
4142 BRW_REGISTER_TYPE_F
);
4143 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
4145 /* Generate the SEND. */
4147 inst
->src
[0] = src_payload
;
4148 inst
->src
[1] = surface
;
4149 inst
->src
[2] = sampler
;
4150 inst
->resize_sources(3);
4151 inst
->base_mrf
= -1;
4153 inst
->header_size
= header_size
;
4155 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4156 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
4160 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
4162 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
4163 const fs_reg
&coordinate
= inst
->src
[TEX_LOGICAL_SRC_COORDINATE
];
4164 const fs_reg
&shadow_c
= inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
];
4165 const fs_reg
&lod
= inst
->src
[TEX_LOGICAL_SRC_LOD
];
4166 const fs_reg
&lod2
= inst
->src
[TEX_LOGICAL_SRC_LOD2
];
4167 const fs_reg
&sample_index
= inst
->src
[TEX_LOGICAL_SRC_SAMPLE_INDEX
];
4168 const fs_reg
&mcs
= inst
->src
[TEX_LOGICAL_SRC_MCS
];
4169 const fs_reg
&surface
= inst
->src
[TEX_LOGICAL_SRC_SURFACE
];
4170 const fs_reg
&sampler
= inst
->src
[TEX_LOGICAL_SRC_SAMPLER
];
4171 const fs_reg
&offset_value
= inst
->src
[TEX_LOGICAL_SRC_OFFSET_VALUE
];
4172 assert(inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
);
4173 const unsigned coord_components
= inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
4174 assert(inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
4175 const unsigned grad_components
= inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
4177 if (devinfo
->gen
>= 7) {
4178 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
4179 shadow_c
, lod
, lod2
, sample_index
,
4180 mcs
, surface
, sampler
, offset_value
,
4181 coord_components
, grad_components
);
4182 } else if (devinfo
->gen
>= 5) {
4183 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
4184 shadow_c
, lod
, lod2
, sample_index
,
4185 surface
, sampler
, offset_value
,
4186 coord_components
, grad_components
);
4188 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
4189 shadow_c
, lod
, lod2
,
4191 coord_components
, grad_components
);
4196 * Initialize the header present in some typed and untyped surface
4200 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
4202 fs_builder ubld
= bld
.exec_all().group(8, 0);
4203 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4204 ubld
.MOV(dst
, brw_imm_d(0));
4205 ubld
.MOV(component(dst
, 7), sample_mask
);
4210 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4211 const fs_reg
&sample_mask
)
4213 /* Get the logical send arguments. */
4214 const fs_reg
&addr
= inst
->src
[0];
4215 const fs_reg
&src
= inst
->src
[1];
4216 const fs_reg
&surface
= inst
->src
[2];
4217 const UNUSED fs_reg
&dims
= inst
->src
[3];
4218 const fs_reg
&arg
= inst
->src
[4];
4220 /* Calculate the total number of components of the payload. */
4221 const unsigned addr_sz
= inst
->components_read(0);
4222 const unsigned src_sz
= inst
->components_read(1);
4223 const unsigned header_sz
= (sample_mask
.file
== BAD_FILE
? 0 : 1);
4224 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
4226 /* Allocate space for the payload. */
4227 fs_reg
*const components
= new fs_reg
[sz
];
4228 const fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
4231 /* Construct the payload. */
4233 components
[n
++] = emit_surface_header(bld
, sample_mask
);
4235 for (unsigned i
= 0; i
< addr_sz
; i
++)
4236 components
[n
++] = offset(addr
, bld
, i
);
4238 for (unsigned i
= 0; i
< src_sz
; i
++)
4239 components
[n
++] = offset(src
, bld
, i
);
4241 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
4243 /* Update the original instruction. */
4245 inst
->mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
4246 inst
->header_size
= header_sz
;
4248 inst
->src
[0] = payload
;
4249 inst
->src
[1] = surface
;
4251 inst
->resize_sources(3);
4253 delete[] components
;
4257 fs_visitor::lower_logical_sends()
4259 bool progress
= false;
4261 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4262 const fs_builder
ibld(this, block
, inst
);
4264 switch (inst
->opcode
) {
4265 case FS_OPCODE_FB_WRITE_LOGICAL
:
4266 assert(stage
== MESA_SHADER_FRAGMENT
);
4267 lower_fb_write_logical_send(ibld
, inst
,
4268 (const brw_wm_prog_data
*)prog_data
,
4269 (const brw_wm_prog_key
*)key
,
4273 case SHADER_OPCODE_TEX_LOGICAL
:
4274 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
4277 case SHADER_OPCODE_TXD_LOGICAL
:
4278 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
4281 case SHADER_OPCODE_TXF_LOGICAL
:
4282 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
4285 case SHADER_OPCODE_TXL_LOGICAL
:
4286 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
4289 case SHADER_OPCODE_TXS_LOGICAL
:
4290 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
4293 case FS_OPCODE_TXB_LOGICAL
:
4294 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
4297 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
4298 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
4301 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
4302 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS_W
);
4305 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
4306 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
4309 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
4310 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
4313 case SHADER_OPCODE_LOD_LOGICAL
:
4314 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
4317 case SHADER_OPCODE_TG4_LOGICAL
:
4318 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
4321 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
4322 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
4325 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
4326 lower_surface_logical_send(ibld
, inst
,
4327 SHADER_OPCODE_UNTYPED_SURFACE_READ
,
4331 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
4332 lower_surface_logical_send(ibld
, inst
,
4333 SHADER_OPCODE_UNTYPED_SURFACE_WRITE
,
4334 ibld
.sample_mask_reg());
4337 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
4338 lower_surface_logical_send(ibld
, inst
,
4339 SHADER_OPCODE_UNTYPED_ATOMIC
,
4340 ibld
.sample_mask_reg());
4343 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4344 lower_surface_logical_send(ibld
, inst
,
4345 SHADER_OPCODE_TYPED_SURFACE_READ
,
4349 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4350 lower_surface_logical_send(ibld
, inst
,
4351 SHADER_OPCODE_TYPED_SURFACE_WRITE
,
4352 ibld
.sample_mask_reg());
4355 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4356 lower_surface_logical_send(ibld
, inst
,
4357 SHADER_OPCODE_TYPED_ATOMIC
,
4358 ibld
.sample_mask_reg());
4369 invalidate_live_intervals();
4375 * Get the closest native SIMD width supported by the hardware for instruction
4376 * \p inst. The instruction will be left untouched by
4377 * fs_visitor::lower_simd_width() if the returned value is equal to the
4378 * original execution size.
4381 get_lowered_simd_width(const struct brw_device_info
*devinfo
,
4382 const fs_inst
*inst
)
4384 switch (inst
->opcode
) {
4385 case BRW_OPCODE_MOV
:
4386 case BRW_OPCODE_SEL
:
4387 case BRW_OPCODE_NOT
:
4388 case BRW_OPCODE_AND
:
4390 case BRW_OPCODE_XOR
:
4391 case BRW_OPCODE_SHR
:
4392 case BRW_OPCODE_SHL
:
4393 case BRW_OPCODE_ASR
:
4394 case BRW_OPCODE_CMP
:
4395 case BRW_OPCODE_CMPN
:
4396 case BRW_OPCODE_CSEL
:
4397 case BRW_OPCODE_F32TO16
:
4398 case BRW_OPCODE_F16TO32
:
4399 case BRW_OPCODE_BFREV
:
4400 case BRW_OPCODE_BFE
:
4401 case BRW_OPCODE_BFI1
:
4402 case BRW_OPCODE_BFI2
:
4403 case BRW_OPCODE_ADD
:
4404 case BRW_OPCODE_MUL
:
4405 case BRW_OPCODE_AVG
:
4406 case BRW_OPCODE_FRC
:
4407 case BRW_OPCODE_RNDU
:
4408 case BRW_OPCODE_RNDD
:
4409 case BRW_OPCODE_RNDE
:
4410 case BRW_OPCODE_RNDZ
:
4411 case BRW_OPCODE_LZD
:
4412 case BRW_OPCODE_FBH
:
4413 case BRW_OPCODE_FBL
:
4414 case BRW_OPCODE_CBIT
:
4415 case BRW_OPCODE_SAD2
:
4416 case BRW_OPCODE_MAD
:
4417 case BRW_OPCODE_LRP
:
4418 case SHADER_OPCODE_RCP
:
4419 case SHADER_OPCODE_RSQ
:
4420 case SHADER_OPCODE_SQRT
:
4421 case SHADER_OPCODE_EXP2
:
4422 case SHADER_OPCODE_LOG2
:
4423 case SHADER_OPCODE_POW
:
4424 case SHADER_OPCODE_INT_QUOTIENT
:
4425 case SHADER_OPCODE_INT_REMAINDER
:
4426 case SHADER_OPCODE_SIN
:
4427 case SHADER_OPCODE_COS
: {
4428 /* According to the PRMs:
4429 * "A. In Direct Addressing mode, a source cannot span more than 2
4430 * adjacent GRF registers.
4431 * B. A destination cannot span more than 2 adjacent GRF registers."
4433 * Look for the source or destination with the largest register region
4434 * which is the one that is going to limit the overal execution size of
4435 * the instruction due to this rule.
4437 unsigned reg_count
= inst
->regs_written
;
4439 for (unsigned i
= 0; i
< inst
->sources
; i
++)
4440 reg_count
= MAX2(reg_count
, (unsigned)inst
->regs_read(i
));
4442 /* Calculate the maximum execution size of the instruction based on the
4443 * factor by which it goes over the hardware limit of 2 GRFs.
4445 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
4447 case SHADER_OPCODE_MULH
:
4448 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
4449 * is 8-wide on Gen7+.
4451 return (devinfo
->gen
>= 7 ? 8 : inst
->exec_size
);
4453 case FS_OPCODE_FB_WRITE_LOGICAL
:
4454 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4457 assert(devinfo
->gen
!= 6 ||
4458 inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
].file
== BAD_FILE
||
4459 inst
->exec_size
== 8);
4460 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4461 return (inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
].file
!= BAD_FILE
?
4462 8 : inst
->exec_size
);
4464 case SHADER_OPCODE_TXD_LOGICAL
:
4465 /* TXD is unsupported in SIMD16 mode. */
4468 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
: {
4469 /* gather4_po_c is unsupported in SIMD16 mode. */
4470 const fs_reg
&shadow_c
= inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
];
4471 return (shadow_c
.file
!= BAD_FILE
? 8 : inst
->exec_size
);
4473 case SHADER_OPCODE_TXL_LOGICAL
:
4474 case FS_OPCODE_TXB_LOGICAL
: {
4475 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4476 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4477 * mode because the message exceeds the maximum length of 11.
4479 const fs_reg
&shadow_c
= inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
];
4480 if (devinfo
->gen
== 4 && shadow_c
.file
== BAD_FILE
)
4482 else if (devinfo
->gen
< 7 && shadow_c
.file
!= BAD_FILE
)
4485 return inst
->exec_size
;
4487 case SHADER_OPCODE_TXF_LOGICAL
:
4488 case SHADER_OPCODE_TXS_LOGICAL
:
4489 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4490 * messages. Use SIMD16 instead.
4492 if (devinfo
->gen
== 4)
4495 return inst
->exec_size
;
4497 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
: {
4498 /* This opcode can take up to 6 arguments which means that in some
4499 * circumstances it can end up with a message that is too long in SIMD16
4502 const unsigned coord_components
=
4503 inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
4504 /* First three arguments are the sample index and the two arguments for
4507 if ((coord_components
+ 3) * 2 > MAX_SAMPLER_MESSAGE_SIZE
)
4510 return inst
->exec_size
;
4513 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4514 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4515 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4518 case SHADER_OPCODE_MOV_INDIRECT
:
4519 /* Prior to Broadwell, we only have 8 address subregisters */
4520 return devinfo
->gen
< 8 ? 8 : inst
->exec_size
;
4523 return inst
->exec_size
;
4528 * The \p rows array of registers represents a \p num_rows by \p num_columns
4529 * matrix in row-major order, write it in column-major order into the register
4530 * passed as destination. \p stride gives the separation between matrix
4531 * elements in the input in fs_builder::dispatch_width() units.
4534 emit_transpose(const fs_builder
&bld
,
4535 const fs_reg
&dst
, const fs_reg
*rows
,
4536 unsigned num_rows
, unsigned num_columns
, unsigned stride
)
4538 fs_reg
*const components
= new fs_reg
[num_rows
* num_columns
];
4540 for (unsigned i
= 0; i
< num_columns
; ++i
) {
4541 for (unsigned j
= 0; j
< num_rows
; ++j
)
4542 components
[num_rows
* i
+ j
] = offset(rows
[j
], bld
, stride
* i
);
4545 bld
.LOAD_PAYLOAD(dst
, components
, num_rows
* num_columns
, 0);
4547 delete[] components
;
4551 fs_visitor::lower_simd_width()
4553 bool progress
= false;
4555 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4556 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
4558 if (lower_width
!= inst
->exec_size
) {
4559 /* Builder matching the original instruction. We may also need to
4560 * emit an instruction of width larger than the original, set the
4561 * execution size of the builder to the highest of both for now so
4562 * we're sure that both cases can be handled.
4564 const fs_builder ibld
= bld
.at(block
, inst
)
4565 .exec_all(inst
->force_writemask_all
)
4566 .group(MAX2(inst
->exec_size
, lower_width
),
4567 inst
->force_sechalf
);
4569 /* Split the copies in chunks of the execution width of either the
4570 * original or the lowered instruction, whichever is lower.
4572 const unsigned copy_width
= MIN2(lower_width
, inst
->exec_size
);
4573 const unsigned n
= inst
->exec_size
/ copy_width
;
4574 const unsigned dst_size
= inst
->regs_written
* REG_SIZE
/
4575 inst
->dst
.component_size(inst
->exec_size
);
4578 assert(n
> 0 && n
<= ARRAY_SIZE(dsts
) &&
4579 !inst
->writes_accumulator
&& !inst
->mlen
);
4581 for (unsigned i
= 0; i
< n
; i
++) {
4582 /* Emit a copy of the original instruction with the lowered width.
4583 * If the EOT flag was set throw it away except for the last
4584 * instruction to avoid killing the thread prematurely.
4586 fs_inst split_inst
= *inst
;
4587 split_inst
.exec_size
= lower_width
;
4588 split_inst
.eot
= inst
->eot
&& i
== n
- 1;
4590 /* Select the correct channel enables for the i-th group, then
4591 * transform the sources and destination and emit the lowered
4594 const fs_builder lbld
= ibld
.group(lower_width
, i
);
4596 for (unsigned j
= 0; j
< inst
->sources
; j
++) {
4597 if (inst
->src
[j
].file
!= BAD_FILE
&&
4598 !is_uniform(inst
->src
[j
])) {
4599 /* Get the i-th copy_width-wide chunk of the source. */
4600 const fs_reg src
= horiz_offset(inst
->src
[j
], copy_width
* i
);
4601 const unsigned src_size
= inst
->components_read(j
);
4603 /* Use a trivial transposition to copy one every n
4604 * copy_width-wide components of the register into a
4605 * temporary passed as source to the lowered instruction.
4607 split_inst
.src
[j
] = lbld
.vgrf(inst
->src
[j
].type
, src_size
);
4608 emit_transpose(lbld
.group(copy_width
, 0),
4609 split_inst
.src
[j
], &src
, 1, src_size
, n
);
4613 if (inst
->regs_written
) {
4614 /* Allocate enough space to hold the result of the lowered
4615 * instruction and fix up the number of registers written.
4617 split_inst
.dst
= dsts
[i
] =
4618 lbld
.vgrf(inst
->dst
.type
, dst_size
);
4619 split_inst
.regs_written
=
4620 DIV_ROUND_UP(inst
->regs_written
* lower_width
,
4624 lbld
.emit(split_inst
);
4627 if (inst
->regs_written
) {
4628 /* Distance between useful channels in the temporaries, skipping
4629 * garbage if the lowered instruction is wider than the original.
4631 const unsigned m
= lower_width
/ copy_width
;
4633 /* Interleave the components of the result from the lowered
4634 * instructions. We need to set exec_all() when copying more than
4635 * one half per component, because LOAD_PAYLOAD (in terms of which
4636 * emit_transpose is implemented) can only use the same channel
4637 * enable signals for all of its non-header sources.
4639 emit_transpose(ibld
.exec_all(inst
->exec_size
> copy_width
)
4640 .group(copy_width
, 0),
4641 inst
->dst
, dsts
, n
, dst_size
, m
);
4644 inst
->remove(block
);
4650 invalidate_live_intervals();
4656 fs_visitor::dump_instructions()
4658 dump_instructions(NULL
);
4662 fs_visitor::dump_instructions(const char *name
)
4664 FILE *file
= stderr
;
4665 if (name
&& geteuid() != 0) {
4666 file
= fopen(name
, "w");
4672 calculate_register_pressure();
4673 int ip
= 0, max_pressure
= 0;
4674 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
4675 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
4676 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
4677 dump_instruction(inst
, file
);
4680 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
4683 foreach_in_list(backend_instruction
, inst
, &instructions
) {
4684 fprintf(file
, "%4d: ", ip
++);
4685 dump_instruction(inst
, file
);
4689 if (file
!= stderr
) {
4695 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
4697 dump_instruction(be_inst
, stderr
);
4701 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
4703 fs_inst
*inst
= (fs_inst
*)be_inst
;
4705 if (inst
->predicate
) {
4706 fprintf(file
, "(%cf0.%d) ",
4707 inst
->predicate_inverse
? '-' : '+',
4711 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
4713 fprintf(file
, ".sat");
4714 if (inst
->conditional_mod
) {
4715 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
4716 if (!inst
->predicate
&&
4717 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
4718 inst
->opcode
!= BRW_OPCODE_IF
&&
4719 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
4720 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
4723 fprintf(file
, "(%d) ", inst
->exec_size
);
4726 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
4729 switch (inst
->dst
.file
) {
4731 fprintf(file
, "vgrf%d", inst
->dst
.nr
);
4732 if (alloc
.sizes
[inst
->dst
.nr
] != inst
->regs_written
||
4733 inst
->dst
.subreg_offset
)
4734 fprintf(file
, "+%d.%d",
4735 inst
->dst
.reg_offset
, inst
->dst
.subreg_offset
);
4738 fprintf(file
, "g%d", inst
->dst
.nr
);
4741 fprintf(file
, "m%d", inst
->dst
.nr
);
4744 fprintf(file
, "(null)");
4747 fprintf(file
, "***u%d***", inst
->dst
.nr
+ inst
->dst
.reg_offset
);
4750 fprintf(file
, "***attr%d***", inst
->dst
.nr
+ inst
->dst
.reg_offset
);
4753 switch (inst
->dst
.nr
) {
4755 fprintf(file
, "null");
4757 case BRW_ARF_ADDRESS
:
4758 fprintf(file
, "a0.%d", inst
->dst
.subnr
);
4760 case BRW_ARF_ACCUMULATOR
:
4761 fprintf(file
, "acc%d", inst
->dst
.subnr
);
4764 fprintf(file
, "f%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
4767 fprintf(file
, "arf%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
4770 if (inst
->dst
.subnr
)
4771 fprintf(file
, "+%d", inst
->dst
.subnr
);
4774 unreachable("not reached");
4776 if (inst
->dst
.stride
!= 1)
4777 fprintf(file
, "<%u>", inst
->dst
.stride
);
4778 fprintf(file
, ":%s, ", brw_reg_type_letters(inst
->dst
.type
));
4780 for (int i
= 0; i
< inst
->sources
; i
++) {
4781 if (inst
->src
[i
].negate
)
4783 if (inst
->src
[i
].abs
)
4785 switch (inst
->src
[i
].file
) {
4787 fprintf(file
, "vgrf%d", inst
->src
[i
].nr
);
4788 if (alloc
.sizes
[inst
->src
[i
].nr
] != (unsigned)inst
->regs_read(i
) ||
4789 inst
->src
[i
].subreg_offset
)
4790 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4791 inst
->src
[i
].subreg_offset
);
4794 fprintf(file
, "g%d", inst
->src
[i
].nr
);
4797 fprintf(file
, "***m%d***", inst
->src
[i
].nr
);
4800 fprintf(file
, "attr%d+%d", inst
->src
[i
].nr
, inst
->src
[i
].reg_offset
);
4803 fprintf(file
, "u%d", inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
);
4804 if (inst
->src
[i
].subreg_offset
) {
4805 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4806 inst
->src
[i
].subreg_offset
);
4810 fprintf(file
, "(null)");
4813 switch (inst
->src
[i
].type
) {
4814 case BRW_REGISTER_TYPE_F
:
4815 fprintf(file
, "%-gf", inst
->src
[i
].f
);
4817 case BRW_REGISTER_TYPE_W
:
4818 case BRW_REGISTER_TYPE_D
:
4819 fprintf(file
, "%dd", inst
->src
[i
].d
);
4821 case BRW_REGISTER_TYPE_UW
:
4822 case BRW_REGISTER_TYPE_UD
:
4823 fprintf(file
, "%uu", inst
->src
[i
].ud
);
4825 case BRW_REGISTER_TYPE_VF
:
4826 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
4827 brw_vf_to_float((inst
->src
[i
].ud
>> 0) & 0xff),
4828 brw_vf_to_float((inst
->src
[i
].ud
>> 8) & 0xff),
4829 brw_vf_to_float((inst
->src
[i
].ud
>> 16) & 0xff),
4830 brw_vf_to_float((inst
->src
[i
].ud
>> 24) & 0xff));
4833 fprintf(file
, "???");
4838 switch (inst
->src
[i
].nr
) {
4840 fprintf(file
, "null");
4842 case BRW_ARF_ADDRESS
:
4843 fprintf(file
, "a0.%d", inst
->src
[i
].subnr
);
4845 case BRW_ARF_ACCUMULATOR
:
4846 fprintf(file
, "acc%d", inst
->src
[i
].subnr
);
4849 fprintf(file
, "f%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
4852 fprintf(file
, "arf%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
4855 if (inst
->src
[i
].subnr
)
4856 fprintf(file
, "+%d", inst
->src
[i
].subnr
);
4859 if (inst
->src
[i
].abs
)
4862 if (inst
->src
[i
].file
!= IMM
) {
4864 if (inst
->src
[i
].file
== ARF
|| inst
->src
[i
].file
== FIXED_GRF
) {
4865 unsigned hstride
= inst
->src
[i
].hstride
;
4866 stride
= (hstride
== 0 ? 0 : (1 << (hstride
- 1)));
4868 stride
= inst
->src
[i
].stride
;
4871 fprintf(file
, "<%u>", stride
);
4873 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
4876 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
4877 fprintf(file
, ", ");
4882 if (inst
->force_writemask_all
)
4883 fprintf(file
, "NoMask ");
4885 if (dispatch_width
== 16 && inst
->exec_size
== 8) {
4886 if (inst
->force_sechalf
)
4887 fprintf(file
, "2ndhalf ");
4889 fprintf(file
, "1sthalf ");
4892 fprintf(file
, "\n");
4896 * Possibly returns an instruction that set up @param reg.
4898 * Sometimes we want to take the result of some expression/variable
4899 * dereference tree and rewrite the instruction generating the result
4900 * of the tree. When processing the tree, we know that the
4901 * instructions generated are all writing temporaries that are dead
4902 * outside of this tree. So, if we have some instructions that write
4903 * a temporary, we're free to point that temp write somewhere else.
4905 * Note that this doesn't guarantee that the instruction generated
4906 * only reg -- it might be the size=4 destination of a texture instruction.
4909 fs_visitor::get_instruction_generating_reg(fs_inst
*start
,
4914 end
->is_partial_write() ||
4915 !reg
.equals(end
->dst
)) {
4923 fs_visitor::setup_fs_payload_gen6()
4925 assert(stage
== MESA_SHADER_FRAGMENT
);
4926 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4927 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
4929 unsigned barycentric_interp_modes
=
4930 (stage
== MESA_SHADER_FRAGMENT
) ?
4931 ((brw_wm_prog_data
*) this->prog_data
)->barycentric_interp_modes
: 0;
4933 assert(devinfo
->gen
>= 6);
4935 /* R0-1: masks, pixel X/Y coordinates. */
4936 payload
.num_regs
= 2;
4937 /* R2: only for 32-pixel dispatch.*/
4939 /* R3-26: barycentric interpolation coordinates. These appear in the
4940 * same order that they appear in the brw_wm_barycentric_interp_mode
4941 * enum. Each set of coordinates occupies 2 registers if dispatch width
4942 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4943 * appear if they were enabled using the "Barycentric Interpolation
4944 * Mode" bits in WM_STATE.
4946 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
4947 if (barycentric_interp_modes
& (1 << i
)) {
4948 payload
.barycentric_coord_reg
[i
] = payload
.num_regs
;
4949 payload
.num_regs
+= 2;
4950 if (dispatch_width
== 16) {
4951 payload
.num_regs
+= 2;
4956 /* R27: interpolated depth if uses source depth */
4957 prog_data
->uses_src_depth
=
4958 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
4959 if (prog_data
->uses_src_depth
) {
4960 payload
.source_depth_reg
= payload
.num_regs
;
4962 if (dispatch_width
== 16) {
4963 /* R28: interpolated depth if not SIMD8. */
4968 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4969 prog_data
->uses_src_w
=
4970 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
4971 if (prog_data
->uses_src_w
) {
4972 payload
.source_w_reg
= payload
.num_regs
;
4974 if (dispatch_width
== 16) {
4975 /* R30: interpolated W if not SIMD8. */
4980 prog_data
->uses_pos_offset
= key
->compute_pos_offset
;
4981 /* R31: MSAA position offsets. */
4982 if (prog_data
->uses_pos_offset
) {
4983 payload
.sample_pos_reg
= payload
.num_regs
;
4987 /* R32: MSAA input coverage mask */
4988 prog_data
->uses_sample_mask
=
4989 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
) != 0;
4990 if (prog_data
->uses_sample_mask
) {
4991 assert(devinfo
->gen
>= 7);
4992 payload
.sample_mask_in_reg
= payload
.num_regs
;
4994 if (dispatch_width
== 16) {
4995 /* R33: input coverage mask if not SIMD8. */
5000 /* R34-: bary for 32-pixel. */
5001 /* R58-59: interp W for 32-pixel. */
5003 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
5004 source_depth_to_render_target
= true;
5009 fs_visitor::setup_vs_payload()
5011 /* R0: thread header, R1: urb handles */
5012 payload
.num_regs
= 2;
5016 * We are building the local ID push constant data using the simplest possible
5017 * method. We simply push the local IDs directly as they should appear in the
5018 * registers for the uvec3 gl_LocalInvocationID variable.
5020 * Therefore, for SIMD8, we use 3 full registers, and for SIMD16 we use 6
5021 * registers worth of push constant space.
5023 * Note: Any updates to brw_cs_prog_local_id_payload_dwords,
5024 * fill_local_id_payload or fs_visitor::emit_cs_local_invocation_id_setup need
5027 * FINISHME: There are a few easy optimizations to consider.
5029 * 1. If gl_WorkGroupSize x, y or z is 1, we can just use zero, and there is
5030 * no need for using push constant space for that dimension.
5032 * 2. Since GL_MAX_COMPUTE_WORK_GROUP_SIZE is currently 1024 or less, we can
5033 * easily use 16-bit words rather than 32-bit dwords in the push constant
5036 * 3. If gl_WorkGroupSize x, y or z is small, then we can use bytes for
5037 * conveying the data, and thereby reduce push constant usage.
5041 fs_visitor::setup_gs_payload()
5043 assert(stage
== MESA_SHADER_GEOMETRY
);
5045 struct brw_gs_prog_data
*gs_prog_data
=
5046 (struct brw_gs_prog_data
*) prog_data
;
5047 struct brw_vue_prog_data
*vue_prog_data
=
5048 (struct brw_vue_prog_data
*) prog_data
;
5050 /* R0: thread header, R1: output URB handles */
5051 payload
.num_regs
= 2;
5053 if (gs_prog_data
->include_primitive_id
) {
5054 /* R2: Primitive ID 0..7 */
5058 /* Use a maximum of 32 registers for push-model inputs. */
5059 const unsigned max_push_components
= 32;
5061 /* If pushing our inputs would take too many registers, reduce the URB read
5062 * length (which is in HWords, or 8 registers), and resort to pulling.
5064 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
5065 * have to multiply by VerticesIn to obtain the total storage requirement.
5067 if (8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
>
5068 max_push_components
) {
5069 gs_prog_data
->base
.include_vue_handles
= true;
5071 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
5072 payload
.num_regs
+= nir
->info
.gs
.vertices_in
;
5074 vue_prog_data
->urb_read_length
=
5075 ROUND_DOWN_TO(max_push_components
/ nir
->info
.gs
.vertices_in
, 8) / 8;
5080 fs_visitor::setup_cs_payload()
5082 assert(devinfo
->gen
>= 7);
5083 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
5085 payload
.num_regs
= 1;
5087 if (nir
->info
.system_values_read
& SYSTEM_BIT_LOCAL_INVOCATION_ID
) {
5088 prog_data
->local_invocation_id_regs
= dispatch_width
* 3 / 8;
5089 payload
.local_invocation_id_reg
= payload
.num_regs
;
5090 payload
.num_regs
+= prog_data
->local_invocation_id_regs
;
5095 fs_visitor::calculate_register_pressure()
5097 invalidate_live_intervals();
5098 calculate_live_intervals();
5100 unsigned num_instructions
= 0;
5101 foreach_block(block
, cfg
)
5102 num_instructions
+= block
->instructions
.length();
5104 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
5106 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
5107 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
5108 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
5113 fs_visitor::optimize()
5115 /* Start by validating the shader we currently have. */
5118 /* bld is the common builder object pointing at the end of the program we
5119 * used to translate it into i965 IR. For the optimization and lowering
5120 * passes coming next, any code added after the end of the program without
5121 * having explicitly called fs_builder::at() clearly points at a mistake.
5122 * Ideally optimization passes wouldn't be part of the visitor so they
5123 * wouldn't have access to bld at all, but they do, so just in case some
5124 * pass forgets to ask for a location explicitly set it to NULL here to
5125 * make it trip. The dispatch width is initialized to a bogus value to
5126 * make sure that optimizations set the execution controls explicitly to
5127 * match the code they are manipulating instead of relying on the defaults.
5129 bld
= fs_builder(this, 64);
5131 assign_constant_locations();
5132 lower_constant_loads();
5136 split_virtual_grfs();
5139 #define OPT(pass, args...) ({ \
5141 bool this_progress = pass(args); \
5143 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
5144 char filename[64]; \
5145 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
5146 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
5148 backend_shader::dump_instructions(filename); \
5153 progress = progress || this_progress; \
5157 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
5159 snprintf(filename
, 64, "%s%d-%s-00-00-start",
5160 stage_abbrev
, dispatch_width
, nir
->info
.name
);
5162 backend_shader::dump_instructions(filename
);
5165 bool progress
= false;
5169 OPT(lower_simd_width
);
5170 OPT(lower_logical_sends
);
5177 OPT(remove_duplicate_mrf_writes
);
5181 OPT(opt_copy_propagate
);
5182 OPT(opt_predicated_break
, this);
5183 OPT(opt_cmod_propagation
);
5184 OPT(dead_code_eliminate
);
5185 OPT(opt_peephole_sel
);
5186 OPT(dead_control_flow_eliminate
, this);
5187 OPT(opt_register_renaming
);
5188 OPT(opt_redundant_discard_jumps
);
5189 OPT(opt_saturate_propagation
);
5190 OPT(opt_zero_samples
);
5191 OPT(register_coalesce
);
5192 OPT(compute_to_mrf
);
5193 OPT(eliminate_find_live_channel
);
5195 OPT(compact_virtual_grfs
);
5200 OPT(opt_sampler_eot
);
5202 if (OPT(lower_load_payload
)) {
5203 split_virtual_grfs();
5204 OPT(register_coalesce
);
5205 OPT(compute_to_mrf
);
5206 OPT(dead_code_eliminate
);
5209 OPT(opt_combine_constants
);
5210 OPT(lower_integer_multiplication
);
5212 if (devinfo
->gen
<= 5 && OPT(lower_minmax
)) {
5213 OPT(opt_cmod_propagation
);
5215 OPT(opt_copy_propagate
);
5216 OPT(dead_code_eliminate
);
5219 lower_uniform_pull_constant_loads();
5225 * Three source instruction must have a GRF/MRF destination register.
5226 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
5229 fs_visitor::fixup_3src_null_dest()
5231 bool progress
= false;
5233 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
5234 if (inst
->is_3src() && inst
->dst
.is_null()) {
5235 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
5242 invalidate_live_intervals();
5246 fs_visitor::allocate_registers()
5248 bool allocated_without_spills
;
5250 static const enum instruction_scheduler_mode pre_modes
[] = {
5252 SCHEDULE_PRE_NON_LIFO
,
5256 /* Try each scheduling heuristic to see if it can successfully register
5257 * allocate without spilling. They should be ordered by decreasing
5258 * performance but increasing likelihood of allocating.
5260 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
5261 schedule_instructions(pre_modes
[i
]);
5264 assign_regs_trivial();
5265 allocated_without_spills
= true;
5267 allocated_without_spills
= assign_regs(false);
5269 if (allocated_without_spills
)
5273 if (!allocated_without_spills
) {
5274 /* We assume that any spilling is worse than just dropping back to
5275 * SIMD8. There's probably actually some intermediate point where
5276 * SIMD16 with a couple of spills is still better.
5278 if (dispatch_width
== 16 && min_dispatch_width
<= 8) {
5279 fail("Failure to register allocate. Reduce number of "
5280 "live scalar values to avoid this.");
5282 compiler
->shader_perf_log(log_data
,
5283 "%s shader triggered register spilling. "
5284 "Try reducing the number of live scalar "
5285 "values to improve performance.\n",
5289 /* Since we're out of heuristics, just go spill registers until we
5290 * get an allocation.
5292 while (!assign_regs(true)) {
5298 /* This must come after all optimization and register allocation, since
5299 * it inserts dead code that happens to have side effects, and it does
5300 * so based on the actual physical registers in use.
5302 insert_gen4_send_dependency_workarounds();
5307 schedule_instructions(SCHEDULE_POST
);
5309 if (last_scratch
> 0)
5310 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
5314 fs_visitor::run_vs(gl_clip_plane
*clip_planes
)
5316 assert(stage
== MESA_SHADER_VERTEX
);
5320 if (shader_time_index
>= 0)
5321 emit_shader_time_begin();
5328 compute_clip_distance(clip_planes
);
5332 if (shader_time_index
>= 0)
5333 emit_shader_time_end();
5339 assign_curb_setup();
5340 assign_vs_urb_setup();
5342 fixup_3src_null_dest();
5343 allocate_registers();
5349 fs_visitor::run_tes()
5351 assert(stage
== MESA_SHADER_TESS_EVAL
);
5353 /* R0: thread header, R1-3: gl_TessCoord.xyz, R4: URB handles */
5354 payload
.num_regs
= 5;
5356 if (shader_time_index
>= 0)
5357 emit_shader_time_begin();
5366 if (shader_time_index
>= 0)
5367 emit_shader_time_end();
5373 assign_curb_setup();
5374 assign_tes_urb_setup();
5376 fixup_3src_null_dest();
5377 allocate_registers();
5383 fs_visitor::run_gs()
5385 assert(stage
== MESA_SHADER_GEOMETRY
);
5389 this->final_gs_vertex_count
= vgrf(glsl_type::uint_type
);
5391 if (gs_compile
->control_data_header_size_bits
> 0) {
5392 /* Create a VGRF to store accumulated control data bits. */
5393 this->control_data_bits
= vgrf(glsl_type::uint_type
);
5395 /* If we're outputting more than 32 control data bits, then EmitVertex()
5396 * will set control_data_bits to 0 after emitting the first vertex.
5397 * Otherwise, we need to initialize it to 0 here.
5399 if (gs_compile
->control_data_header_size_bits
<= 32) {
5400 const fs_builder abld
= bld
.annotate("initialize control data bits");
5401 abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
5405 if (shader_time_index
>= 0)
5406 emit_shader_time_begin();
5410 emit_gs_thread_end();
5412 if (shader_time_index
>= 0)
5413 emit_shader_time_end();
5422 assign_curb_setup();
5423 assign_gs_urb_setup();
5425 fixup_3src_null_dest();
5426 allocate_registers();
5432 fs_visitor::run_fs(bool do_rep_send
)
5434 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
5435 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
5437 assert(stage
== MESA_SHADER_FRAGMENT
);
5439 if (devinfo
->gen
>= 6)
5440 setup_fs_payload_gen6();
5442 setup_fs_payload_gen4();
5446 } else if (do_rep_send
) {
5447 assert(dispatch_width
== 16);
5448 emit_repclear_shader();
5450 if (shader_time_index
>= 0)
5451 emit_shader_time_begin();
5453 calculate_urb_setup();
5454 if (nir
->info
.inputs_read
> 0) {
5455 if (devinfo
->gen
< 6)
5456 emit_interpolation_setup_gen4();
5458 emit_interpolation_setup_gen6();
5461 /* We handle discards by keeping track of the still-live pixels in f0.1.
5462 * Initialize it with the dispatched pixels.
5464 if (wm_prog_data
->uses_kill
) {
5465 fs_inst
*discard_init
= bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
5466 discard_init
->flag_subreg
= 1;
5469 /* Generate FS IR for main(). (the visitor only descends into
5470 * functions called "main").
5477 if (wm_prog_data
->uses_kill
)
5478 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
5480 if (wm_key
->alpha_test_func
)
5485 if (shader_time_index
>= 0)
5486 emit_shader_time_end();
5492 assign_curb_setup();
5495 fixup_3src_null_dest();
5496 allocate_registers();
5502 if (dispatch_width
== 8)
5503 wm_prog_data
->reg_blocks
= brw_register_blocks(grf_used
);
5505 wm_prog_data
->reg_blocks_16
= brw_register_blocks(grf_used
);
5511 fs_visitor::run_cs()
5513 assert(stage
== MESA_SHADER_COMPUTE
);
5517 if (shader_time_index
>= 0)
5518 emit_shader_time_begin();
5520 if (devinfo
->is_haswell
&& prog_data
->total_shared
> 0) {
5521 /* Move SLM index from g0.0[27:24] to sr0.1[11:8] */
5522 const fs_builder abld
= bld
.exec_all().group(1, 0);
5523 abld
.MOV(retype(suboffset(brw_sr0_reg(), 1), BRW_REGISTER_TYPE_UW
),
5524 suboffset(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
), 1));
5532 emit_cs_terminate();
5534 if (shader_time_index
>= 0)
5535 emit_shader_time_end();
5541 assign_curb_setup();
5543 fixup_3src_null_dest();
5544 allocate_registers();
5553 * Return a bitfield where bit n is set if barycentric interpolation mode n
5554 * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
5557 brw_compute_barycentric_interp_modes(const struct brw_device_info
*devinfo
,
5558 bool shade_model_flat
,
5559 bool persample_shading
,
5560 const nir_shader
*shader
)
5562 unsigned barycentric_interp_modes
= 0;
5564 nir_foreach_variable(var
, &shader
->inputs
) {
5565 enum glsl_interp_qualifier interp_qualifier
=
5566 (enum glsl_interp_qualifier
)var
->data
.interpolation
;
5567 bool is_centroid
= var
->data
.centroid
&& !persample_shading
;
5568 bool is_sample
= var
->data
.sample
|| persample_shading
;
5569 bool is_gl_Color
= (var
->data
.location
== VARYING_SLOT_COL0
) ||
5570 (var
->data
.location
== VARYING_SLOT_COL1
);
5572 /* Ignore WPOS and FACE, because they don't require interpolation. */
5573 if (var
->data
.location
== VARYING_SLOT_POS
||
5574 var
->data
.location
== VARYING_SLOT_FACE
)
5577 /* Determine the set (or sets) of barycentric coordinates needed to
5578 * interpolate this variable. Note that when
5579 * brw->needs_unlit_centroid_workaround is set, centroid interpolation
5580 * uses PIXEL interpolation for unlit pixels and CENTROID interpolation
5581 * for lit pixels, so we need both sets of barycentric coordinates.
5583 if (interp_qualifier
== INTERP_QUALIFIER_NOPERSPECTIVE
) {
5585 barycentric_interp_modes
|=
5586 1 << BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
5587 } else if (is_sample
) {
5588 barycentric_interp_modes
|=
5589 1 << BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
5591 if ((!is_centroid
&& !is_sample
) ||
5592 devinfo
->needs_unlit_centroid_workaround
) {
5593 barycentric_interp_modes
|=
5594 1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
5596 } else if (interp_qualifier
== INTERP_QUALIFIER_SMOOTH
||
5597 (!(shade_model_flat
&& is_gl_Color
) &&
5598 interp_qualifier
== INTERP_QUALIFIER_NONE
)) {
5600 barycentric_interp_modes
|=
5601 1 << BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
5602 } else if (is_sample
) {
5603 barycentric_interp_modes
|=
5604 1 << BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
5606 if ((!is_centroid
&& !is_sample
) ||
5607 devinfo
->needs_unlit_centroid_workaround
) {
5608 barycentric_interp_modes
|=
5609 1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
5614 return barycentric_interp_modes
;
5618 computed_depth_mode(const nir_shader
*shader
)
5620 if (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
5621 switch (shader
->info
.fs
.depth_layout
) {
5622 case FRAG_DEPTH_LAYOUT_NONE
:
5623 case FRAG_DEPTH_LAYOUT_ANY
:
5624 return BRW_PSCDEPTH_ON
;
5625 case FRAG_DEPTH_LAYOUT_GREATER
:
5626 return BRW_PSCDEPTH_ON_GE
;
5627 case FRAG_DEPTH_LAYOUT_LESS
:
5628 return BRW_PSCDEPTH_ON_LE
;
5629 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
5630 return BRW_PSCDEPTH_OFF
;
5633 return BRW_PSCDEPTH_OFF
;
5637 brw_compile_fs(const struct brw_compiler
*compiler
, void *log_data
,
5639 const struct brw_wm_prog_key
*key
,
5640 struct brw_wm_prog_data
*prog_data
,
5641 const nir_shader
*src_shader
,
5642 struct gl_program
*prog
,
5643 int shader_time_index8
, int shader_time_index16
,
5645 unsigned *final_assembly_size
,
5648 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
5649 shader
= brw_nir_apply_sampler_key(shader
, compiler
->devinfo
, &key
->tex
,
5651 brw_nir_lower_fs_inputs(shader
);
5652 brw_nir_lower_fs_outputs(shader
);
5653 shader
= brw_postprocess_nir(shader
, compiler
->devinfo
, true);
5655 /* key->alpha_test_func means simulating alpha testing via discards,
5656 * so the shader definitely kills pixels.
5658 prog_data
->uses_kill
= shader
->info
.fs
.uses_discard
|| key
->alpha_test_func
;
5659 prog_data
->uses_omask
=
5660 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
5661 prog_data
->computed_depth_mode
= computed_depth_mode(shader
);
5662 prog_data
->computed_stencil
=
5663 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
5665 prog_data
->early_fragment_tests
= shader
->info
.fs
.early_fragment_tests
;
5667 prog_data
->barycentric_interp_modes
=
5668 brw_compute_barycentric_interp_modes(compiler
->devinfo
,
5670 key
->persample_shading
,
5673 fs_visitor
v(compiler
, log_data
, mem_ctx
, key
,
5674 &prog_data
->base
, prog
, shader
, 8,
5675 shader_time_index8
);
5676 if (!v
.run_fs(false /* do_rep_send */)) {
5678 *error_str
= ralloc_strdup(mem_ctx
, v
.fail_msg
);
5683 cfg_t
*simd16_cfg
= NULL
;
5684 fs_visitor
v2(compiler
, log_data
, mem_ctx
, key
,
5685 &prog_data
->base
, prog
, shader
, 16,
5686 shader_time_index16
);
5687 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
5688 if (!v
.simd16_unsupported
) {
5689 /* Try a SIMD16 compile */
5690 v2
.import_uniforms(&v
);
5691 if (!v2
.run_fs(use_rep_send
)) {
5692 compiler
->shader_perf_log(log_data
,
5693 "SIMD16 shader failed to compile: %s",
5696 simd16_cfg
= v2
.cfg
;
5702 int no_simd8
= (INTEL_DEBUG
& DEBUG_NO8
) || use_rep_send
;
5703 if ((no_simd8
|| compiler
->devinfo
->gen
< 5) && simd16_cfg
) {
5705 prog_data
->no_8
= true;
5708 prog_data
->no_8
= false;
5711 fs_generator
g(compiler
, log_data
, mem_ctx
, (void *) key
, &prog_data
->base
,
5712 v
.promoted_constants
, v
.runtime_check_aads_emit
,
5713 MESA_SHADER_FRAGMENT
);
5715 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
5716 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
5717 shader
->info
.label
? shader
->info
.label
:
5719 shader
->info
.name
));
5723 g
.generate_code(simd8_cfg
, 8);
5725 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
5727 return g
.get_assembly(final_assembly_size
);
5731 fs_visitor::emit_cs_local_invocation_id_setup()
5733 assert(stage
== MESA_SHADER_COMPUTE
);
5735 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5737 struct brw_reg src
=
5738 brw_vec8_grf(payload
.local_invocation_id_reg
, 0);
5739 src
= retype(src
, BRW_REGISTER_TYPE_UD
);
5741 src
.nr
+= dispatch_width
/ 8;
5742 bld
.MOV(offset(*reg
, bld
, 1), src
);
5743 src
.nr
+= dispatch_width
/ 8;
5744 bld
.MOV(offset(*reg
, bld
, 2), src
);
5750 fs_visitor::emit_cs_work_group_id_setup()
5752 assert(stage
== MESA_SHADER_COMPUTE
);
5754 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5756 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
5757 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
5758 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
5760 bld
.MOV(*reg
, r0_1
);
5761 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
5762 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
5768 brw_compile_cs(const struct brw_compiler
*compiler
, void *log_data
,
5770 const struct brw_cs_prog_key
*key
,
5771 struct brw_cs_prog_data
*prog_data
,
5772 const nir_shader
*src_shader
,
5773 int shader_time_index
,
5774 unsigned *final_assembly_size
,
5777 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
5778 shader
= brw_nir_apply_sampler_key(shader
, compiler
->devinfo
, &key
->tex
,
5780 brw_nir_lower_cs_shared(shader
);
5781 prog_data
->base
.total_shared
+= shader
->num_shared
;
5782 shader
= brw_postprocess_nir(shader
, compiler
->devinfo
, true);
5784 prog_data
->local_size
[0] = shader
->info
.cs
.local_size
[0];
5785 prog_data
->local_size
[1] = shader
->info
.cs
.local_size
[1];
5786 prog_data
->local_size
[2] = shader
->info
.cs
.local_size
[2];
5787 unsigned local_workgroup_size
=
5788 shader
->info
.cs
.local_size
[0] * shader
->info
.cs
.local_size
[1] *
5789 shader
->info
.cs
.local_size
[2];
5791 unsigned max_cs_threads
= compiler
->devinfo
->max_cs_threads
;
5792 unsigned simd_required
= DIV_ROUND_UP(local_workgroup_size
, max_cs_threads
);
5795 const char *fail_msg
= NULL
;
5797 /* Now the main event: Visit the shader IR and generate our CS IR for it.
5799 fs_visitor
v8(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5800 NULL
, /* Never used in core profile */
5801 shader
, 8, shader_time_index
);
5802 if (simd_required
<= 8) {
5804 fail_msg
= v8
.fail_msg
;
5807 prog_data
->simd_size
= 8;
5811 fs_visitor
v16(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5812 NULL
, /* Never used in core profile */
5813 shader
, 16, shader_time_index
);
5814 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
5815 !fail_msg
&& !v8
.simd16_unsupported
&&
5816 local_workgroup_size
<= 16 * max_cs_threads
) {
5817 /* Try a SIMD16 compile */
5818 if (simd_required
<= 8)
5819 v16
.import_uniforms(&v8
);
5820 if (!v16
.run_cs()) {
5821 compiler
->shader_perf_log(log_data
,
5822 "SIMD16 shader failed to compile: %s",
5826 "Couldn't generate SIMD16 program and not "
5827 "enough threads for SIMD8";
5831 prog_data
->simd_size
= 16;
5835 if (unlikely(cfg
== NULL
)) {
5838 *error_str
= ralloc_strdup(mem_ctx
, fail_msg
);
5843 fs_generator
g(compiler
, log_data
, mem_ctx
, (void*) key
, &prog_data
->base
,
5844 v8
.promoted_constants
, v8
.runtime_check_aads_emit
,
5845 MESA_SHADER_COMPUTE
);
5846 if (INTEL_DEBUG
& DEBUG_CS
) {
5847 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
5848 shader
->info
.label
? shader
->info
.label
:
5851 g
.enable_debug(name
);
5854 g
.generate_code(cfg
, prog_data
->simd_size
);
5856 return g
.get_assembly(final_assembly_size
);
5860 brw_cs_fill_local_id_payload(const struct brw_cs_prog_data
*prog_data
,
5861 void *buffer
, uint32_t threads
, uint32_t stride
)
5863 if (prog_data
->local_invocation_id_regs
== 0)
5866 /* 'stride' should be an integer number of registers, that is, a multiple
5869 assert(stride
% 32 == 0);
5871 unsigned x
= 0, y
= 0, z
= 0;
5872 for (unsigned t
= 0; t
< threads
; t
++) {
5873 uint32_t *param
= (uint32_t *) buffer
+ stride
* t
/ 4;
5875 for (unsigned i
= 0; i
< prog_data
->simd_size
; i
++) {
5876 param
[0 * prog_data
->simd_size
+ i
] = x
;
5877 param
[1 * prog_data
->simd_size
+ i
] = y
;
5878 param
[2 * prog_data
->simd_size
+ i
] = z
;
5881 if (x
== prog_data
->local_size
[0]) {
5884 if (y
== prog_data
->local_size
[1]) {
5887 if (z
== prog_data
->local_size
[2])