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"
34 #include "brw_fs_live_variables.h"
36 #include "brw_vec4_gs_visitor.h"
38 #include "brw_dead_control_flow.h"
39 #include "dev/gen_debug.h"
40 #include "compiler/glsl_types.h"
41 #include "compiler/nir/nir_builder.h"
42 #include "program/prog_parameter.h"
43 #include "util/u_math.h"
47 static unsigned get_lowered_simd_width(const struct gen_device_info
*devinfo
,
51 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
52 const fs_reg
*src
, unsigned sources
)
54 memset((void*)this, 0, sizeof(*this));
56 this->src
= new fs_reg
[MAX2(sources
, 3)];
57 for (unsigned i
= 0; i
< sources
; i
++)
58 this->src
[i
] = src
[i
];
60 this->opcode
= opcode
;
62 this->sources
= sources
;
63 this->exec_size
= exec_size
;
66 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
68 assert(this->exec_size
!= 0);
70 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
72 /* This will be the case for almost all instructions. */
79 this->size_written
= dst
.component_size(exec_size
);
82 this->size_written
= 0;
86 unreachable("Invalid destination register file");
89 this->writes_accumulator
= false;
94 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
97 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
99 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
102 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
104 init(opcode
, exec_size
, dst
, NULL
, 0);
107 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
110 const fs_reg src
[1] = { src0
};
111 init(opcode
, exec_size
, dst
, src
, 1);
114 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
115 const fs_reg
&src0
, const fs_reg
&src1
)
117 const fs_reg src
[2] = { src0
, src1
};
118 init(opcode
, exec_size
, dst
, src
, 2);
121 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
122 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
124 const fs_reg src
[3] = { src0
, src1
, src2
};
125 init(opcode
, exec_size
, dst
, src
, 3);
128 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
129 const fs_reg src
[], unsigned sources
)
131 init(opcode
, exec_width
, dst
, src
, sources
);
134 fs_inst::fs_inst(const fs_inst
&that
)
136 memcpy((void*)this, &that
, sizeof(that
));
138 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
140 for (unsigned i
= 0; i
< that
.sources
; i
++)
141 this->src
[i
] = that
.src
[i
];
150 fs_inst::resize_sources(uint8_t num_sources
)
152 if (this->sources
!= num_sources
) {
153 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
155 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
156 src
[i
] = this->src
[i
];
160 this->sources
= num_sources
;
165 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
167 const fs_reg
&surf_index
,
168 const fs_reg
&varying_offset
,
169 uint32_t const_offset
)
171 /* We have our constant surface use a pitch of 4 bytes, so our index can
172 * be any component of a vector, and then we load 4 contiguous
173 * components starting from that.
175 * We break down the const_offset to a portion added to the variable offset
176 * and a portion done using fs_reg::offset, which means that if you have
177 * GLSL using something like "uniform vec4 a[20]; gl_FragColor = a[i]",
178 * we'll temporarily generate 4 vec4 loads from offset i * 4, and CSE can
179 * later notice that those loads are all the same and eliminate the
182 fs_reg vec4_offset
= vgrf(glsl_type::uint_type
);
183 bld
.ADD(vec4_offset
, varying_offset
, brw_imm_ud(const_offset
& ~0xf));
185 /* The pull load message will load a vec4 (16 bytes). If we are loading
186 * a double this means we are only loading 2 elements worth of data.
187 * We also want to use a 32-bit data type for the dst of the load operation
188 * so other parts of the driver don't get confused about the size of the
191 fs_reg vec4_result
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
192 fs_inst
*inst
= bld
.emit(FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
,
193 vec4_result
, surf_index
, vec4_offset
);
194 inst
->size_written
= 4 * vec4_result
.component_size(inst
->exec_size
);
196 shuffle_from_32bit_read(bld
, dst
, vec4_result
,
197 (const_offset
& 0xf) / type_sz(dst
.type
), 1);
201 * A helper for MOV generation for fixing up broken hardware SEND dependency
205 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
207 /* The caller always wants uncompressed to emit the minimal extra
208 * dependencies, and to avoid having to deal with aligning its regs to 2.
210 const fs_builder ubld
= bld
.annotate("send dependency resolve")
213 ubld
.MOV(ubld
.null_reg_f(), fs_reg(VGRF
, grf
, BRW_REGISTER_TYPE_F
));
217 fs_inst::is_send_from_grf() const
220 case SHADER_OPCODE_SEND
:
221 case SHADER_OPCODE_SHADER_TIME_ADD
:
222 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
223 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
224 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
225 case SHADER_OPCODE_URB_WRITE_SIMD8
:
226 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
227 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
228 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
229 case SHADER_OPCODE_URB_READ_SIMD8
:
230 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
231 case SHADER_OPCODE_INTERLOCK
:
232 case SHADER_OPCODE_MEMORY_FENCE
:
233 case SHADER_OPCODE_BARRIER
:
235 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
236 return src
[1].file
== VGRF
;
237 case FS_OPCODE_FB_WRITE
:
238 case FS_OPCODE_FB_READ
:
239 return src
[0].file
== VGRF
;
242 return src
[0].file
== VGRF
;
249 fs_inst::is_control_source(unsigned arg
) const
252 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
253 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
254 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
:
257 case SHADER_OPCODE_BROADCAST
:
258 case SHADER_OPCODE_SHUFFLE
:
259 case SHADER_OPCODE_QUAD_SWIZZLE
:
260 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
261 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
262 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
263 case SHADER_OPCODE_GET_BUFFER_SIZE
:
266 case SHADER_OPCODE_MOV_INDIRECT
:
267 case SHADER_OPCODE_CLUSTER_BROADCAST
:
268 case SHADER_OPCODE_TEX
:
270 case SHADER_OPCODE_TXD
:
271 case SHADER_OPCODE_TXF
:
272 case SHADER_OPCODE_TXF_LZ
:
273 case SHADER_OPCODE_TXF_CMS
:
274 case SHADER_OPCODE_TXF_CMS_W
:
275 case SHADER_OPCODE_TXF_UMS
:
276 case SHADER_OPCODE_TXF_MCS
:
277 case SHADER_OPCODE_TXL
:
278 case SHADER_OPCODE_TXL_LZ
:
279 case SHADER_OPCODE_TXS
:
280 case SHADER_OPCODE_LOD
:
281 case SHADER_OPCODE_TG4
:
282 case SHADER_OPCODE_TG4_OFFSET
:
283 case SHADER_OPCODE_SAMPLEINFO
:
284 return arg
== 1 || arg
== 2;
286 case SHADER_OPCODE_SEND
:
287 return arg
== 0 || arg
== 1;
295 fs_inst::is_payload(unsigned arg
) const
298 case FS_OPCODE_FB_WRITE
:
299 case FS_OPCODE_FB_READ
:
300 case SHADER_OPCODE_URB_WRITE_SIMD8
:
301 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
302 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
303 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
304 case SHADER_OPCODE_URB_READ_SIMD8
:
305 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
306 case VEC4_OPCODE_UNTYPED_ATOMIC
:
307 case VEC4_OPCODE_UNTYPED_SURFACE_READ
:
308 case VEC4_OPCODE_UNTYPED_SURFACE_WRITE
:
309 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
310 case SHADER_OPCODE_SHADER_TIME_ADD
:
311 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
312 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
313 case SHADER_OPCODE_INTERLOCK
:
314 case SHADER_OPCODE_MEMORY_FENCE
:
315 case SHADER_OPCODE_BARRIER
:
318 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
321 case SHADER_OPCODE_SEND
:
322 return arg
== 2 || arg
== 3;
333 * Returns true if this instruction's sources and destinations cannot
334 * safely be the same register.
336 * In most cases, a register can be written over safely by the same
337 * instruction that is its last use. For a single instruction, the
338 * sources are dereferenced before writing of the destination starts
341 * However, there are a few cases where this can be problematic:
343 * - Virtual opcodes that translate to multiple instructions in the
344 * code generator: if src == dst and one instruction writes the
345 * destination before a later instruction reads the source, then
346 * src will have been clobbered.
348 * - SIMD16 compressed instructions with certain regioning (see below).
350 * The register allocator uses this information to set up conflicts between
351 * GRF sources and the destination.
354 fs_inst::has_source_and_destination_hazard() const
357 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
358 /* Multiple partial writes to the destination */
360 case SHADER_OPCODE_SHUFFLE
:
361 /* This instruction returns an arbitrary channel from the source and
362 * gets split into smaller instructions in the generator. It's possible
363 * that one of the instructions will read from a channel corresponding
364 * to an earlier instruction.
366 case SHADER_OPCODE_SEL_EXEC
:
367 /* This is implemented as
369 * mov(16) g4<1>D 0D { align1 WE_all 1H };
370 * mov(16) g4<1>D g5<8,8,1>D { align1 1H }
372 * Because the source is only read in the second instruction, the first
373 * may stomp all over it.
376 case SHADER_OPCODE_QUAD_SWIZZLE
:
378 case BRW_SWIZZLE_XXXX
:
379 case BRW_SWIZZLE_YYYY
:
380 case BRW_SWIZZLE_ZZZZ
:
381 case BRW_SWIZZLE_WWWW
:
382 case BRW_SWIZZLE_XXZZ
:
383 case BRW_SWIZZLE_YYWW
:
384 case BRW_SWIZZLE_XYXY
:
385 case BRW_SWIZZLE_ZWZW
:
386 /* These can be implemented as a single Align1 region on all
387 * platforms, so there's never a hazard between source and
388 * destination. C.f. fs_generator::generate_quad_swizzle().
392 return !is_uniform(src
[0]);
395 /* The SIMD16 compressed instruction
397 * add(16) g4<1>F g4<8,8,1>F g6<8,8,1>F
399 * is actually decoded in hardware as:
401 * add(8) g4<1>F g4<8,8,1>F g6<8,8,1>F
402 * add(8) g5<1>F g5<8,8,1>F g7<8,8,1>F
404 * Which is safe. However, if we have uniform accesses
405 * happening, we get into trouble:
407 * add(8) g4<1>F g4<0,1,0>F g6<8,8,1>F
408 * add(8) g5<1>F g4<0,1,0>F g7<8,8,1>F
410 * Now our destination for the first instruction overwrote the
411 * second instruction's src0, and we get garbage for those 8
412 * pixels. There's a similar issue for the pre-gen6
413 * pixel_x/pixel_y, which are registers of 16-bit values and thus
414 * would get stomped by the first decode as well.
416 if (exec_size
== 16) {
417 for (int i
= 0; i
< sources
; i
++) {
418 if (src
[i
].file
== VGRF
&& (src
[i
].stride
== 0 ||
419 src
[i
].type
== BRW_REGISTER_TYPE_UW
||
420 src
[i
].type
== BRW_REGISTER_TYPE_W
||
421 src
[i
].type
== BRW_REGISTER_TYPE_UB
||
422 src
[i
].type
== BRW_REGISTER_TYPE_B
)) {
432 fs_inst::can_do_source_mods(const struct gen_device_info
*devinfo
) const
434 if (devinfo
->gen
== 6 && is_math())
437 if (is_send_from_grf())
440 /* From GEN:BUG:1604601757:
442 * "When multiplying a DW and any lower precision integer, source modifier
445 if (devinfo
->gen
>= 12 && (opcode
== BRW_OPCODE_MUL
||
446 opcode
== BRW_OPCODE_MAD
)) {
447 const brw_reg_type exec_type
= get_exec_type(this);
448 const unsigned min_type_sz
= opcode
== BRW_OPCODE_MAD
?
449 MIN2(type_sz(src
[1].type
), type_sz(src
[2].type
)) :
450 MIN2(type_sz(src
[0].type
), type_sz(src
[1].type
));
452 if (brw_reg_type_is_integer(exec_type
) &&
453 type_sz(exec_type
) >= 4 &&
454 type_sz(exec_type
) != min_type_sz
)
458 if (!backend_instruction::can_do_source_mods())
465 fs_inst::can_do_cmod()
467 if (!backend_instruction::can_do_cmod())
470 /* The accumulator result appears to get used for the conditional modifier
471 * generation. When negating a UD value, there is a 33rd bit generated for
472 * the sign in the accumulator value, so now you can't check, for example,
473 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
475 for (unsigned i
= 0; i
< sources
; i
++) {
476 if (type_is_unsigned_int(src
[i
].type
) && src
[i
].negate
)
484 fs_inst::can_change_types() const
486 return dst
.type
== src
[0].type
&&
487 !src
[0].abs
&& !src
[0].negate
&& !saturate
&&
488 (opcode
== BRW_OPCODE_MOV
||
489 (opcode
== BRW_OPCODE_SEL
&&
490 dst
.type
== src
[1].type
&&
491 predicate
!= BRW_PREDICATE_NONE
&&
492 !src
[1].abs
&& !src
[1].negate
));
498 memset((void*)this, 0, sizeof(*this));
499 type
= BRW_REGISTER_TYPE_UD
;
503 /** Generic unset register constructor. */
507 this->file
= BAD_FILE
;
510 fs_reg::fs_reg(struct ::brw_reg reg
) :
515 if (this->file
== IMM
&&
516 (this->type
!= BRW_REGISTER_TYPE_V
&&
517 this->type
!= BRW_REGISTER_TYPE_UV
&&
518 this->type
!= BRW_REGISTER_TYPE_VF
)) {
524 fs_reg::equals(const fs_reg
&r
) const
526 return (this->backend_reg::equals(r
) &&
531 fs_reg::negative_equals(const fs_reg
&r
) const
533 return (this->backend_reg::negative_equals(r
) &&
538 fs_reg::is_contiguous() const
543 return hstride
== BRW_HORIZONTAL_STRIDE_1
&&
544 vstride
== width
+ hstride
;
555 unreachable("Invalid register file");
559 fs_reg::component_size(unsigned width
) const
561 const unsigned stride
= ((file
!= ARF
&& file
!= FIXED_GRF
) ? this->stride
:
564 return MAX2(width
* stride
, 1) * type_sz(type
);
568 * Create a MOV to read the timestamp register.
571 fs_visitor::get_timestamp(const fs_builder
&bld
)
573 assert(devinfo
->gen
>= 7);
575 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
578 BRW_REGISTER_TYPE_UD
));
580 fs_reg dst
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
582 /* We want to read the 3 fields we care about even if it's not enabled in
585 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
591 fs_visitor::emit_shader_time_begin()
593 /* We want only the low 32 bits of the timestamp. Since it's running
594 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
595 * which is plenty of time for our purposes. It is identical across the
596 * EUs, but since it's tracking GPU core speed it will increment at a
597 * varying rate as render P-states change.
599 shader_start_time
= component(
600 get_timestamp(bld
.annotate("shader time start")), 0);
604 fs_visitor::emit_shader_time_end()
606 /* Insert our code just before the final SEND with EOT. */
607 exec_node
*end
= this->instructions
.get_tail();
608 assert(end
&& ((fs_inst
*) end
)->eot
);
609 const fs_builder ibld
= bld
.annotate("shader time end")
610 .exec_all().at(NULL
, end
);
611 const fs_reg timestamp
= get_timestamp(ibld
);
613 /* We only use the low 32 bits of the timestamp - see
614 * emit_shader_time_begin()).
616 * We could also check if render P-states have changed (or anything
617 * else that might disrupt timing) by setting smear to 2 and checking if
618 * that field is != 0.
620 const fs_reg shader_end_time
= component(timestamp
, 0);
622 /* Check that there weren't any timestamp reset events (assuming these
623 * were the only two timestamp reads that happened).
625 const fs_reg reset
= component(timestamp
, 2);
626 set_condmod(BRW_CONDITIONAL_Z
,
627 ibld
.AND(ibld
.null_reg_ud(), reset
, brw_imm_ud(1u)));
628 ibld
.IF(BRW_PREDICATE_NORMAL
);
630 fs_reg start
= shader_start_time
;
632 const fs_reg diff
= component(fs_reg(VGRF
, alloc
.allocate(1),
633 BRW_REGISTER_TYPE_UD
),
635 const fs_builder cbld
= ibld
.group(1, 0);
636 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
638 /* If there were no instructions between the two timestamp gets, the diff
639 * is 2 cycles. Remove that overhead, so I can forget about that when
640 * trying to determine the time taken for single instructions.
642 cbld
.ADD(diff
, diff
, brw_imm_ud(-2u));
643 SHADER_TIME_ADD(cbld
, 0, diff
);
644 SHADER_TIME_ADD(cbld
, 1, brw_imm_ud(1u));
645 ibld
.emit(BRW_OPCODE_ELSE
);
646 SHADER_TIME_ADD(cbld
, 2, brw_imm_ud(1u));
647 ibld
.emit(BRW_OPCODE_ENDIF
);
651 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
652 int shader_time_subindex
,
655 int index
= shader_time_index
* 3 + shader_time_subindex
;
656 struct brw_reg offset
= brw_imm_d(index
* BRW_SHADER_TIME_STRIDE
);
659 if (dispatch_width
== 8)
660 payload
= vgrf(glsl_type::uvec2_type
);
662 payload
= vgrf(glsl_type::uint_type
);
664 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
668 fs_visitor::vfail(const char *format
, va_list va
)
677 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
678 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
680 this->fail_msg
= msg
;
683 fprintf(stderr
, "%s", msg
);
688 fs_visitor::fail(const char *format
, ...)
692 va_start(va
, format
);
698 * Mark this program as impossible to compile with dispatch width greater
701 * During the SIMD8 compile (which happens first), we can detect and flag
702 * things that are unsupported in SIMD16+ mode, so the compiler can skip the
703 * SIMD16+ compile altogether.
705 * During a compile of dispatch width greater than n (if one happens anyway),
706 * this just calls fail().
709 fs_visitor::limit_dispatch_width(unsigned n
, const char *msg
)
711 if (dispatch_width
> n
) {
714 max_dispatch_width
= n
;
715 compiler
->shader_perf_log(log_data
,
716 "Shader dispatch width limited to SIMD%d: %s",
722 * Returns true if the instruction has a flag that means it won't
723 * update an entire destination register.
725 * For example, dead code elimination and live variable analysis want to know
726 * when a write to a variable screens off any preceding values that were in
730 fs_inst::is_partial_write() const
732 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
733 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
734 !this->dst
.is_contiguous() ||
735 this->dst
.offset
% REG_SIZE
!= 0);
739 fs_inst::components_read(unsigned i
) const
741 /* Return zero if the source is not present. */
742 if (src
[i
].file
== BAD_FILE
)
746 case FS_OPCODE_LINTERP
:
752 case FS_OPCODE_PIXEL_X
:
753 case FS_OPCODE_PIXEL_Y
:
757 case FS_OPCODE_FB_WRITE_LOGICAL
:
758 assert(src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
759 /* First/second FB write color. */
761 return src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
765 case SHADER_OPCODE_TEX_LOGICAL
:
766 case SHADER_OPCODE_TXD_LOGICAL
:
767 case SHADER_OPCODE_TXF_LOGICAL
:
768 case SHADER_OPCODE_TXL_LOGICAL
:
769 case SHADER_OPCODE_TXS_LOGICAL
:
770 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
771 case FS_OPCODE_TXB_LOGICAL
:
772 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
773 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
774 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
775 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
776 case SHADER_OPCODE_LOD_LOGICAL
:
777 case SHADER_OPCODE_TG4_LOGICAL
:
778 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
779 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
780 assert(src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
&&
781 src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
782 /* Texture coordinates. */
783 if (i
== TEX_LOGICAL_SRC_COORDINATE
)
784 return src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
785 /* Texture derivatives. */
786 else if ((i
== TEX_LOGICAL_SRC_LOD
|| i
== TEX_LOGICAL_SRC_LOD2
) &&
787 opcode
== SHADER_OPCODE_TXD_LOGICAL
)
788 return src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
789 /* Texture offset. */
790 else if (i
== TEX_LOGICAL_SRC_TG4_OFFSET
)
793 else if (i
== TEX_LOGICAL_SRC_MCS
&& opcode
== SHADER_OPCODE_TXF_CMS_W_LOGICAL
)
798 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
799 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
800 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
);
801 /* Surface coordinates. */
802 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
803 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
804 /* Surface operation source (ignored for reads). */
805 else if (i
== SURFACE_LOGICAL_SRC_DATA
)
810 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
811 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
812 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
813 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
814 /* Surface coordinates. */
815 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
816 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
817 /* Surface operation source. */
818 else if (i
== SURFACE_LOGICAL_SRC_DATA
)
819 return src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
823 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
824 assert(src
[2].file
== IMM
);
827 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
828 assert(src
[2].file
== IMM
);
829 return i
== 1 ? src
[2].ud
: 1;
831 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
832 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
833 assert(src
[2].file
== IMM
);
836 const unsigned op
= src
[2].ud
;
851 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
852 assert(src
[2].file
== IMM
);
855 const unsigned op
= src
[2].ud
;
856 return op
== BRW_AOP_FCMPWR
? 2 : 1;
861 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
862 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL
:
863 /* Scattered logical opcodes use the following params:
864 * src[0] Surface coordinates
865 * src[1] Surface operation source (ignored for reads)
867 * src[3] IMM with always 1 dimension.
868 * src[4] IMM with arg bitsize for scattered read/write 8, 16, 32
870 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
871 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
872 return i
== SURFACE_LOGICAL_SRC_DATA
? 0 : 1;
874 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
875 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL
:
876 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
877 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
880 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
881 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
882 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
883 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
884 const unsigned op
= src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
885 /* Surface coordinates. */
886 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
887 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
888 /* Surface operation source. */
889 else if (i
== SURFACE_LOGICAL_SRC_DATA
&& op
== BRW_AOP_CMPWR
)
891 else if (i
== SURFACE_LOGICAL_SRC_DATA
&&
892 (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
|| op
== BRW_AOP_PREDEC
))
897 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
898 return (i
== 0 ? 2 : 1);
900 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
: {
901 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
902 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
903 const unsigned op
= src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
904 /* Surface coordinates. */
905 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
906 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
907 /* Surface operation source. */
908 else if (i
== SURFACE_LOGICAL_SRC_DATA
&& op
== BRW_AOP_FCMPWR
)
920 fs_inst::size_read(int arg
) const
923 case SHADER_OPCODE_SEND
:
925 return mlen
* REG_SIZE
;
926 } else if (arg
== 3) {
927 return ex_mlen
* REG_SIZE
;
931 case FS_OPCODE_FB_WRITE
:
932 case FS_OPCODE_REP_FB_WRITE
:
935 return src
[0].file
== BAD_FILE
? 0 : 2 * REG_SIZE
;
937 return mlen
* REG_SIZE
;
941 case FS_OPCODE_FB_READ
:
942 case SHADER_OPCODE_URB_WRITE_SIMD8
:
943 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
944 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
945 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
946 case SHADER_OPCODE_URB_READ_SIMD8
:
947 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
948 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
949 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
951 return mlen
* REG_SIZE
;
954 case FS_OPCODE_SET_SAMPLE_ID
:
959 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
960 /* The payload is actually stored in src1 */
962 return mlen
* REG_SIZE
;
965 case FS_OPCODE_LINTERP
:
970 case SHADER_OPCODE_LOAD_PAYLOAD
:
971 if (arg
< this->header_size
)
975 case CS_OPCODE_CS_TERMINATE
:
976 case SHADER_OPCODE_BARRIER
:
979 case SHADER_OPCODE_MOV_INDIRECT
:
981 assert(src
[2].file
== IMM
);
987 if (is_tex() && arg
== 0 && src
[0].file
== VGRF
)
988 return mlen
* REG_SIZE
;
992 switch (src
[arg
].file
) {
995 return components_read(arg
) * type_sz(src
[arg
].type
);
1001 return components_read(arg
) * src
[arg
].component_size(exec_size
);
1003 unreachable("MRF registers are not allowed as sources");
1010 predicate_width(brw_predicate predicate
)
1012 switch (predicate
) {
1013 case BRW_PREDICATE_NONE
: return 1;
1014 case BRW_PREDICATE_NORMAL
: return 1;
1015 case BRW_PREDICATE_ALIGN1_ANY2H
: return 2;
1016 case BRW_PREDICATE_ALIGN1_ALL2H
: return 2;
1017 case BRW_PREDICATE_ALIGN1_ANY4H
: return 4;
1018 case BRW_PREDICATE_ALIGN1_ALL4H
: return 4;
1019 case BRW_PREDICATE_ALIGN1_ANY8H
: return 8;
1020 case BRW_PREDICATE_ALIGN1_ALL8H
: return 8;
1021 case BRW_PREDICATE_ALIGN1_ANY16H
: return 16;
1022 case BRW_PREDICATE_ALIGN1_ALL16H
: return 16;
1023 case BRW_PREDICATE_ALIGN1_ANY32H
: return 32;
1024 case BRW_PREDICATE_ALIGN1_ALL32H
: return 32;
1025 default: unreachable("Unsupported predicate");
1029 /* Return the subset of flag registers that an instruction could
1030 * potentially read or write based on the execution controls and flag
1031 * subregister number of the instruction.
1034 flag_mask(const fs_inst
*inst
, unsigned width
)
1036 assert(util_is_power_of_two_nonzero(width
));
1037 const unsigned start
= (inst
->flag_subreg
* 16 + inst
->group
) &
1039 const unsigned end
= start
+ ALIGN(inst
->exec_size
, width
);
1040 return ((1 << DIV_ROUND_UP(end
, 8)) - 1) & ~((1 << (start
/ 8)) - 1);
1044 bit_mask(unsigned n
)
1046 return (n
>= CHAR_BIT
* sizeof(bit_mask(n
)) ? ~0u : (1u << n
) - 1);
1050 flag_mask(const fs_reg
&r
, unsigned sz
)
1052 if (r
.file
== ARF
) {
1053 const unsigned start
= (r
.nr
- BRW_ARF_FLAG
) * 4 + r
.subnr
;
1054 const unsigned end
= start
+ sz
;
1055 return bit_mask(end
) & ~bit_mask(start
);
1063 fs_inst::flags_read(const gen_device_info
*devinfo
) const
1065 if (predicate
== BRW_PREDICATE_ALIGN1_ANYV
||
1066 predicate
== BRW_PREDICATE_ALIGN1_ALLV
) {
1067 /* The vertical predication modes combine corresponding bits from
1068 * f0.0 and f1.0 on Gen7+, and f0.0 and f0.1 on older hardware.
1070 const unsigned shift
= devinfo
->gen
>= 7 ? 4 : 2;
1071 return flag_mask(this, 1) << shift
| flag_mask(this, 1);
1072 } else if (predicate
) {
1073 return flag_mask(this, predicate_width(predicate
));
1076 for (int i
= 0; i
< sources
; i
++) {
1077 mask
|= flag_mask(src
[i
], size_read(i
));
1084 fs_inst::flags_written() const
1086 if ((conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
1087 opcode
!= BRW_OPCODE_CSEL
&&
1088 opcode
!= BRW_OPCODE_IF
&&
1089 opcode
!= BRW_OPCODE_WHILE
)) ||
1090 opcode
== FS_OPCODE_FB_WRITE
) {
1091 return flag_mask(this, 1);
1092 } else if (opcode
== SHADER_OPCODE_FIND_LIVE_CHANNEL
||
1093 opcode
== FS_OPCODE_LOAD_LIVE_CHANNELS
) {
1094 return flag_mask(this, 32);
1096 return flag_mask(dst
, size_written
);
1101 * Returns how many MRFs an FS opcode will write over.
1103 * Note that this is not the 0 or 1 implied writes in an actual gen
1104 * instruction -- the FS opcodes often generate MOVs in addition.
1107 fs_inst::implied_mrf_writes() const
1116 case SHADER_OPCODE_RCP
:
1117 case SHADER_OPCODE_RSQ
:
1118 case SHADER_OPCODE_SQRT
:
1119 case SHADER_OPCODE_EXP2
:
1120 case SHADER_OPCODE_LOG2
:
1121 case SHADER_OPCODE_SIN
:
1122 case SHADER_OPCODE_COS
:
1123 return 1 * exec_size
/ 8;
1124 case SHADER_OPCODE_POW
:
1125 case SHADER_OPCODE_INT_QUOTIENT
:
1126 case SHADER_OPCODE_INT_REMAINDER
:
1127 return 2 * exec_size
/ 8;
1128 case SHADER_OPCODE_TEX
:
1130 case SHADER_OPCODE_TXD
:
1131 case SHADER_OPCODE_TXF
:
1132 case SHADER_OPCODE_TXF_CMS
:
1133 case SHADER_OPCODE_TXF_MCS
:
1134 case SHADER_OPCODE_TG4
:
1135 case SHADER_OPCODE_TG4_OFFSET
:
1136 case SHADER_OPCODE_TXL
:
1137 case SHADER_OPCODE_TXS
:
1138 case SHADER_OPCODE_LOD
:
1139 case SHADER_OPCODE_SAMPLEINFO
:
1141 case FS_OPCODE_FB_WRITE
:
1142 case FS_OPCODE_REP_FB_WRITE
:
1143 return src
[0].file
== BAD_FILE
? 0 : 2;
1144 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
1145 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
1147 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
:
1149 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
1152 unreachable("not reached");
1157 fs_visitor::vgrf(const glsl_type
*const type
)
1159 int reg_width
= dispatch_width
/ 8;
1161 alloc
.allocate(glsl_count_dword_slots(type
, false) * reg_width
),
1162 brw_type_for_base_type(type
));
1165 fs_reg::fs_reg(enum brw_reg_file file
, int nr
)
1170 this->type
= BRW_REGISTER_TYPE_F
;
1171 this->stride
= (file
== UNIFORM
? 0 : 1);
1174 fs_reg::fs_reg(enum brw_reg_file file
, int nr
, enum brw_reg_type type
)
1180 this->stride
= (file
== UNIFORM
? 0 : 1);
1183 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
1184 * This brings in those uniform definitions
1187 fs_visitor::import_uniforms(fs_visitor
*v
)
1189 this->push_constant_loc
= v
->push_constant_loc
;
1190 this->pull_constant_loc
= v
->pull_constant_loc
;
1191 this->uniforms
= v
->uniforms
;
1192 this->subgroup_id
= v
->subgroup_id
;
1193 for (unsigned i
= 0; i
< ARRAY_SIZE(this->group_size
); i
++)
1194 this->group_size
[i
] = v
->group_size
[i
];
1198 fs_visitor::emit_fragcoord_interpolation(fs_reg wpos
)
1200 assert(stage
== MESA_SHADER_FRAGMENT
);
1202 /* gl_FragCoord.x */
1203 bld
.MOV(wpos
, this->pixel_x
);
1204 wpos
= offset(wpos
, bld
, 1);
1206 /* gl_FragCoord.y */
1207 bld
.MOV(wpos
, this->pixel_y
);
1208 wpos
= offset(wpos
, bld
, 1);
1210 /* gl_FragCoord.z */
1211 if (devinfo
->gen
>= 6) {
1212 bld
.MOV(wpos
, fetch_payload_reg(bld
, payload
.source_depth_reg
));
1214 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
1215 this->delta_xy
[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL
],
1216 component(interp_reg(VARYING_SLOT_POS
, 2), 0));
1218 wpos
= offset(wpos
, bld
, 1);
1220 /* gl_FragCoord.w: Already set up in emit_interpolation */
1221 bld
.MOV(wpos
, this->wpos_w
);
1224 enum brw_barycentric_mode
1225 brw_barycentric_mode(enum glsl_interp_mode mode
, nir_intrinsic_op op
)
1227 /* Barycentric modes don't make sense for flat inputs. */
1228 assert(mode
!= INTERP_MODE_FLAT
);
1232 case nir_intrinsic_load_barycentric_pixel
:
1233 case nir_intrinsic_load_barycentric_at_offset
:
1234 bary
= BRW_BARYCENTRIC_PERSPECTIVE_PIXEL
;
1236 case nir_intrinsic_load_barycentric_centroid
:
1237 bary
= BRW_BARYCENTRIC_PERSPECTIVE_CENTROID
;
1239 case nir_intrinsic_load_barycentric_sample
:
1240 case nir_intrinsic_load_barycentric_at_sample
:
1241 bary
= BRW_BARYCENTRIC_PERSPECTIVE_SAMPLE
;
1244 unreachable("invalid intrinsic");
1247 if (mode
== INTERP_MODE_NOPERSPECTIVE
)
1250 return (enum brw_barycentric_mode
) bary
;
1254 * Turn one of the two CENTROID barycentric modes into PIXEL mode.
1256 static enum brw_barycentric_mode
1257 centroid_to_pixel(enum brw_barycentric_mode bary
)
1259 assert(bary
== BRW_BARYCENTRIC_PERSPECTIVE_CENTROID
||
1260 bary
== BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID
);
1261 return (enum brw_barycentric_mode
) ((unsigned) bary
- 1);
1265 fs_visitor::emit_frontfacing_interpolation()
1267 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1269 if (devinfo
->gen
>= 12) {
1270 fs_reg g1
= fs_reg(retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_W
));
1272 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
1273 bld
.ASR(tmp
, g1
, brw_imm_d(15));
1275 } else if (devinfo
->gen
>= 6) {
1276 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1277 * a boolean result from this (~0/true or 0/false).
1279 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1280 * this task in only one instruction:
1281 * - a negation source modifier will flip the bit; and
1282 * - a W -> D type conversion will sign extend the bit into the high
1283 * word of the destination.
1285 * An ASR 15 fills the low word of the destination.
1287 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1290 bld
.ASR(*reg
, g0
, brw_imm_d(15));
1292 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1293 * a boolean result from this (1/true or 0/false).
1295 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1296 * the negation source modifier to flip it. Unfortunately the SHR
1297 * instruction only operates on UD (or D with an abs source modifier)
1298 * sources without negation.
1300 * Instead, use ASR (which will give ~0/true or 0/false).
1302 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1305 bld
.ASR(*reg
, g1_6
, brw_imm_d(31));
1312 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1314 assert(stage
== MESA_SHADER_FRAGMENT
);
1315 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
1316 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1318 if (wm_prog_data
->persample_dispatch
) {
1319 /* Convert int_sample_pos to floating point */
1320 bld
.MOV(dst
, int_sample_pos
);
1321 /* Scale to the range [0, 1] */
1322 bld
.MUL(dst
, dst
, brw_imm_f(1 / 16.0f
));
1325 /* From ARB_sample_shading specification:
1326 * "When rendering to a non-multisample buffer, or if multisample
1327 * rasterization is disabled, gl_SamplePosition will always be
1330 bld
.MOV(dst
, brw_imm_f(0.5f
));
1335 fs_visitor::emit_samplepos_setup()
1337 assert(devinfo
->gen
>= 6);
1339 const fs_builder abld
= bld
.annotate("compute sample position");
1340 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1342 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1343 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1345 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1346 * mode will be enabled.
1348 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1349 * R31.1:0 Position Offset X/Y for Slot[3:0]
1350 * R31.3:2 Position Offset X/Y for Slot[7:4]
1353 * The X, Y sample positions come in as bytes in thread payload. So, read
1354 * the positions using vstride=16, width=8, hstride=2.
1356 const fs_reg sample_pos_reg
=
1357 fetch_payload_reg(abld
, payload
.sample_pos_reg
, BRW_REGISTER_TYPE_W
);
1359 /* Compute gl_SamplePosition.x */
1360 abld
.MOV(int_sample_x
, subscript(sample_pos_reg
, BRW_REGISTER_TYPE_B
, 0));
1361 compute_sample_position(offset(pos
, abld
, 0), int_sample_x
);
1363 /* Compute gl_SamplePosition.y */
1364 abld
.MOV(int_sample_y
, subscript(sample_pos_reg
, BRW_REGISTER_TYPE_B
, 1));
1365 compute_sample_position(offset(pos
, abld
, 1), int_sample_y
);
1370 fs_visitor::emit_sampleid_setup()
1372 assert(stage
== MESA_SHADER_FRAGMENT
);
1373 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1374 assert(devinfo
->gen
>= 6);
1376 const fs_builder abld
= bld
.annotate("compute sample id");
1377 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uint_type
));
1379 if (!key
->multisample_fbo
) {
1380 /* As per GL_ARB_sample_shading specification:
1381 * "When rendering to a non-multisample buffer, or if multisample
1382 * rasterization is disabled, gl_SampleID will always be zero."
1384 abld
.MOV(*reg
, brw_imm_d(0));
1385 } else if (devinfo
->gen
>= 8) {
1386 /* Sample ID comes in as 4-bit numbers in g1.0:
1388 * 15:12 Slot 3 SampleID (only used in SIMD16)
1389 * 11:8 Slot 2 SampleID (only used in SIMD16)
1390 * 7:4 Slot 1 SampleID
1391 * 3:0 Slot 0 SampleID
1393 * Each slot corresponds to four channels, so we want to replicate each
1394 * half-byte value to 4 channels in a row:
1396 * dst+0: .7 .6 .5 .4 .3 .2 .1 .0
1397 * 7:4 7:4 7:4 7:4 3:0 3:0 3:0 3:0
1399 * dst+1: .7 .6 .5 .4 .3 .2 .1 .0 (if SIMD16)
1400 * 15:12 15:12 15:12 15:12 11:8 11:8 11:8 11:8
1402 * First, we read g1.0 with a <1,8,0>UB region, causing the first 8
1403 * channels to read the first byte (7:0), and the second group of 8
1404 * channels to read the second byte (15:8). Then, we shift right by
1405 * a vector immediate of <4, 4, 4, 4, 0, 0, 0, 0>, moving the slot 1 / 3
1406 * values into place. Finally, we AND with 0xf to keep the low nibble.
1408 * shr(16) tmp<1>W g1.0<1,8,0>B 0x44440000:V
1409 * and(16) dst<1>D tmp<8,8,1>W 0xf:W
1411 * TODO: These payload bits exist on Gen7 too, but they appear to always
1412 * be zero, so this code fails to work. We should find out why.
1414 const fs_reg tmp
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
1416 for (unsigned i
= 0; i
< DIV_ROUND_UP(dispatch_width
, 16); i
++) {
1417 const fs_builder hbld
= abld
.group(MIN2(16, dispatch_width
), i
);
1418 hbld
.SHR(offset(tmp
, hbld
, i
),
1419 stride(retype(brw_vec1_grf(1 + i
, 0), BRW_REGISTER_TYPE_UB
),
1421 brw_imm_v(0x44440000));
1424 abld
.AND(*reg
, tmp
, brw_imm_w(0xf));
1426 const fs_reg t1
= component(abld
.vgrf(BRW_REGISTER_TYPE_UD
), 0);
1427 const fs_reg t2
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
1429 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1430 * 8x multisampling, subspan 0 will represent sample N (where N
1431 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1432 * 7. We can find the value of N by looking at R0.0 bits 7:6
1433 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1434 * (since samples are always delivered in pairs). That is, we
1435 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1436 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1437 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1438 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1439 * populating a temporary variable with the sequence (0, 1, 2, 3),
1440 * and then reading from it using vstride=1, width=4, hstride=0.
1441 * These computations hold good for 4x multisampling as well.
1443 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1444 * the first four slots are sample 0 of subspan 0; the next four
1445 * are sample 1 of subspan 0; the third group is sample 0 of
1446 * subspan 1, and finally sample 1 of subspan 1.
1449 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1450 * accomodate 16x MSAA.
1452 abld
.exec_all().group(1, 0)
1453 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
1455 abld
.exec_all().group(1, 0).SHR(t1
, t1
, brw_imm_d(5));
1457 /* This works for SIMD8-SIMD16. It also works for SIMD32 but only if we
1458 * can assume 4x MSAA. Disallow it on IVB+
1460 * FINISHME: One day, we could come up with a way to do this that
1461 * actually works on gen7.
1463 if (devinfo
->gen
>= 7)
1464 limit_dispatch_width(16, "gl_SampleId is unsupported in SIMD32 on gen7");
1465 abld
.exec_all().group(8, 0).MOV(t2
, brw_imm_v(0x32103210));
1467 /* This special instruction takes care of setting vstride=1,
1468 * width=4, hstride=0 of t2 during an ADD instruction.
1470 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1477 fs_visitor::emit_samplemaskin_setup()
1479 assert(stage
== MESA_SHADER_FRAGMENT
);
1480 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
1481 assert(devinfo
->gen
>= 6);
1483 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1485 fs_reg coverage_mask
=
1486 fetch_payload_reg(bld
, payload
.sample_mask_in_reg
, BRW_REGISTER_TYPE_D
);
1488 if (wm_prog_data
->persample_dispatch
) {
1489 /* gl_SampleMaskIn[] comes from two sources: the input coverage mask,
1490 * and a mask representing which sample is being processed by the
1491 * current shader invocation.
1493 * From the OES_sample_variables specification:
1494 * "When per-sample shading is active due to the use of a fragment input
1495 * qualified by "sample" or due to the use of the gl_SampleID or
1496 * gl_SamplePosition variables, only the bit for the current sample is
1497 * set in gl_SampleMaskIn."
1499 const fs_builder abld
= bld
.annotate("compute gl_SampleMaskIn");
1501 if (nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
].file
== BAD_FILE
)
1502 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
] = *emit_sampleid_setup();
1504 fs_reg one
= vgrf(glsl_type::int_type
);
1505 fs_reg enabled_mask
= vgrf(glsl_type::int_type
);
1506 abld
.MOV(one
, brw_imm_d(1));
1507 abld
.SHL(enabled_mask
, one
, nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
]);
1508 abld
.AND(*reg
, enabled_mask
, coverage_mask
);
1510 /* In per-pixel mode, the coverage mask is sufficient. */
1511 *reg
= coverage_mask
;
1517 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1519 if (!src
.abs
&& !src
.negate
)
1522 fs_reg temp
= bld
.vgrf(src
.type
);
1529 fs_visitor::emit_discard_jump()
1531 assert(brw_wm_prog_data(this->prog_data
)->uses_kill
);
1533 /* For performance, after a discard, jump to the end of the
1534 * shader if all relevant channels have been discarded.
1536 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1537 discard_jump
->flag_subreg
= sample_mask_flag_subreg(this);
1539 discard_jump
->predicate
= BRW_PREDICATE_ALIGN1_ANY4H
;
1540 discard_jump
->predicate_inverse
= true;
1544 fs_visitor::emit_gs_thread_end()
1546 assert(stage
== MESA_SHADER_GEOMETRY
);
1548 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1550 if (gs_compile
->control_data_header_size_bits
> 0) {
1551 emit_gs_control_data_bits(this->final_gs_vertex_count
);
1554 const fs_builder abld
= bld
.annotate("thread end");
1557 if (gs_prog_data
->static_vertex_count
!= -1) {
1558 foreach_in_list_reverse(fs_inst
, prev
, &this->instructions
) {
1559 if (prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
||
1560 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
1561 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
1562 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
) {
1565 /* Delete now dead instructions. */
1566 foreach_in_list_reverse_safe(exec_node
, dead
, &this->instructions
) {
1572 } else if (prev
->is_control_flow() || prev
->has_side_effects()) {
1576 fs_reg hdr
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1577 abld
.MOV(hdr
, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
)));
1578 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, hdr
);
1581 fs_reg payload
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1582 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
1583 sources
[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1584 sources
[1] = this->final_gs_vertex_count
;
1585 abld
.LOAD_PAYLOAD(payload
, sources
, 2, 2);
1586 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1594 fs_visitor::assign_curb_setup()
1596 unsigned uniform_push_length
= DIV_ROUND_UP(stage_prog_data
->nr_params
, 8);
1598 unsigned ubo_push_length
= 0;
1599 unsigned ubo_push_start
[4];
1600 for (int i
= 0; i
< 4; i
++) {
1601 ubo_push_start
[i
] = 8 * (ubo_push_length
+ uniform_push_length
);
1602 ubo_push_length
+= stage_prog_data
->ubo_ranges
[i
].length
;
1605 prog_data
->curb_read_length
= uniform_push_length
+ ubo_push_length
;
1609 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1610 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1611 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1612 if (inst
->src
[i
].file
== UNIFORM
) {
1613 int uniform_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].offset
/ 4;
1615 if (inst
->src
[i
].nr
>= UBO_START
) {
1616 /* constant_nr is in 32-bit units, the rest are in bytes */
1617 constant_nr
= ubo_push_start
[inst
->src
[i
].nr
- UBO_START
] +
1618 inst
->src
[i
].offset
/ 4;
1619 } else if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1620 constant_nr
= push_constant_loc
[uniform_nr
];
1622 /* Section 5.11 of the OpenGL 4.1 spec says:
1623 * "Out-of-bounds reads return undefined values, which include
1624 * values from other variables of the active program or zero."
1625 * Just return the first push constant.
1630 assert(constant_nr
/ 8 < 64);
1631 used
|= BITFIELD64_BIT(constant_nr
/ 8);
1633 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1636 brw_reg
.abs
= inst
->src
[i
].abs
;
1637 brw_reg
.negate
= inst
->src
[i
].negate
;
1639 assert(inst
->src
[i
].stride
== 0);
1640 inst
->src
[i
] = byte_offset(
1641 retype(brw_reg
, inst
->src
[i
].type
),
1642 inst
->src
[i
].offset
% 4);
1647 uint64_t want_zero
= used
& stage_prog_data
->zero_push_reg
;
1649 assert(!compiler
->compact_params
);
1650 fs_builder ubld
= bld
.exec_all().group(8, 0).at(
1651 cfg
->first_block(), cfg
->first_block()->start());
1653 /* push_reg_mask_param is in 32-bit units */
1654 unsigned mask_param
= stage_prog_data
->push_reg_mask_param
;
1655 struct brw_reg mask
= brw_vec1_grf(payload
.num_regs
+ mask_param
/ 8,
1659 for (unsigned i
= 0; i
< 64; i
++) {
1660 if (i
% 16 == 0 && (want_zero
& BITFIELD64_RANGE(i
, 16))) {
1661 fs_reg shifted
= ubld
.vgrf(BRW_REGISTER_TYPE_W
, 2);
1662 ubld
.SHL(horiz_offset(shifted
, 8),
1663 byte_offset(retype(mask
, BRW_REGISTER_TYPE_W
), i
/ 8),
1664 brw_imm_v(0x01234567));
1665 ubld
.SHL(shifted
, horiz_offset(shifted
, 8), brw_imm_w(8));
1667 fs_builder ubld16
= ubld
.group(16, 0);
1668 b32
= ubld16
.vgrf(BRW_REGISTER_TYPE_D
);
1669 ubld16
.group(16, 0).ASR(b32
, shifted
, brw_imm_w(15));
1672 if (want_zero
& BITFIELD64_BIT(i
)) {
1673 assert(i
< prog_data
->curb_read_length
);
1674 struct brw_reg push_reg
=
1675 retype(brw_vec8_grf(payload
.num_regs
+ i
, 0),
1676 BRW_REGISTER_TYPE_D
);
1678 ubld
.AND(push_reg
, push_reg
, component(b32
, i
% 16));
1682 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
1685 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1686 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1690 * Build up an array of indices into the urb_setup array that
1691 * references the active entries of the urb_setup array.
1692 * Used to accelerate walking the active entries of the urb_setup array
1696 brw_compute_urb_setup_index(struct brw_wm_prog_data
*wm_prog_data
)
1698 /* Make sure uint8_t is sufficient */
1699 STATIC_ASSERT(VARYING_SLOT_MAX
<= 0xff);
1701 for (uint8_t attr
= 0; attr
< VARYING_SLOT_MAX
; attr
++) {
1702 if (wm_prog_data
->urb_setup
[attr
] >= 0) {
1703 wm_prog_data
->urb_setup_attribs
[index
++] = attr
;
1706 wm_prog_data
->urb_setup_attribs_count
= index
;
1710 calculate_urb_setup(const struct gen_device_info
*devinfo
,
1711 const struct brw_wm_prog_key
*key
,
1712 struct brw_wm_prog_data
*prog_data
,
1713 const nir_shader
*nir
)
1715 memset(prog_data
->urb_setup
, -1,
1716 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1719 /* Figure out where each of the incoming setup attributes lands. */
1720 if (devinfo
->gen
>= 6) {
1721 if (util_bitcount64(nir
->info
.inputs_read
&
1722 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1723 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1724 * first 16 varying inputs, so we can put them wherever we want.
1725 * Just put them in order.
1727 * This is useful because it means that (a) inputs not used by the
1728 * fragment shader won't take up valuable register space, and (b) we
1729 * won't have to recompile the fragment shader if it gets paired with
1730 * a different vertex (or geometry) shader.
1732 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1733 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1734 BITFIELD64_BIT(i
)) {
1735 prog_data
->urb_setup
[i
] = urb_next
++;
1739 /* We have enough input varyings that the SF/SBE pipeline stage can't
1740 * arbitrarily rearrange them to suit our whim; we have to put them
1741 * in an order that matches the output of the previous pipeline stage
1742 * (geometry or vertex shader).
1744 struct brw_vue_map prev_stage_vue_map
;
1745 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1746 key
->input_slots_valid
,
1747 nir
->info
.separate_shader
, 1);
1750 brw_compute_first_urb_slot_required(nir
->info
.inputs_read
,
1751 &prev_stage_vue_map
);
1753 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1754 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1756 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1757 if (varying
!= BRW_VARYING_SLOT_PAD
&&
1758 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1759 BITFIELD64_BIT(varying
))) {
1760 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1763 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1766 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1767 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1768 /* Point size is packed into the header, not as a general attribute */
1769 if (i
== VARYING_SLOT_PSIZ
)
1772 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1773 /* The back color slot is skipped when the front color is
1774 * also written to. In addition, some slots can be
1775 * written in the vertex shader and not read in the
1776 * fragment shader. So the register number must always be
1777 * incremented, mapped or not.
1779 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1780 prog_data
->urb_setup
[i
] = urb_next
;
1786 * It's a FS only attribute, and we did interpolation for this attribute
1787 * in SF thread. So, count it here, too.
1789 * See compile_sf_prog() for more info.
1791 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1792 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1795 prog_data
->num_varying_inputs
= urb_next
;
1796 prog_data
->inputs
= nir
->info
.inputs_read
;
1798 brw_compute_urb_setup_index(prog_data
);
1802 fs_visitor::assign_urb_setup()
1804 assert(stage
== MESA_SHADER_FRAGMENT
);
1805 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
1807 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1809 /* Offset all the urb_setup[] index by the actual position of the
1810 * setup regs, now that the location of the constants has been chosen.
1812 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1813 for (int i
= 0; i
< inst
->sources
; i
++) {
1814 if (inst
->src
[i
].file
== ATTR
) {
1815 /* ATTR regs in the FS are in units of logical scalar inputs each
1816 * of which consumes half of a GRF register.
1818 assert(inst
->src
[i
].offset
< REG_SIZE
/ 2);
1819 const unsigned grf
= urb_start
+ inst
->src
[i
].nr
/ 2;
1820 const unsigned offset
= (inst
->src
[i
].nr
% 2) * (REG_SIZE
/ 2) +
1821 inst
->src
[i
].offset
;
1822 const unsigned width
= inst
->src
[i
].stride
== 0 ?
1823 1 : MIN2(inst
->exec_size
, 8);
1824 struct brw_reg reg
= stride(
1825 byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1827 width
* inst
->src
[i
].stride
,
1828 width
, inst
->src
[i
].stride
);
1829 reg
.abs
= inst
->src
[i
].abs
;
1830 reg
.negate
= inst
->src
[i
].negate
;
1836 /* Each attribute is 4 setup channels, each of which is half a reg. */
1837 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1841 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst
*inst
)
1843 for (int i
= 0; i
< inst
->sources
; i
++) {
1844 if (inst
->src
[i
].file
== ATTR
) {
1845 int grf
= payload
.num_regs
+
1846 prog_data
->curb_read_length
+
1848 inst
->src
[i
].offset
/ REG_SIZE
;
1850 /* As explained at brw_reg_from_fs_reg, From the Haswell PRM:
1852 * VertStride must be used to cross GRF register boundaries. This
1853 * rule implies that elements within a 'Width' cannot cross GRF
1856 * So, for registers that are large enough, we have to split the exec
1857 * size in two and trust the compression state to sort it out.
1859 unsigned total_size
= inst
->exec_size
*
1860 inst
->src
[i
].stride
*
1861 type_sz(inst
->src
[i
].type
);
1863 assert(total_size
<= 2 * REG_SIZE
);
1864 const unsigned exec_size
=
1865 (total_size
<= REG_SIZE
) ? inst
->exec_size
: inst
->exec_size
/ 2;
1867 unsigned width
= inst
->src
[i
].stride
== 0 ? 1 : exec_size
;
1868 struct brw_reg reg
=
1869 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1870 inst
->src
[i
].offset
% REG_SIZE
),
1871 exec_size
* inst
->src
[i
].stride
,
1872 width
, inst
->src
[i
].stride
);
1873 reg
.abs
= inst
->src
[i
].abs
;
1874 reg
.negate
= inst
->src
[i
].negate
;
1882 fs_visitor::assign_vs_urb_setup()
1884 struct brw_vs_prog_data
*vs_prog_data
= brw_vs_prog_data(prog_data
);
1886 assert(stage
== MESA_SHADER_VERTEX
);
1888 /* Each attribute is 4 regs. */
1889 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attribute_slots
;
1891 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1893 /* Rewrite all ATTR file references to the hw grf that they land in. */
1894 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1895 convert_attr_sources_to_hw_regs(inst
);
1900 fs_visitor::assign_tcs_urb_setup()
1902 assert(stage
== MESA_SHADER_TESS_CTRL
);
1904 /* Rewrite all ATTR file references to HW_REGs. */
1905 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1906 convert_attr_sources_to_hw_regs(inst
);
1911 fs_visitor::assign_tes_urb_setup()
1913 assert(stage
== MESA_SHADER_TESS_EVAL
);
1915 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
1917 first_non_payload_grf
+= 8 * vue_prog_data
->urb_read_length
;
1919 /* Rewrite all ATTR file references to HW_REGs. */
1920 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1921 convert_attr_sources_to_hw_regs(inst
);
1926 fs_visitor::assign_gs_urb_setup()
1928 assert(stage
== MESA_SHADER_GEOMETRY
);
1930 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
1932 first_non_payload_grf
+=
1933 8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
;
1935 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1936 /* Rewrite all ATTR file references to GRFs. */
1937 convert_attr_sources_to_hw_regs(inst
);
1943 * Split large virtual GRFs into separate components if we can.
1945 * This is mostly duplicated with what brw_fs_vector_splitting does,
1946 * but that's really conservative because it's afraid of doing
1947 * splitting that doesn't result in real progress after the rest of
1948 * the optimization phases, which would cause infinite looping in
1949 * optimization. We can do it once here, safely. This also has the
1950 * opportunity to split interpolated values, or maybe even uniforms,
1951 * which we don't have at the IR level.
1953 * We want to split, because virtual GRFs are what we register
1954 * allocate and spill (due to contiguousness requirements for some
1955 * instructions), and they're what we naturally generate in the
1956 * codegen process, but most virtual GRFs don't actually need to be
1957 * contiguous sets of GRFs. If we split, we'll end up with reduced
1958 * live intervals and better dead code elimination and coalescing.
1961 fs_visitor::split_virtual_grfs()
1963 /* Compact the register file so we eliminate dead vgrfs. This
1964 * only defines split points for live registers, so if we have
1965 * too large dead registers they will hit assertions later.
1967 compact_virtual_grfs();
1969 int num_vars
= this->alloc
.count
;
1971 /* Count the total number of registers */
1973 int vgrf_to_reg
[num_vars
];
1974 for (int i
= 0; i
< num_vars
; i
++) {
1975 vgrf_to_reg
[i
] = reg_count
;
1976 reg_count
+= alloc
.sizes
[i
];
1979 /* An array of "split points". For each register slot, this indicates
1980 * if this slot can be separated from the previous slot. Every time an
1981 * instruction uses multiple elements of a register (as a source or
1982 * destination), we mark the used slots as inseparable. Then we go
1983 * through and split the registers into the smallest pieces we can.
1985 bool *split_points
= new bool[reg_count
];
1986 memset(split_points
, 0, reg_count
* sizeof(*split_points
));
1988 /* Mark all used registers as fully splittable */
1989 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1990 if (inst
->dst
.file
== VGRF
) {
1991 int reg
= vgrf_to_reg
[inst
->dst
.nr
];
1992 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.nr
]; j
++)
1993 split_points
[reg
+ j
] = true;
1996 for (int i
= 0; i
< inst
->sources
; i
++) {
1997 if (inst
->src
[i
].file
== VGRF
) {
1998 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
];
1999 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].nr
]; j
++)
2000 split_points
[reg
+ j
] = true;
2005 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2006 /* We fix up undef instructions later */
2007 if (inst
->opcode
== SHADER_OPCODE_UNDEF
) {
2008 /* UNDEF instructions are currently only used to undef entire
2009 * registers. We need this invariant later when we split them.
2011 assert(inst
->dst
.file
== VGRF
);
2012 assert(inst
->dst
.offset
== 0);
2013 assert(inst
->size_written
== alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
);
2017 if (inst
->dst
.file
== VGRF
) {
2018 int reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.offset
/ REG_SIZE
;
2019 for (unsigned j
= 1; j
< regs_written(inst
); j
++)
2020 split_points
[reg
+ j
] = false;
2022 for (int i
= 0; i
< inst
->sources
; i
++) {
2023 if (inst
->src
[i
].file
== VGRF
) {
2024 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].offset
/ REG_SIZE
;
2025 for (unsigned j
= 1; j
< regs_read(inst
, i
); j
++)
2026 split_points
[reg
+ j
] = false;
2031 int *new_virtual_grf
= new int[reg_count
];
2032 int *new_reg_offset
= new int[reg_count
];
2035 for (int i
= 0; i
< num_vars
; i
++) {
2036 /* The first one should always be 0 as a quick sanity check. */
2037 assert(split_points
[reg
] == false);
2040 new_reg_offset
[reg
] = 0;
2045 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
2046 /* If this is a split point, reset the offset to 0 and allocate a
2047 * new virtual GRF for the previous offset many registers
2049 if (split_points
[reg
]) {
2050 assert(offset
<= MAX_VGRF_SIZE
);
2051 int grf
= alloc
.allocate(offset
);
2052 for (int k
= reg
- offset
; k
< reg
; k
++)
2053 new_virtual_grf
[k
] = grf
;
2056 new_reg_offset
[reg
] = offset
;
2061 /* The last one gets the original register number */
2062 assert(offset
<= MAX_VGRF_SIZE
);
2063 alloc
.sizes
[i
] = offset
;
2064 for (int k
= reg
- offset
; k
< reg
; k
++)
2065 new_virtual_grf
[k
] = i
;
2067 assert(reg
== reg_count
);
2069 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2070 if (inst
->opcode
== SHADER_OPCODE_UNDEF
) {
2071 const fs_builder
ibld(this, block
, inst
);
2072 assert(inst
->size_written
% REG_SIZE
== 0);
2073 unsigned reg_offset
= 0;
2074 while (reg_offset
< inst
->size_written
/ REG_SIZE
) {
2075 reg
= vgrf_to_reg
[inst
->dst
.nr
] + reg_offset
;
2076 ibld
.UNDEF(fs_reg(VGRF
, new_virtual_grf
[reg
], inst
->dst
.type
));
2077 reg_offset
+= alloc
.sizes
[new_virtual_grf
[reg
]];
2079 inst
->remove(block
);
2083 if (inst
->dst
.file
== VGRF
) {
2084 reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.offset
/ REG_SIZE
;
2085 inst
->dst
.nr
= new_virtual_grf
[reg
];
2086 inst
->dst
.offset
= new_reg_offset
[reg
] * REG_SIZE
+
2087 inst
->dst
.offset
% REG_SIZE
;
2088 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
2090 for (int i
= 0; i
< inst
->sources
; i
++) {
2091 if (inst
->src
[i
].file
== VGRF
) {
2092 reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].offset
/ REG_SIZE
;
2093 inst
->src
[i
].nr
= new_virtual_grf
[reg
];
2094 inst
->src
[i
].offset
= new_reg_offset
[reg
] * REG_SIZE
+
2095 inst
->src
[i
].offset
% REG_SIZE
;
2096 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
2100 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL
| DEPENDENCY_VARIABLES
);
2102 delete[] split_points
;
2103 delete[] new_virtual_grf
;
2104 delete[] new_reg_offset
;
2108 * Remove unused virtual GRFs and compact the vgrf_* arrays.
2110 * During code generation, we create tons of temporary variables, many of
2111 * which get immediately killed and are never used again. Yet, in later
2112 * optimization and analysis passes, such as compute_live_intervals, we need
2113 * to loop over all the virtual GRFs. Compacting them can save a lot of
2117 fs_visitor::compact_virtual_grfs()
2119 bool progress
= false;
2120 int *remap_table
= new int[this->alloc
.count
];
2121 memset(remap_table
, -1, this->alloc
.count
* sizeof(int));
2123 /* Mark which virtual GRFs are used. */
2124 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
2125 if (inst
->dst
.file
== VGRF
)
2126 remap_table
[inst
->dst
.nr
] = 0;
2128 for (int i
= 0; i
< inst
->sources
; i
++) {
2129 if (inst
->src
[i
].file
== VGRF
)
2130 remap_table
[inst
->src
[i
].nr
] = 0;
2134 /* Compact the GRF arrays. */
2136 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
2137 if (remap_table
[i
] == -1) {
2138 /* We just found an unused register. This means that we are
2139 * actually going to compact something.
2143 remap_table
[i
] = new_index
;
2144 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
2145 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL
| DEPENDENCY_VARIABLES
);
2150 this->alloc
.count
= new_index
;
2152 /* Patch all the instructions to use the newly renumbered registers */
2153 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2154 if (inst
->dst
.file
== VGRF
)
2155 inst
->dst
.nr
= remap_table
[inst
->dst
.nr
];
2157 for (int i
= 0; i
< inst
->sources
; i
++) {
2158 if (inst
->src
[i
].file
== VGRF
)
2159 inst
->src
[i
].nr
= remap_table
[inst
->src
[i
].nr
];
2163 /* Patch all the references to delta_xy, since they're used in register
2164 * allocation. If they're unused, switch them to BAD_FILE so we don't
2165 * think some random VGRF is delta_xy.
2167 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2168 if (delta_xy
[i
].file
== VGRF
) {
2169 if (remap_table
[delta_xy
[i
].nr
] != -1) {
2170 delta_xy
[i
].nr
= remap_table
[delta_xy
[i
].nr
];
2172 delta_xy
[i
].file
= BAD_FILE
;
2177 delete[] remap_table
;
2183 get_subgroup_id_param_index(const brw_stage_prog_data
*prog_data
)
2185 if (prog_data
->nr_params
== 0)
2188 /* The local thread id is always the last parameter in the list */
2189 uint32_t last_param
= prog_data
->param
[prog_data
->nr_params
- 1];
2190 if (last_param
== BRW_PARAM_BUILTIN_SUBGROUP_ID
)
2191 return prog_data
->nr_params
- 1;
2197 * Struct for handling complex alignments.
2199 * A complex alignment is stored as multiplier and an offset. A value is
2200 * considered to be aligned if it is {offset} larger than a multiple of {mul}.
2201 * For instance, with an alignment of {8, 2}, cplx_align_apply would do the
2204 * N | cplx_align_apply({8, 2}, N)
2205 * ----+-----------------------------
2219 #define CPLX_ALIGN_MAX_MUL 8
2222 cplx_align_assert_sane(struct cplx_align a
)
2224 assert(a
.mul
> 0 && util_is_power_of_two_nonzero(a
.mul
));
2225 assert(a
.offset
< a
.mul
);
2229 * Combines two alignments to produce a least multiple of sorts.
2231 * The returned alignment is the smallest (in terms of multiplier) such that
2232 * anything aligned to both a and b will be aligned to the new alignment.
2233 * This function will assert-fail if a and b are not compatible, i.e. if the
2234 * offset parameters are such that no common alignment is possible.
2236 static struct cplx_align
2237 cplx_align_combine(struct cplx_align a
, struct cplx_align b
)
2239 cplx_align_assert_sane(a
);
2240 cplx_align_assert_sane(b
);
2242 /* Assert that the alignments agree. */
2243 assert((a
.offset
& (b
.mul
- 1)) == (b
.offset
& (a
.mul
- 1)));
2245 return a
.mul
> b
.mul
? a
: b
;
2249 * Apply a complex alignment
2251 * This function will return the smallest number greater than or equal to
2252 * offset that is aligned to align.
2255 cplx_align_apply(struct cplx_align align
, unsigned offset
)
2257 return ALIGN(offset
- align
.offset
, align
.mul
) + align
.offset
;
2260 #define UNIFORM_SLOT_SIZE 4
2262 struct uniform_slot_info
{
2263 /** True if the given uniform slot is live */
2266 /** True if this slot and the next slot must remain contiguous */
2267 unsigned contiguous
:1;
2269 struct cplx_align align
;
2273 mark_uniform_slots_read(struct uniform_slot_info
*slots
,
2274 unsigned num_slots
, unsigned alignment
)
2276 assert(alignment
> 0 && util_is_power_of_two_nonzero(alignment
));
2277 assert(alignment
<= CPLX_ALIGN_MAX_MUL
);
2279 /* We can't align a slot to anything less than the slot size */
2280 alignment
= MAX2(alignment
, UNIFORM_SLOT_SIZE
);
2282 struct cplx_align align
= {alignment
, 0};
2283 cplx_align_assert_sane(align
);
2285 for (unsigned i
= 0; i
< num_slots
; i
++) {
2286 slots
[i
].is_live
= true;
2287 if (i
< num_slots
- 1)
2288 slots
[i
].contiguous
= true;
2290 align
.offset
= (i
* UNIFORM_SLOT_SIZE
) & (align
.mul
- 1);
2291 if (slots
[i
].align
.mul
== 0) {
2292 slots
[i
].align
= align
;
2294 slots
[i
].align
= cplx_align_combine(slots
[i
].align
, align
);
2300 * Assign UNIFORM file registers to either push constants or pull constants.
2302 * We allow a fragment shader to have more than the specified minimum
2303 * maximum number of fragment shader uniform components (64). If
2304 * there are too many of these, they'd fill up all of register space.
2305 * So, this will push some of them out to the pull constant buffer and
2306 * update the program to load them.
2309 fs_visitor::assign_constant_locations()
2311 /* Only the first compile gets to decide on locations. */
2312 if (push_constant_loc
) {
2313 assert(pull_constant_loc
);
2317 if (compiler
->compact_params
) {
2318 struct uniform_slot_info slots
[uniforms
+ 1];
2319 memset(slots
, 0, sizeof(slots
));
2321 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2322 for (int i
= 0 ; i
< inst
->sources
; i
++) {
2323 if (inst
->src
[i
].file
!= UNIFORM
)
2326 /* NIR tightly packs things so the uniform number might not be
2327 * aligned (if we have a double right after a float, for
2328 * instance). This is fine because the process of re-arranging
2329 * them will ensure that things are properly aligned. The offset
2330 * into that uniform, however, must be aligned.
2332 * In Vulkan, we have explicit offsets but everything is crammed
2333 * into a single "variable" so inst->src[i].nr will always be 0.
2334 * Everything will be properly aligned relative to that one base.
2336 assert(inst
->src
[i
].offset
% type_sz(inst
->src
[i
].type
) == 0);
2338 unsigned u
= inst
->src
[i
].nr
+
2339 inst
->src
[i
].offset
/ UNIFORM_SLOT_SIZE
;
2344 unsigned slots_read
;
2345 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0) {
2346 slots_read
= DIV_ROUND_UP(inst
->src
[2].ud
, UNIFORM_SLOT_SIZE
);
2348 unsigned bytes_read
= inst
->components_read(i
) *
2349 type_sz(inst
->src
[i
].type
);
2350 slots_read
= DIV_ROUND_UP(bytes_read
, UNIFORM_SLOT_SIZE
);
2353 assert(u
+ slots_read
<= uniforms
);
2354 mark_uniform_slots_read(&slots
[u
], slots_read
,
2355 type_sz(inst
->src
[i
].type
));
2359 int subgroup_id_index
= get_subgroup_id_param_index(stage_prog_data
);
2361 /* Only allow 16 registers (128 uniform components) as push constants.
2363 * Just demote the end of the list. We could probably do better
2364 * here, demoting things that are rarely used in the program first.
2366 * If changing this value, note the limitation about total_regs in
2369 unsigned int max_push_components
= 16 * 8;
2370 if (subgroup_id_index
>= 0)
2371 max_push_components
--; /* Save a slot for the thread ID */
2373 /* We push small arrays, but no bigger than 16 floats. This is big
2374 * enough for a vec4 but hopefully not large enough to push out other
2375 * stuff. We should probably use a better heuristic at some point.
2377 const unsigned int max_chunk_size
= 16;
2379 unsigned int num_push_constants
= 0;
2380 unsigned int num_pull_constants
= 0;
2382 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2383 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2385 /* Default to -1 meaning no location */
2386 memset(push_constant_loc
, -1, uniforms
* sizeof(*push_constant_loc
));
2387 memset(pull_constant_loc
, -1, uniforms
* sizeof(*pull_constant_loc
));
2389 int chunk_start
= -1;
2390 struct cplx_align align
;
2391 for (unsigned u
= 0; u
< uniforms
; u
++) {
2392 if (!slots
[u
].is_live
) {
2393 assert(chunk_start
== -1);
2397 /* Skip subgroup_id_index to put it in the last push register. */
2398 if (subgroup_id_index
== (int)u
)
2401 if (chunk_start
== -1) {
2403 align
= slots
[u
].align
;
2405 /* Offset into the chunk */
2406 unsigned chunk_offset
= (u
- chunk_start
) * UNIFORM_SLOT_SIZE
;
2408 /* Shift the slot alignment down by the chunk offset so it is
2409 * comparable with the base chunk alignment.
2411 struct cplx_align slot_align
= slots
[u
].align
;
2413 (slot_align
.offset
- chunk_offset
) & (align
.mul
- 1);
2415 align
= cplx_align_combine(align
, slot_align
);
2418 /* Sanity check the alignment */
2419 cplx_align_assert_sane(align
);
2421 if (slots
[u
].contiguous
)
2424 /* Adjust the alignment to be in terms of slots, not bytes */
2425 assert((align
.mul
& (UNIFORM_SLOT_SIZE
- 1)) == 0);
2426 assert((align
.offset
& (UNIFORM_SLOT_SIZE
- 1)) == 0);
2427 align
.mul
/= UNIFORM_SLOT_SIZE
;
2428 align
.offset
/= UNIFORM_SLOT_SIZE
;
2430 unsigned push_start_align
= cplx_align_apply(align
, num_push_constants
);
2431 unsigned chunk_size
= u
- chunk_start
+ 1;
2432 if ((!compiler
->supports_pull_constants
&& u
< UBO_START
) ||
2433 (chunk_size
< max_chunk_size
&&
2434 push_start_align
+ chunk_size
<= max_push_components
)) {
2435 /* Align up the number of push constants */
2436 num_push_constants
= push_start_align
;
2437 for (unsigned i
= 0; i
< chunk_size
; i
++)
2438 push_constant_loc
[chunk_start
+ i
] = num_push_constants
++;
2440 /* We need to pull this one */
2441 num_pull_constants
= cplx_align_apply(align
, num_pull_constants
);
2442 for (unsigned i
= 0; i
< chunk_size
; i
++)
2443 pull_constant_loc
[chunk_start
+ i
] = num_pull_constants
++;
2446 /* Reset the chunk and start again */
2450 /* Add the CS local thread ID uniform at the end of the push constants */
2451 if (subgroup_id_index
>= 0)
2452 push_constant_loc
[subgroup_id_index
] = num_push_constants
++;
2454 /* As the uniforms are going to be reordered, stash the old array and
2455 * create two new arrays for push/pull params.
2457 uint32_t *param
= stage_prog_data
->param
;
2458 stage_prog_data
->nr_params
= num_push_constants
;
2459 if (num_push_constants
) {
2460 stage_prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t,
2461 num_push_constants
);
2463 stage_prog_data
->param
= NULL
;
2465 assert(stage_prog_data
->nr_pull_params
== 0);
2466 assert(stage_prog_data
->pull_param
== NULL
);
2467 if (num_pull_constants
> 0) {
2468 stage_prog_data
->nr_pull_params
= num_pull_constants
;
2469 stage_prog_data
->pull_param
= rzalloc_array(mem_ctx
, uint32_t,
2470 num_pull_constants
);
2473 /* Up until now, the param[] array has been indexed by reg + offset
2474 * of UNIFORM registers. Move pull constants into pull_param[] and
2475 * condense param[] to only contain the uniforms we chose to push.
2477 * NOTE: Because we are condensing the params[] array, we know that
2478 * push_constant_loc[i] <= i and we can do it in one smooth loop without
2479 * having to make a copy.
2481 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2482 uint32_t value
= param
[i
];
2483 if (pull_constant_loc
[i
] != -1) {
2484 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
2485 } else if (push_constant_loc
[i
] != -1) {
2486 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
2491 /* If we don't want to compact anything, just set up dummy push/pull
2492 * arrays. All the rest of the compiler cares about are these arrays.
2494 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2495 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2497 for (unsigned u
= 0; u
< uniforms
; u
++)
2498 push_constant_loc
[u
] = u
;
2500 memset(pull_constant_loc
, -1, uniforms
* sizeof(*pull_constant_loc
));
2503 /* Now that we know how many regular uniforms we'll push, reduce the
2504 * UBO push ranges so we don't exceed the 3DSTATE_CONSTANT limits.
2506 unsigned push_length
= DIV_ROUND_UP(stage_prog_data
->nr_params
, 8);
2507 for (int i
= 0; i
< 4; i
++) {
2508 struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
2510 if (push_length
+ range
->length
> 64)
2511 range
->length
= 64 - push_length
;
2513 push_length
+= range
->length
;
2515 assert(push_length
<= 64);
2519 fs_visitor::get_pull_locs(const fs_reg
&src
,
2520 unsigned *out_surf_index
,
2521 unsigned *out_pull_index
)
2523 assert(src
.file
== UNIFORM
);
2525 if (src
.nr
>= UBO_START
) {
2526 const struct brw_ubo_range
*range
=
2527 &prog_data
->ubo_ranges
[src
.nr
- UBO_START
];
2529 /* If this access is in our (reduced) range, use the push data. */
2530 if (src
.offset
/ 32 < range
->length
)
2533 *out_surf_index
= prog_data
->binding_table
.ubo_start
+ range
->block
;
2534 *out_pull_index
= (32 * range
->start
+ src
.offset
) / 4;
2536 prog_data
->has_ubo_pull
= true;
2540 const unsigned location
= src
.nr
+ src
.offset
/ 4;
2542 if (location
< uniforms
&& pull_constant_loc
[location
] != -1) {
2543 /* A regular uniform push constant */
2544 *out_surf_index
= stage_prog_data
->binding_table
.pull_constants_start
;
2545 *out_pull_index
= pull_constant_loc
[location
];
2547 prog_data
->has_ubo_pull
= true;
2555 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2556 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2559 fs_visitor::lower_constant_loads()
2561 unsigned index
, pull_index
;
2563 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2564 /* Set up the annotation tracking for new generated instructions. */
2565 const fs_builder
ibld(this, block
, inst
);
2567 for (int i
= 0; i
< inst
->sources
; i
++) {
2568 if (inst
->src
[i
].file
!= UNIFORM
)
2571 /* We'll handle this case later */
2572 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0)
2575 if (!get_pull_locs(inst
->src
[i
], &index
, &pull_index
))
2578 assert(inst
->src
[i
].stride
== 0);
2580 const unsigned block_sz
= 64; /* Fetch one cacheline at a time. */
2581 const fs_builder ubld
= ibld
.exec_all().group(block_sz
/ 4, 0);
2582 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
2583 const unsigned base
= pull_index
* 4;
2585 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
2586 dst
, brw_imm_ud(index
), brw_imm_ud(base
& ~(block_sz
- 1)));
2588 /* Rewrite the instruction to use the temporary VGRF. */
2589 inst
->src
[i
].file
= VGRF
;
2590 inst
->src
[i
].nr
= dst
.nr
;
2591 inst
->src
[i
].offset
= (base
& (block_sz
- 1)) +
2592 inst
->src
[i
].offset
% 4;
2595 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&&
2596 inst
->src
[0].file
== UNIFORM
) {
2598 if (!get_pull_locs(inst
->src
[0], &index
, &pull_index
))
2601 VARYING_PULL_CONSTANT_LOAD(ibld
, inst
->dst
,
2605 inst
->remove(block
);
2608 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
2612 fs_visitor::opt_algebraic()
2614 bool progress
= false;
2616 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2617 switch (inst
->opcode
) {
2618 case BRW_OPCODE_MOV
:
2619 if (!devinfo
->has_64bit_float
&&
2620 !devinfo
->has_64bit_int
&&
2621 (inst
->dst
.type
== BRW_REGISTER_TYPE_DF
||
2622 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
||
2623 inst
->dst
.type
== BRW_REGISTER_TYPE_Q
)) {
2624 assert(inst
->dst
.type
== inst
->src
[0].type
);
2625 assert(!inst
->saturate
);
2626 assert(!inst
->src
[0].abs
);
2627 assert(!inst
->src
[0].negate
);
2628 const brw::fs_builder
ibld(this, block
, inst
);
2630 if (inst
->src
[0].file
== IMM
) {
2631 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2632 brw_imm_ud(inst
->src
[0].u64
>> 32));
2633 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2634 brw_imm_ud(inst
->src
[0].u64
));
2636 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2637 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1));
2638 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2639 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0));
2642 inst
->remove(block
);
2646 if ((inst
->conditional_mod
== BRW_CONDITIONAL_Z
||
2647 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) &&
2648 inst
->dst
.is_null() &&
2649 (inst
->src
[0].abs
|| inst
->src
[0].negate
)) {
2650 inst
->src
[0].abs
= false;
2651 inst
->src
[0].negate
= false;
2656 if (inst
->src
[0].file
!= IMM
)
2659 if (inst
->saturate
) {
2660 /* Full mixed-type saturates don't happen. However, we can end up
2663 * mov.sat(8) g21<1>DF -1F
2665 * Other mixed-size-but-same-base-type cases may also be possible.
2667 if (inst
->dst
.type
!= inst
->src
[0].type
&&
2668 inst
->dst
.type
!= BRW_REGISTER_TYPE_DF
&&
2669 inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
)
2670 assert(!"unimplemented: saturate mixed types");
2672 if (brw_saturate_immediate(inst
->src
[0].type
,
2673 &inst
->src
[0].as_brw_reg())) {
2674 inst
->saturate
= false;
2680 case BRW_OPCODE_MUL
:
2681 if (inst
->src
[1].file
!= IMM
)
2685 if (inst
->src
[1].is_one()) {
2686 inst
->opcode
= BRW_OPCODE_MOV
;
2687 inst
->src
[1] = reg_undef
;
2693 if (inst
->src
[1].is_negative_one()) {
2694 inst
->opcode
= BRW_OPCODE_MOV
;
2695 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2696 inst
->src
[1] = reg_undef
;
2702 case BRW_OPCODE_ADD
:
2703 if (inst
->src
[1].file
!= IMM
)
2706 if (brw_reg_type_is_integer(inst
->src
[1].type
) &&
2707 inst
->src
[1].is_zero()) {
2708 inst
->opcode
= BRW_OPCODE_MOV
;
2709 inst
->src
[1] = reg_undef
;
2714 if (inst
->src
[0].file
== IMM
) {
2715 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2716 inst
->opcode
= BRW_OPCODE_MOV
;
2717 inst
->src
[0].f
+= inst
->src
[1].f
;
2718 inst
->src
[1] = reg_undef
;
2724 if (inst
->src
[0].equals(inst
->src
[1]) ||
2725 inst
->src
[1].is_zero()) {
2726 /* On Gen8+, the OR instruction can have a source modifier that
2727 * performs logical not on the operand. Cases of 'OR r0, ~r1, 0'
2728 * or 'OR r0, ~r1, ~r1' should become a NOT instead of a MOV.
2730 if (inst
->src
[0].negate
) {
2731 inst
->opcode
= BRW_OPCODE_NOT
;
2732 inst
->src
[0].negate
= false;
2734 inst
->opcode
= BRW_OPCODE_MOV
;
2736 inst
->src
[1] = reg_undef
;
2741 case BRW_OPCODE_CMP
:
2742 if ((inst
->conditional_mod
== BRW_CONDITIONAL_Z
||
2743 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) &&
2744 inst
->src
[1].is_zero() &&
2745 (inst
->src
[0].abs
|| inst
->src
[0].negate
)) {
2746 inst
->src
[0].abs
= false;
2747 inst
->src
[0].negate
= false;
2752 case BRW_OPCODE_SEL
:
2753 if (!devinfo
->has_64bit_float
&&
2754 !devinfo
->has_64bit_int
&&
2755 (inst
->dst
.type
== BRW_REGISTER_TYPE_DF
||
2756 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
||
2757 inst
->dst
.type
== BRW_REGISTER_TYPE_Q
)) {
2758 assert(inst
->dst
.type
== inst
->src
[0].type
);
2759 assert(!inst
->saturate
);
2760 assert(!inst
->src
[0].abs
&& !inst
->src
[0].negate
);
2761 assert(!inst
->src
[1].abs
&& !inst
->src
[1].negate
);
2762 const brw::fs_builder
ibld(this, block
, inst
);
2764 set_predicate(inst
->predicate
,
2765 ibld
.SEL(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2766 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
2767 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 0)));
2768 set_predicate(inst
->predicate
,
2769 ibld
.SEL(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2770 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1),
2771 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 1)));
2773 inst
->remove(block
);
2776 if (inst
->src
[0].equals(inst
->src
[1])) {
2777 inst
->opcode
= BRW_OPCODE_MOV
;
2778 inst
->src
[1] = reg_undef
;
2779 inst
->predicate
= BRW_PREDICATE_NONE
;
2780 inst
->predicate_inverse
= false;
2782 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2783 switch (inst
->conditional_mod
) {
2784 case BRW_CONDITIONAL_LE
:
2785 case BRW_CONDITIONAL_L
:
2786 switch (inst
->src
[1].type
) {
2787 case BRW_REGISTER_TYPE_F
:
2788 if (inst
->src
[1].f
>= 1.0f
) {
2789 inst
->opcode
= BRW_OPCODE_MOV
;
2790 inst
->src
[1] = reg_undef
;
2791 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2799 case BRW_CONDITIONAL_GE
:
2800 case BRW_CONDITIONAL_G
:
2801 switch (inst
->src
[1].type
) {
2802 case BRW_REGISTER_TYPE_F
:
2803 if (inst
->src
[1].f
<= 0.0f
) {
2804 inst
->opcode
= BRW_OPCODE_MOV
;
2805 inst
->src
[1] = reg_undef
;
2806 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2818 case BRW_OPCODE_MAD
:
2819 if (inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
||
2820 inst
->src
[1].type
!= BRW_REGISTER_TYPE_F
||
2821 inst
->src
[2].type
!= BRW_REGISTER_TYPE_F
)
2823 if (inst
->src
[1].is_one()) {
2824 inst
->opcode
= BRW_OPCODE_ADD
;
2825 inst
->src
[1] = inst
->src
[2];
2826 inst
->src
[2] = reg_undef
;
2828 } else if (inst
->src
[2].is_one()) {
2829 inst
->opcode
= BRW_OPCODE_ADD
;
2830 inst
->src
[2] = reg_undef
;
2834 case SHADER_OPCODE_BROADCAST
:
2835 if (is_uniform(inst
->src
[0])) {
2836 inst
->opcode
= BRW_OPCODE_MOV
;
2838 inst
->force_writemask_all
= true;
2840 } else if (inst
->src
[1].file
== IMM
) {
2841 inst
->opcode
= BRW_OPCODE_MOV
;
2842 /* It's possible that the selected component will be too large and
2843 * overflow the register. This can happen if someone does a
2844 * readInvocation() from GLSL or SPIR-V and provides an OOB
2845 * invocationIndex. If this happens and we some how manage
2846 * to constant fold it in and get here, then component() may cause
2847 * us to start reading outside of the VGRF which will lead to an
2848 * assert later. Instead, just let it wrap around if it goes over
2851 const unsigned comp
= inst
->src
[1].ud
& (inst
->exec_size
- 1);
2852 inst
->src
[0] = component(inst
->src
[0], comp
);
2854 inst
->force_writemask_all
= true;
2859 case SHADER_OPCODE_SHUFFLE
:
2860 if (is_uniform(inst
->src
[0])) {
2861 inst
->opcode
= BRW_OPCODE_MOV
;
2864 } else if (inst
->src
[1].file
== IMM
) {
2865 inst
->opcode
= BRW_OPCODE_MOV
;
2866 inst
->src
[0] = component(inst
->src
[0],
2877 /* Swap if src[0] is immediate. */
2878 if (progress
&& inst
->is_commutative()) {
2879 if (inst
->src
[0].file
== IMM
) {
2880 fs_reg tmp
= inst
->src
[1];
2881 inst
->src
[1] = inst
->src
[0];
2888 invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW
|
2889 DEPENDENCY_INSTRUCTION_DETAIL
);
2895 * Optimize sample messages that have constant zero values for the trailing
2896 * texture coordinates. We can just reduce the message length for these
2897 * instructions instead of reserving a register for it. Trailing parameters
2898 * that aren't sent default to zero anyway. This will cause the dead code
2899 * eliminator to remove the MOV instruction that would otherwise be emitted to
2900 * set up the zero value.
2903 fs_visitor::opt_zero_samples()
2905 /* Gen4 infers the texturing opcode based on the message length so we can't
2908 if (devinfo
->gen
< 5)
2911 bool progress
= false;
2913 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2914 if (!inst
->is_tex())
2917 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2919 if (load_payload
->is_head_sentinel() ||
2920 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2923 /* We don't want to remove the message header or the first parameter.
2924 * Removing the first parameter is not allowed, see the Haswell PRM
2925 * volume 7, page 149:
2927 * "Parameter 0 is required except for the sampleinfo message, which
2928 * has no parameter 0"
2930 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2931 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2932 (inst
->exec_size
/ 8) +
2933 inst
->header_size
- 1].is_zero()) {
2934 inst
->mlen
-= inst
->exec_size
/ 8;
2940 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL
);
2946 fs_visitor::opt_register_renaming()
2948 bool progress
= false;
2951 unsigned remap
[alloc
.count
];
2952 memset(remap
, ~0u, sizeof(unsigned) * alloc
.count
);
2954 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2955 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2957 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2958 inst
->opcode
== BRW_OPCODE_WHILE
) {
2962 /* Rewrite instruction sources. */
2963 for (int i
= 0; i
< inst
->sources
; i
++) {
2964 if (inst
->src
[i
].file
== VGRF
&&
2965 remap
[inst
->src
[i
].nr
] != ~0u &&
2966 remap
[inst
->src
[i
].nr
] != inst
->src
[i
].nr
) {
2967 inst
->src
[i
].nr
= remap
[inst
->src
[i
].nr
];
2972 const unsigned dst
= inst
->dst
.nr
;
2975 inst
->dst
.file
== VGRF
&&
2976 alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
== inst
->size_written
&&
2977 !inst
->is_partial_write()) {
2978 if (remap
[dst
] == ~0u) {
2981 remap
[dst
] = alloc
.allocate(regs_written(inst
));
2982 inst
->dst
.nr
= remap
[dst
];
2985 } else if (inst
->dst
.file
== VGRF
&&
2986 remap
[dst
] != ~0u &&
2987 remap
[dst
] != dst
) {
2988 inst
->dst
.nr
= remap
[dst
];
2994 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL
|
2995 DEPENDENCY_VARIABLES
);
2997 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2998 if (delta_xy
[i
].file
== VGRF
&& remap
[delta_xy
[i
].nr
] != ~0u) {
2999 delta_xy
[i
].nr
= remap
[delta_xy
[i
].nr
];
3008 * Remove redundant or useless discard jumps.
3010 * For example, we can eliminate jumps in the following sequence:
3012 * discard-jump (redundant with the next jump)
3013 * discard-jump (useless; jumps to the next instruction)
3017 fs_visitor::opt_redundant_discard_jumps()
3019 bool progress
= false;
3021 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
3023 fs_inst
*placeholder_halt
= NULL
;
3024 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
3025 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
3026 placeholder_halt
= inst
;
3031 if (!placeholder_halt
)
3034 /* Delete any HALTs immediately before the placeholder halt. */
3035 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
3036 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
3037 prev
= (fs_inst
*) placeholder_halt
->prev
) {
3038 prev
->remove(last_bblock
);
3043 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
3049 * Compute a bitmask with GRF granularity with a bit set for each GRF starting
3050 * from \p r.offset which overlaps the region starting at \p s.offset and
3051 * spanning \p ds bytes.
3053 static inline unsigned
3054 mask_relative_to(const fs_reg
&r
, const fs_reg
&s
, unsigned ds
)
3056 const int rel_offset
= reg_offset(s
) - reg_offset(r
);
3057 const int shift
= rel_offset
/ REG_SIZE
;
3058 const unsigned n
= DIV_ROUND_UP(rel_offset
% REG_SIZE
+ ds
, REG_SIZE
);
3059 assert(reg_space(r
) == reg_space(s
) &&
3060 shift
>= 0 && shift
< int(8 * sizeof(unsigned)));
3061 return ((1 << n
) - 1) << shift
;
3065 fs_visitor::compute_to_mrf()
3067 bool progress
= false;
3070 /* No MRFs on Gen >= 7. */
3071 if (devinfo
->gen
>= 7)
3074 const fs_live_variables
&live
= live_analysis
.require();
3076 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3080 if (inst
->opcode
!= BRW_OPCODE_MOV
||
3081 inst
->is_partial_write() ||
3082 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= VGRF
||
3083 inst
->dst
.type
!= inst
->src
[0].type
||
3084 inst
->src
[0].abs
|| inst
->src
[0].negate
||
3085 !inst
->src
[0].is_contiguous() ||
3086 inst
->src
[0].offset
% REG_SIZE
!= 0)
3089 /* Can't compute-to-MRF this GRF if someone else was going to
3092 if (live
.vgrf_end
[inst
->src
[0].nr
] > ip
)
3095 /* Found a move of a GRF to a MRF. Let's see if we can go rewrite the
3096 * things that computed the value of all GRFs of the source region. The
3097 * regs_left bitset keeps track of the registers we haven't yet found a
3098 * generating instruction for.
3100 unsigned regs_left
= (1 << regs_read(inst
, 0)) - 1;
3102 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3103 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3104 inst
->src
[0], inst
->size_read(0))) {
3105 /* Found the last thing to write our reg we want to turn
3106 * into a compute-to-MRF.
3109 /* If this one instruction didn't populate all the
3110 * channels, bail. We might be able to rewrite everything
3111 * that writes that reg, but it would require smarter
3114 if (scan_inst
->is_partial_write())
3117 /* Handling things not fully contained in the source of the copy
3118 * would need us to understand coalescing out more than one MOV at
3121 if (!region_contained_in(scan_inst
->dst
, scan_inst
->size_written
,
3122 inst
->src
[0], inst
->size_read(0)))
3125 /* SEND instructions can't have MRF as a destination. */
3126 if (scan_inst
->mlen
)
3129 if (devinfo
->gen
== 6) {
3130 /* gen6 math instructions must have the destination be
3131 * GRF, so no compute-to-MRF for them.
3133 if (scan_inst
->is_math()) {
3138 /* Clear the bits for any registers this instruction overwrites. */
3139 regs_left
&= ~mask_relative_to(
3140 inst
->src
[0], scan_inst
->dst
, scan_inst
->size_written
);
3145 /* We don't handle control flow here. Most computation of
3146 * values that end up in MRFs are shortly before the MRF
3149 if (block
->start() == scan_inst
)
3152 /* You can't read from an MRF, so if someone else reads our
3153 * MRF's source GRF that we wanted to rewrite, that stops us.
3155 bool interfered
= false;
3156 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
3157 if (regions_overlap(scan_inst
->src
[i
], scan_inst
->size_read(i
),
3158 inst
->src
[0], inst
->size_read(0))) {
3165 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3166 inst
->dst
, inst
->size_written
)) {
3167 /* If somebody else writes our MRF here, we can't
3168 * compute-to-MRF before that.
3173 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1 &&
3174 regions_overlap(fs_reg(MRF
, scan_inst
->base_mrf
), scan_inst
->mlen
* REG_SIZE
,
3175 inst
->dst
, inst
->size_written
)) {
3176 /* Found a SEND instruction, which means that there are
3177 * live values in MRFs from base_mrf to base_mrf +
3178 * scan_inst->mlen - 1. Don't go pushing our MRF write up
3188 /* Found all generating instructions of our MRF's source value, so it
3189 * should be safe to rewrite them to point to the MRF directly.
3191 regs_left
= (1 << regs_read(inst
, 0)) - 1;
3193 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3194 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3195 inst
->src
[0], inst
->size_read(0))) {
3196 /* Clear the bits for any registers this instruction overwrites. */
3197 regs_left
&= ~mask_relative_to(
3198 inst
->src
[0], scan_inst
->dst
, scan_inst
->size_written
);
3200 const unsigned rel_offset
= reg_offset(scan_inst
->dst
) -
3201 reg_offset(inst
->src
[0]);
3203 if (inst
->dst
.nr
& BRW_MRF_COMPR4
) {
3204 /* Apply the same address transformation done by the hardware
3205 * for COMPR4 MRF writes.
3207 assert(rel_offset
< 2 * REG_SIZE
);
3208 scan_inst
->dst
.nr
= inst
->dst
.nr
+ rel_offset
/ REG_SIZE
* 4;
3210 /* Clear the COMPR4 bit if the generating instruction is not
3213 if (scan_inst
->size_written
< 2 * REG_SIZE
)
3214 scan_inst
->dst
.nr
&= ~BRW_MRF_COMPR4
;
3217 /* Calculate the MRF number the result of this instruction is
3218 * ultimately written to.
3220 scan_inst
->dst
.nr
= inst
->dst
.nr
+ rel_offset
/ REG_SIZE
;
3223 scan_inst
->dst
.file
= MRF
;
3224 scan_inst
->dst
.offset
= inst
->dst
.offset
+ rel_offset
% REG_SIZE
;
3225 scan_inst
->saturate
|= inst
->saturate
;
3232 inst
->remove(block
);
3237 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
3243 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
3244 * flow. We could probably do better here with some form of divergence
3248 fs_visitor::eliminate_find_live_channel()
3250 bool progress
= false;
3253 if (!brw_stage_has_packed_dispatch(devinfo
, stage
, stage_prog_data
)) {
3254 /* The optimization below assumes that channel zero is live on thread
3255 * dispatch, which may not be the case if the fixed function dispatches
3261 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3262 switch (inst
->opcode
) {
3268 case BRW_OPCODE_ENDIF
:
3269 case BRW_OPCODE_WHILE
:
3273 case FS_OPCODE_DISCARD_JUMP
:
3274 /* This can potentially make control flow non-uniform until the end
3279 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
3281 inst
->opcode
= BRW_OPCODE_MOV
;
3282 inst
->src
[0] = brw_imm_ud(0u);
3284 inst
->force_writemask_all
= true;
3295 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL
);
3301 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
3302 * instructions to FS_OPCODE_REP_FB_WRITE.
3305 fs_visitor::emit_repclear_shader()
3307 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3309 int color_mrf
= base_mrf
+ 2;
3313 mov
= bld
.exec_all().group(4, 0)
3314 .MOV(brw_message_reg(color_mrf
),
3315 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
3317 struct brw_reg reg
=
3318 brw_reg(BRW_GENERAL_REGISTER_FILE
, 2, 3, 0, 0, BRW_REGISTER_TYPE_F
,
3319 BRW_VERTICAL_STRIDE_8
, BRW_WIDTH_2
, BRW_HORIZONTAL_STRIDE_4
,
3320 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
3322 mov
= bld
.exec_all().group(4, 0)
3323 .MOV(vec4(brw_message_reg(color_mrf
)), fs_reg(reg
));
3326 fs_inst
*write
= NULL
;
3327 if (key
->nr_color_regions
== 1) {
3328 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
3329 write
->saturate
= key
->clamp_fragment_color
;
3330 write
->base_mrf
= color_mrf
;
3332 write
->header_size
= 0;
3335 assume(key
->nr_color_regions
> 0);
3337 struct brw_reg header
=
3338 retype(brw_message_reg(base_mrf
), BRW_REGISTER_TYPE_UD
);
3339 bld
.exec_all().group(16, 0)
3340 .MOV(header
, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
3342 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
3344 bld
.exec_all().group(1, 0)
3345 .MOV(component(header
, 2), brw_imm_ud(i
));
3348 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
3349 write
->saturate
= key
->clamp_fragment_color
;
3350 write
->base_mrf
= base_mrf
;
3352 write
->header_size
= 2;
3357 write
->last_rt
= true;
3361 assign_constant_locations();
3362 assign_curb_setup();
3364 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
3366 assert(mov
->src
[0].file
== FIXED_GRF
);
3367 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].nr
, 0);
3374 * Walks through basic blocks, looking for repeated MRF writes and
3375 * removing the later ones.
3378 fs_visitor::remove_duplicate_mrf_writes()
3380 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
3381 bool progress
= false;
3383 /* Need to update the MRF tracking for compressed instructions. */
3384 if (dispatch_width
>= 16)
3387 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3389 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3390 if (inst
->is_control_flow()) {
3391 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3394 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3395 inst
->dst
.file
== MRF
) {
3396 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.nr
];
3397 if (prev_inst
&& prev_inst
->opcode
== BRW_OPCODE_MOV
&&
3398 inst
->dst
.equals(prev_inst
->dst
) &&
3399 inst
->src
[0].equals(prev_inst
->src
[0]) &&
3400 inst
->saturate
== prev_inst
->saturate
&&
3401 inst
->predicate
== prev_inst
->predicate
&&
3402 inst
->conditional_mod
== prev_inst
->conditional_mod
&&
3403 inst
->exec_size
== prev_inst
->exec_size
) {
3404 inst
->remove(block
);
3410 /* Clear out the last-write records for MRFs that were overwritten. */
3411 if (inst
->dst
.file
== MRF
) {
3412 last_mrf_move
[inst
->dst
.nr
] = NULL
;
3415 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
3416 /* Found a SEND instruction, which will include two or fewer
3417 * implied MRF writes. We could do better here.
3419 for (unsigned i
= 0; i
< inst
->implied_mrf_writes(); i
++) {
3420 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
3424 /* Clear out any MRF move records whose sources got overwritten. */
3425 for (unsigned i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
3426 if (last_mrf_move
[i
] &&
3427 regions_overlap(inst
->dst
, inst
->size_written
,
3428 last_mrf_move
[i
]->src
[0],
3429 last_mrf_move
[i
]->size_read(0))) {
3430 last_mrf_move
[i
] = NULL
;
3434 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3435 inst
->dst
.file
== MRF
&&
3436 inst
->src
[0].file
!= ARF
&&
3437 !inst
->is_partial_write()) {
3438 last_mrf_move
[inst
->dst
.nr
] = inst
;
3443 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
3449 * Rounding modes for conversion instructions are included for each
3450 * conversion, but right now it is a state. So once it is set,
3451 * we don't need to call it again for subsequent calls.
3453 * This is useful for vector/matrices conversions, as setting the
3454 * mode once is enough for the full vector/matrix
3457 fs_visitor::remove_extra_rounding_modes()
3459 bool progress
= false;
3460 unsigned execution_mode
= this->nir
->info
.float_controls_execution_mode
;
3462 brw_rnd_mode base_mode
= BRW_RND_MODE_UNSPECIFIED
;
3463 if ((FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
|
3464 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
|
3465 FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
) &
3467 base_mode
= BRW_RND_MODE_RTNE
;
3468 if ((FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
|
3469 FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
|
3470 FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
) &
3472 base_mode
= BRW_RND_MODE_RTZ
;
3474 foreach_block (block
, cfg
) {
3475 brw_rnd_mode prev_mode
= base_mode
;
3477 foreach_inst_in_block_safe (fs_inst
, inst
, block
) {
3478 if (inst
->opcode
== SHADER_OPCODE_RND_MODE
) {
3479 assert(inst
->src
[0].file
== BRW_IMMEDIATE_VALUE
);
3480 const brw_rnd_mode mode
= (brw_rnd_mode
) inst
->src
[0].d
;
3481 if (mode
== prev_mode
) {
3482 inst
->remove(block
);
3492 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
3498 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
3500 /* Clear the flag for registers that actually got read (as expected). */
3501 for (int i
= 0; i
< inst
->sources
; i
++) {
3503 if (inst
->src
[i
].file
== VGRF
|| inst
->src
[i
].file
== FIXED_GRF
) {
3504 grf
= inst
->src
[i
].nr
;
3509 if (grf
>= first_grf
&&
3510 grf
< first_grf
+ grf_len
) {
3511 deps
[grf
- first_grf
] = false;
3512 if (inst
->exec_size
== 16)
3513 deps
[grf
- first_grf
+ 1] = false;
3519 * Implements this workaround for the original 965:
3521 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
3522 * check for post destination dependencies on this instruction, software
3523 * must ensure that there is no destination hazard for the case of ‘write
3524 * followed by a posted write’ shown in the following example.
3527 * 2. send r3.xy <rest of send instruction>
3530 * Due to no post-destination dependency check on the ‘send’, the above
3531 * code sequence could have two instructions (1 and 2) in flight at the
3532 * same time that both consider ‘r3’ as the target of their final writes.
3535 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
3538 int write_len
= regs_written(inst
);
3539 int first_write_grf
= inst
->dst
.nr
;
3540 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3541 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3543 memset(needs_dep
, false, sizeof(needs_dep
));
3544 memset(needs_dep
, true, write_len
);
3546 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
3548 /* Walk backwards looking for writes to registers we're writing which
3549 * aren't read since being written. If we hit the start of the program,
3550 * we assume that there are no outstanding dependencies on entry to the
3553 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3554 /* If we hit control flow, assume that there *are* outstanding
3555 * dependencies, and force their cleanup before our instruction.
3557 if (block
->start() == scan_inst
&& block
->num
!= 0) {
3558 for (int i
= 0; i
< write_len
; i
++) {
3560 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
3561 first_write_grf
+ i
);
3566 /* We insert our reads as late as possible on the assumption that any
3567 * instruction but a MOV that might have left us an outstanding
3568 * dependency has more latency than a MOV.
3570 if (scan_inst
->dst
.file
== VGRF
) {
3571 for (unsigned i
= 0; i
< regs_written(scan_inst
); i
++) {
3572 int reg
= scan_inst
->dst
.nr
+ i
;
3574 if (reg
>= first_write_grf
&&
3575 reg
< first_write_grf
+ write_len
&&
3576 needs_dep
[reg
- first_write_grf
]) {
3577 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
3578 needs_dep
[reg
- first_write_grf
] = false;
3579 if (scan_inst
->exec_size
== 16)
3580 needs_dep
[reg
- first_write_grf
+ 1] = false;
3585 /* Clear the flag for registers that actually got read (as expected). */
3586 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3588 /* Continue the loop only if we haven't resolved all the dependencies */
3590 for (i
= 0; i
< write_len
; i
++) {
3600 * Implements this workaround for the original 965:
3602 * "[DevBW, DevCL] Errata: A destination register from a send can not be
3603 * used as a destination register until after it has been sourced by an
3604 * instruction with a different destination register.
3607 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
3609 int write_len
= regs_written(inst
);
3610 unsigned first_write_grf
= inst
->dst
.nr
;
3611 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3612 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3614 memset(needs_dep
, false, sizeof(needs_dep
));
3615 memset(needs_dep
, true, write_len
);
3616 /* Walk forwards looking for writes to registers we're writing which aren't
3617 * read before being written.
3619 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
) {
3620 /* If we hit control flow, force resolve all remaining dependencies. */
3621 if (block
->end() == scan_inst
&& block
->num
!= cfg
->num_blocks
- 1) {
3622 for (int i
= 0; i
< write_len
; i
++) {
3624 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3625 first_write_grf
+ i
);
3630 /* Clear the flag for registers that actually got read (as expected). */
3631 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3633 /* We insert our reads as late as possible since they're reading the
3634 * result of a SEND, which has massive latency.
3636 if (scan_inst
->dst
.file
== VGRF
&&
3637 scan_inst
->dst
.nr
>= first_write_grf
&&
3638 scan_inst
->dst
.nr
< first_write_grf
+ write_len
&&
3639 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
]) {
3640 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3642 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
] = false;
3645 /* Continue the loop only if we haven't resolved all the dependencies */
3647 for (i
= 0; i
< write_len
; i
++) {
3657 fs_visitor::insert_gen4_send_dependency_workarounds()
3659 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
3662 bool progress
= false;
3664 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3665 if (inst
->mlen
!= 0 && inst
->dst
.file
== VGRF
) {
3666 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3667 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3673 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
3677 * Turns the generic expression-style uniform pull constant load instruction
3678 * into a hardware-specific series of instructions for loading a pull
3681 * The expression style allows the CSE pass before this to optimize out
3682 * repeated loads from the same offset, and gives the pre-register-allocation
3683 * scheduling full flexibility, while the conversion to native instructions
3684 * allows the post-register-allocation scheduler the best information
3687 * Note that execution masking for setting up pull constant loads is special:
3688 * the channels that need to be written are unrelated to the current execution
3689 * mask, since a later instruction will use one of the result channels as a
3690 * source operand for all 8 or 16 of its channels.
3693 fs_visitor::lower_uniform_pull_constant_loads()
3695 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3696 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3699 if (devinfo
->gen
>= 7) {
3700 const fs_builder ubld
= fs_builder(this, block
, inst
).exec_all();
3701 const fs_reg payload
= ubld
.group(8, 0).vgrf(BRW_REGISTER_TYPE_UD
);
3703 ubld
.group(8, 0).MOV(payload
,
3704 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
3705 ubld
.group(1, 0).MOV(component(payload
, 2),
3706 brw_imm_ud(inst
->src
[1].ud
/ 16));
3708 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3709 inst
->src
[1] = payload
;
3710 inst
->header_size
= 1;
3713 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
3715 /* Before register allocation, we didn't tell the scheduler about the
3716 * MRF we use. We know it's safe to use this MRF because nothing
3717 * else does except for register spill/unspill, which generates and
3718 * uses its MRF within a single IR instruction.
3720 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
3727 fs_visitor::lower_load_payload()
3729 bool progress
= false;
3731 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3732 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3735 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== VGRF
);
3736 assert(inst
->saturate
== false);
3737 fs_reg dst
= inst
->dst
;
3739 /* Get rid of COMPR4. We'll add it back in if we need it */
3740 if (dst
.file
== MRF
)
3741 dst
.nr
= dst
.nr
& ~BRW_MRF_COMPR4
;
3743 const fs_builder
ibld(this, block
, inst
);
3744 const fs_builder ubld
= ibld
.exec_all();
3746 for (uint8_t i
= 0; i
< inst
->header_size
;) {
3747 /* Number of header GRFs to initialize at once with a single MOV
3751 (i
+ 1 < inst
->header_size
&& inst
->src
[i
].stride
== 1 &&
3752 inst
->src
[i
+ 1].equals(byte_offset(inst
->src
[i
], REG_SIZE
))) ?
3755 if (inst
->src
[i
].file
!= BAD_FILE
)
3756 ubld
.group(8 * n
, 0).MOV(retype(dst
, BRW_REGISTER_TYPE_UD
),
3757 retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
));
3759 dst
= byte_offset(dst
, n
* REG_SIZE
);
3763 if (inst
->dst
.file
== MRF
&& (inst
->dst
.nr
& BRW_MRF_COMPR4
) &&
3764 inst
->exec_size
> 8) {
3765 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3766 * a straightforward copy. Instead, the result of the
3767 * LOAD_PAYLOAD is treated as interleaved and the first four
3768 * non-header sources are unpacked as:
3779 * This is used for gen <= 5 fb writes.
3781 assert(inst
->exec_size
== 16);
3782 assert(inst
->header_size
+ 4 <= inst
->sources
);
3783 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3784 if (inst
->src
[i
].file
!= BAD_FILE
) {
3785 if (devinfo
->has_compr4
) {
3786 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3787 compr4_dst
.nr
|= BRW_MRF_COMPR4
;
3788 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3790 /* Platform doesn't have COMPR4. We have to fake it */
3791 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3792 ibld
.quarter(0).MOV(mov_dst
, quarter(inst
->src
[i
], 0));
3794 ibld
.quarter(1).MOV(mov_dst
, quarter(inst
->src
[i
], 1));
3801 /* The loop above only ever incremented us through the first set
3802 * of 4 registers. However, thanks to the magic of COMPR4, we
3803 * actually wrote to the first 8 registers, so we need to take
3804 * that into account now.
3808 /* The COMPR4 code took care of the first 4 sources. We'll let
3809 * the regular path handle any remaining sources. Yes, we are
3810 * modifying the instruction but we're about to delete it so
3811 * this really doesn't hurt anything.
3813 inst
->header_size
+= 4;
3816 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3817 if (inst
->src
[i
].file
!= BAD_FILE
) {
3818 dst
.type
= inst
->src
[i
].type
;
3819 ibld
.MOV(dst
, inst
->src
[i
]);
3821 dst
.type
= BRW_REGISTER_TYPE_UD
;
3823 dst
= offset(dst
, ibld
, 1);
3826 inst
->remove(block
);
3831 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
3837 fs_visitor::lower_mul_dword_inst(fs_inst
*inst
, bblock_t
*block
)
3839 const fs_builder
ibld(this, block
, inst
);
3841 const bool ud
= (inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3842 if (inst
->src
[1].file
== IMM
&&
3843 (( ud
&& inst
->src
[1].ud
<= UINT16_MAX
) ||
3844 (!ud
&& inst
->src
[1].d
<= INT16_MAX
&& inst
->src
[1].d
>= INT16_MIN
))) {
3845 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3846 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3849 * If multiplying by an immediate value that fits in 16-bits, do a
3850 * single MUL instruction with that value in the proper location.
3852 if (devinfo
->gen
< 7) {
3853 fs_reg
imm(VGRF
, alloc
.allocate(dispatch_width
/ 8), inst
->dst
.type
);
3854 ibld
.MOV(imm
, inst
->src
[1]);
3855 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3857 ibld
.MUL(inst
->dst
, inst
->src
[0],
3858 ud
? brw_imm_uw(inst
->src
[1].ud
)
3859 : brw_imm_w(inst
->src
[1].d
));
3862 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3863 * do 32-bit integer multiplication in one instruction, but instead
3864 * must do a sequence (which actually calculates a 64-bit result):
3866 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3867 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3868 * mov(8) g2<1>D acc0<8,8,1>D
3870 * But on Gen > 6, the ability to use second accumulator register
3871 * (acc1) for non-float data types was removed, preventing a simple
3872 * implementation in SIMD16. A 16-channel result can be calculated by
3873 * executing the three instructions twice in SIMD8, once with quarter
3874 * control of 1Q for the first eight channels and again with 2Q for
3875 * the second eight channels.
3877 * Which accumulator register is implicitly accessed (by AccWrEnable
3878 * for instance) is determined by the quarter control. Unfortunately
3879 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3880 * implicit accumulator access by an instruction with 2Q will access
3881 * acc1 regardless of whether the data type is usable in acc1.
3883 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3884 * integer data types.
3886 * Since we only want the low 32-bits of the result, we can do two
3887 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3888 * adjust the high result and add them (like the mach is doing):
3890 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3891 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3892 * shl(8) g9<1>D g8<8,8,1>D 16D
3893 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3895 * We avoid the shl instruction by realizing that we only want to add
3896 * the low 16-bits of the "high" result to the high 16-bits of the
3897 * "low" result and using proper regioning on the add:
3899 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3900 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3901 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3903 * Since it does not use the (single) accumulator register, we can
3904 * schedule multi-component multiplications much better.
3907 bool needs_mov
= false;
3908 fs_reg orig_dst
= inst
->dst
;
3910 /* Get a new VGRF for the "low" 32x16-bit multiplication result if
3911 * reusing the original destination is impossible due to hardware
3912 * restrictions, source/destination overlap, or it being the null
3915 fs_reg low
= inst
->dst
;
3916 if (orig_dst
.is_null() || orig_dst
.file
== MRF
||
3917 regions_overlap(inst
->dst
, inst
->size_written
,
3918 inst
->src
[0], inst
->size_read(0)) ||
3919 regions_overlap(inst
->dst
, inst
->size_written
,
3920 inst
->src
[1], inst
->size_read(1)) ||
3921 inst
->dst
.stride
>= 4) {
3923 low
= fs_reg(VGRF
, alloc
.allocate(regs_written(inst
)),
3927 /* Get a new VGRF but keep the same stride as inst->dst */
3928 fs_reg
high(VGRF
, alloc
.allocate(regs_written(inst
)), inst
->dst
.type
);
3929 high
.stride
= inst
->dst
.stride
;
3930 high
.offset
= inst
->dst
.offset
% REG_SIZE
;
3932 if (devinfo
->gen
>= 7) {
3933 /* From GEN:BUG:1604601757:
3935 * "When multiplying a DW and any lower precision integer, source modifier
3936 * is not supported."
3938 * An unsupported negate modifier on src[1] would ordinarily be
3939 * lowered by the subsequent lower_regioning pass. In this case that
3940 * pass would spawn another dword multiply. Instead, lower the
3943 const bool source_mods_unsupported
= (devinfo
->gen
>= 12);
3945 if (inst
->src
[1].abs
|| (inst
->src
[1].negate
&&
3946 source_mods_unsupported
))
3947 lower_src_modifiers(this, block
, inst
, 1);
3949 if (inst
->src
[1].file
== IMM
) {
3950 ibld
.MUL(low
, inst
->src
[0],
3951 brw_imm_uw(inst
->src
[1].ud
& 0xffff));
3952 ibld
.MUL(high
, inst
->src
[0],
3953 brw_imm_uw(inst
->src
[1].ud
>> 16));
3955 ibld
.MUL(low
, inst
->src
[0],
3956 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 0));
3957 ibld
.MUL(high
, inst
->src
[0],
3958 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 1));
3961 if (inst
->src
[0].abs
)
3962 lower_src_modifiers(this, block
, inst
, 0);
3964 ibld
.MUL(low
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UW
, 0),
3966 ibld
.MUL(high
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UW
, 1),
3970 ibld
.ADD(subscript(low
, BRW_REGISTER_TYPE_UW
, 1),
3971 subscript(low
, BRW_REGISTER_TYPE_UW
, 1),
3972 subscript(high
, BRW_REGISTER_TYPE_UW
, 0));
3974 if (needs_mov
|| inst
->conditional_mod
)
3975 set_condmod(inst
->conditional_mod
, ibld
.MOV(orig_dst
, low
));
3980 fs_visitor::lower_mul_qword_inst(fs_inst
*inst
, bblock_t
*block
)
3982 const fs_builder
ibld(this, block
, inst
);
3984 /* Considering two 64-bit integers ab and cd where each letter ab
3985 * corresponds to 32 bits, we get a 128-bit result WXYZ. We * cd
3986 * only need to provide the YZ part of the result. -------
3988 * Only BD needs to be 64 bits. For AD and BC we only care + AD
3989 * about the lower 32 bits (since they are part of the upper + BC
3990 * 32 bits of our result). AC is not needed since it starts + AC
3991 * on the 65th bit of the result. -------
3994 unsigned int q_regs
= regs_written(inst
);
3995 unsigned int d_regs
= (q_regs
+ 1) / 2;
3997 fs_reg
bd(VGRF
, alloc
.allocate(q_regs
), BRW_REGISTER_TYPE_UQ
);
3998 fs_reg
ad(VGRF
, alloc
.allocate(d_regs
), BRW_REGISTER_TYPE_UD
);
3999 fs_reg
bc(VGRF
, alloc
.allocate(d_regs
), BRW_REGISTER_TYPE_UD
);
4001 /* Here we need the full 64 bit result for 32b * 32b. */
4002 if (devinfo
->has_integer_dword_mul
) {
4003 ibld
.MUL(bd
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
4004 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 0));
4006 fs_reg
bd_high(VGRF
, alloc
.allocate(d_regs
), BRW_REGISTER_TYPE_UD
);
4007 fs_reg
bd_low(VGRF
, alloc
.allocate(d_regs
), BRW_REGISTER_TYPE_UD
);
4008 fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
), BRW_REGISTER_TYPE_UD
);
4010 fs_inst
*mul
= ibld
.MUL(acc
,
4011 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
4012 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 0));
4013 mul
->writes_accumulator
= true;
4015 ibld
.MACH(bd_high
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
4016 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 0));
4017 ibld
.MOV(bd_low
, acc
);
4019 ibld
.MOV(subscript(bd
, BRW_REGISTER_TYPE_UD
, 0), bd_low
);
4020 ibld
.MOV(subscript(bd
, BRW_REGISTER_TYPE_UD
, 1), bd_high
);
4023 ibld
.MUL(ad
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1),
4024 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 0));
4025 ibld
.MUL(bc
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
4026 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 1));
4028 ibld
.ADD(ad
, ad
, bc
);
4029 ibld
.ADD(subscript(bd
, BRW_REGISTER_TYPE_UD
, 1),
4030 subscript(bd
, BRW_REGISTER_TYPE_UD
, 1), ad
);
4032 ibld
.MOV(inst
->dst
, bd
);
4036 fs_visitor::lower_mulh_inst(fs_inst
*inst
, bblock_t
*block
)
4038 const fs_builder
ibld(this, block
, inst
);
4040 /* According to the BDW+ BSpec page for the "Multiply Accumulate
4041 * High" instruction:
4043 * "An added preliminary mov is required for source modification on
4045 * mov (8) r3.0<1>:d -r3<8;8,1>:d
4046 * mul (8) acc0:d r2.0<8;8,1>:d r3.0<16;8,2>:uw
4047 * mach (8) r5.0<1>:d r2.0<8;8,1>:d r3.0<8;8,1>:d"
4049 if (devinfo
->gen
>= 8 && (inst
->src
[1].negate
|| inst
->src
[1].abs
))
4050 lower_src_modifiers(this, block
, inst
, 1);
4052 /* Should have been lowered to 8-wide. */
4053 assert(inst
->exec_size
<= get_lowered_simd_width(devinfo
, inst
));
4054 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
), inst
->dst
.type
);
4055 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
4056 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
4058 if (devinfo
->gen
>= 8) {
4059 /* Until Gen8, integer multiplies read 32-bits from one source,
4060 * and 16-bits from the other, and relying on the MACH instruction
4061 * to generate the high bits of the result.
4063 * On Gen8, the multiply instruction does a full 32x32-bit
4064 * multiply, but in order to do a 64-bit multiply we can simulate
4065 * the previous behavior and then use a MACH instruction.
4067 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
4068 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
4069 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
4070 mul
->src
[1].stride
*= 2;
4072 if (mul
->src
[1].file
== IMM
) {
4073 mul
->src
[1] = brw_imm_uw(mul
->src
[1].ud
);
4075 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
4077 /* Among other things the quarter control bits influence which
4078 * accumulator register is used by the hardware for instructions
4079 * that access the accumulator implicitly (e.g. MACH). A
4080 * second-half instruction would normally map to acc1, which
4081 * doesn't exist on Gen7 and up (the hardware does emulate it for
4082 * floating-point instructions *only* by taking advantage of the
4083 * extra precision of acc0 not normally used for floating point
4086 * HSW and up are careful enough not to try to access an
4087 * accumulator register that doesn't exist, but on earlier Gen7
4088 * hardware we need to make sure that the quarter control bits are
4089 * zero to avoid non-deterministic behaviour and emit an extra MOV
4090 * to get the result masked correctly according to the current
4094 mach
->force_writemask_all
= true;
4095 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
4096 ibld
.MOV(inst
->dst
, mach
->dst
);
4101 fs_visitor::lower_integer_multiplication()
4103 bool progress
= false;
4105 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4106 if (inst
->opcode
== BRW_OPCODE_MUL
) {
4107 /* If the instruction is already in a form that does not need lowering,
4110 if (devinfo
->gen
>= 7) {
4111 if (type_sz(inst
->src
[1].type
) < 4 && type_sz(inst
->src
[0].type
) <= 4)
4114 if (type_sz(inst
->src
[0].type
) < 4 && type_sz(inst
->src
[1].type
) <= 4)
4118 if ((inst
->dst
.type
== BRW_REGISTER_TYPE_Q
||
4119 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
) &&
4120 (inst
->src
[0].type
== BRW_REGISTER_TYPE_Q
||
4121 inst
->src
[0].type
== BRW_REGISTER_TYPE_UQ
) &&
4122 (inst
->src
[1].type
== BRW_REGISTER_TYPE_Q
||
4123 inst
->src
[1].type
== BRW_REGISTER_TYPE_UQ
)) {
4124 lower_mul_qword_inst(inst
, block
);
4125 inst
->remove(block
);
4127 } else if (!inst
->dst
.is_accumulator() &&
4128 (inst
->dst
.type
== BRW_REGISTER_TYPE_D
||
4129 inst
->dst
.type
== BRW_REGISTER_TYPE_UD
) &&
4130 !devinfo
->has_integer_dword_mul
) {
4131 lower_mul_dword_inst(inst
, block
);
4132 inst
->remove(block
);
4135 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
4136 lower_mulh_inst(inst
, block
);
4137 inst
->remove(block
);
4144 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
4150 fs_visitor::lower_minmax()
4152 assert(devinfo
->gen
< 6);
4154 bool progress
= false;
4156 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4157 const fs_builder
ibld(this, block
, inst
);
4159 if (inst
->opcode
== BRW_OPCODE_SEL
&&
4160 inst
->predicate
== BRW_PREDICATE_NONE
) {
4161 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
4162 * the original SEL.L/GE instruction
4164 ibld
.CMP(ibld
.null_reg_d(), inst
->src
[0], inst
->src
[1],
4165 inst
->conditional_mod
);
4166 inst
->predicate
= BRW_PREDICATE_NORMAL
;
4167 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
4174 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
);
4180 fs_visitor::lower_sub_sat()
4182 bool progress
= false;
4184 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4185 const fs_builder
ibld(this, block
, inst
);
4187 if (inst
->opcode
== SHADER_OPCODE_USUB_SAT
||
4188 inst
->opcode
== SHADER_OPCODE_ISUB_SAT
) {
4189 /* The fundamental problem is the hardware performs source negation
4190 * at the bit width of the source. If the source is 0x80000000D, the
4191 * negation is 0x80000000D. As a result, subtractSaturate(0,
4192 * 0x80000000) will produce 0x80000000 instead of 0x7fffffff. There
4193 * are at least three ways to resolve this:
4195 * 1. Use the accumulator for the negated source. The accumulator is
4196 * 33 bits, so our source 0x80000000 is sign-extended to
4197 * 0x1800000000. The negation of which is 0x080000000. This
4198 * doesn't help for 64-bit integers (which are already bigger than
4199 * 33 bits). There are also only 8 accumulators, so SIMD16 or
4200 * SIMD32 instructions would have to be split into multiple SIMD8
4203 * 2. Use slightly different math. For any n-bit value x, we know (x
4204 * >> 1) != -(x >> 1). We can use this fact to only do
4205 * subtractions involving (x >> 1). subtractSaturate(a, b) ==
4206 * subtractSaturate(subtractSaturate(a, (b >> 1)), b - (b >> 1)).
4208 * 3. For unsigned sources, it is sufficient to replace the
4209 * subtractSaturate with (a > b) ? a - b : 0.
4211 * It may also be possible to use the SUBB instruction. This
4212 * implicitly writes the accumulator, so it could only be used in the
4213 * same situations as #1 above. It is further limited by only
4214 * allowing UD sources.
4216 if (inst
->exec_size
== 8 && inst
->src
[0].type
!= BRW_REGISTER_TYPE_Q
&&
4217 inst
->src
[0].type
!= BRW_REGISTER_TYPE_UQ
) {
4218 fs_reg
acc(ARF
, BRW_ARF_ACCUMULATOR
, inst
->src
[1].type
);
4220 ibld
.MOV(acc
, inst
->src
[1]);
4221 fs_inst
*add
= ibld
.ADD(inst
->dst
, acc
, inst
->src
[0]);
4222 add
->saturate
= true;
4223 add
->src
[0].negate
= true;
4224 } else if (inst
->opcode
== SHADER_OPCODE_ISUB_SAT
) {
4226 * dst = add.sat(add.sat(src0, -tmp), -(src1 - tmp));
4228 fs_reg tmp1
= ibld
.vgrf(inst
->src
[0].type
);
4229 fs_reg tmp2
= ibld
.vgrf(inst
->src
[0].type
);
4230 fs_reg tmp3
= ibld
.vgrf(inst
->src
[0].type
);
4233 ibld
.SHR(tmp1
, inst
->src
[1], brw_imm_d(1));
4235 add
= ibld
.ADD(tmp2
, inst
->src
[1], tmp1
);
4236 add
->src
[1].negate
= true;
4238 add
= ibld
.ADD(tmp3
, inst
->src
[0], tmp1
);
4239 add
->src
[1].negate
= true;
4240 add
->saturate
= true;
4242 add
= ibld
.ADD(inst
->dst
, tmp3
, tmp2
);
4243 add
->src
[1].negate
= true;
4244 add
->saturate
= true;
4246 /* a > b ? a - b : 0 */
4247 ibld
.CMP(ibld
.null_reg_d(), inst
->src
[0], inst
->src
[1],
4250 fs_inst
*add
= ibld
.ADD(inst
->dst
, inst
->src
[0], inst
->src
[1]);
4251 add
->src
[1].negate
= !add
->src
[1].negate
;
4253 ibld
.SEL(inst
->dst
, inst
->dst
, brw_imm_ud(0))
4254 ->predicate
= BRW_PREDICATE_NORMAL
;
4257 inst
->remove(block
);
4263 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
4269 * Get the mask of SIMD channels enabled during dispatch and not yet disabled
4270 * by discard. Due to the layout of the sample mask in the fragment shader
4271 * thread payload, \p bld is required to have a dispatch_width() not greater
4272 * than 16 for fragment shaders.
4275 sample_mask_reg(const fs_builder
&bld
)
4277 const fs_visitor
*v
= static_cast<const fs_visitor
*>(bld
.shader
);
4279 if (v
->stage
!= MESA_SHADER_FRAGMENT
) {
4280 return brw_imm_ud(0xffffffff);
4281 } else if (brw_wm_prog_data(v
->stage_prog_data
)->uses_kill
) {
4282 assert(bld
.dispatch_width() <= 16);
4283 return brw_flag_subreg(sample_mask_flag_subreg(v
) + bld
.group() / 16);
4285 assert(v
->devinfo
->gen
>= 6 && bld
.dispatch_width() <= 16);
4286 return retype(brw_vec1_grf((bld
.group() >= 16 ? 2 : 1), 7),
4287 BRW_REGISTER_TYPE_UW
);
4292 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
4293 fs_reg
*dst
, fs_reg color
, unsigned components
)
4295 if (key
->clamp_fragment_color
) {
4296 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
4297 assert(color
.type
== BRW_REGISTER_TYPE_F
);
4299 for (unsigned i
= 0; i
< components
; i
++)
4301 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
4306 for (unsigned i
= 0; i
< components
; i
++)
4307 dst
[i
] = offset(color
, bld
, i
);
4311 brw_fb_write_msg_control(const fs_inst
*inst
,
4312 const struct brw_wm_prog_data
*prog_data
)
4316 if (inst
->opcode
== FS_OPCODE_REP_FB_WRITE
) {
4317 assert(inst
->group
== 0 && inst
->exec_size
== 16);
4318 mctl
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE_REPLICATED
;
4319 } else if (prog_data
->dual_src_blend
) {
4320 assert(inst
->exec_size
== 8);
4322 if (inst
->group
% 16 == 0)
4323 mctl
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN01
;
4324 else if (inst
->group
% 16 == 8)
4325 mctl
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_DUAL_SOURCE_SUBSPAN23
;
4327 unreachable("Invalid dual-source FB write instruction group");
4329 assert(inst
->group
== 0 || (inst
->group
== 16 && inst
->exec_size
== 16));
4331 if (inst
->exec_size
== 16)
4332 mctl
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE
;
4333 else if (inst
->exec_size
== 8)
4334 mctl
= BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01
;
4336 unreachable("Invalid FB write execution size");
4343 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
4344 const struct brw_wm_prog_data
*prog_data
,
4345 const brw_wm_prog_key
*key
,
4346 const fs_visitor::thread_payload
&payload
)
4348 assert(inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
4349 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4350 const fs_reg
&color0
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR0
];
4351 const fs_reg
&color1
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
];
4352 const fs_reg
&src0_alpha
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA
];
4353 const fs_reg
&src_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
];
4354 const fs_reg
&dst_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_DST_DEPTH
];
4355 const fs_reg
&src_stencil
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_STENCIL
];
4356 fs_reg sample_mask
= inst
->src
[FB_WRITE_LOGICAL_SRC_OMASK
];
4357 const unsigned components
=
4358 inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
4360 assert(inst
->target
!= 0 || src0_alpha
.file
== BAD_FILE
);
4362 /* We can potentially have a message length of up to 15, so we have to set
4363 * base_mrf to either 0 or 1 in order to fit in m0..m15.
4366 int header_size
= 2, payload_header_size
;
4367 unsigned length
= 0;
4369 if (devinfo
->gen
< 6) {
4370 /* TODO: Support SIMD32 on gen4-5 */
4371 assert(bld
.group() < 16);
4373 /* For gen4-5, we always have a header consisting of g0 and g1. We have
4374 * an implied MOV from g0,g1 to the start of the message. The MOV from
4375 * g0 is handled by the hardware and the MOV from g1 is provided by the
4376 * generator. This is required because, on gen4-5, the generator may
4377 * generate two write messages with different message lengths in order
4378 * to handle AA data properly.
4380 * Also, since the pixel mask goes in the g0 portion of the message and
4381 * since render target writes are the last thing in the shader, we write
4382 * the pixel mask directly into g0 and it will get copied as part of the
4385 if (prog_data
->uses_kill
) {
4386 bld
.exec_all().group(1, 0)
4387 .MOV(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
),
4388 sample_mask_reg(bld
));
4391 assert(length
== 0);
4393 } else if ((devinfo
->gen
<= 7 && !devinfo
->is_haswell
&&
4394 prog_data
->uses_kill
) ||
4395 (devinfo
->gen
< 11 &&
4396 (color1
.file
!= BAD_FILE
|| key
->nr_color_regions
> 1))) {
4397 /* From the Sandy Bridge PRM, volume 4, page 198:
4399 * "Dispatched Pixel Enables. One bit per pixel indicating
4400 * which pixels were originally enabled when the thread was
4401 * dispatched. This field is only required for the end-of-
4402 * thread message and on all dual-source messages."
4404 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4406 fs_reg header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
4407 if (bld
.group() < 16) {
4408 /* The header starts off as g0 and g1 for the first half */
4409 ubld
.group(16, 0).MOV(header
, retype(brw_vec8_grf(0, 0),
4410 BRW_REGISTER_TYPE_UD
));
4412 /* The header starts off as g0 and g2 for the second half */
4413 assert(bld
.group() < 32);
4414 const fs_reg header_sources
[2] = {
4415 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
),
4416 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD
),
4418 ubld
.LOAD_PAYLOAD(header
, header_sources
, 2, 0);
4420 /* Gen12 will require additional fix-ups if we ever hit this path. */
4421 assert(devinfo
->gen
< 12);
4424 uint32_t g00_bits
= 0;
4426 /* Set "Source0 Alpha Present to RenderTarget" bit in message
4429 if (src0_alpha
.file
!= BAD_FILE
)
4430 g00_bits
|= 1 << 11;
4432 /* Set computes stencil to render target */
4433 if (prog_data
->computed_stencil
)
4434 g00_bits
|= 1 << 14;
4437 /* OR extra bits into g0.0 */
4438 ubld
.group(1, 0).OR(component(header
, 0),
4439 retype(brw_vec1_grf(0, 0),
4440 BRW_REGISTER_TYPE_UD
),
4441 brw_imm_ud(g00_bits
));
4444 /* Set the render target index for choosing BLEND_STATE. */
4445 if (inst
->target
> 0) {
4446 ubld
.group(1, 0).MOV(component(header
, 2), brw_imm_ud(inst
->target
));
4449 if (prog_data
->uses_kill
) {
4450 ubld
.group(1, 0).MOV(retype(component(header
, 15),
4451 BRW_REGISTER_TYPE_UW
),
4452 sample_mask_reg(bld
));
4455 assert(length
== 0);
4456 sources
[0] = header
;
4457 sources
[1] = horiz_offset(header
, 8);
4460 assert(length
== 0 || length
== 2);
4461 header_size
= length
;
4463 if (payload
.aa_dest_stencil_reg
[0]) {
4464 assert(inst
->group
< 16);
4465 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1));
4466 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
4467 .MOV(sources
[length
],
4468 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
[0], 0)));
4472 if (src0_alpha
.file
!= BAD_FILE
) {
4473 for (unsigned i
= 0; i
< bld
.dispatch_width() / 8; i
++) {
4474 const fs_builder
&ubld
= bld
.exec_all().group(8, i
)
4475 .annotate("FB write src0 alpha");
4476 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_F
);
4477 ubld
.MOV(tmp
, horiz_offset(src0_alpha
, i
* 8));
4478 setup_color_payload(ubld
, key
, &sources
[length
], tmp
, 1);
4483 if (sample_mask
.file
!= BAD_FILE
) {
4484 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1),
4485 BRW_REGISTER_TYPE_UD
);
4487 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
4488 * relevant. Since it's unsigned single words one vgrf is always
4489 * 16-wide, but only the lower or higher 8 channels will be used by the
4490 * hardware when doing a SIMD8 write depending on whether we have
4491 * selected the subspans for the first or second half respectively.
4493 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
4494 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
4495 sample_mask
.stride
*= 2;
4497 bld
.exec_all().annotate("FB write oMask")
4498 .MOV(horiz_offset(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
4504 payload_header_size
= length
;
4506 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
4509 if (color1
.file
!= BAD_FILE
) {
4510 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
4514 if (src_depth
.file
!= BAD_FILE
) {
4515 sources
[length
] = src_depth
;
4519 if (dst_depth
.file
!= BAD_FILE
) {
4520 sources
[length
] = dst_depth
;
4524 if (src_stencil
.file
!= BAD_FILE
) {
4525 assert(devinfo
->gen
>= 9);
4526 assert(bld
.dispatch_width() == 8);
4528 /* XXX: src_stencil is only available on gen9+. dst_depth is never
4529 * available on gen9+. As such it's impossible to have both enabled at the
4530 * same time and therefore length cannot overrun the array.
4532 assert(length
< 15);
4534 sources
[length
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4535 bld
.exec_all().annotate("FB write OS")
4536 .MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UB
),
4537 subscript(src_stencil
, BRW_REGISTER_TYPE_UB
, 0));
4542 if (devinfo
->gen
>= 7) {
4543 /* Send from the GRF */
4544 fs_reg payload
= fs_reg(VGRF
, -1, BRW_REGISTER_TYPE_F
);
4545 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
4546 payload
.nr
= bld
.shader
->alloc
.allocate(regs_written(load
));
4547 load
->dst
= payload
;
4549 uint32_t msg_ctl
= brw_fb_write_msg_control(inst
, prog_data
);
4550 uint32_t ex_desc
= 0;
4553 (inst
->group
/ 16) << 11 | /* rt slot group */
4554 brw_dp_write_desc(devinfo
, inst
->target
, msg_ctl
,
4555 GEN6_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE
,
4556 inst
->last_rt
, false);
4558 if (devinfo
->gen
>= 11) {
4559 /* Set the "Render Target Index" and "Src0 Alpha Present" fields
4560 * in the extended message descriptor, in lieu of using a header.
4562 ex_desc
= inst
->target
<< 12 | (src0_alpha
.file
!= BAD_FILE
) << 15;
4564 if (key
->nr_color_regions
== 0)
4565 ex_desc
|= 1 << 20; /* Null Render Target */
4568 inst
->opcode
= SHADER_OPCODE_SEND
;
4569 inst
->resize_sources(3);
4570 inst
->sfid
= GEN6_SFID_DATAPORT_RENDER_CACHE
;
4571 inst
->src
[0] = brw_imm_ud(inst
->desc
);
4572 inst
->src
[1] = brw_imm_ud(ex_desc
);
4573 inst
->src
[2] = payload
;
4574 inst
->mlen
= regs_written(load
);
4576 inst
->header_size
= header_size
;
4577 inst
->check_tdr
= true;
4578 inst
->send_has_side_effects
= true;
4580 /* Send from the MRF */
4581 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
4582 sources
, length
, payload_header_size
);
4584 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
4585 * will do this for us if we just give it a COMPR4 destination.
4587 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
4588 load
->dst
.nr
|= BRW_MRF_COMPR4
;
4590 if (devinfo
->gen
< 6) {
4591 /* Set up src[0] for the implied MOV from grf0-1 */
4592 inst
->resize_sources(1);
4593 inst
->src
[0] = brw_vec8_grf(0, 0);
4595 inst
->resize_sources(0);
4598 inst
->opcode
= FS_OPCODE_FB_WRITE
;
4599 inst
->mlen
= regs_written(load
);
4600 inst
->header_size
= header_size
;
4605 lower_fb_read_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
4607 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4608 const fs_builder
&ubld
= bld
.exec_all().group(8, 0);
4609 const unsigned length
= 2;
4610 const fs_reg header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, length
);
4612 if (bld
.group() < 16) {
4613 ubld
.group(16, 0).MOV(header
, retype(brw_vec8_grf(0, 0),
4614 BRW_REGISTER_TYPE_UD
));
4616 assert(bld
.group() < 32);
4617 const fs_reg header_sources
[] = {
4618 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
),
4619 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD
)
4621 ubld
.LOAD_PAYLOAD(header
, header_sources
, ARRAY_SIZE(header_sources
), 0);
4623 if (devinfo
->gen
>= 12) {
4624 /* On Gen12 the Viewport and Render Target Array Index fields (AKA
4625 * Poly 0 Info) are provided in r1.1 instead of r0.0, and the render
4626 * target message header format was updated accordingly -- However
4627 * the updated format only works for the lower 16 channels in a
4628 * SIMD32 thread, since the higher 16 channels want the subspan data
4629 * from r2 instead of r1, so we need to copy over the contents of
4630 * r1.1 in order to fix things up.
4632 ubld
.group(1, 0).MOV(component(header
, 9),
4633 retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_UD
));
4637 inst
->resize_sources(1);
4638 inst
->src
[0] = header
;
4639 inst
->opcode
= FS_OPCODE_FB_READ
;
4640 inst
->mlen
= length
;
4641 inst
->header_size
= length
;
4645 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4646 const fs_reg
&coordinate
,
4647 const fs_reg
&shadow_c
,
4648 const fs_reg
&lod
, const fs_reg
&lod2
,
4649 const fs_reg
&surface
,
4650 const fs_reg
&sampler
,
4651 unsigned coord_components
,
4652 unsigned grad_components
)
4654 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
4655 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
4656 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
4657 fs_reg msg_end
= msg_begin
;
4660 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
4662 for (unsigned i
= 0; i
< coord_components
; i
++)
4663 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
4664 offset(coordinate
, bld
, i
));
4666 msg_end
= offset(msg_end
, bld
, coord_components
);
4668 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
4669 * require all three components to be present and zero if they are unused.
4671 if (coord_components
> 0 &&
4672 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
4673 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
4674 assert(coord_components
<= 3);
4675 for (unsigned i
= 0; i
< 3 - coord_components
; i
++)
4676 bld
.MOV(offset(msg_end
, bld
, i
), brw_imm_f(0.0f
));
4678 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
4681 if (op
== SHADER_OPCODE_TXD
) {
4682 /* TXD unsupported in SIMD16 mode. */
4683 assert(bld
.dispatch_width() == 8);
4685 /* the slots for u and v are always present, but r is optional */
4686 if (coord_components
< 2)
4687 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
4690 * dPdx = dudx, dvdx, drdx
4691 * dPdy = dudy, dvdy, drdy
4693 * 1-arg: Does not exist.
4695 * 2-arg: dudx dvdx dudy dvdy
4696 * dPdx.x dPdx.y dPdy.x dPdy.y
4699 * 3-arg: dudx dvdx drdx dudy dvdy drdy
4700 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
4701 * m5 m6 m7 m8 m9 m10
4703 for (unsigned i
= 0; i
< grad_components
; i
++)
4704 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
4706 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
4708 for (unsigned i
= 0; i
< grad_components
; i
++)
4709 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
4711 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
4715 /* Bias/LOD with shadow comparator is unsupported in SIMD16 -- *Without*
4716 * shadow comparator (including RESINFO) it's unsupported in SIMD8 mode.
4718 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
4719 bld
.dispatch_width() == 16);
4721 const brw_reg_type type
=
4722 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
4723 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
4724 bld
.MOV(retype(msg_end
, type
), lod
);
4725 msg_end
= offset(msg_end
, bld
, 1);
4728 if (shadow_c
.file
!= BAD_FILE
) {
4729 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
4730 /* There's no plain shadow compare message, so we use shadow
4731 * compare with a bias of 0.0.
4733 bld
.MOV(msg_end
, brw_imm_f(0.0f
));
4734 msg_end
= offset(msg_end
, bld
, 1);
4737 bld
.MOV(msg_end
, shadow_c
);
4738 msg_end
= offset(msg_end
, bld
, 1);
4742 inst
->src
[0] = reg_undef
;
4743 inst
->src
[1] = surface
;
4744 inst
->src
[2] = sampler
;
4745 inst
->resize_sources(3);
4746 inst
->base_mrf
= msg_begin
.nr
;
4747 inst
->mlen
= msg_end
.nr
- msg_begin
.nr
;
4748 inst
->header_size
= 1;
4752 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4753 const fs_reg
&coordinate
,
4754 const fs_reg
&shadow_c
,
4755 const fs_reg
&lod
, const fs_reg
&lod2
,
4756 const fs_reg
&sample_index
,
4757 const fs_reg
&surface
,
4758 const fs_reg
&sampler
,
4759 unsigned coord_components
,
4760 unsigned grad_components
)
4762 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
4763 fs_reg msg_coords
= message
;
4764 unsigned header_size
= 0;
4766 if (inst
->offset
!= 0) {
4767 /* The offsets set up by the visitor are in the m1 header, so we can't
4774 for (unsigned i
= 0; i
< coord_components
; i
++)
4775 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
),
4776 offset(coordinate
, bld
, i
));
4778 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
4779 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
4781 if (shadow_c
.file
!= BAD_FILE
) {
4782 fs_reg msg_shadow
= msg_lod
;
4783 bld
.MOV(msg_shadow
, shadow_c
);
4784 msg_lod
= offset(msg_shadow
, bld
, 1);
4789 case SHADER_OPCODE_TXL
:
4791 bld
.MOV(msg_lod
, lod
);
4792 msg_end
= offset(msg_lod
, bld
, 1);
4794 case SHADER_OPCODE_TXD
:
4797 * dPdx = dudx, dvdx, drdx
4798 * dPdy = dudy, dvdy, drdy
4800 * Load up these values:
4801 * - dudx dudy dvdx dvdy drdx drdy
4802 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
4805 for (unsigned i
= 0; i
< grad_components
; i
++) {
4806 bld
.MOV(msg_end
, offset(lod
, bld
, i
));
4807 msg_end
= offset(msg_end
, bld
, 1);
4809 bld
.MOV(msg_end
, offset(lod2
, bld
, i
));
4810 msg_end
= offset(msg_end
, bld
, 1);
4813 case SHADER_OPCODE_TXS
:
4814 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
4815 bld
.MOV(msg_lod
, lod
);
4816 msg_end
= offset(msg_lod
, bld
, 1);
4818 case SHADER_OPCODE_TXF
:
4819 msg_lod
= offset(msg_coords
, bld
, 3);
4820 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
4821 msg_end
= offset(msg_lod
, bld
, 1);
4823 case SHADER_OPCODE_TXF_CMS
:
4824 msg_lod
= offset(msg_coords
, bld
, 3);
4826 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), brw_imm_ud(0u));
4828 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
4829 msg_end
= offset(msg_lod
, bld
, 2);
4836 inst
->src
[0] = reg_undef
;
4837 inst
->src
[1] = surface
;
4838 inst
->src
[2] = sampler
;
4839 inst
->resize_sources(3);
4840 inst
->base_mrf
= message
.nr
;
4841 inst
->mlen
= msg_end
.nr
- message
.nr
;
4842 inst
->header_size
= header_size
;
4844 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4845 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
4849 is_high_sampler(const struct gen_device_info
*devinfo
, const fs_reg
&sampler
)
4851 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
4854 return sampler
.file
!= IMM
|| sampler
.ud
>= 16;
4858 sampler_msg_type(const gen_device_info
*devinfo
,
4859 opcode opcode
, bool shadow_compare
)
4861 assert(devinfo
->gen
>= 5);
4863 case SHADER_OPCODE_TEX
:
4864 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
:
4865 GEN5_SAMPLER_MESSAGE_SAMPLE
;
4867 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
:
4868 GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
4869 case SHADER_OPCODE_TXL
:
4870 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
:
4871 GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
4872 case SHADER_OPCODE_TXL_LZ
:
4873 return shadow_compare
? GEN9_SAMPLER_MESSAGE_SAMPLE_C_LZ
:
4874 GEN9_SAMPLER_MESSAGE_SAMPLE_LZ
;
4875 case SHADER_OPCODE_TXS
:
4876 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4877 return GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
4878 case SHADER_OPCODE_TXD
:
4879 assert(!shadow_compare
|| devinfo
->gen
>= 8 || devinfo
->is_haswell
);
4880 return shadow_compare
? HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE
:
4881 GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
4882 case SHADER_OPCODE_TXF
:
4883 return GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
4884 case SHADER_OPCODE_TXF_LZ
:
4885 assert(devinfo
->gen
>= 9);
4886 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD_LZ
;
4887 case SHADER_OPCODE_TXF_CMS_W
:
4888 assert(devinfo
->gen
>= 9);
4889 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD2DMS_W
;
4890 case SHADER_OPCODE_TXF_CMS
:
4891 return devinfo
->gen
>= 7 ? GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
:
4892 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
4893 case SHADER_OPCODE_TXF_UMS
:
4894 assert(devinfo
->gen
>= 7);
4895 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
;
4896 case SHADER_OPCODE_TXF_MCS
:
4897 assert(devinfo
->gen
>= 7);
4898 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
;
4899 case SHADER_OPCODE_LOD
:
4900 return GEN5_SAMPLER_MESSAGE_LOD
;
4901 case SHADER_OPCODE_TG4
:
4902 assert(devinfo
->gen
>= 7);
4903 return shadow_compare
? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C
:
4904 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4
;
4906 case SHADER_OPCODE_TG4_OFFSET
:
4907 assert(devinfo
->gen
>= 7);
4908 return shadow_compare
? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C
:
4909 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO
;
4910 case SHADER_OPCODE_SAMPLEINFO
:
4911 return GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO
;
4913 unreachable("not reached");
4918 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4919 const fs_reg
&coordinate
,
4920 const fs_reg
&shadow_c
,
4921 fs_reg lod
, const fs_reg
&lod2
,
4922 const fs_reg
&min_lod
,
4923 const fs_reg
&sample_index
,
4925 const fs_reg
&surface
,
4926 const fs_reg
&sampler
,
4927 const fs_reg
&surface_handle
,
4928 const fs_reg
&sampler_handle
,
4929 const fs_reg
&tg4_offset
,
4930 unsigned coord_components
,
4931 unsigned grad_components
)
4933 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4934 const brw_stage_prog_data
*prog_data
= bld
.shader
->stage_prog_data
;
4935 unsigned reg_width
= bld
.dispatch_width() / 8;
4936 unsigned header_size
= 0, length
= 0;
4937 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
4938 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
4939 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
4941 /* We must have exactly one of surface/sampler and surface/sampler_handle */
4942 assert((surface
.file
== BAD_FILE
) != (surface_handle
.file
== BAD_FILE
));
4943 assert((sampler
.file
== BAD_FILE
) != (sampler_handle
.file
== BAD_FILE
));
4945 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
4946 inst
->offset
!= 0 || inst
->eot
||
4947 op
== SHADER_OPCODE_SAMPLEINFO
||
4948 sampler_handle
.file
!= BAD_FILE
||
4949 is_high_sampler(devinfo
, sampler
)) {
4950 /* For general texture offsets (no txf workaround), we need a header to
4953 * TG4 needs to place its channel select in the header, for interaction
4954 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
4955 * larger sampler numbers we need to offset the Sampler State Pointer in
4958 fs_reg header
= retype(sources
[0], BRW_REGISTER_TYPE_UD
);
4962 /* If we're requesting fewer than four channels worth of response,
4963 * and we have an explicit header, we need to set up the sampler
4964 * writemask. It's reversed from normal: 1 means "don't write".
4966 if (!inst
->eot
&& regs_written(inst
) != 4 * reg_width
) {
4967 assert(regs_written(inst
) % reg_width
== 0);
4968 unsigned mask
= ~((1 << (regs_written(inst
) / reg_width
)) - 1) & 0xf;
4969 inst
->offset
|= mask
<< 12;
4972 /* Build the actual header */
4973 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4974 const fs_builder ubld1
= ubld
.group(1, 0);
4975 ubld
.MOV(header
, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
4977 ubld1
.MOV(component(header
, 2), brw_imm_ud(inst
->offset
));
4978 } else if (bld
.shader
->stage
!= MESA_SHADER_VERTEX
&&
4979 bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
) {
4980 /* The vertex and fragment stages have g0.2 set to 0, so
4981 * header0.2 is 0 when g0 is copied. Other stages may not, so we
4982 * must set it to 0 to avoid setting undesirable bits in the
4985 ubld1
.MOV(component(header
, 2), brw_imm_ud(0));
4988 if (sampler_handle
.file
!= BAD_FILE
) {
4989 /* Bindless sampler handles aren't relative to the sampler state
4990 * pointer passed into the shader through SAMPLER_STATE_POINTERS_*.
4991 * Instead, it's an absolute pointer relative to dynamic state base
4994 * Sampler states are 16 bytes each and the pointer we give here has
4995 * to be 32-byte aligned. In order to avoid more indirect messages
4996 * than required, we assume that all bindless sampler states are
4997 * 32-byte aligned. This sacrifices a bit of general state base
4998 * address space but means we can do something more efficient in the
5001 ubld1
.MOV(component(header
, 3), sampler_handle
);
5002 } else if (is_high_sampler(devinfo
, sampler
)) {
5003 if (sampler
.file
== BRW_IMMEDIATE_VALUE
) {
5004 assert(sampler
.ud
>= 16);
5005 const int sampler_state_size
= 16; /* 16 bytes */
5007 ubld1
.ADD(component(header
, 3),
5008 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD
),
5009 brw_imm_ud(16 * (sampler
.ud
/ 16) * sampler_state_size
));
5011 fs_reg tmp
= ubld1
.vgrf(BRW_REGISTER_TYPE_UD
);
5012 ubld1
.AND(tmp
, sampler
, brw_imm_ud(0x0f0));
5013 ubld1
.SHL(tmp
, tmp
, brw_imm_ud(4));
5014 ubld1
.ADD(component(header
, 3),
5015 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD
),
5021 if (shadow_c
.file
!= BAD_FILE
) {
5022 bld
.MOV(sources
[length
], shadow_c
);
5026 bool coordinate_done
= false;
5028 /* Set up the LOD info */
5031 case SHADER_OPCODE_TXL
:
5032 if (devinfo
->gen
>= 9 && op
== SHADER_OPCODE_TXL
&& lod
.is_zero()) {
5033 op
= SHADER_OPCODE_TXL_LZ
;
5036 bld
.MOV(sources
[length
], lod
);
5039 case SHADER_OPCODE_TXD
:
5040 /* TXD should have been lowered in SIMD16 mode. */
5041 assert(bld
.dispatch_width() == 8);
5043 /* Load dPdx and the coordinate together:
5044 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
5046 for (unsigned i
= 0; i
< coord_components
; i
++) {
5047 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
5049 /* For cube map array, the coordinate is (u,v,r,ai) but there are
5050 * only derivatives for (u, v, r).
5052 if (i
< grad_components
) {
5053 bld
.MOV(sources
[length
++], offset(lod
, bld
, i
));
5054 bld
.MOV(sources
[length
++], offset(lod2
, bld
, i
));
5058 coordinate_done
= true;
5060 case SHADER_OPCODE_TXS
:
5061 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
5064 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
5065 /* We need an LOD; just use 0 */
5066 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), brw_imm_ud(0));
5069 case SHADER_OPCODE_TXF
:
5070 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
5071 * On Gen9 they are u, v, lod, r
5073 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
), coordinate
);
5075 if (devinfo
->gen
>= 9) {
5076 if (coord_components
>= 2) {
5077 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
),
5078 offset(coordinate
, bld
, 1));
5080 sources
[length
] = brw_imm_d(0);
5085 if (devinfo
->gen
>= 9 && lod
.is_zero()) {
5086 op
= SHADER_OPCODE_TXF_LZ
;
5088 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
5092 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++)
5093 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
5094 offset(coordinate
, bld
, i
));
5096 coordinate_done
= true;
5099 case SHADER_OPCODE_TXF_CMS
:
5100 case SHADER_OPCODE_TXF_CMS_W
:
5101 case SHADER_OPCODE_TXF_UMS
:
5102 case SHADER_OPCODE_TXF_MCS
:
5103 if (op
== SHADER_OPCODE_TXF_UMS
||
5104 op
== SHADER_OPCODE_TXF_CMS
||
5105 op
== SHADER_OPCODE_TXF_CMS_W
) {
5106 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
5110 if (op
== SHADER_OPCODE_TXF_CMS
|| op
== SHADER_OPCODE_TXF_CMS_W
) {
5111 /* Data from the multisample control surface. */
5112 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
5115 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
5118 if (op
== SHADER_OPCODE_TXF_CMS_W
) {
5119 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
),
5122 offset(mcs
, bld
, 1));
5127 /* There is no offsetting for this message; just copy in the integer
5128 * texture coordinates.
5130 for (unsigned i
= 0; i
< coord_components
; i
++)
5131 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
5132 offset(coordinate
, bld
, i
));
5134 coordinate_done
= true;
5136 case SHADER_OPCODE_TG4_OFFSET
:
5137 /* More crazy intermixing */
5138 for (unsigned i
= 0; i
< 2; i
++) /* u, v */
5139 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
5141 for (unsigned i
= 0; i
< 2; i
++) /* offu, offv */
5142 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
5143 offset(tg4_offset
, bld
, i
));
5145 if (coord_components
== 3) /* r if present */
5146 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, 2));
5148 coordinate_done
= true;
5154 /* Set up the coordinate (except for cases where it was done above) */
5155 if (!coordinate_done
) {
5156 for (unsigned i
= 0; i
< coord_components
; i
++)
5157 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
5160 if (min_lod
.file
!= BAD_FILE
) {
5161 /* Account for all of the missing coordinate sources */
5162 length
+= 4 - coord_components
;
5163 if (op
== SHADER_OPCODE_TXD
)
5164 length
+= (3 - grad_components
) * 2;
5166 bld
.MOV(sources
[length
++], min_lod
);
5171 mlen
= length
* reg_width
- header_size
;
5173 mlen
= length
* reg_width
;
5175 const fs_reg src_payload
= fs_reg(VGRF
, bld
.shader
->alloc
.allocate(mlen
),
5176 BRW_REGISTER_TYPE_F
);
5177 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
5179 /* Generate the SEND. */
5180 inst
->opcode
= SHADER_OPCODE_SEND
;
5182 inst
->header_size
= header_size
;
5184 const unsigned msg_type
=
5185 sampler_msg_type(devinfo
, op
, inst
->shadow_compare
);
5186 const unsigned simd_mode
=
5187 inst
->exec_size
<= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8
:
5188 BRW_SAMPLER_SIMD_MODE_SIMD16
;
5190 uint32_t base_binding_table_index
;
5192 case SHADER_OPCODE_TG4
:
5193 case SHADER_OPCODE_TG4_OFFSET
:
5194 base_binding_table_index
= prog_data
->binding_table
.gather_texture_start
;
5196 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
5197 base_binding_table_index
= prog_data
->binding_table
.image_start
;
5200 base_binding_table_index
= prog_data
->binding_table
.texture_start
;
5204 inst
->sfid
= BRW_SFID_SAMPLER
;
5205 if (surface
.file
== IMM
&&
5206 (sampler
.file
== IMM
|| sampler_handle
.file
!= BAD_FILE
)) {
5207 inst
->desc
= brw_sampler_desc(devinfo
,
5208 surface
.ud
+ base_binding_table_index
,
5209 sampler
.file
== IMM
? sampler
.ud
% 16 : 0,
5212 0 /* return_format unused on gen7+ */);
5213 inst
->src
[0] = brw_imm_ud(0);
5214 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5215 } else if (surface_handle
.file
!= BAD_FILE
) {
5216 /* Bindless surface */
5217 assert(devinfo
->gen
>= 9);
5218 inst
->desc
= brw_sampler_desc(devinfo
,
5220 sampler
.file
== IMM
? sampler
.ud
% 16 : 0,
5223 0 /* return_format unused on gen7+ */);
5225 /* For bindless samplers, the entire address is included in the message
5226 * header so we can leave the portion in the message descriptor 0.
5228 if (sampler_handle
.file
!= BAD_FILE
|| sampler
.file
== IMM
) {
5229 inst
->src
[0] = brw_imm_ud(0);
5231 const fs_builder ubld
= bld
.group(1, 0).exec_all();
5232 fs_reg desc
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5233 ubld
.SHL(desc
, sampler
, brw_imm_ud(8));
5234 inst
->src
[0] = desc
;
5237 /* We assume that the driver provided the handle in the top 20 bits so
5238 * we can use the surface handle directly as the extended descriptor.
5240 inst
->src
[1] = retype(surface_handle
, BRW_REGISTER_TYPE_UD
);
5242 /* Immediate portion of the descriptor */
5243 inst
->desc
= brw_sampler_desc(devinfo
,
5248 0 /* return_format unused on gen7+ */);
5249 const fs_builder ubld
= bld
.group(1, 0).exec_all();
5250 fs_reg desc
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5251 if (surface
.equals(sampler
)) {
5252 /* This case is common in GL */
5253 ubld
.MUL(desc
, surface
, brw_imm_ud(0x101));
5255 if (sampler_handle
.file
!= BAD_FILE
) {
5256 ubld
.MOV(desc
, surface
);
5257 } else if (sampler
.file
== IMM
) {
5258 ubld
.OR(desc
, surface
, brw_imm_ud(sampler
.ud
<< 8));
5260 ubld
.SHL(desc
, sampler
, brw_imm_ud(8));
5261 ubld
.OR(desc
, desc
, surface
);
5264 if (base_binding_table_index
)
5265 ubld
.ADD(desc
, desc
, brw_imm_ud(base_binding_table_index
));
5266 ubld
.AND(desc
, desc
, brw_imm_ud(0xfff));
5268 inst
->src
[0] = component(desc
, 0);
5269 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5272 inst
->src
[2] = src_payload
;
5273 inst
->resize_sources(3);
5276 /* EOT sampler messages don't make sense to split because it would
5277 * involve ending half of the thread early.
5279 assert(inst
->group
== 0);
5280 /* We need to use SENDC for EOT sampler messages */
5281 inst
->check_tdr
= true;
5282 inst
->send_has_side_effects
= true;
5285 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
5286 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
5290 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
5292 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5293 const fs_reg
&coordinate
= inst
->src
[TEX_LOGICAL_SRC_COORDINATE
];
5294 const fs_reg
&shadow_c
= inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
];
5295 const fs_reg
&lod
= inst
->src
[TEX_LOGICAL_SRC_LOD
];
5296 const fs_reg
&lod2
= inst
->src
[TEX_LOGICAL_SRC_LOD2
];
5297 const fs_reg
&min_lod
= inst
->src
[TEX_LOGICAL_SRC_MIN_LOD
];
5298 const fs_reg
&sample_index
= inst
->src
[TEX_LOGICAL_SRC_SAMPLE_INDEX
];
5299 const fs_reg
&mcs
= inst
->src
[TEX_LOGICAL_SRC_MCS
];
5300 const fs_reg
&surface
= inst
->src
[TEX_LOGICAL_SRC_SURFACE
];
5301 const fs_reg
&sampler
= inst
->src
[TEX_LOGICAL_SRC_SAMPLER
];
5302 const fs_reg
&surface_handle
= inst
->src
[TEX_LOGICAL_SRC_SURFACE_HANDLE
];
5303 const fs_reg
&sampler_handle
= inst
->src
[TEX_LOGICAL_SRC_SAMPLER_HANDLE
];
5304 const fs_reg
&tg4_offset
= inst
->src
[TEX_LOGICAL_SRC_TG4_OFFSET
];
5305 assert(inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
);
5306 const unsigned coord_components
= inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
5307 assert(inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
5308 const unsigned grad_components
= inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
5310 if (devinfo
->gen
>= 7) {
5311 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
5312 shadow_c
, lod
, lod2
, min_lod
,
5314 mcs
, surface
, sampler
,
5315 surface_handle
, sampler_handle
,
5317 coord_components
, grad_components
);
5318 } else if (devinfo
->gen
>= 5) {
5319 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
5320 shadow_c
, lod
, lod2
, sample_index
,
5322 coord_components
, grad_components
);
5324 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
5325 shadow_c
, lod
, lod2
,
5327 coord_components
, grad_components
);
5332 * Predicate the specified instruction on the sample mask.
5335 emit_predicate_on_sample_mask(const fs_builder
&bld
, fs_inst
*inst
)
5337 assert(bld
.shader
->stage
== MESA_SHADER_FRAGMENT
&&
5338 bld
.group() == inst
->group
&&
5339 bld
.dispatch_width() == inst
->exec_size
);
5341 const fs_visitor
*v
= static_cast<const fs_visitor
*>(bld
.shader
);
5342 const fs_reg sample_mask
= sample_mask_reg(bld
);
5343 const unsigned subreg
= sample_mask_flag_subreg(v
);
5345 if (brw_wm_prog_data(v
->stage_prog_data
)->uses_kill
) {
5346 assert(sample_mask
.file
== ARF
&&
5347 sample_mask
.nr
== brw_flag_subreg(subreg
).nr
&&
5348 sample_mask
.subnr
== brw_flag_subreg(
5349 subreg
+ inst
->group
/ 16).subnr
);
5351 bld
.group(1, 0).exec_all()
5352 .MOV(brw_flag_subreg(subreg
+ inst
->group
/ 16), sample_mask
);
5355 if (inst
->predicate
) {
5356 assert(inst
->predicate
== BRW_PREDICATE_NORMAL
);
5357 assert(!inst
->predicate_inverse
);
5358 assert(inst
->flag_subreg
== 0);
5359 /* Combine the sample mask with the existing predicate by using a
5360 * vertical predication mode.
5362 inst
->predicate
= BRW_PREDICATE_ALIGN1_ALLV
;
5364 inst
->flag_subreg
= subreg
;
5365 inst
->predicate
= BRW_PREDICATE_NORMAL
;
5366 inst
->predicate_inverse
= false;
5371 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5373 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5375 /* Get the logical send arguments. */
5376 const fs_reg
&addr
= inst
->src
[SURFACE_LOGICAL_SRC_ADDRESS
];
5377 const fs_reg
&src
= inst
->src
[SURFACE_LOGICAL_SRC_DATA
];
5378 const fs_reg
&surface
= inst
->src
[SURFACE_LOGICAL_SRC_SURFACE
];
5379 const fs_reg
&surface_handle
= inst
->src
[SURFACE_LOGICAL_SRC_SURFACE_HANDLE
];
5380 const UNUSED fs_reg
&dims
= inst
->src
[SURFACE_LOGICAL_SRC_IMM_DIMS
];
5381 const fs_reg
&arg
= inst
->src
[SURFACE_LOGICAL_SRC_IMM_ARG
];
5382 assert(arg
.file
== IMM
);
5384 /* We must have exactly one of surface and surface_handle */
5385 assert((surface
.file
== BAD_FILE
) != (surface_handle
.file
== BAD_FILE
));
5387 /* Calculate the total number of components of the payload. */
5388 const unsigned addr_sz
= inst
->components_read(SURFACE_LOGICAL_SRC_ADDRESS
);
5389 const unsigned src_sz
= inst
->components_read(SURFACE_LOGICAL_SRC_DATA
);
5391 const bool is_typed_access
=
5392 inst
->opcode
== SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
||
5393 inst
->opcode
== SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
||
5394 inst
->opcode
== SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
;
5396 const bool is_surface_access
= is_typed_access
||
5397 inst
->opcode
== SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
||
5398 inst
->opcode
== SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
||
5399 inst
->opcode
== SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
;
5401 const bool is_stateless
=
5402 surface
.file
== IMM
&& (surface
.ud
== BRW_BTI_STATELESS
||
5403 surface
.ud
== GEN8_BTI_STATELESS_NON_COHERENT
);
5405 const bool has_side_effects
= inst
->has_side_effects();
5406 fs_reg sample_mask
= has_side_effects
? sample_mask_reg(bld
) :
5407 fs_reg(brw_imm_d(0xffff));
5409 /* From the BDW PRM Volume 7, page 147:
5411 * "For the Data Cache Data Port*, the header must be present for the
5412 * following message types: [...] Typed read/write/atomics"
5414 * Earlier generations have a similar wording. Because of this restriction
5415 * we don't attempt to implement sample masks via predication for such
5416 * messages prior to Gen9, since we have to provide a header anyway. On
5417 * Gen11+ the header has been removed so we can only use predication.
5419 * For all stateless A32 messages, we also need a header
5422 if ((devinfo
->gen
< 9 && is_typed_access
) || is_stateless
) {
5423 fs_builder ubld
= bld
.exec_all().group(8, 0);
5424 header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5425 ubld
.MOV(header
, brw_imm_d(0));
5427 /* Both the typed and scattered byte/dword A32 messages take a buffer
5428 * base address in R0.5:[31:0] (See MH1_A32_PSM for typed messages or
5429 * MH_A32_GO for byte/dword scattered messages in the SKL PRM Vol. 2d
5430 * for more details.) This is conveniently where the HW places the
5431 * scratch surface base address.
5433 * From the SKL PRM Vol. 7 "Per-Thread Scratch Space":
5435 * "When a thread becomes 'active' it is allocated a portion of
5436 * scratch space, sized according to PerThreadScratchSpace. The
5437 * starting location of each thread’s scratch space allocation,
5438 * ScratchSpaceOffset, is passed in the thread payload in
5439 * R0.5[31:10] and is specified as a 1KB-granular offset from the
5440 * GeneralStateBaseAddress. The computation of ScratchSpaceOffset
5441 * includes the starting address of the stage’s scratch space
5442 * allocation, as programmed by ScratchSpaceBasePointer."
5444 * The base address is passed in bits R0.5[31:10] and the bottom 10
5445 * bits of R0.5 are used for other things. Therefore, we have to
5446 * mask off the bottom 10 bits so that we don't get a garbage base
5449 ubld
.group(1, 0).AND(component(header
, 5),
5450 retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD
),
5451 brw_imm_ud(0xfffffc00));
5453 if (is_surface_access
)
5454 ubld
.group(1, 0).MOV(component(header
, 7), sample_mask
);
5456 const unsigned header_sz
= header
.file
!= BAD_FILE
? 1 : 0;
5458 fs_reg payload
, payload2
;
5459 unsigned mlen
, ex_mlen
= 0;
5460 if (devinfo
->gen
>= 9 &&
5461 (src
.file
== BAD_FILE
|| header
.file
== BAD_FILE
)) {
5462 /* We have split sends on gen9 and above */
5463 if (header
.file
== BAD_FILE
) {
5464 payload
= bld
.move_to_vgrf(addr
, addr_sz
);
5465 payload2
= bld
.move_to_vgrf(src
, src_sz
);
5466 mlen
= addr_sz
* (inst
->exec_size
/ 8);
5467 ex_mlen
= src_sz
* (inst
->exec_size
/ 8);
5469 assert(src
.file
== BAD_FILE
);
5471 payload2
= bld
.move_to_vgrf(addr
, addr_sz
);
5473 ex_mlen
= addr_sz
* (inst
->exec_size
/ 8);
5476 /* Allocate space for the payload. */
5477 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
5478 payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
5479 fs_reg
*const components
= new fs_reg
[sz
];
5482 /* Construct the payload. */
5483 if (header
.file
!= BAD_FILE
)
5484 components
[n
++] = header
;
5486 for (unsigned i
= 0; i
< addr_sz
; i
++)
5487 components
[n
++] = offset(addr
, bld
, i
);
5489 for (unsigned i
= 0; i
< src_sz
; i
++)
5490 components
[n
++] = offset(src
, bld
, i
);
5492 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
5493 mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
5495 delete[] components
;
5498 /* Predicate the instruction on the sample mask if no header is
5501 if ((header
.file
== BAD_FILE
|| !is_surface_access
) &&
5502 sample_mask
.file
!= BAD_FILE
&& sample_mask
.file
!= IMM
)
5503 emit_predicate_on_sample_mask(bld
, inst
);
5506 switch (inst
->opcode
) {
5507 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5508 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5509 /* Byte scattered opcodes go through the normal data cache */
5510 sfid
= GEN7_SFID_DATAPORT_DATA_CACHE
;
5513 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL
:
5514 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL
:
5515 sfid
= devinfo
->gen
>= 7 ? GEN7_SFID_DATAPORT_DATA_CACHE
:
5516 devinfo
->gen
>= 6 ? GEN6_SFID_DATAPORT_RENDER_CACHE
:
5517 BRW_DATAPORT_READ_TARGET_RENDER_CACHE
;
5520 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5521 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5522 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5523 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5524 /* Untyped Surface messages go through the data cache but the SFID value
5525 * changed on Haswell.
5527 sfid
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
?
5528 HSW_SFID_DATAPORT_DATA_CACHE_1
:
5529 GEN7_SFID_DATAPORT_DATA_CACHE
);
5532 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5533 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5534 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5535 /* Typed surface messages go through the render cache on IVB and the
5536 * data cache on HSW+.
5538 sfid
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
?
5539 HSW_SFID_DATAPORT_DATA_CACHE_1
:
5540 GEN6_SFID_DATAPORT_RENDER_CACHE
);
5544 unreachable("Unsupported surface opcode");
5548 switch (inst
->opcode
) {
5549 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5550 desc
= brw_dp_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5551 arg
.ud
, /* num_channels */
5555 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5556 desc
= brw_dp_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5557 arg
.ud
, /* num_channels */
5561 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5562 desc
= brw_dp_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5563 arg
.ud
, /* bit_size */
5567 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5568 desc
= brw_dp_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5569 arg
.ud
, /* bit_size */
5573 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL
:
5574 assert(arg
.ud
== 32); /* bit_size */
5575 desc
= brw_dp_dword_scattered_rw_desc(devinfo
, inst
->exec_size
,
5579 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL
:
5580 assert(arg
.ud
== 32); /* bit_size */
5581 desc
= brw_dp_dword_scattered_rw_desc(devinfo
, inst
->exec_size
,
5585 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5586 desc
= brw_dp_untyped_atomic_desc(devinfo
, inst
->exec_size
,
5587 arg
.ud
, /* atomic_op */
5588 !inst
->dst
.is_null());
5591 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5592 desc
= brw_dp_untyped_atomic_float_desc(devinfo
, inst
->exec_size
,
5593 arg
.ud
, /* atomic_op */
5594 !inst
->dst
.is_null());
5597 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5598 desc
= brw_dp_typed_surface_rw_desc(devinfo
, inst
->exec_size
, inst
->group
,
5599 arg
.ud
, /* num_channels */
5603 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5604 desc
= brw_dp_typed_surface_rw_desc(devinfo
, inst
->exec_size
, inst
->group
,
5605 arg
.ud
, /* num_channels */
5609 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5610 desc
= brw_dp_typed_atomic_desc(devinfo
, inst
->exec_size
, inst
->group
,
5611 arg
.ud
, /* atomic_op */
5612 !inst
->dst
.is_null());
5616 unreachable("Unknown surface logical instruction");
5619 /* Update the original instruction. */
5620 inst
->opcode
= SHADER_OPCODE_SEND
;
5622 inst
->ex_mlen
= ex_mlen
;
5623 inst
->header_size
= header_sz
;
5624 inst
->send_has_side_effects
= has_side_effects
;
5625 inst
->send_is_volatile
= !has_side_effects
;
5627 /* Set up SFID and descriptors */
5630 if (surface
.file
== IMM
) {
5631 inst
->desc
|= surface
.ud
& 0xff;
5632 inst
->src
[0] = brw_imm_ud(0);
5633 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5634 } else if (surface_handle
.file
!= BAD_FILE
) {
5635 /* Bindless surface */
5636 assert(devinfo
->gen
>= 9);
5637 inst
->desc
|= GEN9_BTI_BINDLESS
;
5638 inst
->src
[0] = brw_imm_ud(0);
5640 /* We assume that the driver provided the handle in the top 20 bits so
5641 * we can use the surface handle directly as the extended descriptor.
5643 inst
->src
[1] = retype(surface_handle
, BRW_REGISTER_TYPE_UD
);
5645 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5646 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5647 ubld
.AND(tmp
, surface
, brw_imm_ud(0xff));
5648 inst
->src
[0] = component(tmp
, 0);
5649 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5652 /* Finally, the payload */
5653 inst
->src
[2] = payload
;
5654 inst
->src
[3] = payload2
;
5656 inst
->resize_sources(4);
5660 lower_a64_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5662 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5664 const fs_reg
&addr
= inst
->src
[0];
5665 const fs_reg
&src
= inst
->src
[1];
5666 const unsigned src_comps
= inst
->components_read(1);
5667 assert(inst
->src
[2].file
== IMM
);
5668 const unsigned arg
= inst
->src
[2].ud
;
5669 const bool has_side_effects
= inst
->has_side_effects();
5671 /* If the surface message has side effects and we're a fragment shader, we
5672 * have to predicate with the sample mask to avoid helper invocations.
5674 if (has_side_effects
&& bld
.shader
->stage
== MESA_SHADER_FRAGMENT
)
5675 emit_predicate_on_sample_mask(bld
, inst
);
5677 fs_reg payload
, payload2
;
5678 unsigned mlen
, ex_mlen
= 0;
5679 if (devinfo
->gen
>= 9) {
5680 /* On Skylake and above, we have SENDS */
5681 mlen
= 2 * (inst
->exec_size
/ 8);
5682 ex_mlen
= src_comps
* type_sz(src
.type
) * inst
->exec_size
/ REG_SIZE
;
5683 payload
= retype(bld
.move_to_vgrf(addr
, 1), BRW_REGISTER_TYPE_UD
);
5684 payload2
= retype(bld
.move_to_vgrf(src
, src_comps
),
5685 BRW_REGISTER_TYPE_UD
);
5687 /* Add two because the address is 64-bit */
5688 const unsigned dwords
= 2 + src_comps
;
5689 mlen
= dwords
* (inst
->exec_size
/ 8);
5695 for (unsigned i
= 0; i
< src_comps
; i
++)
5696 sources
[1 + i
] = offset(src
, bld
, i
);
5698 payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, dwords
);
5699 bld
.LOAD_PAYLOAD(payload
, sources
, 1 + src_comps
, 0);
5703 switch (inst
->opcode
) {
5704 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
5705 desc
= brw_dp_a64_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5706 arg
, /* num_channels */
5710 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
5711 desc
= brw_dp_a64_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5712 arg
, /* num_channels */
5716 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
5717 desc
= brw_dp_a64_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5722 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
5723 desc
= brw_dp_a64_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5728 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
5729 desc
= brw_dp_a64_untyped_atomic_desc(devinfo
, inst
->exec_size
, 32,
5730 arg
, /* atomic_op */
5731 !inst
->dst
.is_null());
5734 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
5735 desc
= brw_dp_a64_untyped_atomic_desc(devinfo
, inst
->exec_size
, 64,
5736 arg
, /* atomic_op */
5737 !inst
->dst
.is_null());
5741 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5742 desc
= brw_dp_a64_untyped_atomic_float_desc(devinfo
, inst
->exec_size
,
5743 arg
, /* atomic_op */
5744 !inst
->dst
.is_null());
5748 unreachable("Unknown A64 logical instruction");
5751 /* Update the original instruction. */
5752 inst
->opcode
= SHADER_OPCODE_SEND
;
5754 inst
->ex_mlen
= ex_mlen
;
5755 inst
->header_size
= 0;
5756 inst
->send_has_side_effects
= has_side_effects
;
5757 inst
->send_is_volatile
= !has_side_effects
;
5759 /* Set up SFID and descriptors */
5760 inst
->sfid
= HSW_SFID_DATAPORT_DATA_CACHE_1
;
5762 inst
->resize_sources(4);
5763 inst
->src
[0] = brw_imm_ud(0); /* desc */
5764 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5765 inst
->src
[2] = payload
;
5766 inst
->src
[3] = payload2
;
5770 lower_varying_pull_constant_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5772 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5774 if (devinfo
->gen
>= 7) {
5775 fs_reg index
= inst
->src
[0];
5776 /* We are switching the instruction from an ALU-like instruction to a
5777 * send-from-grf instruction. Since sends can't handle strides or
5778 * source modifiers, we have to make a copy of the offset source.
5780 fs_reg offset
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
5781 bld
.MOV(offset
, inst
->src
[1]);
5783 const unsigned simd_mode
=
5784 inst
->exec_size
<= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8
:
5785 BRW_SAMPLER_SIMD_MODE_SIMD16
;
5787 inst
->opcode
= SHADER_OPCODE_SEND
;
5788 inst
->mlen
= inst
->exec_size
/ 8;
5789 inst
->resize_sources(3);
5791 inst
->sfid
= BRW_SFID_SAMPLER
;
5792 inst
->desc
= brw_sampler_desc(devinfo
, 0, 0,
5793 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
5795 if (index
.file
== IMM
) {
5796 inst
->desc
|= index
.ud
& 0xff;
5797 inst
->src
[0] = brw_imm_ud(0);
5799 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5800 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5801 ubld
.AND(tmp
, index
, brw_imm_ud(0xff));
5802 inst
->src
[0] = component(tmp
, 0);
5804 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5805 inst
->src
[2] = offset
; /* payload */
5807 const fs_reg
payload(MRF
, FIRST_PULL_LOAD_MRF(devinfo
->gen
),
5808 BRW_REGISTER_TYPE_UD
);
5810 bld
.MOV(byte_offset(payload
, REG_SIZE
), inst
->src
[1]);
5812 inst
->opcode
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
;
5813 inst
->resize_sources(1);
5814 inst
->base_mrf
= payload
.nr
;
5815 inst
->header_size
= 1;
5816 inst
->mlen
= 1 + inst
->exec_size
/ 8;
5821 lower_math_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5823 assert(bld
.shader
->devinfo
->gen
< 6);
5826 inst
->mlen
= inst
->sources
* inst
->exec_size
/ 8;
5828 if (inst
->sources
> 1) {
5829 /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
5830 * "Message Payload":
5832 * "Operand0[7]. For the INT DIV functions, this operand is the
5835 * "Operand1[7]. For the INT DIV functions, this operand is the
5838 const bool is_int_div
= inst
->opcode
!= SHADER_OPCODE_POW
;
5839 const fs_reg src0
= is_int_div
? inst
->src
[1] : inst
->src
[0];
5840 const fs_reg src1
= is_int_div
? inst
->src
[0] : inst
->src
[1];
5842 inst
->resize_sources(1);
5843 inst
->src
[0] = src0
;
5845 assert(inst
->exec_size
== 8);
5846 bld
.MOV(fs_reg(MRF
, inst
->base_mrf
+ 1, src1
.type
), src1
);
5851 fs_visitor::lower_logical_sends()
5853 bool progress
= false;
5855 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
5856 const fs_builder
ibld(this, block
, inst
);
5858 switch (inst
->opcode
) {
5859 case FS_OPCODE_FB_WRITE_LOGICAL
:
5860 assert(stage
== MESA_SHADER_FRAGMENT
);
5861 lower_fb_write_logical_send(ibld
, inst
,
5862 brw_wm_prog_data(prog_data
),
5863 (const brw_wm_prog_key
*)key
,
5867 case FS_OPCODE_FB_READ_LOGICAL
:
5868 lower_fb_read_logical_send(ibld
, inst
);
5871 case SHADER_OPCODE_TEX_LOGICAL
:
5872 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
5875 case SHADER_OPCODE_TXD_LOGICAL
:
5876 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
5879 case SHADER_OPCODE_TXF_LOGICAL
:
5880 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
5883 case SHADER_OPCODE_TXL_LOGICAL
:
5884 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
5887 case SHADER_OPCODE_TXS_LOGICAL
:
5888 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
5891 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
5892 lower_sampler_logical_send(ibld
, inst
,
5893 SHADER_OPCODE_IMAGE_SIZE_LOGICAL
);
5896 case FS_OPCODE_TXB_LOGICAL
:
5897 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
5900 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
5901 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
5904 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
5905 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS_W
);
5908 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
5909 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
5912 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
5913 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
5916 case SHADER_OPCODE_LOD_LOGICAL
:
5917 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
5920 case SHADER_OPCODE_TG4_LOGICAL
:
5921 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
5924 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
5925 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
5928 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
5929 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_SAMPLEINFO
);
5932 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5933 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5934 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5935 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5936 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL
:
5937 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL
:
5938 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5939 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5940 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5941 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5942 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5943 lower_surface_logical_send(ibld
, inst
);
5946 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
5947 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
5948 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
5949 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
5950 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
5951 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
5952 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5953 lower_a64_logical_send(ibld
, inst
);
5956 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
:
5957 lower_varying_pull_constant_logical_send(ibld
, inst
);
5960 case SHADER_OPCODE_RCP
:
5961 case SHADER_OPCODE_RSQ
:
5962 case SHADER_OPCODE_SQRT
:
5963 case SHADER_OPCODE_EXP2
:
5964 case SHADER_OPCODE_LOG2
:
5965 case SHADER_OPCODE_SIN
:
5966 case SHADER_OPCODE_COS
:
5967 case SHADER_OPCODE_POW
:
5968 case SHADER_OPCODE_INT_QUOTIENT
:
5969 case SHADER_OPCODE_INT_REMAINDER
:
5970 /* The math opcodes are overloaded for the send-like and
5971 * expression-like instructions which seems kind of icky. Gen6+ has
5972 * a native (but rather quirky) MATH instruction so we don't need to
5973 * do anything here. On Gen4-5 we'll have to lower the Gen6-like
5974 * logical instructions (which we can easily recognize because they
5975 * have mlen = 0) into send-like virtual instructions.
5977 if (devinfo
->gen
< 6 && inst
->mlen
== 0) {
5978 lower_math_logical_send(ibld
, inst
);
5993 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
5999 is_mixed_float_with_fp32_dst(const fs_inst
*inst
)
6001 /* This opcode sometimes uses :W type on the source even if the operand is
6002 * a :HF, because in gen7 there is no support for :HF, and thus it uses :W.
6004 if (inst
->opcode
== BRW_OPCODE_F16TO32
)
6007 if (inst
->dst
.type
!= BRW_REGISTER_TYPE_F
)
6010 for (int i
= 0; i
< inst
->sources
; i
++) {
6011 if (inst
->src
[i
].type
== BRW_REGISTER_TYPE_HF
)
6019 is_mixed_float_with_packed_fp16_dst(const fs_inst
*inst
)
6021 /* This opcode sometimes uses :W type on the destination even if the
6022 * destination is a :HF, because in gen7 there is no support for :HF, and
6025 if (inst
->opcode
== BRW_OPCODE_F32TO16
&&
6026 inst
->dst
.stride
== 1)
6029 if (inst
->dst
.type
!= BRW_REGISTER_TYPE_HF
||
6030 inst
->dst
.stride
!= 1)
6033 for (int i
= 0; i
< inst
->sources
; i
++) {
6034 if (inst
->src
[i
].type
== BRW_REGISTER_TYPE_F
)
6042 * Get the closest allowed SIMD width for instruction \p inst accounting for
6043 * some common regioning and execution control restrictions that apply to FPU
6044 * instructions. These restrictions don't necessarily have any relevance to
6045 * instructions not executed by the FPU pipeline like extended math, control
6046 * flow or send message instructions.
6048 * For virtual opcodes it's really up to the instruction -- In some cases
6049 * (e.g. where a virtual instruction unrolls into a simple sequence of FPU
6050 * instructions) it may simplify virtual instruction lowering if we can
6051 * enforce FPU-like regioning restrictions already on the virtual instruction,
6052 * in other cases (e.g. virtual send-like instructions) this may be
6053 * excessively restrictive.
6056 get_fpu_lowered_simd_width(const struct gen_device_info
*devinfo
,
6057 const fs_inst
*inst
)
6059 /* Maximum execution size representable in the instruction controls. */
6060 unsigned max_width
= MIN2(32, inst
->exec_size
);
6062 /* According to the PRMs:
6063 * "A. In Direct Addressing mode, a source cannot span more than 2
6064 * adjacent GRF registers.
6065 * B. A destination cannot span more than 2 adjacent GRF registers."
6067 * Look for the source or destination with the largest register region
6068 * which is the one that is going to limit the overall execution size of
6069 * the instruction due to this rule.
6071 unsigned reg_count
= DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
6073 for (unsigned i
= 0; i
< inst
->sources
; i
++)
6074 reg_count
= MAX2(reg_count
, DIV_ROUND_UP(inst
->size_read(i
), REG_SIZE
));
6076 /* Calculate the maximum execution size of the instruction based on the
6077 * factor by which it goes over the hardware limit of 2 GRFs.
6080 max_width
= MIN2(max_width
, inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2));
6082 /* According to the IVB PRMs:
6083 * "When destination spans two registers, the source MUST span two
6084 * registers. The exception to the above rule:
6086 * - When source is scalar, the source registers are not incremented.
6087 * - When source is packed integer Word and destination is packed
6088 * integer DWord, the source register is not incremented but the
6089 * source sub register is incremented."
6091 * The hardware specs from Gen4 to Gen7.5 mention similar regioning
6092 * restrictions. The code below intentionally doesn't check whether the
6093 * destination type is integer because empirically the hardware doesn't
6094 * seem to care what the actual type is as long as it's dword-aligned.
6096 if (devinfo
->gen
< 8) {
6097 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
6098 /* IVB implements DF scalars as <0;2,1> regions. */
6099 const bool is_scalar_exception
= is_uniform(inst
->src
[i
]) &&
6100 (devinfo
->is_haswell
|| type_sz(inst
->src
[i
].type
) != 8);
6101 const bool is_packed_word_exception
=
6102 type_sz(inst
->dst
.type
) == 4 && inst
->dst
.stride
== 1 &&
6103 type_sz(inst
->src
[i
].type
) == 2 && inst
->src
[i
].stride
== 1;
6105 /* We check size_read(i) against size_written instead of REG_SIZE
6106 * because we want to properly handle SIMD32. In SIMD32, you can end
6107 * up with writes to 4 registers and a source that reads 2 registers
6108 * and we may still need to lower all the way to SIMD8 in that case.
6110 if (inst
->size_written
> REG_SIZE
&&
6111 inst
->size_read(i
) != 0 &&
6112 inst
->size_read(i
) < inst
->size_written
&&
6113 !is_scalar_exception
&& !is_packed_word_exception
) {
6114 const unsigned reg_count
= DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
6115 max_width
= MIN2(max_width
, inst
->exec_size
/ reg_count
);
6120 if (devinfo
->gen
< 6) {
6121 /* From the G45 PRM, Volume 4 Page 361:
6123 * "Operand Alignment Rule: With the exceptions listed below, a
6124 * source/destination operand in general should be aligned to even
6125 * 256-bit physical register with a region size equal to two 256-bit
6126 * physical registers."
6128 * Normally we enforce this by allocating virtual registers to the
6129 * even-aligned class. But we need to handle payload registers.
6131 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
6132 if (inst
->src
[i
].file
== FIXED_GRF
&& (inst
->src
[i
].nr
& 1) &&
6133 inst
->size_read(i
) > REG_SIZE
) {
6134 max_width
= MIN2(max_width
, 8);
6139 /* From the IVB PRMs:
6140 * "When an instruction is SIMD32, the low 16 bits of the execution mask
6141 * are applied for both halves of the SIMD32 instruction. If different
6142 * execution mask channels are required, split the instruction into two
6143 * SIMD16 instructions."
6145 * There is similar text in the HSW PRMs. Gen4-6 don't even implement
6146 * 32-wide control flow support in hardware and will behave similarly.
6148 if (devinfo
->gen
< 8 && !inst
->force_writemask_all
)
6149 max_width
= MIN2(max_width
, 16);
6151 /* From the IVB PRMs (applies to HSW too):
6152 * "Instructions with condition modifiers must not use SIMD32."
6154 * From the BDW PRMs (applies to later hardware too):
6155 * "Ternary instruction with condition modifiers must not use SIMD32."
6157 if (inst
->conditional_mod
&& (devinfo
->gen
< 8 || inst
->is_3src(devinfo
)))
6158 max_width
= MIN2(max_width
, 16);
6160 /* From the IVB PRMs (applies to other devices that don't have the
6161 * gen_device_info::supports_simd16_3src flag set):
6162 * "In Align16 access mode, SIMD16 is not allowed for DW operations and
6163 * SIMD8 is not allowed for DF operations."
6165 if (inst
->is_3src(devinfo
) && !devinfo
->supports_simd16_3src
)
6166 max_width
= MIN2(max_width
, inst
->exec_size
/ reg_count
);
6168 /* Pre-Gen8 EUs are hardwired to use the QtrCtrl+1 (where QtrCtrl is
6169 * the 8-bit quarter of the execution mask signals specified in the
6170 * instruction control fields) for the second compressed half of any
6171 * single-precision instruction (for double-precision instructions
6172 * it's hardwired to use NibCtrl+1, at least on HSW), which means that
6173 * the EU will apply the wrong execution controls for the second
6174 * sequential GRF write if the number of channels per GRF is not exactly
6175 * eight in single-precision mode (or four in double-float mode).
6177 * In this situation we calculate the maximum size of the split
6178 * instructions so they only ever write to a single register.
6180 if (devinfo
->gen
< 8 && inst
->size_written
> REG_SIZE
&&
6181 !inst
->force_writemask_all
) {
6182 const unsigned channels_per_grf
= inst
->exec_size
/
6183 DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
6184 const unsigned exec_type_size
= get_exec_type_size(inst
);
6185 assert(exec_type_size
);
6187 /* The hardware shifts exactly 8 channels per compressed half of the
6188 * instruction in single-precision mode and exactly 4 in double-precision.
6190 if (channels_per_grf
!= (exec_type_size
== 8 ? 4 : 8))
6191 max_width
= MIN2(max_width
, channels_per_grf
);
6193 /* Lower all non-force_writemask_all DF instructions to SIMD4 on IVB/BYT
6194 * because HW applies the same channel enable signals to both halves of
6195 * the compressed instruction which will be just wrong under
6196 * non-uniform control flow.
6198 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
6199 (exec_type_size
== 8 || type_sz(inst
->dst
.type
) == 8))
6200 max_width
= MIN2(max_width
, 4);
6203 /* From the SKL PRM, Special Restrictions for Handling Mixed Mode
6206 * "No SIMD16 in mixed mode when destination is f32. Instruction
6207 * execution size must be no more than 8."
6209 * FIXME: the simulator doesn't seem to complain if we don't do this and
6210 * empirical testing with existing CTS tests show that they pass just fine
6211 * without implementing this, however, since our interpretation of the PRM
6212 * is that conversion MOVs between HF and F are still mixed-float
6213 * instructions (and therefore subject to this restriction) we decided to
6214 * split them to be safe. Might be useful to do additional investigation to
6215 * lift the restriction if we can ensure that it is safe though, since these
6216 * conversions are common when half-float types are involved since many
6217 * instructions do not support HF types and conversions from/to F are
6220 if (is_mixed_float_with_fp32_dst(inst
))
6221 max_width
= MIN2(max_width
, 8);
6223 /* From the SKL PRM, Special Restrictions for Handling Mixed Mode
6226 * "No SIMD16 in mixed mode when destination is packed f16 for both
6227 * Align1 and Align16."
6229 if (is_mixed_float_with_packed_fp16_dst(inst
))
6230 max_width
= MIN2(max_width
, 8);
6232 /* Only power-of-two execution sizes are representable in the instruction
6235 return 1 << util_logbase2(max_width
);
6239 * Get the maximum allowed SIMD width for instruction \p inst accounting for
6240 * various payload size restrictions that apply to sampler message
6243 * This is only intended to provide a maximum theoretical bound for the
6244 * execution size of the message based on the number of argument components
6245 * alone, which in most cases will determine whether the SIMD8 or SIMD16
6246 * variant of the message can be used, though some messages may have
6247 * additional restrictions not accounted for here (e.g. pre-ILK hardware uses
6248 * the message length to determine the exact SIMD width and argument count,
6249 * which makes a number of sampler message combinations impossible to
6253 get_sampler_lowered_simd_width(const struct gen_device_info
*devinfo
,
6254 const fs_inst
*inst
)
6256 /* If we have a min_lod parameter on anything other than a simple sample
6257 * message, it will push it over 5 arguments and we have to fall back to
6260 if (inst
->opcode
!= SHADER_OPCODE_TEX
&&
6261 inst
->components_read(TEX_LOGICAL_SRC_MIN_LOD
))
6264 /* Calculate the number of coordinate components that have to be present
6265 * assuming that additional arguments follow the texel coordinates in the
6266 * message payload. On IVB+ there is no need for padding, on ILK-SNB we
6267 * need to pad to four or three components depending on the message,
6268 * pre-ILK we need to pad to at most three components.
6270 const unsigned req_coord_components
=
6271 (devinfo
->gen
>= 7 ||
6272 !inst
->components_read(TEX_LOGICAL_SRC_COORDINATE
)) ? 0 :
6273 (devinfo
->gen
>= 5 && inst
->opcode
!= SHADER_OPCODE_TXF_LOGICAL
&&
6274 inst
->opcode
!= SHADER_OPCODE_TXF_CMS_LOGICAL
) ? 4 :
6277 /* On Gen9+ the LOD argument is for free if we're able to use the LZ
6278 * variant of the TXL or TXF message.
6280 const bool implicit_lod
= devinfo
->gen
>= 9 &&
6281 (inst
->opcode
== SHADER_OPCODE_TXL
||
6282 inst
->opcode
== SHADER_OPCODE_TXF
) &&
6283 inst
->src
[TEX_LOGICAL_SRC_LOD
].is_zero();
6285 /* Calculate the total number of argument components that need to be passed
6286 * to the sampler unit.
6288 const unsigned num_payload_components
=
6289 MAX2(inst
->components_read(TEX_LOGICAL_SRC_COORDINATE
),
6290 req_coord_components
) +
6291 inst
->components_read(TEX_LOGICAL_SRC_SHADOW_C
) +
6292 (implicit_lod
? 0 : inst
->components_read(TEX_LOGICAL_SRC_LOD
)) +
6293 inst
->components_read(TEX_LOGICAL_SRC_LOD2
) +
6294 inst
->components_read(TEX_LOGICAL_SRC_SAMPLE_INDEX
) +
6295 (inst
->opcode
== SHADER_OPCODE_TG4_OFFSET_LOGICAL
?
6296 inst
->components_read(TEX_LOGICAL_SRC_TG4_OFFSET
) : 0) +
6297 inst
->components_read(TEX_LOGICAL_SRC_MCS
);
6299 /* SIMD16 messages with more than five arguments exceed the maximum message
6300 * size supported by the sampler, regardless of whether a header is
6303 return MIN2(inst
->exec_size
,
6304 num_payload_components
> MAX_SAMPLER_MESSAGE_SIZE
/ 2 ? 8 : 16);
6308 * Get the closest native SIMD width supported by the hardware for instruction
6309 * \p inst. The instruction will be left untouched by
6310 * fs_visitor::lower_simd_width() if the returned value is equal to the
6311 * original execution size.
6314 get_lowered_simd_width(const struct gen_device_info
*devinfo
,
6315 const fs_inst
*inst
)
6317 switch (inst
->opcode
) {
6318 case BRW_OPCODE_MOV
:
6319 case BRW_OPCODE_SEL
:
6320 case BRW_OPCODE_NOT
:
6321 case BRW_OPCODE_AND
:
6323 case BRW_OPCODE_XOR
:
6324 case BRW_OPCODE_SHR
:
6325 case BRW_OPCODE_SHL
:
6326 case BRW_OPCODE_ASR
:
6327 case BRW_OPCODE_ROR
:
6328 case BRW_OPCODE_ROL
:
6329 case BRW_OPCODE_CMPN
:
6330 case BRW_OPCODE_CSEL
:
6331 case BRW_OPCODE_F32TO16
:
6332 case BRW_OPCODE_F16TO32
:
6333 case BRW_OPCODE_BFREV
:
6334 case BRW_OPCODE_BFE
:
6335 case BRW_OPCODE_ADD
:
6336 case BRW_OPCODE_MUL
:
6337 case BRW_OPCODE_AVG
:
6338 case BRW_OPCODE_FRC
:
6339 case BRW_OPCODE_RNDU
:
6340 case BRW_OPCODE_RNDD
:
6341 case BRW_OPCODE_RNDE
:
6342 case BRW_OPCODE_RNDZ
:
6343 case BRW_OPCODE_LZD
:
6344 case BRW_OPCODE_FBH
:
6345 case BRW_OPCODE_FBL
:
6346 case BRW_OPCODE_CBIT
:
6347 case BRW_OPCODE_SAD2
:
6348 case BRW_OPCODE_MAD
:
6349 case BRW_OPCODE_LRP
:
6350 case FS_OPCODE_PACK
:
6351 case SHADER_OPCODE_SEL_EXEC
:
6352 case SHADER_OPCODE_CLUSTER_BROADCAST
:
6353 return get_fpu_lowered_simd_width(devinfo
, inst
);
6355 case BRW_OPCODE_CMP
: {
6356 /* The Ivybridge/BayTrail WaCMPInstFlagDepClearedEarly workaround says that
6357 * when the destination is a GRF the dependency-clear bit on the flag
6358 * register is cleared early.
6360 * Suggested workarounds are to disable coissuing CMP instructions
6361 * or to split CMP(16) instructions into two CMP(8) instructions.
6363 * We choose to split into CMP(8) instructions since disabling
6364 * coissuing would affect CMP instructions not otherwise affected by
6367 const unsigned max_width
= (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
6368 !inst
->dst
.is_null() ? 8 : ~0);
6369 return MIN2(max_width
, get_fpu_lowered_simd_width(devinfo
, inst
));
6371 case BRW_OPCODE_BFI1
:
6372 case BRW_OPCODE_BFI2
:
6373 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
6375 * "Force BFI instructions to be executed always in SIMD8."
6377 return MIN2(devinfo
->is_haswell
? 8 : ~0u,
6378 get_fpu_lowered_simd_width(devinfo
, inst
));
6381 assert(inst
->src
[0].file
== BAD_FILE
|| inst
->exec_size
<= 16);
6382 return inst
->exec_size
;
6384 case SHADER_OPCODE_RCP
:
6385 case SHADER_OPCODE_RSQ
:
6386 case SHADER_OPCODE_SQRT
:
6387 case SHADER_OPCODE_EXP2
:
6388 case SHADER_OPCODE_LOG2
:
6389 case SHADER_OPCODE_SIN
:
6390 case SHADER_OPCODE_COS
: {
6391 /* Unary extended math instructions are limited to SIMD8 on Gen4 and
6392 * Gen6. Extended Math Function is limited to SIMD8 with half-float.
6394 if (devinfo
->gen
== 6 || (devinfo
->gen
== 4 && !devinfo
->is_g4x
))
6395 return MIN2(8, inst
->exec_size
);
6396 if (inst
->dst
.type
== BRW_REGISTER_TYPE_HF
)
6397 return MIN2(8, inst
->exec_size
);
6398 return MIN2(16, inst
->exec_size
);
6401 case SHADER_OPCODE_POW
: {
6402 /* SIMD16 is only allowed on Gen7+. Extended Math Function is limited
6403 * to SIMD8 with half-float
6405 if (devinfo
->gen
< 7)
6406 return MIN2(8, inst
->exec_size
);
6407 if (inst
->dst
.type
== BRW_REGISTER_TYPE_HF
)
6408 return MIN2(8, inst
->exec_size
);
6409 return MIN2(16, inst
->exec_size
);
6412 case SHADER_OPCODE_USUB_SAT
:
6413 case SHADER_OPCODE_ISUB_SAT
:
6414 return get_fpu_lowered_simd_width(devinfo
, inst
);
6416 case SHADER_OPCODE_INT_QUOTIENT
:
6417 case SHADER_OPCODE_INT_REMAINDER
:
6418 /* Integer division is limited to SIMD8 on all generations. */
6419 return MIN2(8, inst
->exec_size
);
6421 case FS_OPCODE_LINTERP
:
6422 case SHADER_OPCODE_GET_BUFFER_SIZE
:
6423 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
6424 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
6425 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
6426 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
6427 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
6428 return MIN2(16, inst
->exec_size
);
6430 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
:
6431 /* Pre-ILK hardware doesn't have a SIMD8 variant of the texel fetch
6432 * message used to implement varying pull constant loads, so expand it
6433 * to SIMD16. An alternative with longer message payload length but
6434 * shorter return payload would be to use the SIMD8 sampler message that
6435 * takes (header, u, v, r) as parameters instead of (header, u).
6437 return (devinfo
->gen
== 4 ? 16 : MIN2(16, inst
->exec_size
));
6439 case FS_OPCODE_DDX_COARSE
:
6440 case FS_OPCODE_DDX_FINE
:
6441 case FS_OPCODE_DDY_COARSE
:
6442 case FS_OPCODE_DDY_FINE
:
6443 /* The implementation of this virtual opcode may require emitting
6444 * compressed Align16 instructions, which are severely limited on some
6447 * From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
6448 * Region Restrictions):
6450 * "In Align16 access mode, SIMD16 is not allowed for DW operations
6451 * and SIMD8 is not allowed for DF operations."
6453 * In this context, "DW operations" means "operations acting on 32-bit
6454 * values", so it includes operations on floats.
6456 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
6457 * (Instruction Compression -> Rules and Restrictions):
6459 * "A compressed instruction must be in Align1 access mode. Align16
6460 * mode instructions cannot be compressed."
6462 * Similar text exists in the g45 PRM.
6464 * Empirically, compressed align16 instructions using odd register
6465 * numbers don't appear to work on Sandybridge either.
6467 return (devinfo
->gen
== 4 || devinfo
->gen
== 6 ||
6468 (devinfo
->gen
== 7 && !devinfo
->is_haswell
) ?
6469 MIN2(8, inst
->exec_size
) : MIN2(16, inst
->exec_size
));
6471 case SHADER_OPCODE_MULH
:
6472 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
6473 * is 8-wide on Gen7+.
6475 return (devinfo
->gen
>= 7 ? 8 :
6476 get_fpu_lowered_simd_width(devinfo
, inst
));
6478 case FS_OPCODE_FB_WRITE_LOGICAL
:
6479 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
6482 assert(devinfo
->gen
!= 6 ||
6483 inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
].file
== BAD_FILE
||
6484 inst
->exec_size
== 8);
6485 /* Dual-source FB writes are unsupported in SIMD16 mode. */
6486 return (inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
].file
!= BAD_FILE
?
6487 8 : MIN2(16, inst
->exec_size
));
6489 case FS_OPCODE_FB_READ_LOGICAL
:
6490 return MIN2(16, inst
->exec_size
);
6492 case SHADER_OPCODE_TEX_LOGICAL
:
6493 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
6494 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
6495 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
6496 case SHADER_OPCODE_LOD_LOGICAL
:
6497 case SHADER_OPCODE_TG4_LOGICAL
:
6498 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
6499 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
6500 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
6501 return get_sampler_lowered_simd_width(devinfo
, inst
);
6503 case SHADER_OPCODE_TXD_LOGICAL
:
6504 /* TXD is unsupported in SIMD16 mode. */
6507 case SHADER_OPCODE_TXL_LOGICAL
:
6508 case FS_OPCODE_TXB_LOGICAL
:
6509 /* Only one execution size is representable pre-ILK depending on whether
6510 * the shadow reference argument is present.
6512 if (devinfo
->gen
== 4)
6513 return inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
].file
== BAD_FILE
? 16 : 8;
6515 return get_sampler_lowered_simd_width(devinfo
, inst
);
6517 case SHADER_OPCODE_TXF_LOGICAL
:
6518 case SHADER_OPCODE_TXS_LOGICAL
:
6519 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
6520 * messages. Use SIMD16 instead.
6522 if (devinfo
->gen
== 4)
6525 return get_sampler_lowered_simd_width(devinfo
, inst
);
6527 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
6528 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
6529 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
6532 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
6533 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
6534 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
6535 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
6536 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
6537 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
6538 case SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL
:
6539 case SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL
:
6540 return MIN2(16, inst
->exec_size
);
6542 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
6543 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
6544 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
6545 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
6546 return devinfo
->gen
<= 8 ? 8 : MIN2(16, inst
->exec_size
);
6548 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
6549 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
6550 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
6553 case SHADER_OPCODE_URB_READ_SIMD8
:
6554 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
6555 case SHADER_OPCODE_URB_WRITE_SIMD8
:
6556 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
6557 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
6558 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
6559 return MIN2(8, inst
->exec_size
);
6561 case SHADER_OPCODE_QUAD_SWIZZLE
: {
6562 const unsigned swiz
= inst
->src
[1].ud
;
6563 return (is_uniform(inst
->src
[0]) ?
6564 get_fpu_lowered_simd_width(devinfo
, inst
) :
6565 devinfo
->gen
< 11 && type_sz(inst
->src
[0].type
) == 4 ? 8 :
6566 swiz
== BRW_SWIZZLE_XYXY
|| swiz
== BRW_SWIZZLE_ZWZW
? 4 :
6567 get_fpu_lowered_simd_width(devinfo
, inst
));
6569 case SHADER_OPCODE_MOV_INDIRECT
: {
6570 /* From IVB and HSW PRMs:
6572 * "2.When the destination requires two registers and the sources are
6573 * indirect, the sources must use 1x1 regioning mode.
6575 * In case of DF instructions in HSW/IVB, the exec_size is limited by
6576 * the EU decompression logic not handling VxH indirect addressing
6579 const unsigned max_size
= (devinfo
->gen
>= 8 ? 2 : 1) * REG_SIZE
;
6580 /* Prior to Broadwell, we only have 8 address subregisters. */
6581 return MIN3(devinfo
->gen
>= 8 ? 16 : 8,
6582 max_size
/ (inst
->dst
.stride
* type_sz(inst
->dst
.type
)),
6586 case SHADER_OPCODE_LOAD_PAYLOAD
: {
6587 const unsigned reg_count
=
6588 DIV_ROUND_UP(inst
->dst
.component_size(inst
->exec_size
), REG_SIZE
);
6590 if (reg_count
> 2) {
6591 /* Only LOAD_PAYLOAD instructions with per-channel destination region
6592 * can be easily lowered (which excludes headers and heterogeneous
6595 assert(!inst
->header_size
);
6596 for (unsigned i
= 0; i
< inst
->sources
; i
++)
6597 assert(type_sz(inst
->dst
.type
) == type_sz(inst
->src
[i
].type
) ||
6598 inst
->src
[i
].file
== BAD_FILE
);
6600 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
6602 return inst
->exec_size
;
6606 return inst
->exec_size
;
6611 * Return true if splitting out the group of channels of instruction \p inst
6612 * given by lbld.group() requires allocating a temporary for the i-th source
6613 * of the lowered instruction.
6616 needs_src_copy(const fs_builder
&lbld
, const fs_inst
*inst
, unsigned i
)
6618 return !(is_periodic(inst
->src
[i
], lbld
.dispatch_width()) ||
6619 (inst
->components_read(i
) == 1 &&
6620 lbld
.dispatch_width() <= inst
->exec_size
)) ||
6621 (inst
->flags_written() &
6622 flag_mask(inst
->src
[i
], type_sz(inst
->src
[i
].type
)));
6626 * Extract the data that would be consumed by the channel group given by
6627 * lbld.group() from the i-th source region of instruction \p inst and return
6628 * it as result in packed form.
6631 emit_unzip(const fs_builder
&lbld
, fs_inst
*inst
, unsigned i
)
6633 assert(lbld
.group() >= inst
->group
);
6635 /* Specified channel group from the source region. */
6636 const fs_reg src
= horiz_offset(inst
->src
[i
], lbld
.group() - inst
->group
);
6638 if (needs_src_copy(lbld
, inst
, i
)) {
6639 /* Builder of the right width to perform the copy avoiding uninitialized
6640 * data if the lowered execution size is greater than the original
6641 * execution size of the instruction.
6643 const fs_builder cbld
= lbld
.group(MIN2(lbld
.dispatch_width(),
6644 inst
->exec_size
), 0);
6645 const fs_reg tmp
= lbld
.vgrf(inst
->src
[i
].type
, inst
->components_read(i
));
6647 for (unsigned k
= 0; k
< inst
->components_read(i
); ++k
)
6648 cbld
.MOV(offset(tmp
, lbld
, k
), offset(src
, inst
->exec_size
, k
));
6652 } else if (is_periodic(inst
->src
[i
], lbld
.dispatch_width())) {
6653 /* The source is invariant for all dispatch_width-wide groups of the
6656 return inst
->src
[i
];
6659 /* We can just point the lowered instruction at the right channel group
6660 * from the original region.
6667 * Return true if splitting out the group of channels of instruction \p inst
6668 * given by lbld.group() requires allocating a temporary for the destination
6669 * of the lowered instruction and copying the data back to the original
6670 * destination region.
6673 needs_dst_copy(const fs_builder
&lbld
, const fs_inst
*inst
)
6675 /* If the instruction writes more than one component we'll have to shuffle
6676 * the results of multiple lowered instructions in order to make sure that
6677 * they end up arranged correctly in the original destination region.
6679 if (inst
->size_written
> inst
->dst
.component_size(inst
->exec_size
))
6682 /* If the lowered execution size is larger than the original the result of
6683 * the instruction won't fit in the original destination, so we'll have to
6684 * allocate a temporary in any case.
6686 if (lbld
.dispatch_width() > inst
->exec_size
)
6689 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
6690 /* If we already made a copy of the source for other reasons there won't
6691 * be any overlap with the destination.
6693 if (needs_src_copy(lbld
, inst
, i
))
6696 /* In order to keep the logic simple we emit a copy whenever the
6697 * destination region doesn't exactly match an overlapping source, which
6698 * may point at the source and destination not being aligned group by
6699 * group which could cause one of the lowered instructions to overwrite
6700 * the data read from the same source by other lowered instructions.
6702 if (regions_overlap(inst
->dst
, inst
->size_written
,
6703 inst
->src
[i
], inst
->size_read(i
)) &&
6704 !inst
->dst
.equals(inst
->src
[i
]))
6712 * Insert data from a packed temporary into the channel group given by
6713 * lbld.group() of the destination region of instruction \p inst and return
6714 * the temporary as result. Any copy instructions that are required for
6715 * unzipping the previous value (in the case of partial writes) will be
6716 * inserted using \p lbld_before and any copy instructions required for
6717 * zipping up the destination of \p inst will be inserted using \p lbld_after.
6720 emit_zip(const fs_builder
&lbld_before
, const fs_builder
&lbld_after
,
6723 assert(lbld_before
.dispatch_width() == lbld_after
.dispatch_width());
6724 assert(lbld_before
.group() == lbld_after
.group());
6725 assert(lbld_after
.group() >= inst
->group
);
6727 /* Specified channel group from the destination region. */
6728 const fs_reg dst
= horiz_offset(inst
->dst
, lbld_after
.group() - inst
->group
);
6729 const unsigned dst_size
= inst
->size_written
/
6730 inst
->dst
.component_size(inst
->exec_size
);
6732 if (needs_dst_copy(lbld_after
, inst
)) {
6733 const fs_reg tmp
= lbld_after
.vgrf(inst
->dst
.type
, dst_size
);
6735 if (inst
->predicate
) {
6736 /* Handle predication by copying the original contents of
6737 * the destination into the temporary before emitting the
6738 * lowered instruction.
6740 const fs_builder gbld_before
=
6741 lbld_before
.group(MIN2(lbld_before
.dispatch_width(),
6742 inst
->exec_size
), 0);
6743 for (unsigned k
= 0; k
< dst_size
; ++k
) {
6744 gbld_before
.MOV(offset(tmp
, lbld_before
, k
),
6745 offset(dst
, inst
->exec_size
, k
));
6749 const fs_builder gbld_after
=
6750 lbld_after
.group(MIN2(lbld_after
.dispatch_width(),
6751 inst
->exec_size
), 0);
6752 for (unsigned k
= 0; k
< dst_size
; ++k
) {
6753 /* Use a builder of the right width to perform the copy avoiding
6754 * uninitialized data if the lowered execution size is greater than
6755 * the original execution size of the instruction.
6757 gbld_after
.MOV(offset(dst
, inst
->exec_size
, k
),
6758 offset(tmp
, lbld_after
, k
));
6764 /* No need to allocate a temporary for the lowered instruction, just
6765 * take the right group of channels from the original region.
6772 fs_visitor::lower_simd_width()
6774 bool progress
= false;
6776 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
6777 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
6779 if (lower_width
!= inst
->exec_size
) {
6780 /* Builder matching the original instruction. We may also need to
6781 * emit an instruction of width larger than the original, set the
6782 * execution size of the builder to the highest of both for now so
6783 * we're sure that both cases can be handled.
6785 const unsigned max_width
= MAX2(inst
->exec_size
, lower_width
);
6786 const fs_builder ibld
= bld
.at(block
, inst
)
6787 .exec_all(inst
->force_writemask_all
)
6788 .group(max_width
, inst
->group
/ max_width
);
6790 /* Split the copies in chunks of the execution width of either the
6791 * original or the lowered instruction, whichever is lower.
6793 const unsigned n
= DIV_ROUND_UP(inst
->exec_size
, lower_width
);
6794 const unsigned dst_size
= inst
->size_written
/
6795 inst
->dst
.component_size(inst
->exec_size
);
6797 assert(!inst
->writes_accumulator
&& !inst
->mlen
);
6799 /* Inserting the zip, unzip, and duplicated instructions in all of
6800 * the right spots is somewhat tricky. All of the unzip and any
6801 * instructions from the zip which unzip the destination prior to
6802 * writing need to happen before all of the per-group instructions
6803 * and the zip instructions need to happen after. In order to sort
6804 * this all out, we insert the unzip instructions before \p inst,
6805 * insert the per-group instructions after \p inst (i.e. before
6806 * inst->next), and insert the zip instructions before the
6807 * instruction after \p inst. Since we are inserting instructions
6808 * after \p inst, inst->next is a moving target and we need to save
6809 * it off here so that we insert the zip instructions in the right
6812 * Since we're inserting split instructions after after_inst, the
6813 * instructions will end up in the reverse order that we insert them.
6814 * However, certain render target writes require that the low group
6815 * instructions come before the high group. From the Ivy Bridge PRM
6816 * Vol. 4, Pt. 1, Section 3.9.11:
6818 * "If multiple SIMD8 Dual Source messages are delivered by the
6819 * pixel shader thread, each SIMD8_DUALSRC_LO message must be
6820 * issued before the SIMD8_DUALSRC_HI message with the same Slot
6821 * Group Select setting."
6823 * And, from Section 3.9.11.1 of the same PRM:
6825 * "When SIMD32 or SIMD16 PS threads send render target writes
6826 * with multiple SIMD8 and SIMD16 messages, the following must
6829 * All the slots (as described above) must have a corresponding
6830 * render target write irrespective of the slot's validity. A slot
6831 * is considered valid when at least one sample is enabled. For
6832 * example, a SIMD16 PS thread must send two SIMD8 render target
6833 * writes to cover all the slots.
6835 * PS thread must send SIMD render target write messages with
6836 * increasing slot numbers. For example, SIMD16 thread has
6837 * Slot[15:0] and if two SIMD8 render target writes are used, the
6838 * first SIMD8 render target write must send Slot[7:0] and the
6839 * next one must send Slot[15:8]."
6841 * In order to make low group instructions come before high group
6842 * instructions (this is required for some render target writes), we
6843 * split from the highest group to lowest.
6845 exec_node
*const after_inst
= inst
->next
;
6846 for (int i
= n
- 1; i
>= 0; i
--) {
6847 /* Emit a copy of the original instruction with the lowered width.
6848 * If the EOT flag was set throw it away except for the last
6849 * instruction to avoid killing the thread prematurely.
6851 fs_inst split_inst
= *inst
;
6852 split_inst
.exec_size
= lower_width
;
6853 split_inst
.eot
= inst
->eot
&& i
== int(n
- 1);
6855 /* Select the correct channel enables for the i-th group, then
6856 * transform the sources and destination and emit the lowered
6859 const fs_builder lbld
= ibld
.group(lower_width
, i
);
6861 for (unsigned j
= 0; j
< inst
->sources
; j
++)
6862 split_inst
.src
[j
] = emit_unzip(lbld
.at(block
, inst
), inst
, j
);
6864 split_inst
.dst
= emit_zip(lbld
.at(block
, inst
),
6865 lbld
.at(block
, after_inst
), inst
);
6866 split_inst
.size_written
=
6867 split_inst
.dst
.component_size(lower_width
) * dst_size
;
6869 lbld
.at(block
, inst
->next
).emit(split_inst
);
6872 inst
->remove(block
);
6878 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
6884 * Transform barycentric vectors into the interleaved form expected by the PLN
6885 * instruction and returned by the Gen7+ PI shared function.
6887 * For channels 0-15 in SIMD16 mode they are expected to be laid out as
6888 * follows in the register file:
6895 * There is no need to handle SIMD32 here -- This is expected to be run after
6896 * SIMD lowering, since SIMD lowering relies on vectors having the standard
6900 fs_visitor::lower_barycentrics()
6902 const bool has_interleaved_layout
= devinfo
->has_pln
|| devinfo
->gen
>= 7;
6903 bool progress
= false;
6905 if (stage
!= MESA_SHADER_FRAGMENT
|| !has_interleaved_layout
)
6908 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
6909 if (inst
->exec_size
< 16)
6912 const fs_builder
ibld(this, block
, inst
);
6913 const fs_builder ubld
= ibld
.exec_all().group(8, 0);
6915 switch (inst
->opcode
) {
6916 case FS_OPCODE_LINTERP
: {
6917 assert(inst
->exec_size
== 16);
6918 const fs_reg tmp
= ibld
.vgrf(inst
->src
[0].type
, 2);
6921 for (unsigned i
= 0; i
< ARRAY_SIZE(srcs
); i
++)
6922 srcs
[i
] = horiz_offset(offset(inst
->src
[0], ibld
, i
% 2),
6925 ubld
.LOAD_PAYLOAD(tmp
, srcs
, ARRAY_SIZE(srcs
), ARRAY_SIZE(srcs
));
6931 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
6932 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
6933 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
: {
6934 assert(inst
->exec_size
== 16);
6935 const fs_reg tmp
= ibld
.vgrf(inst
->dst
.type
, 2);
6937 for (unsigned i
= 0; i
< 2; i
++) {
6938 for (unsigned g
= 0; g
< inst
->exec_size
/ 8; g
++) {
6939 fs_inst
*mov
= ibld
.at(block
, inst
->next
).group(8, g
)
6940 .MOV(horiz_offset(offset(inst
->dst
, ibld
, i
),
6942 offset(tmp
, ubld
, 2 * g
+ i
));
6943 mov
->predicate
= inst
->predicate
;
6944 mov
->predicate_inverse
= inst
->predicate_inverse
;
6945 mov
->flag_subreg
= inst
->flag_subreg
;
6959 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
6965 fs_visitor::dump_instructions() const
6967 dump_instructions(NULL
);
6971 fs_visitor::dump_instructions(const char *name
) const
6973 FILE *file
= stderr
;
6974 if (name
&& geteuid() != 0) {
6975 file
= fopen(name
, "w");
6981 const register_pressure
&rp
= regpressure_analysis
.require();
6982 unsigned ip
= 0, max_pressure
= 0;
6983 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
6984 max_pressure
= MAX2(max_pressure
, rp
.regs_live_at_ip
[ip
]);
6985 fprintf(file
, "{%3d} %4d: ", rp
.regs_live_at_ip
[ip
], ip
);
6986 dump_instruction(inst
, file
);
6989 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
6992 foreach_in_list(backend_instruction
, inst
, &instructions
) {
6993 fprintf(file
, "%4d: ", ip
++);
6994 dump_instruction(inst
, file
);
6998 if (file
!= stderr
) {
7004 fs_visitor::dump_instruction(const backend_instruction
*be_inst
) const
7006 dump_instruction(be_inst
, stderr
);
7010 fs_visitor::dump_instruction(const backend_instruction
*be_inst
, FILE *file
) const
7012 const fs_inst
*inst
= (const fs_inst
*)be_inst
;
7014 if (inst
->predicate
) {
7015 fprintf(file
, "(%cf%d.%d) ",
7016 inst
->predicate_inverse
? '-' : '+',
7017 inst
->flag_subreg
/ 2,
7018 inst
->flag_subreg
% 2);
7021 fprintf(file
, "%s", brw_instruction_name(devinfo
, inst
->opcode
));
7023 fprintf(file
, ".sat");
7024 if (inst
->conditional_mod
) {
7025 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
7026 if (!inst
->predicate
&&
7027 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
7028 inst
->opcode
!= BRW_OPCODE_CSEL
&&
7029 inst
->opcode
!= BRW_OPCODE_IF
&&
7030 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
7031 fprintf(file
, ".f%d.%d", inst
->flag_subreg
/ 2,
7032 inst
->flag_subreg
% 2);
7035 fprintf(file
, "(%d) ", inst
->exec_size
);
7038 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
7041 if (inst
->ex_mlen
) {
7042 fprintf(file
, "(ex_mlen: %d) ", inst
->ex_mlen
);
7046 fprintf(file
, "(EOT) ");
7049 switch (inst
->dst
.file
) {
7051 fprintf(file
, "vgrf%d", inst
->dst
.nr
);
7054 fprintf(file
, "g%d", inst
->dst
.nr
);
7057 fprintf(file
, "m%d", inst
->dst
.nr
);
7060 fprintf(file
, "(null)");
7063 fprintf(file
, "***u%d***", inst
->dst
.nr
);
7066 fprintf(file
, "***attr%d***", inst
->dst
.nr
);
7069 switch (inst
->dst
.nr
) {
7071 fprintf(file
, "null");
7073 case BRW_ARF_ADDRESS
:
7074 fprintf(file
, "a0.%d", inst
->dst
.subnr
);
7076 case BRW_ARF_ACCUMULATOR
:
7077 fprintf(file
, "acc%d", inst
->dst
.subnr
);
7080 fprintf(file
, "f%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
7083 fprintf(file
, "arf%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
7088 unreachable("not reached");
7091 if (inst
->dst
.offset
||
7092 (inst
->dst
.file
== VGRF
&&
7093 alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
!= inst
->size_written
)) {
7094 const unsigned reg_size
= (inst
->dst
.file
== UNIFORM
? 4 : REG_SIZE
);
7095 fprintf(file
, "+%d.%d", inst
->dst
.offset
/ reg_size
,
7096 inst
->dst
.offset
% reg_size
);
7099 if (inst
->dst
.stride
!= 1)
7100 fprintf(file
, "<%u>", inst
->dst
.stride
);
7101 fprintf(file
, ":%s, ", brw_reg_type_to_letters(inst
->dst
.type
));
7103 for (int i
= 0; i
< inst
->sources
; i
++) {
7104 if (inst
->src
[i
].negate
)
7106 if (inst
->src
[i
].abs
)
7108 switch (inst
->src
[i
].file
) {
7110 fprintf(file
, "vgrf%d", inst
->src
[i
].nr
);
7113 fprintf(file
, "g%d", inst
->src
[i
].nr
);
7116 fprintf(file
, "***m%d***", inst
->src
[i
].nr
);
7119 fprintf(file
, "attr%d", inst
->src
[i
].nr
);
7122 fprintf(file
, "u%d", inst
->src
[i
].nr
);
7125 fprintf(file
, "(null)");
7128 switch (inst
->src
[i
].type
) {
7129 case BRW_REGISTER_TYPE_F
:
7130 fprintf(file
, "%-gf", inst
->src
[i
].f
);
7132 case BRW_REGISTER_TYPE_DF
:
7133 fprintf(file
, "%fdf", inst
->src
[i
].df
);
7135 case BRW_REGISTER_TYPE_W
:
7136 case BRW_REGISTER_TYPE_D
:
7137 fprintf(file
, "%dd", inst
->src
[i
].d
);
7139 case BRW_REGISTER_TYPE_UW
:
7140 case BRW_REGISTER_TYPE_UD
:
7141 fprintf(file
, "%uu", inst
->src
[i
].ud
);
7143 case BRW_REGISTER_TYPE_Q
:
7144 fprintf(file
, "%" PRId64
"q", inst
->src
[i
].d64
);
7146 case BRW_REGISTER_TYPE_UQ
:
7147 fprintf(file
, "%" PRIu64
"uq", inst
->src
[i
].u64
);
7149 case BRW_REGISTER_TYPE_VF
:
7150 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
7151 brw_vf_to_float((inst
->src
[i
].ud
>> 0) & 0xff),
7152 brw_vf_to_float((inst
->src
[i
].ud
>> 8) & 0xff),
7153 brw_vf_to_float((inst
->src
[i
].ud
>> 16) & 0xff),
7154 brw_vf_to_float((inst
->src
[i
].ud
>> 24) & 0xff));
7156 case BRW_REGISTER_TYPE_V
:
7157 case BRW_REGISTER_TYPE_UV
:
7158 fprintf(file
, "%08x%s", inst
->src
[i
].ud
,
7159 inst
->src
[i
].type
== BRW_REGISTER_TYPE_V
? "V" : "UV");
7162 fprintf(file
, "???");
7167 switch (inst
->src
[i
].nr
) {
7169 fprintf(file
, "null");
7171 case BRW_ARF_ADDRESS
:
7172 fprintf(file
, "a0.%d", inst
->src
[i
].subnr
);
7174 case BRW_ARF_ACCUMULATOR
:
7175 fprintf(file
, "acc%d", inst
->src
[i
].subnr
);
7178 fprintf(file
, "f%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
7181 fprintf(file
, "arf%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
7187 if (inst
->src
[i
].offset
||
7188 (inst
->src
[i
].file
== VGRF
&&
7189 alloc
.sizes
[inst
->src
[i
].nr
] * REG_SIZE
!= inst
->size_read(i
))) {
7190 const unsigned reg_size
= (inst
->src
[i
].file
== UNIFORM
? 4 : REG_SIZE
);
7191 fprintf(file
, "+%d.%d", inst
->src
[i
].offset
/ reg_size
,
7192 inst
->src
[i
].offset
% reg_size
);
7195 if (inst
->src
[i
].abs
)
7198 if (inst
->src
[i
].file
!= IMM
) {
7200 if (inst
->src
[i
].file
== ARF
|| inst
->src
[i
].file
== FIXED_GRF
) {
7201 unsigned hstride
= inst
->src
[i
].hstride
;
7202 stride
= (hstride
== 0 ? 0 : (1 << (hstride
- 1)));
7204 stride
= inst
->src
[i
].stride
;
7207 fprintf(file
, "<%u>", stride
);
7209 fprintf(file
, ":%s", brw_reg_type_to_letters(inst
->src
[i
].type
));
7212 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
7213 fprintf(file
, ", ");
7218 if (inst
->force_writemask_all
)
7219 fprintf(file
, "NoMask ");
7221 if (inst
->exec_size
!= dispatch_width
)
7222 fprintf(file
, "group%d ", inst
->group
);
7224 fprintf(file
, "\n");
7228 fs_visitor::setup_fs_payload_gen6()
7230 assert(stage
== MESA_SHADER_FRAGMENT
);
7231 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
7232 const unsigned payload_width
= MIN2(16, dispatch_width
);
7233 assert(dispatch_width
% payload_width
== 0);
7234 assert(devinfo
->gen
>= 6);
7236 /* R0: PS thread payload header. */
7239 for (unsigned j
= 0; j
< dispatch_width
/ payload_width
; j
++) {
7240 /* R1: masks, pixel X/Y coordinates. */
7241 payload
.subspan_coord_reg
[j
] = payload
.num_regs
++;
7244 for (unsigned j
= 0; j
< dispatch_width
/ payload_width
; j
++) {
7245 /* R3-26: barycentric interpolation coordinates. These appear in the
7246 * same order that they appear in the brw_barycentric_mode enum. Each
7247 * set of coordinates occupies 2 registers if dispatch width == 8 and 4
7248 * registers if dispatch width == 16. Coordinates only appear if they
7249 * were enabled using the "Barycentric Interpolation Mode" bits in
7252 for (int i
= 0; i
< BRW_BARYCENTRIC_MODE_COUNT
; ++i
) {
7253 if (prog_data
->barycentric_interp_modes
& (1 << i
)) {
7254 payload
.barycentric_coord_reg
[i
][j
] = payload
.num_regs
;
7255 payload
.num_regs
+= payload_width
/ 4;
7259 /* R27-28: interpolated depth if uses source depth */
7260 if (prog_data
->uses_src_depth
) {
7261 payload
.source_depth_reg
[j
] = payload
.num_regs
;
7262 payload
.num_regs
+= payload_width
/ 8;
7265 /* R29-30: interpolated W set if GEN6_WM_USES_SOURCE_W. */
7266 if (prog_data
->uses_src_w
) {
7267 payload
.source_w_reg
[j
] = payload
.num_regs
;
7268 payload
.num_regs
+= payload_width
/ 8;
7271 /* R31: MSAA position offsets. */
7272 if (prog_data
->uses_pos_offset
) {
7273 payload
.sample_pos_reg
[j
] = payload
.num_regs
;
7277 /* R32-33: MSAA input coverage mask */
7278 if (prog_data
->uses_sample_mask
) {
7279 assert(devinfo
->gen
>= 7);
7280 payload
.sample_mask_in_reg
[j
] = payload
.num_regs
;
7281 payload
.num_regs
+= payload_width
/ 8;
7285 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
7286 source_depth_to_render_target
= true;
7291 fs_visitor::setup_vs_payload()
7293 /* R0: thread header, R1: urb handles */
7294 payload
.num_regs
= 2;
7298 fs_visitor::setup_gs_payload()
7300 assert(stage
== MESA_SHADER_GEOMETRY
);
7302 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
7303 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
7305 /* R0: thread header, R1: output URB handles */
7306 payload
.num_regs
= 2;
7308 if (gs_prog_data
->include_primitive_id
) {
7309 /* R2: Primitive ID 0..7 */
7313 /* Always enable VUE handles so we can safely use pull model if needed.
7315 * The push model for a GS uses a ton of register space even for trivial
7316 * scenarios with just a few inputs, so just make things easier and a bit
7317 * safer by always having pull model available.
7319 gs_prog_data
->base
.include_vue_handles
= true;
7321 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
7322 payload
.num_regs
+= nir
->info
.gs
.vertices_in
;
7324 /* Use a maximum of 24 registers for push-model inputs. */
7325 const unsigned max_push_components
= 24;
7327 /* If pushing our inputs would take too many registers, reduce the URB read
7328 * length (which is in HWords, or 8 registers), and resort to pulling.
7330 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
7331 * have to multiply by VerticesIn to obtain the total storage requirement.
7333 if (8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
>
7334 max_push_components
) {
7335 vue_prog_data
->urb_read_length
=
7336 ROUND_DOWN_TO(max_push_components
/ nir
->info
.gs
.vertices_in
, 8) / 8;
7341 fs_visitor::setup_cs_payload()
7343 assert(devinfo
->gen
>= 7);
7344 payload
.num_regs
= 1;
7347 brw::register_pressure::register_pressure(const fs_visitor
*v
)
7349 const fs_live_variables
&live
= v
->live_analysis
.require();
7350 const unsigned num_instructions
= v
->cfg
->num_blocks
?
7351 v
->cfg
->blocks
[v
->cfg
->num_blocks
- 1]->end_ip
+ 1 : 0;
7353 regs_live_at_ip
= new unsigned[num_instructions
]();
7355 for (unsigned reg
= 0; reg
< v
->alloc
.count
; reg
++) {
7356 for (int ip
= live
.vgrf_start
[reg
]; ip
<= live
.vgrf_end
[reg
]; ip
++)
7357 regs_live_at_ip
[ip
] += v
->alloc
.sizes
[reg
];
7361 brw::register_pressure::~register_pressure()
7363 delete[] regs_live_at_ip
;
7367 fs_visitor::invalidate_analysis(brw::analysis_dependency_class c
)
7369 backend_shader::invalidate_analysis(c
);
7370 live_analysis
.invalidate(c
);
7371 regpressure_analysis
.invalidate(c
);
7375 fs_visitor::optimize()
7377 /* Start by validating the shader we currently have. */
7380 /* bld is the common builder object pointing at the end of the program we
7381 * used to translate it into i965 IR. For the optimization and lowering
7382 * passes coming next, any code added after the end of the program without
7383 * having explicitly called fs_builder::at() clearly points at a mistake.
7384 * Ideally optimization passes wouldn't be part of the visitor so they
7385 * wouldn't have access to bld at all, but they do, so just in case some
7386 * pass forgets to ask for a location explicitly set it to NULL here to
7387 * make it trip. The dispatch width is initialized to a bogus value to
7388 * make sure that optimizations set the execution controls explicitly to
7389 * match the code they are manipulating instead of relying on the defaults.
7391 bld
= fs_builder(this, 64);
7393 assign_constant_locations();
7394 lower_constant_loads();
7398 split_virtual_grfs();
7401 #define OPT(pass, args...) ({ \
7403 bool this_progress = pass(args); \
7405 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
7406 char filename[64]; \
7407 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
7408 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
7410 backend_shader::dump_instructions(filename); \
7415 progress = progress || this_progress; \
7419 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
7421 snprintf(filename
, 64, "%s%d-%s-00-00-start",
7422 stage_abbrev
, dispatch_width
, nir
->info
.name
);
7424 backend_shader::dump_instructions(filename
);
7427 bool progress
= false;
7431 /* Before anything else, eliminate dead code. The results of some NIR
7432 * instructions may effectively be calculated twice. Once when the
7433 * instruction is encountered, and again when the user of that result is
7434 * encountered. Wipe those away before algebraic optimizations and
7435 * especially copy propagation can mix things up.
7437 OPT(dead_code_eliminate
);
7439 OPT(remove_extra_rounding_modes
);
7446 OPT(remove_duplicate_mrf_writes
);
7450 OPT(opt_copy_propagation
);
7451 OPT(opt_predicated_break
, this);
7452 OPT(opt_cmod_propagation
);
7453 OPT(dead_code_eliminate
);
7454 OPT(opt_peephole_sel
);
7455 OPT(dead_control_flow_eliminate
, this);
7456 OPT(opt_register_renaming
);
7457 OPT(opt_saturate_propagation
);
7458 OPT(register_coalesce
);
7459 OPT(compute_to_mrf
);
7460 OPT(eliminate_find_live_channel
);
7462 OPT(compact_virtual_grfs
);
7468 if (OPT(lower_pack
)) {
7469 OPT(register_coalesce
);
7470 OPT(dead_code_eliminate
);
7473 OPT(lower_simd_width
);
7474 OPT(lower_barycentrics
);
7475 OPT(lower_logical_sends
);
7477 /* After logical SEND lowering. */
7478 OPT(fixup_nomask_control_flow
);
7481 OPT(opt_copy_propagation
);
7482 /* Only run after logical send lowering because it's easier to implement
7483 * in terms of physical sends.
7485 if (OPT(opt_zero_samples
))
7486 OPT(opt_copy_propagation
);
7487 /* Run after logical send lowering to give it a chance to CSE the
7488 * LOAD_PAYLOAD instructions created to construct the payloads of
7489 * e.g. texturing messages in cases where it wasn't possible to CSE the
7490 * whole logical instruction.
7493 OPT(register_coalesce
);
7494 OPT(compute_to_mrf
);
7495 OPT(dead_code_eliminate
);
7496 OPT(remove_duplicate_mrf_writes
);
7497 OPT(opt_peephole_sel
);
7500 OPT(opt_redundant_discard_jumps
);
7502 if (OPT(lower_load_payload
)) {
7503 split_virtual_grfs();
7505 /* Lower 64 bit MOVs generated by payload lowering. */
7506 if (!devinfo
->has_64bit_float
&& !devinfo
->has_64bit_int
)
7509 OPT(register_coalesce
);
7510 OPT(lower_simd_width
);
7511 OPT(compute_to_mrf
);
7512 OPT(dead_code_eliminate
);
7515 OPT(opt_combine_constants
);
7516 OPT(lower_integer_multiplication
);
7519 if (devinfo
->gen
<= 5 && OPT(lower_minmax
)) {
7520 OPT(opt_cmod_propagation
);
7522 OPT(opt_copy_propagation
);
7523 OPT(dead_code_eliminate
);
7526 if (OPT(lower_regioning
)) {
7527 OPT(opt_copy_propagation
);
7528 OPT(dead_code_eliminate
);
7529 OPT(lower_simd_width
);
7532 OPT(fixup_sends_duplicate_payload
);
7534 lower_uniform_pull_constant_loads();
7540 * From the Skylake PRM Vol. 2a docs for sends:
7542 * "It is required that the second block of GRFs does not overlap with the
7545 * There are plenty of cases where we may accidentally violate this due to
7546 * having, for instance, both sources be the constant 0. This little pass
7547 * just adds a new vgrf for the second payload and copies it over.
7550 fs_visitor::fixup_sends_duplicate_payload()
7552 bool progress
= false;
7554 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
7555 if (inst
->opcode
== SHADER_OPCODE_SEND
&& inst
->ex_mlen
> 0 &&
7556 regions_overlap(inst
->src
[2], inst
->mlen
* REG_SIZE
,
7557 inst
->src
[3], inst
->ex_mlen
* REG_SIZE
)) {
7558 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(inst
->ex_mlen
),
7559 BRW_REGISTER_TYPE_UD
);
7560 /* Sadly, we've lost all notion of channels and bit sizes at this
7561 * point. Just WE_all it.
7563 const fs_builder ibld
= bld
.at(block
, inst
).exec_all().group(16, 0);
7564 fs_reg copy_src
= retype(inst
->src
[3], BRW_REGISTER_TYPE_UD
);
7565 fs_reg copy_dst
= tmp
;
7566 for (unsigned i
= 0; i
< inst
->ex_mlen
; i
+= 2) {
7567 if (inst
->ex_mlen
== i
+ 1) {
7568 /* Only one register left; do SIMD8 */
7569 ibld
.group(8, 0).MOV(copy_dst
, copy_src
);
7571 ibld
.MOV(copy_dst
, copy_src
);
7573 copy_src
= offset(copy_src
, ibld
, 1);
7574 copy_dst
= offset(copy_dst
, ibld
, 1);
7582 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
7588 * Three source instruction must have a GRF/MRF destination register.
7589 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
7592 fs_visitor::fixup_3src_null_dest()
7594 bool progress
= false;
7596 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
7597 if (inst
->is_3src(devinfo
) && inst
->dst
.is_null()) {
7598 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
7605 invalidate_analysis(DEPENDENCY_INSTRUCTION_DETAIL
|
7606 DEPENDENCY_VARIABLES
);
7610 * Find the first instruction in the program that might start a region of
7611 * divergent control flow due to a HALT jump. There is no
7612 * find_halt_control_flow_region_end(), the region of divergence extends until
7613 * the only FS_OPCODE_PLACEHOLDER_HALT in the program.
7615 static const fs_inst
*
7616 find_halt_control_flow_region_start(const fs_visitor
*v
)
7618 if (brw_wm_prog_data(v
->prog_data
)->uses_kill
) {
7619 foreach_block_and_inst(block
, fs_inst
, inst
, v
->cfg
) {
7620 if (inst
->opcode
== FS_OPCODE_DISCARD_JUMP
||
7621 inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
)
7630 * Work around the Gen12 hardware bug filed as GEN:BUG:1407528679. EU fusion
7631 * can cause a BB to be executed with all channels disabled, which will lead
7632 * to the execution of any NoMask instructions in it, even though any
7633 * execution-masked instructions will be correctly shot down. This may break
7634 * assumptions of some NoMask SEND messages whose descriptor depends on data
7635 * generated by live invocations of the shader.
7637 * This avoids the problem by predicating certain instructions on an ANY
7638 * horizontal predicate that makes sure that their execution is omitted when
7639 * all channels of the program are disabled.
7642 fs_visitor::fixup_nomask_control_flow()
7644 if (devinfo
->gen
!= 12)
7647 const brw_predicate pred
= dispatch_width
> 16 ? BRW_PREDICATE_ALIGN1_ANY32H
:
7648 dispatch_width
> 8 ? BRW_PREDICATE_ALIGN1_ANY16H
:
7649 BRW_PREDICATE_ALIGN1_ANY8H
;
7650 const fs_inst
*halt_start
= find_halt_control_flow_region_start(this);
7652 bool progress
= false;
7654 const fs_live_variables
&live_vars
= live_analysis
.require();
7656 /* Scan the program backwards in order to be able to easily determine
7657 * whether the flag register is live at any point.
7659 foreach_block_reverse_safe(block
, cfg
) {
7660 BITSET_WORD flag_liveout
= live_vars
.block_data
[block
->num
]
7662 STATIC_ASSERT(ARRAY_SIZE(live_vars
.block_data
[0].flag_liveout
) == 1);
7664 foreach_inst_in_block_reverse_safe(fs_inst
, inst
, block
) {
7665 if (!inst
->predicate
&& inst
->exec_size
>= 8)
7666 flag_liveout
&= ~inst
->flags_written();
7668 switch (inst
->opcode
) {
7671 /* Note that this doesn't handle FS_OPCODE_DISCARD_JUMP since only
7672 * the first one in the program closes the region of divergent
7673 * control flow due to any HALT instructions -- Instead this is
7674 * handled with the halt_start check below.
7679 case BRW_OPCODE_WHILE
:
7680 case BRW_OPCODE_ENDIF
:
7681 case FS_OPCODE_PLACEHOLDER_HALT
:
7686 /* Note that the vast majority of NoMask SEND instructions in the
7687 * program are harmless while executed in a block with all
7688 * channels disabled, since any instructions with side effects we
7689 * could hit here should be execution-masked.
7691 * The main concern is NoMask SEND instructions where the message
7692 * descriptor or header depends on data generated by live
7693 * invocations of the shader (RESINFO and
7694 * FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD with a dynamically
7695 * computed surface index seem to be the only examples right now
7696 * where this could easily lead to GPU hangs). Unfortunately we
7697 * have no straightforward way to detect that currently, so just
7698 * predicate any NoMask SEND instructions we find under control
7701 * If this proves to have a measurable performance impact it can
7702 * be easily extended with a whitelist of messages we know we can
7703 * safely omit the predication for.
7705 if (depth
&& inst
->force_writemask_all
&&
7706 is_send(inst
) && !inst
->predicate
) {
7707 /* We need to load the execution mask into the flag register by
7708 * using a builder with channel group matching the whole shader
7709 * (rather than the default which is derived from the original
7710 * instruction), in order to avoid getting a right-shifted
7713 const fs_builder ubld
= fs_builder(this, block
, inst
)
7714 .exec_all().group(dispatch_width
, 0);
7715 const fs_reg flag
= retype(brw_flag_reg(0, 0),
7716 BRW_REGISTER_TYPE_UD
);
7718 /* Due to the lack of flag register allocation we need to save
7719 * and restore the flag register if it's live.
7721 const bool save_flag
= flag_liveout
&
7722 flag_mask(flag
, dispatch_width
/ 8);
7723 const fs_reg tmp
= ubld
.group(1, 0).vgrf(flag
.type
);
7726 ubld
.group(1, 0).MOV(tmp
, flag
);
7728 ubld
.emit(FS_OPCODE_LOAD_LIVE_CHANNELS
);
7730 set_predicate(pred
, inst
);
7731 inst
->flag_subreg
= 0;
7734 ubld
.group(1, 0).at(block
, inst
->next
).MOV(flag
, tmp
);
7741 if (inst
== halt_start
)
7744 flag_liveout
|= inst
->flags_read(devinfo
);
7749 invalidate_analysis(DEPENDENCY_INSTRUCTIONS
| DEPENDENCY_VARIABLES
);
7755 fs_visitor::allocate_registers(bool allow_spilling
)
7759 static const enum instruction_scheduler_mode pre_modes
[] = {
7761 SCHEDULE_PRE_NON_LIFO
,
7765 static const char *scheduler_mode_name
[] = {
7771 bool spill_all
= allow_spilling
&& (INTEL_DEBUG
& DEBUG_SPILL_FS
);
7773 /* Try each scheduling heuristic to see if it can successfully register
7774 * allocate without spilling. They should be ordered by decreasing
7775 * performance but increasing likelihood of allocating.
7777 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
7778 schedule_instructions(pre_modes
[i
]);
7779 this->shader_stats
.scheduler_mode
= scheduler_mode_name
[i
];
7782 assign_regs_trivial();
7787 /* Scheduling may create additional opportunities for CMOD propagation,
7788 * so let's do it again. If CMOD propagation made any progress,
7789 * elminate dead code one more time.
7791 bool progress
= false;
7792 const int iteration
= 99;
7795 if (OPT(opt_cmod_propagation
)) {
7796 /* dead_code_eliminate "undoes" the fixing done by
7797 * fixup_3src_null_dest, so we have to do it again if
7798 * dead_code_eliminiate makes any progress.
7800 if (OPT(dead_code_eliminate
))
7801 fixup_3src_null_dest();
7804 bool can_spill
= allow_spilling
&&
7805 (i
== ARRAY_SIZE(pre_modes
) - 1);
7807 /* We should only spill registers on the last scheduling. */
7808 assert(!spilled_any_registers
);
7810 allocated
= assign_regs(can_spill
, spill_all
);
7816 fail("Failure to register allocate. Reduce number of "
7817 "live scalar values to avoid this.");
7818 } else if (spilled_any_registers
) {
7819 compiler
->shader_perf_log(log_data
,
7820 "%s shader triggered register spilling. "
7821 "Try reducing the number of live scalar "
7822 "values to improve performance.\n",
7826 /* This must come after all optimization and register allocation, since
7827 * it inserts dead code that happens to have side effects, and it does
7828 * so based on the actual physical registers in use.
7830 insert_gen4_send_dependency_workarounds();
7835 opt_bank_conflicts();
7837 schedule_instructions(SCHEDULE_POST
);
7839 if (last_scratch
> 0) {
7840 ASSERTED
unsigned max_scratch_size
= 2 * 1024 * 1024;
7842 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
7844 if (stage
== MESA_SHADER_COMPUTE
|| stage
== MESA_SHADER_KERNEL
) {
7845 if (devinfo
->is_haswell
) {
7846 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7847 * field documentation, Haswell supports a minimum of 2kB of
7848 * scratch space for compute shaders, unlike every other stage
7851 prog_data
->total_scratch
= MAX2(prog_data
->total_scratch
, 2048);
7852 } else if (devinfo
->gen
<= 7) {
7853 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7854 * field documentation, platforms prior to Haswell measure scratch
7855 * size linearly with a range of [1kB, 12kB] and 1kB granularity.
7857 prog_data
->total_scratch
= ALIGN(last_scratch
, 1024);
7858 max_scratch_size
= 12 * 1024;
7862 /* We currently only support up to 2MB of scratch space. If we
7863 * need to support more eventually, the documentation suggests
7864 * that we could allocate a larger buffer, and partition it out
7865 * ourselves. We'd just have to undo the hardware's address
7866 * calculation by subtracting (FFTID * Per Thread Scratch Space)
7867 * and then add FFTID * (Larger Per Thread Scratch Space).
7869 * See 3D-Media-GPGPU Engine > Media GPGPU Pipeline >
7870 * Thread Group Tracking > Local Memory/Scratch Space.
7872 assert(prog_data
->total_scratch
< max_scratch_size
);
7879 fs_visitor::run_vs()
7881 assert(stage
== MESA_SHADER_VERTEX
);
7885 if (shader_time_index
>= 0)
7886 emit_shader_time_begin();
7895 if (shader_time_index
>= 0)
7896 emit_shader_time_end();
7902 assign_curb_setup();
7903 assign_vs_urb_setup();
7905 fixup_3src_null_dest();
7906 allocate_registers(true /* allow_spilling */);
7912 fs_visitor::set_tcs_invocation_id()
7914 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
7915 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
7917 const unsigned instance_id_mask
=
7918 devinfo
->gen
>= 11 ? INTEL_MASK(22, 16) : INTEL_MASK(23, 17);
7919 const unsigned instance_id_shift
=
7920 devinfo
->gen
>= 11 ? 16 : 17;
7922 /* Get instance number from g0.2 bits 22:16 or 23:17 */
7923 fs_reg t
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7924 bld
.AND(t
, fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
)),
7925 brw_imm_ud(instance_id_mask
));
7927 invocation_id
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7929 if (vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
) {
7930 /* gl_InvocationID is just the thread number */
7931 bld
.SHR(invocation_id
, t
, brw_imm_ud(instance_id_shift
));
7935 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
);
7937 fs_reg channels_uw
= bld
.vgrf(BRW_REGISTER_TYPE_UW
);
7938 fs_reg channels_ud
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7939 bld
.MOV(channels_uw
, fs_reg(brw_imm_uv(0x76543210)));
7940 bld
.MOV(channels_ud
, channels_uw
);
7942 if (tcs_prog_data
->instances
== 1) {
7943 invocation_id
= channels_ud
;
7945 fs_reg instance_times_8
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7946 bld
.SHR(instance_times_8
, t
, brw_imm_ud(instance_id_shift
- 3));
7947 bld
.ADD(invocation_id
, instance_times_8
, channels_ud
);
7952 fs_visitor::run_tcs()
7954 assert(stage
== MESA_SHADER_TESS_CTRL
);
7956 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
7957 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
7958 struct brw_tcs_prog_key
*tcs_key
= (struct brw_tcs_prog_key
*) key
;
7960 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
||
7961 vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
);
7963 if (vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
) {
7964 /* r1-r4 contain the ICP handles. */
7965 payload
.num_regs
= 5;
7967 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
);
7968 assert(tcs_key
->input_vertices
> 0);
7969 /* r1 contains output handles, r2 may contain primitive ID, then the
7970 * ICP handles occupy the next 1-32 registers.
7972 payload
.num_regs
= 2 + tcs_prog_data
->include_primitive_id
+
7973 tcs_key
->input_vertices
;
7976 if (shader_time_index
>= 0)
7977 emit_shader_time_begin();
7979 /* Initialize gl_InvocationID */
7980 set_tcs_invocation_id();
7982 const bool fix_dispatch_mask
=
7983 vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
&&
7984 (nir
->info
.tess
.tcs_vertices_out
% 8) != 0;
7986 /* Fix the disptach mask */
7987 if (fix_dispatch_mask
) {
7988 bld
.CMP(bld
.null_reg_ud(), invocation_id
,
7989 brw_imm_ud(nir
->info
.tess
.tcs_vertices_out
), BRW_CONDITIONAL_L
);
7990 bld
.IF(BRW_PREDICATE_NORMAL
);
7995 if (fix_dispatch_mask
) {
7996 bld
.emit(BRW_OPCODE_ENDIF
);
7999 /* Emit EOT write; set TR DS Cache bit */
8001 fs_reg(get_tcs_output_urb_handle()),
8002 fs_reg(brw_imm_ud(WRITEMASK_X
<< 16)),
8003 fs_reg(brw_imm_ud(0)),
8005 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 3);
8006 bld
.LOAD_PAYLOAD(payload
, srcs
, 3, 2);
8008 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
,
8009 bld
.null_reg_ud(), payload
);
8013 if (shader_time_index
>= 0)
8014 emit_shader_time_end();
8023 assign_curb_setup();
8024 assign_tcs_urb_setup();
8026 fixup_3src_null_dest();
8027 allocate_registers(true /* allow_spilling */);
8033 fs_visitor::run_tes()
8035 assert(stage
== MESA_SHADER_TESS_EVAL
);
8037 /* R0: thread header, R1-3: gl_TessCoord.xyz, R4: URB handles */
8038 payload
.num_regs
= 5;
8040 if (shader_time_index
>= 0)
8041 emit_shader_time_begin();
8050 if (shader_time_index
>= 0)
8051 emit_shader_time_end();
8057 assign_curb_setup();
8058 assign_tes_urb_setup();
8060 fixup_3src_null_dest();
8061 allocate_registers(true /* allow_spilling */);
8067 fs_visitor::run_gs()
8069 assert(stage
== MESA_SHADER_GEOMETRY
);
8073 this->final_gs_vertex_count
= vgrf(glsl_type::uint_type
);
8075 if (gs_compile
->control_data_header_size_bits
> 0) {
8076 /* Create a VGRF to store accumulated control data bits. */
8077 this->control_data_bits
= vgrf(glsl_type::uint_type
);
8079 /* If we're outputting more than 32 control data bits, then EmitVertex()
8080 * will set control_data_bits to 0 after emitting the first vertex.
8081 * Otherwise, we need to initialize it to 0 here.
8083 if (gs_compile
->control_data_header_size_bits
<= 32) {
8084 const fs_builder abld
= bld
.annotate("initialize control data bits");
8085 abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
8089 if (shader_time_index
>= 0)
8090 emit_shader_time_begin();
8094 emit_gs_thread_end();
8096 if (shader_time_index
>= 0)
8097 emit_shader_time_end();
8106 assign_curb_setup();
8107 assign_gs_urb_setup();
8109 fixup_3src_null_dest();
8110 allocate_registers(true /* allow_spilling */);
8115 /* From the SKL PRM, Volume 16, Workarounds:
8117 * 0877 3D Pixel Shader Hang possible when pixel shader dispatched with
8118 * only header phases (R0-R2)
8120 * WA: Enable a non-header phase (e.g. push constant) when dispatch would
8121 * have been header only.
8123 * Instead of enabling push constants one can alternatively enable one of the
8124 * inputs. Here one simply chooses "layer" which shouldn't impose much
8128 gen9_ps_header_only_workaround(struct brw_wm_prog_data
*wm_prog_data
)
8130 if (wm_prog_data
->num_varying_inputs
)
8133 if (wm_prog_data
->base
.curb_read_length
)
8136 wm_prog_data
->urb_setup
[VARYING_SLOT_LAYER
] = 0;
8137 wm_prog_data
->num_varying_inputs
= 1;
8139 brw_compute_urb_setup_index(wm_prog_data
);
8143 fs_visitor::run_fs(bool allow_spilling
, bool do_rep_send
)
8145 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
8146 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
8148 assert(stage
== MESA_SHADER_FRAGMENT
);
8150 if (devinfo
->gen
>= 6)
8151 setup_fs_payload_gen6();
8153 setup_fs_payload_gen4();
8157 } else if (do_rep_send
) {
8158 assert(dispatch_width
== 16);
8159 emit_repclear_shader();
8161 if (shader_time_index
>= 0)
8162 emit_shader_time_begin();
8164 if (nir
->info
.inputs_read
> 0 ||
8165 (nir
->info
.system_values_read
& (1ull << SYSTEM_VALUE_FRAG_COORD
)) ||
8166 (nir
->info
.outputs_read
> 0 && !wm_key
->coherent_fb_fetch
)) {
8167 if (devinfo
->gen
< 6)
8168 emit_interpolation_setup_gen4();
8170 emit_interpolation_setup_gen6();
8173 /* We handle discards by keeping track of the still-live pixels in f0.1.
8174 * Initialize it with the dispatched pixels.
8176 if (wm_prog_data
->uses_kill
) {
8177 const unsigned lower_width
= MIN2(dispatch_width
, 16);
8178 for (unsigned i
= 0; i
< dispatch_width
/ lower_width
; i
++) {
8179 const fs_reg dispatch_mask
=
8180 devinfo
->gen
>= 6 ? brw_vec1_grf((i
? 2 : 1), 7) :
8182 bld
.exec_all().group(1, 0)
8183 .MOV(sample_mask_reg(bld
.group(lower_width
, i
)),
8184 retype(dispatch_mask
, BRW_REGISTER_TYPE_UW
));
8188 if (nir
->info
.writes_memory
)
8189 wm_prog_data
->has_side_effects
= true;
8196 if (wm_prog_data
->uses_kill
)
8197 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
8199 if (wm_key
->alpha_test_func
)
8204 if (shader_time_index
>= 0)
8205 emit_shader_time_end();
8211 assign_curb_setup();
8213 if (devinfo
->gen
>= 9)
8214 gen9_ps_header_only_workaround(wm_prog_data
);
8218 fixup_3src_null_dest();
8220 allocate_registers(allow_spilling
);
8230 fs_visitor::run_cs(bool allow_spilling
)
8232 assert(stage
== MESA_SHADER_COMPUTE
|| stage
== MESA_SHADER_KERNEL
);
8236 if (shader_time_index
>= 0)
8237 emit_shader_time_begin();
8239 if (devinfo
->is_haswell
&& prog_data
->total_shared
> 0) {
8240 /* Move SLM index from g0.0[27:24] to sr0.1[11:8] */
8241 const fs_builder abld
= bld
.exec_all().group(1, 0);
8242 abld
.MOV(retype(brw_sr0_reg(1), BRW_REGISTER_TYPE_UW
),
8243 suboffset(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
), 1));
8251 emit_cs_terminate();
8253 if (shader_time_index
>= 0)
8254 emit_shader_time_end();
8260 assign_curb_setup();
8262 fixup_3src_null_dest();
8263 allocate_registers(allow_spilling
);
8272 is_used_in_not_interp_frag_coord(nir_ssa_def
*def
)
8274 nir_foreach_use(src
, def
) {
8275 if (src
->parent_instr
->type
!= nir_instr_type_intrinsic
)
8278 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(src
->parent_instr
);
8279 if (intrin
->intrinsic
!= nir_intrinsic_load_frag_coord
)
8283 nir_foreach_if_use(src
, def
)
8290 * Return a bitfield where bit n is set if barycentric interpolation mode n
8291 * (see enum brw_barycentric_mode) is needed by the fragment shader.
8293 * We examine the load_barycentric intrinsics rather than looking at input
8294 * variables so that we catch interpolateAtCentroid() messages too, which
8295 * also need the BRW_BARYCENTRIC_[NON]PERSPECTIVE_CENTROID mode set up.
8298 brw_compute_barycentric_interp_modes(const struct gen_device_info
*devinfo
,
8299 const nir_shader
*shader
)
8301 unsigned barycentric_interp_modes
= 0;
8303 nir_foreach_function(f
, shader
) {
8307 nir_foreach_block(block
, f
->impl
) {
8308 nir_foreach_instr(instr
, block
) {
8309 if (instr
->type
!= nir_instr_type_intrinsic
)
8312 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
8313 switch (intrin
->intrinsic
) {
8314 case nir_intrinsic_load_barycentric_pixel
:
8315 case nir_intrinsic_load_barycentric_centroid
:
8316 case nir_intrinsic_load_barycentric_sample
:
8322 /* Ignore WPOS; it doesn't require interpolation. */
8323 assert(intrin
->dest
.is_ssa
);
8324 if (!is_used_in_not_interp_frag_coord(&intrin
->dest
.ssa
))
8327 enum glsl_interp_mode interp
= (enum glsl_interp_mode
)
8328 nir_intrinsic_interp_mode(intrin
);
8329 nir_intrinsic_op bary_op
= intrin
->intrinsic
;
8330 enum brw_barycentric_mode bary
=
8331 brw_barycentric_mode(interp
, bary_op
);
8333 barycentric_interp_modes
|= 1 << bary
;
8335 if (devinfo
->needs_unlit_centroid_workaround
&&
8336 bary_op
== nir_intrinsic_load_barycentric_centroid
)
8337 barycentric_interp_modes
|= 1 << centroid_to_pixel(bary
);
8342 return barycentric_interp_modes
;
8346 brw_compute_flat_inputs(struct brw_wm_prog_data
*prog_data
,
8347 const nir_shader
*shader
)
8349 prog_data
->flat_inputs
= 0;
8351 nir_foreach_shader_in_variable(var
, shader
) {
8352 unsigned slots
= glsl_count_attribute_slots(var
->type
, false);
8353 for (unsigned s
= 0; s
< slots
; s
++) {
8354 int input_index
= prog_data
->urb_setup
[var
->data
.location
+ s
];
8356 if (input_index
< 0)
8360 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
8361 prog_data
->flat_inputs
|= 1 << input_index
;
8367 computed_depth_mode(const nir_shader
*shader
)
8369 if (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
8370 switch (shader
->info
.fs
.depth_layout
) {
8371 case FRAG_DEPTH_LAYOUT_NONE
:
8372 case FRAG_DEPTH_LAYOUT_ANY
:
8373 return BRW_PSCDEPTH_ON
;
8374 case FRAG_DEPTH_LAYOUT_GREATER
:
8375 return BRW_PSCDEPTH_ON_GE
;
8376 case FRAG_DEPTH_LAYOUT_LESS
:
8377 return BRW_PSCDEPTH_ON_LE
;
8378 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
8379 return BRW_PSCDEPTH_OFF
;
8382 return BRW_PSCDEPTH_OFF
;
8386 * Move load_interpolated_input with simple (payload-based) barycentric modes
8387 * to the top of the program so we don't emit multiple PLNs for the same input.
8389 * This works around CSE not being able to handle non-dominating cases
8395 * interpolate the same exact input
8398 * This should be replaced by global value numbering someday.
8401 brw_nir_move_interpolation_to_top(nir_shader
*nir
)
8403 bool progress
= false;
8405 nir_foreach_function(f
, nir
) {
8409 nir_block
*top
= nir_start_block(f
->impl
);
8410 exec_node
*cursor_node
= NULL
;
8412 nir_foreach_block(block
, f
->impl
) {
8416 nir_foreach_instr_safe(instr
, block
) {
8417 if (instr
->type
!= nir_instr_type_intrinsic
)
8420 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
8421 if (intrin
->intrinsic
!= nir_intrinsic_load_interpolated_input
)
8423 nir_intrinsic_instr
*bary_intrinsic
=
8424 nir_instr_as_intrinsic(intrin
->src
[0].ssa
->parent_instr
);
8425 nir_intrinsic_op op
= bary_intrinsic
->intrinsic
;
8427 /* Leave interpolateAtSample/Offset() where they are. */
8428 if (op
== nir_intrinsic_load_barycentric_at_sample
||
8429 op
== nir_intrinsic_load_barycentric_at_offset
)
8432 nir_instr
*move
[3] = {
8433 &bary_intrinsic
->instr
,
8434 intrin
->src
[1].ssa
->parent_instr
,
8438 for (unsigned i
= 0; i
< ARRAY_SIZE(move
); i
++) {
8439 if (move
[i
]->block
!= top
) {
8440 move
[i
]->block
= top
;
8441 exec_node_remove(&move
[i
]->node
);
8443 exec_node_insert_after(cursor_node
, &move
[i
]->node
);
8445 exec_list_push_head(&top
->instr_list
, &move
[i
]->node
);
8447 cursor_node
= &move
[i
]->node
;
8453 nir_metadata_preserve(f
->impl
, (nir_metadata
)
8454 ((unsigned) nir_metadata_block_index
|
8455 (unsigned) nir_metadata_dominance
));
8462 * Demote per-sample barycentric intrinsics to centroid.
8464 * Useful when rendering to a non-multisampled buffer.
8467 brw_nir_demote_sample_qualifiers(nir_shader
*nir
)
8469 bool progress
= true;
8471 nir_foreach_function(f
, nir
) {
8476 nir_builder_init(&b
, f
->impl
);
8478 nir_foreach_block(block
, f
->impl
) {
8479 nir_foreach_instr_safe(instr
, block
) {
8480 if (instr
->type
!= nir_instr_type_intrinsic
)
8483 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
8484 if (intrin
->intrinsic
!= nir_intrinsic_load_barycentric_sample
&&
8485 intrin
->intrinsic
!= nir_intrinsic_load_barycentric_at_sample
)
8488 b
.cursor
= nir_before_instr(instr
);
8489 nir_ssa_def
*centroid
=
8490 nir_load_barycentric(&b
, nir_intrinsic_load_barycentric_centroid
,
8491 nir_intrinsic_interp_mode(intrin
));
8492 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
8493 nir_src_for_ssa(centroid
));
8494 nir_instr_remove(instr
);
8499 nir_metadata_preserve(f
->impl
, (nir_metadata
)
8500 ((unsigned) nir_metadata_block_index
|
8501 (unsigned) nir_metadata_dominance
));
8508 brw_nir_populate_wm_prog_data(const nir_shader
*shader
,
8509 const struct gen_device_info
*devinfo
,
8510 const struct brw_wm_prog_key
*key
,
8511 struct brw_wm_prog_data
*prog_data
)
8513 prog_data
->uses_src_depth
= prog_data
->uses_src_w
=
8514 shader
->info
.system_values_read
& BITFIELD64_BIT(SYSTEM_VALUE_FRAG_COORD
);
8516 /* key->alpha_test_func means simulating alpha testing via discards,
8517 * so the shader definitely kills pixels.
8519 prog_data
->uses_kill
= shader
->info
.fs
.uses_discard
||
8520 key
->alpha_test_func
;
8521 prog_data
->uses_omask
= !key
->ignore_sample_mask_out
&&
8522 (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
));
8523 prog_data
->computed_depth_mode
= computed_depth_mode(shader
);
8524 prog_data
->computed_stencil
=
8525 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
8527 prog_data
->persample_dispatch
=
8528 key
->multisample_fbo
&&
8529 (key
->persample_interp
||
8530 (shader
->info
.system_values_read
& (SYSTEM_BIT_SAMPLE_ID
|
8531 SYSTEM_BIT_SAMPLE_POS
)) ||
8532 shader
->info
.fs
.uses_sample_qualifier
||
8533 shader
->info
.outputs_read
);
8535 if (devinfo
->gen
>= 6) {
8536 prog_data
->uses_sample_mask
=
8537 shader
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
;
8539 /* From the Ivy Bridge PRM documentation for 3DSTATE_PS:
8541 * "MSDISPMODE_PERSAMPLE is required in order to select
8544 * So we can only really get sample positions if we are doing real
8545 * per-sample dispatch. If we need gl_SamplePosition and we don't have
8546 * persample dispatch, we hard-code it to 0.5.
8548 prog_data
->uses_pos_offset
= prog_data
->persample_dispatch
&&
8549 (shader
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
8552 prog_data
->has_render_target_reads
= shader
->info
.outputs_read
!= 0ull;
8554 prog_data
->early_fragment_tests
= shader
->info
.fs
.early_fragment_tests
;
8555 prog_data
->post_depth_coverage
= shader
->info
.fs
.post_depth_coverage
;
8556 prog_data
->inner_coverage
= shader
->info
.fs
.inner_coverage
;
8558 prog_data
->barycentric_interp_modes
=
8559 brw_compute_barycentric_interp_modes(devinfo
, shader
);
8561 calculate_urb_setup(devinfo
, key
, prog_data
, shader
);
8562 brw_compute_flat_inputs(prog_data
, shader
);
8566 * Pre-gen6, the register file of the EUs was shared between threads,
8567 * and each thread used some subset allocated on a 16-register block
8568 * granularity. The unit states wanted these block counts.
8571 brw_register_blocks(int reg_count
)
8573 return ALIGN(reg_count
, 16) / 16 - 1;
8577 brw_compile_fs(const struct brw_compiler
*compiler
, void *log_data
,
8579 const struct brw_wm_prog_key
*key
,
8580 struct brw_wm_prog_data
*prog_data
,
8582 int shader_time_index8
, int shader_time_index16
,
8583 int shader_time_index32
, bool allow_spilling
,
8584 bool use_rep_send
, struct brw_vue_map
*vue_map
,
8585 struct brw_compile_stats
*stats
,
8588 const struct gen_device_info
*devinfo
= compiler
->devinfo
;
8589 const unsigned max_subgroup_size
= compiler
->devinfo
->gen
>= 6 ? 32 : 16;
8591 brw_nir_apply_key(shader
, compiler
, &key
->base
, max_subgroup_size
, true);
8592 brw_nir_lower_fs_inputs(shader
, devinfo
, key
);
8593 brw_nir_lower_fs_outputs(shader
);
8595 if (devinfo
->gen
< 6)
8596 brw_setup_vue_interpolation(vue_map
, shader
, prog_data
);
8598 /* From the SKL PRM, Volume 7, "Alpha Coverage":
8599 * "If Pixel Shader outputs oMask, AlphaToCoverage is disabled in
8600 * hardware, regardless of the state setting for this feature."
8602 if (devinfo
->gen
> 6 && key
->alpha_to_coverage
) {
8603 /* Run constant fold optimization in order to get the correct source
8604 * offset to determine render target 0 store instruction in
8605 * emit_alpha_to_coverage pass.
8607 NIR_PASS_V(shader
, nir_opt_constant_folding
);
8608 NIR_PASS_V(shader
, brw_nir_lower_alpha_to_coverage
);
8611 if (!key
->multisample_fbo
)
8612 NIR_PASS_V(shader
, brw_nir_demote_sample_qualifiers
);
8613 NIR_PASS_V(shader
, brw_nir_move_interpolation_to_top
);
8614 brw_postprocess_nir(shader
, compiler
, true);
8616 brw_nir_populate_wm_prog_data(shader
, compiler
->devinfo
, key
, prog_data
);
8618 fs_visitor
*v8
= NULL
, *v16
= NULL
, *v32
= NULL
;
8619 cfg_t
*simd8_cfg
= NULL
, *simd16_cfg
= NULL
, *simd32_cfg
= NULL
;
8620 float throughput
= 0;
8621 bool has_spilled
= false;
8623 v8
= new fs_visitor(compiler
, log_data
, mem_ctx
, &key
->base
,
8624 &prog_data
->base
, shader
, 8, shader_time_index8
);
8625 if (!v8
->run_fs(allow_spilling
, false /* do_rep_send */)) {
8627 *error_str
= ralloc_strdup(mem_ctx
, v8
->fail_msg
);
8631 } else if (likely(!(INTEL_DEBUG
& DEBUG_NO8
))) {
8632 simd8_cfg
= v8
->cfg
;
8633 prog_data
->base
.dispatch_grf_start_reg
= v8
->payload
.num_regs
;
8634 prog_data
->reg_blocks_8
= brw_register_blocks(v8
->grf_used
);
8635 const performance
&perf
= v8
->performance_analysis
.require();
8636 throughput
= MAX2(throughput
, perf
.throughput
);
8637 has_spilled
= v8
->spilled_any_registers
;
8638 allow_spilling
= false;
8641 /* Limit dispatch width to simd8 with dual source blending on gen8.
8642 * See: https://gitlab.freedesktop.org/mesa/mesa/-/issues/1917
8644 if (devinfo
->gen
== 8 && prog_data
->dual_src_blend
&&
8645 !(INTEL_DEBUG
& DEBUG_NO8
)) {
8646 assert(!use_rep_send
);
8647 v8
->limit_dispatch_width(8, "gen8 workaround: "
8648 "using SIMD8 when dual src blending.\n");
8652 v8
->max_dispatch_width
>= 16 &&
8653 likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
8654 /* Try a SIMD16 compile */
8655 v16
= new fs_visitor(compiler
, log_data
, mem_ctx
, &key
->base
,
8656 &prog_data
->base
, shader
, 16, shader_time_index16
);
8657 v16
->import_uniforms(v8
);
8658 if (!v16
->run_fs(allow_spilling
, use_rep_send
)) {
8659 compiler
->shader_perf_log(log_data
,
8660 "SIMD16 shader failed to compile: %s",
8663 simd16_cfg
= v16
->cfg
;
8664 prog_data
->dispatch_grf_start_reg_16
= v16
->payload
.num_regs
;
8665 prog_data
->reg_blocks_16
= brw_register_blocks(v16
->grf_used
);
8666 const performance
&perf
= v16
->performance_analysis
.require();
8667 throughput
= MAX2(throughput
, perf
.throughput
);
8668 has_spilled
= v16
->spilled_any_registers
;
8669 allow_spilling
= false;
8673 const bool simd16_failed
= v16
&& !simd16_cfg
;
8675 /* Currently, the compiler only supports SIMD32 on SNB+ */
8677 v8
->max_dispatch_width
>= 32 && !use_rep_send
&&
8678 devinfo
->gen
>= 6 && !simd16_failed
&&
8679 !(INTEL_DEBUG
& DEBUG_NO32
)) {
8680 /* Try a SIMD32 compile */
8681 v32
= new fs_visitor(compiler
, log_data
, mem_ctx
, &key
->base
,
8682 &prog_data
->base
, shader
, 32, shader_time_index32
);
8683 v32
->import_uniforms(v8
);
8684 if (!v32
->run_fs(allow_spilling
, false)) {
8685 compiler
->shader_perf_log(log_data
,
8686 "SIMD32 shader failed to compile: %s",
8689 const performance
&perf
= v32
->performance_analysis
.require();
8691 if (!(INTEL_DEBUG
& DEBUG_DO32
) && throughput
>= perf
.throughput
) {
8692 compiler
->shader_perf_log(log_data
, "SIMD32 shader inefficient\n");
8694 simd32_cfg
= v32
->cfg
;
8695 prog_data
->dispatch_grf_start_reg_32
= v32
->payload
.num_regs
;
8696 prog_data
->reg_blocks_32
= brw_register_blocks(v32
->grf_used
);
8697 throughput
= MAX2(throughput
, perf
.throughput
);
8702 /* When the caller requests a repclear shader, they want SIMD16-only */
8706 /* Prior to Iron Lake, the PS had a single shader offset with a jump table
8707 * at the top to select the shader. We've never implemented that.
8708 * Instead, we just give them exactly one shader and we pick the widest one
8711 if (compiler
->devinfo
->gen
< 5) {
8712 if (simd32_cfg
|| simd16_cfg
)
8718 /* If computed depth is enabled SNB only allows SIMD8. */
8719 if (compiler
->devinfo
->gen
== 6 &&
8720 prog_data
->computed_depth_mode
!= BRW_PSCDEPTH_OFF
)
8721 assert(simd16_cfg
== NULL
&& simd32_cfg
== NULL
);
8723 if (compiler
->devinfo
->gen
<= 5 && !simd8_cfg
) {
8724 /* Iron lake and earlier only have one Dispatch GRF start field. Make
8725 * the data available in the base prog data struct for convenience.
8728 prog_data
->base
.dispatch_grf_start_reg
=
8729 prog_data
->dispatch_grf_start_reg_16
;
8730 } else if (simd32_cfg
) {
8731 prog_data
->base
.dispatch_grf_start_reg
=
8732 prog_data
->dispatch_grf_start_reg_32
;
8736 if (prog_data
->persample_dispatch
) {
8737 /* Starting with SandyBridge (where we first get MSAA), the different
8738 * pixel dispatch combinations are grouped into classifications A
8739 * through F (SNB PRM Vol. 2 Part 1 Section 7.7.1). On most hardware
8740 * generations, the only configurations supporting persample dispatch
8741 * are those in which only one dispatch width is enabled.
8743 * The Gen12 hardware spec has a similar dispatch grouping table, but
8744 * the following conflicting restriction applies (from the page on
8745 * "Structure_3DSTATE_PS_BODY"), so we need to keep the SIMD16 shader:
8747 * "SIMD32 may only be enabled if SIMD16 or (dual)SIMD8 is also
8750 if (simd32_cfg
|| simd16_cfg
)
8752 if (simd32_cfg
&& devinfo
->gen
< 12)
8756 fs_generator
g(compiler
, log_data
, mem_ctx
, &prog_data
->base
,
8757 v8
->runtime_check_aads_emit
, MESA_SHADER_FRAGMENT
);
8759 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
8760 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
8761 shader
->info
.label
?
8762 shader
->info
.label
: "unnamed",
8763 shader
->info
.name
));
8767 prog_data
->dispatch_8
= true;
8768 g
.generate_code(simd8_cfg
, 8, v8
->shader_stats
,
8769 v8
->performance_analysis
.require(), stats
);
8770 stats
= stats
? stats
+ 1 : NULL
;
8774 prog_data
->dispatch_16
= true;
8775 prog_data
->prog_offset_16
= g
.generate_code(
8776 simd16_cfg
, 16, v16
->shader_stats
,
8777 v16
->performance_analysis
.require(), stats
);
8778 stats
= stats
? stats
+ 1 : NULL
;
8782 prog_data
->dispatch_32
= true;
8783 prog_data
->prog_offset_32
= g
.generate_code(
8784 simd32_cfg
, 32, v32
->shader_stats
,
8785 v32
->performance_analysis
.require(), stats
);
8786 stats
= stats
? stats
+ 1 : NULL
;
8793 return g
.get_assembly();
8797 fs_visitor::emit_cs_work_group_id_setup()
8799 assert(stage
== MESA_SHADER_COMPUTE
|| stage
== MESA_SHADER_KERNEL
);
8801 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
8803 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
8804 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
8805 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
8807 bld
.MOV(*reg
, r0_1
);
8808 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
8809 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
8815 brw_cs_push_const_total_size(const struct brw_cs_prog_data
*cs_prog_data
,
8818 assert(cs_prog_data
->push
.per_thread
.size
% REG_SIZE
== 0);
8819 assert(cs_prog_data
->push
.cross_thread
.size
% REG_SIZE
== 0);
8820 return cs_prog_data
->push
.per_thread
.size
* threads
+
8821 cs_prog_data
->push
.cross_thread
.size
;
8825 fill_push_const_block_info(struct brw_push_const_block
*block
, unsigned dwords
)
8827 block
->dwords
= dwords
;
8828 block
->regs
= DIV_ROUND_UP(dwords
, 8);
8829 block
->size
= block
->regs
* 32;
8833 cs_fill_push_const_info(const struct gen_device_info
*devinfo
,
8834 struct brw_cs_prog_data
*cs_prog_data
)
8836 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
8837 int subgroup_id_index
= get_subgroup_id_param_index(prog_data
);
8838 bool cross_thread_supported
= devinfo
->gen
> 7 || devinfo
->is_haswell
;
8840 /* The thread ID should be stored in the last param dword */
8841 assert(subgroup_id_index
== -1 ||
8842 subgroup_id_index
== (int)prog_data
->nr_params
- 1);
8844 unsigned cross_thread_dwords
, per_thread_dwords
;
8845 if (!cross_thread_supported
) {
8846 cross_thread_dwords
= 0u;
8847 per_thread_dwords
= prog_data
->nr_params
;
8848 } else if (subgroup_id_index
>= 0) {
8849 /* Fill all but the last register with cross-thread payload */
8850 cross_thread_dwords
= 8 * (subgroup_id_index
/ 8);
8851 per_thread_dwords
= prog_data
->nr_params
- cross_thread_dwords
;
8852 assert(per_thread_dwords
> 0 && per_thread_dwords
<= 8);
8854 /* Fill all data using cross-thread payload */
8855 cross_thread_dwords
= prog_data
->nr_params
;
8856 per_thread_dwords
= 0u;
8859 fill_push_const_block_info(&cs_prog_data
->push
.cross_thread
, cross_thread_dwords
);
8860 fill_push_const_block_info(&cs_prog_data
->push
.per_thread
, per_thread_dwords
);
8862 assert(cs_prog_data
->push
.cross_thread
.dwords
% 8 == 0 ||
8863 cs_prog_data
->push
.per_thread
.size
== 0);
8864 assert(cs_prog_data
->push
.cross_thread
.dwords
+
8865 cs_prog_data
->push
.per_thread
.dwords
==
8866 prog_data
->nr_params
);
8870 filter_simd(const nir_instr
*instr
, const void *_options
)
8872 if (instr
->type
!= nir_instr_type_intrinsic
)
8875 switch (nir_instr_as_intrinsic(instr
)->intrinsic
) {
8876 case nir_intrinsic_load_simd_width_intel
:
8877 case nir_intrinsic_load_subgroup_id
:
8885 static nir_ssa_def
*
8886 lower_simd(nir_builder
*b
, nir_instr
*instr
, void *options
)
8888 uintptr_t simd_width
= (uintptr_t)options
;
8890 switch (nir_instr_as_intrinsic(instr
)->intrinsic
) {
8891 case nir_intrinsic_load_simd_width_intel
:
8892 return nir_imm_int(b
, simd_width
);
8894 case nir_intrinsic_load_subgroup_id
:
8895 /* If the whole workgroup fits in one thread, we can lower subgroup_id
8896 * to a constant zero.
8898 if (!b
->shader
->info
.cs
.local_size_variable
) {
8899 unsigned local_workgroup_size
= b
->shader
->info
.cs
.local_size
[0] *
8900 b
->shader
->info
.cs
.local_size
[1] *
8901 b
->shader
->info
.cs
.local_size
[2];
8902 if (local_workgroup_size
<= simd_width
)
8903 return nir_imm_int(b
, 0);
8913 brw_nir_lower_simd(nir_shader
*nir
, unsigned dispatch_width
)
8915 nir_shader_lower_instructions(nir
, filter_simd
, lower_simd
,
8916 (void *)(uintptr_t)dispatch_width
);
8920 compile_cs_to_nir(const struct brw_compiler
*compiler
,
8922 const struct brw_cs_prog_key
*key
,
8923 const nir_shader
*src_shader
,
8924 unsigned dispatch_width
)
8926 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
8927 brw_nir_apply_key(shader
, compiler
, &key
->base
, dispatch_width
, true);
8929 NIR_PASS_V(shader
, brw_nir_lower_simd
, dispatch_width
);
8931 /* Clean up after the local index and ID calculations. */
8932 NIR_PASS_V(shader
, nir_opt_constant_folding
);
8933 NIR_PASS_V(shader
, nir_opt_dce
);
8935 brw_postprocess_nir(shader
, compiler
, true);
8941 brw_compile_cs(const struct brw_compiler
*compiler
, void *log_data
,
8943 const struct brw_cs_prog_key
*key
,
8944 struct brw_cs_prog_data
*prog_data
,
8945 const nir_shader
*src_shader
,
8946 int shader_time_index
,
8947 struct brw_compile_stats
*stats
,
8950 prog_data
->base
.total_shared
= src_shader
->info
.cs
.shared_size
;
8951 prog_data
->slm_size
= src_shader
->num_shared
;
8953 /* Generate code for all the possible SIMD variants. */
8956 unsigned min_dispatch_width
;
8957 unsigned max_dispatch_width
;
8959 if (src_shader
->info
.cs
.local_size_variable
) {
8960 generate_all
= true;
8961 min_dispatch_width
= 8;
8962 max_dispatch_width
= 32;
8964 generate_all
= false;
8965 prog_data
->local_size
[0] = src_shader
->info
.cs
.local_size
[0];
8966 prog_data
->local_size
[1] = src_shader
->info
.cs
.local_size
[1];
8967 prog_data
->local_size
[2] = src_shader
->info
.cs
.local_size
[2];
8968 unsigned local_workgroup_size
= prog_data
->local_size
[0] *
8969 prog_data
->local_size
[1] *
8970 prog_data
->local_size
[2];
8972 /* Limit max_threads to 64 for the GPGPU_WALKER command */
8973 const uint32_t max_threads
= MIN2(64, compiler
->devinfo
->max_cs_threads
);
8974 min_dispatch_width
= util_next_power_of_two(
8975 MAX2(8, DIV_ROUND_UP(local_workgroup_size
, max_threads
)));
8976 assert(min_dispatch_width
<= 32);
8977 max_dispatch_width
= 32;
8980 if ((int)key
->base
.subgroup_size_type
>= (int)BRW_SUBGROUP_SIZE_REQUIRE_8
) {
8981 /* These enum values are expressly chosen to be equal to the subgroup
8982 * size that they require.
8984 const unsigned required_dispatch_width
=
8985 (unsigned)key
->base
.subgroup_size_type
;
8986 assert(required_dispatch_width
== 8 ||
8987 required_dispatch_width
== 16 ||
8988 required_dispatch_width
== 32);
8989 if (required_dispatch_width
< min_dispatch_width
||
8990 required_dispatch_width
> max_dispatch_width
) {
8992 *error_str
= ralloc_strdup(mem_ctx
,
8993 "Cannot satisfy explicit subgroup size");
8997 min_dispatch_width
= max_dispatch_width
= required_dispatch_width
;
9000 assert(min_dispatch_width
<= max_dispatch_width
);
9002 fs_visitor
*v8
= NULL
, *v16
= NULL
, *v32
= NULL
;
9003 fs_visitor
*v
= NULL
;
9005 if (likely(!(INTEL_DEBUG
& DEBUG_NO8
)) &&
9006 min_dispatch_width
<= 8 && max_dispatch_width
>= 8) {
9007 nir_shader
*nir8
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
9009 v8
= new fs_visitor(compiler
, log_data
, mem_ctx
, &key
->base
,
9011 nir8
, 8, shader_time_index
);
9012 if (!v8
->run_cs(true /* allow_spilling */)) {
9014 *error_str
= ralloc_strdup(mem_ctx
, v8
->fail_msg
);
9019 /* We should always be able to do SIMD32 for compute shaders */
9020 assert(v8
->max_dispatch_width
>= 32);
9023 prog_data
->prog_mask
|= 1 << 0;
9024 if (v8
->spilled_any_registers
)
9025 prog_data
->prog_spilled
|= 1 << 0;
9026 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
9029 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
9030 (generate_all
|| !prog_data
->prog_spilled
) &&
9031 min_dispatch_width
<= 16 && max_dispatch_width
>= 16) {
9032 /* Try a SIMD16 compile */
9033 nir_shader
*nir16
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
9035 v16
= new fs_visitor(compiler
, log_data
, mem_ctx
, &key
->base
,
9037 nir16
, 16, shader_time_index
);
9039 v16
->import_uniforms(v8
);
9041 const bool allow_spilling
= generate_all
|| v
== NULL
;
9042 if (!v16
->run_cs(allow_spilling
)) {
9043 compiler
->shader_perf_log(log_data
,
9044 "SIMD16 shader failed to compile: %s",
9049 *error_str
= ralloc_asprintf(
9050 mem_ctx
, "Not enough threads for SIMD8 and "
9051 "couldn't generate SIMD16: %s", v16
->fail_msg
);
9057 /* We should always be able to do SIMD32 for compute shaders */
9058 assert(v16
->max_dispatch_width
>= 32);
9061 prog_data
->prog_mask
|= 1 << 1;
9062 if (v16
->spilled_any_registers
)
9063 prog_data
->prog_spilled
|= 1 << 1;
9064 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
9068 /* The SIMD32 is only enabled for cases it is needed unless forced.
9070 * TODO: Use performance_analysis and drop this boolean.
9072 const bool needs_32
= v
== NULL
||
9073 (INTEL_DEBUG
& DEBUG_DO32
) ||
9076 if (likely(!(INTEL_DEBUG
& DEBUG_NO32
)) &&
9077 (generate_all
|| !prog_data
->prog_spilled
) &&
9079 min_dispatch_width
<= 32 && max_dispatch_width
>= 32) {
9080 /* Try a SIMD32 compile */
9081 nir_shader
*nir32
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
9083 v32
= new fs_visitor(compiler
, log_data
, mem_ctx
, &key
->base
,
9085 nir32
, 32, shader_time_index
);
9087 v32
->import_uniforms(v8
);
9089 v32
->import_uniforms(v16
);
9091 const bool allow_spilling
= generate_all
|| v
== NULL
;
9092 if (!v32
->run_cs(allow_spilling
)) {
9093 compiler
->shader_perf_log(log_data
,
9094 "SIMD32 shader failed to compile: %s",
9098 assert(v16
== NULL
);
9100 *error_str
= ralloc_asprintf(
9101 mem_ctx
, "Not enough threads for SIMD16 and "
9102 "couldn't generate SIMD32: %s", v32
->fail_msg
);
9109 prog_data
->prog_mask
|= 1 << 2;
9110 if (v32
->spilled_any_registers
)
9111 prog_data
->prog_spilled
|= 1 << 2;
9112 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
9116 if (unlikely(!v
&& (INTEL_DEBUG
& (DEBUG_NO8
| DEBUG_NO16
| DEBUG_NO32
)))) {
9119 ralloc_strdup(mem_ctx
,
9120 "Cannot satisfy INTEL_DEBUG flags SIMD restrictions");
9127 const unsigned *ret
= NULL
;
9129 fs_generator
g(compiler
, log_data
, mem_ctx
, &prog_data
->base
,
9130 v
->runtime_check_aads_emit
, MESA_SHADER_COMPUTE
);
9131 if (INTEL_DEBUG
& DEBUG_CS
) {
9132 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
9133 src_shader
->info
.label
?
9134 src_shader
->info
.label
: "unnamed",
9135 src_shader
->info
.name
);
9136 g
.enable_debug(name
);
9140 if (prog_data
->prog_mask
& (1 << 0)) {
9142 prog_data
->prog_offset
[0] =
9143 g
.generate_code(v8
->cfg
, 8, v8
->shader_stats
,
9144 v8
->performance_analysis
.require(), stats
);
9145 stats
= stats
? stats
+ 1 : NULL
;
9148 if (prog_data
->prog_mask
& (1 << 1)) {
9150 prog_data
->prog_offset
[1] =
9151 g
.generate_code(v16
->cfg
, 16, v16
->shader_stats
,
9152 v16
->performance_analysis
.require(), stats
);
9153 stats
= stats
? stats
+ 1 : NULL
;
9156 if (prog_data
->prog_mask
& (1 << 2)) {
9158 prog_data
->prog_offset
[2] =
9159 g
.generate_code(v32
->cfg
, 32, v32
->shader_stats
,
9160 v32
->performance_analysis
.require(), stats
);
9161 stats
= stats
? stats
+ 1 : NULL
;
9164 /* Only one dispatch width will be valid, and will be at offset 0,
9165 * which is already the default value of prog_offset_* fields.
9167 prog_data
->prog_mask
= 1 << (v
->dispatch_width
/ 16);
9168 g
.generate_code(v
->cfg
, v
->dispatch_width
, v
->shader_stats
,
9169 v
->performance_analysis
.require(), stats
);
9172 ret
= g
.get_assembly();
9182 brw_cs_simd_size_for_group_size(const struct gen_device_info
*devinfo
,
9183 const struct brw_cs_prog_data
*cs_prog_data
,
9184 unsigned group_size
)
9186 const unsigned mask
= cs_prog_data
->prog_mask
;
9189 static const unsigned simd8
= 1 << 0;
9190 static const unsigned simd16
= 1 << 1;
9191 static const unsigned simd32
= 1 << 2;
9193 if (unlikely(INTEL_DEBUG
& DEBUG_DO32
) && (mask
& simd32
))
9196 /* Limit max_threads to 64 for the GPGPU_WALKER command */
9197 const uint32_t max_threads
= MIN2(64, devinfo
->max_cs_threads
);
9199 if ((mask
& simd8
) && group_size
<= 8 * max_threads
) {
9200 /* Prefer SIMD16 if can do without spilling. Matches logic in
9203 if ((mask
& simd16
) && (~cs_prog_data
->prog_spilled
& simd16
))
9208 if ((mask
& simd16
) && group_size
<= 16 * max_threads
)
9211 assert(mask
& simd32
);
9212 assert(group_size
<= 32 * max_threads
);
9217 * Test the dispatch mask packing assumptions of
9218 * brw_stage_has_packed_dispatch(). Call this from e.g. the top of
9219 * fs_visitor::emit_nir_code() to cause a GPU hang if any shader invocation is
9220 * executed with an unexpected dispatch mask.
9223 brw_fs_test_dispatch_packing(const fs_builder
&bld
)
9225 const gl_shader_stage stage
= bld
.shader
->stage
;
9227 if (brw_stage_has_packed_dispatch(bld
.shader
->devinfo
, stage
,
9228 bld
.shader
->stage_prog_data
)) {
9229 const fs_builder ubld
= bld
.exec_all().group(1, 0);
9230 const fs_reg tmp
= component(bld
.vgrf(BRW_REGISTER_TYPE_UD
), 0);
9231 const fs_reg mask
= (stage
== MESA_SHADER_FRAGMENT
? brw_vmask_reg() :
9234 ubld
.ADD(tmp
, mask
, brw_imm_ud(1));
9235 ubld
.AND(tmp
, mask
, tmp
);
9237 /* This will loop forever if the dispatch mask doesn't have the expected
9238 * form '2^n-1', in which case tmp will be non-zero.
9240 bld
.emit(BRW_OPCODE_DO
);
9241 bld
.CMP(bld
.null_reg_ud(), tmp
, brw_imm_ud(0), BRW_CONDITIONAL_NZ
);
9242 set_predicate(BRW_PREDICATE_NORMAL
, bld
.emit(BRW_OPCODE_WHILE
));
9247 fs_visitor::workgroup_size() const
9249 assert(stage
== MESA_SHADER_COMPUTE
);
9250 const struct brw_cs_prog_data
*cs
= brw_cs_prog_data(prog_data
);
9251 return cs
->local_size
[0] * cs
->local_size
[1] * cs
->local_size
[2];