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"
35 #include "brw_vec4_gs_visitor.h"
37 #include "brw_dead_control_flow.h"
38 #include "dev/gen_debug.h"
39 #include "compiler/glsl_types.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "program/prog_parameter.h"
42 #include "util/u_math.h"
46 static unsigned get_lowered_simd_width(const struct gen_device_info
*devinfo
,
50 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
51 const fs_reg
*src
, unsigned sources
)
53 memset((void*)this, 0, sizeof(*this));
55 this->src
= new fs_reg
[MAX2(sources
, 3)];
56 for (unsigned i
= 0; i
< sources
; i
++)
57 this->src
[i
] = src
[i
];
59 this->opcode
= opcode
;
61 this->sources
= sources
;
62 this->exec_size
= exec_size
;
65 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
67 assert(this->exec_size
!= 0);
69 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
71 /* This will be the case for almost all instructions. */
78 this->size_written
= dst
.component_size(exec_size
);
81 this->size_written
= 0;
85 unreachable("Invalid destination register file");
88 this->writes_accumulator
= false;
93 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
96 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
98 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
101 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
103 init(opcode
, exec_size
, dst
, NULL
, 0);
106 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
109 const fs_reg src
[1] = { src0
};
110 init(opcode
, exec_size
, dst
, src
, 1);
113 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
114 const fs_reg
&src0
, const fs_reg
&src1
)
116 const fs_reg src
[2] = { src0
, src1
};
117 init(opcode
, exec_size
, dst
, src
, 2);
120 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
121 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
123 const fs_reg src
[3] = { src0
, src1
, src2
};
124 init(opcode
, exec_size
, dst
, src
, 3);
127 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
128 const fs_reg src
[], unsigned sources
)
130 init(opcode
, exec_width
, dst
, src
, sources
);
133 fs_inst::fs_inst(const fs_inst
&that
)
135 memcpy((void*)this, &that
, sizeof(that
));
137 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
139 for (unsigned i
= 0; i
< that
.sources
; i
++)
140 this->src
[i
] = that
.src
[i
];
149 fs_inst::resize_sources(uint8_t num_sources
)
151 if (this->sources
!= num_sources
) {
152 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
154 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
155 src
[i
] = this->src
[i
];
159 this->sources
= num_sources
;
164 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
166 const fs_reg
&surf_index
,
167 const fs_reg
&varying_offset
,
168 uint32_t const_offset
)
170 /* We have our constant surface use a pitch of 4 bytes, so our index can
171 * be any component of a vector, and then we load 4 contiguous
172 * components starting from that.
174 * We break down the const_offset to a portion added to the variable offset
175 * and a portion done using fs_reg::offset, which means that if you have
176 * GLSL using something like "uniform vec4 a[20]; gl_FragColor = a[i]",
177 * we'll temporarily generate 4 vec4 loads from offset i * 4, and CSE can
178 * later notice that those loads are all the same and eliminate the
181 fs_reg vec4_offset
= vgrf(glsl_type::uint_type
);
182 bld
.ADD(vec4_offset
, varying_offset
, brw_imm_ud(const_offset
& ~0xf));
184 /* The pull load message will load a vec4 (16 bytes). If we are loading
185 * a double this means we are only loading 2 elements worth of data.
186 * We also want to use a 32-bit data type for the dst of the load operation
187 * so other parts of the driver don't get confused about the size of the
190 fs_reg vec4_result
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
191 fs_inst
*inst
= bld
.emit(FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
,
192 vec4_result
, surf_index
, vec4_offset
);
193 inst
->size_written
= 4 * vec4_result
.component_size(inst
->exec_size
);
195 shuffle_from_32bit_read(bld
, dst
, vec4_result
,
196 (const_offset
& 0xf) / type_sz(dst
.type
), 1);
200 * A helper for MOV generation for fixing up broken hardware SEND dependency
204 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
206 /* The caller always wants uncompressed to emit the minimal extra
207 * dependencies, and to avoid having to deal with aligning its regs to 2.
209 const fs_builder ubld
= bld
.annotate("send dependency resolve")
212 ubld
.MOV(ubld
.null_reg_f(), fs_reg(VGRF
, grf
, BRW_REGISTER_TYPE_F
));
216 fs_inst::is_send_from_grf() const
219 case SHADER_OPCODE_SEND
:
220 case SHADER_OPCODE_SHADER_TIME_ADD
:
221 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
222 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
223 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
224 case SHADER_OPCODE_URB_WRITE_SIMD8
:
225 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
226 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
227 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
228 case SHADER_OPCODE_URB_READ_SIMD8
:
229 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
231 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
232 return src
[1].file
== VGRF
;
233 case FS_OPCODE_FB_WRITE
:
234 case FS_OPCODE_FB_READ
:
235 return src
[0].file
== VGRF
;
238 return src
[0].file
== VGRF
;
245 fs_inst::is_control_source(unsigned arg
) const
248 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
249 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
250 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
:
253 case SHADER_OPCODE_BROADCAST
:
254 case SHADER_OPCODE_SHUFFLE
:
255 case SHADER_OPCODE_QUAD_SWIZZLE
:
256 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
257 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
258 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
259 case SHADER_OPCODE_GET_BUFFER_SIZE
:
262 case SHADER_OPCODE_MOV_INDIRECT
:
263 case SHADER_OPCODE_CLUSTER_BROADCAST
:
264 case SHADER_OPCODE_TEX
:
266 case SHADER_OPCODE_TXD
:
267 case SHADER_OPCODE_TXF
:
268 case SHADER_OPCODE_TXF_LZ
:
269 case SHADER_OPCODE_TXF_CMS
:
270 case SHADER_OPCODE_TXF_CMS_W
:
271 case SHADER_OPCODE_TXF_UMS
:
272 case SHADER_OPCODE_TXF_MCS
:
273 case SHADER_OPCODE_TXL
:
274 case SHADER_OPCODE_TXL_LZ
:
275 case SHADER_OPCODE_TXS
:
276 case SHADER_OPCODE_LOD
:
277 case SHADER_OPCODE_TG4
:
278 case SHADER_OPCODE_TG4_OFFSET
:
279 case SHADER_OPCODE_SAMPLEINFO
:
280 return arg
== 1 || arg
== 2;
282 case SHADER_OPCODE_SEND
:
283 return arg
== 0 || arg
== 1;
291 * Returns true if this instruction's sources and destinations cannot
292 * safely be the same register.
294 * In most cases, a register can be written over safely by the same
295 * instruction that is its last use. For a single instruction, the
296 * sources are dereferenced before writing of the destination starts
299 * However, there are a few cases where this can be problematic:
301 * - Virtual opcodes that translate to multiple instructions in the
302 * code generator: if src == dst and one instruction writes the
303 * destination before a later instruction reads the source, then
304 * src will have been clobbered.
306 * - SIMD16 compressed instructions with certain regioning (see below).
308 * The register allocator uses this information to set up conflicts between
309 * GRF sources and the destination.
312 fs_inst::has_source_and_destination_hazard() const
315 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
316 /* Multiple partial writes to the destination */
318 case SHADER_OPCODE_SHUFFLE
:
319 /* This instruction returns an arbitrary channel from the source and
320 * gets split into smaller instructions in the generator. It's possible
321 * that one of the instructions will read from a channel corresponding
322 * to an earlier instruction.
324 case SHADER_OPCODE_SEL_EXEC
:
325 /* This is implemented as
327 * mov(16) g4<1>D 0D { align1 WE_all 1H };
328 * mov(16) g4<1>D g5<8,8,1>D { align1 1H }
330 * Because the source is only read in the second instruction, the first
331 * may stomp all over it.
334 case SHADER_OPCODE_QUAD_SWIZZLE
:
336 case BRW_SWIZZLE_XXXX
:
337 case BRW_SWIZZLE_YYYY
:
338 case BRW_SWIZZLE_ZZZZ
:
339 case BRW_SWIZZLE_WWWW
:
340 case BRW_SWIZZLE_XXZZ
:
341 case BRW_SWIZZLE_YYWW
:
342 case BRW_SWIZZLE_XYXY
:
343 case BRW_SWIZZLE_ZWZW
:
344 /* These can be implemented as a single Align1 region on all
345 * platforms, so there's never a hazard between source and
346 * destination. C.f. fs_generator::generate_quad_swizzle().
350 return !is_uniform(src
[0]);
353 /* The SIMD16 compressed instruction
355 * add(16) g4<1>F g4<8,8,1>F g6<8,8,1>F
357 * is actually decoded in hardware as:
359 * add(8) g4<1>F g4<8,8,1>F g6<8,8,1>F
360 * add(8) g5<1>F g5<8,8,1>F g7<8,8,1>F
362 * Which is safe. However, if we have uniform accesses
363 * happening, we get into trouble:
365 * add(8) g4<1>F g4<0,1,0>F g6<8,8,1>F
366 * add(8) g5<1>F g4<0,1,0>F g7<8,8,1>F
368 * Now our destination for the first instruction overwrote the
369 * second instruction's src0, and we get garbage for those 8
370 * pixels. There's a similar issue for the pre-gen6
371 * pixel_x/pixel_y, which are registers of 16-bit values and thus
372 * would get stomped by the first decode as well.
374 if (exec_size
== 16) {
375 for (int i
= 0; i
< sources
; i
++) {
376 if (src
[i
].file
== VGRF
&& (src
[i
].stride
== 0 ||
377 src
[i
].type
== BRW_REGISTER_TYPE_UW
||
378 src
[i
].type
== BRW_REGISTER_TYPE_W
||
379 src
[i
].type
== BRW_REGISTER_TYPE_UB
||
380 src
[i
].type
== BRW_REGISTER_TYPE_B
)) {
390 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
392 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
395 fs_reg reg
= this->src
[0];
396 if (reg
.file
!= VGRF
|| reg
.offset
!= 0 || reg
.stride
!= 1)
399 if (grf_alloc
.sizes
[reg
.nr
] * REG_SIZE
!= this->size_written
)
402 for (int i
= 0; i
< this->sources
; i
++) {
403 reg
.type
= this->src
[i
].type
;
404 if (!this->src
[i
].equals(reg
))
407 if (i
< this->header_size
) {
408 reg
.offset
+= REG_SIZE
;
410 reg
= horiz_offset(reg
, this->exec_size
);
418 fs_inst::can_do_source_mods(const struct gen_device_info
*devinfo
) const
420 if (devinfo
->gen
== 6 && is_math())
423 if (is_send_from_grf())
426 if (!backend_instruction::can_do_source_mods())
433 fs_inst::can_do_cmod()
435 if (!backend_instruction::can_do_cmod())
438 /* The accumulator result appears to get used for the conditional modifier
439 * generation. When negating a UD value, there is a 33rd bit generated for
440 * the sign in the accumulator value, so now you can't check, for example,
441 * equality with a 32-bit value. See piglit fs-op-neg-uvec4.
443 for (unsigned i
= 0; i
< sources
; i
++) {
444 if (type_is_unsigned_int(src
[i
].type
) && src
[i
].negate
)
452 fs_inst::can_change_types() const
454 return dst
.type
== src
[0].type
&&
455 !src
[0].abs
&& !src
[0].negate
&& !saturate
&&
456 (opcode
== BRW_OPCODE_MOV
||
457 (opcode
== BRW_OPCODE_SEL
&&
458 dst
.type
== src
[1].type
&&
459 predicate
!= BRW_PREDICATE_NONE
&&
460 !src
[1].abs
&& !src
[1].negate
));
466 memset((void*)this, 0, sizeof(*this));
467 type
= BRW_REGISTER_TYPE_UD
;
471 /** Generic unset register constructor. */
475 this->file
= BAD_FILE
;
478 fs_reg::fs_reg(struct ::brw_reg reg
) :
483 if (this->file
== IMM
&&
484 (this->type
!= BRW_REGISTER_TYPE_V
&&
485 this->type
!= BRW_REGISTER_TYPE_UV
&&
486 this->type
!= BRW_REGISTER_TYPE_VF
)) {
492 fs_reg::equals(const fs_reg
&r
) const
494 return (this->backend_reg::equals(r
) &&
499 fs_reg::negative_equals(const fs_reg
&r
) const
501 return (this->backend_reg::negative_equals(r
) &&
506 fs_reg::is_contiguous() const
512 fs_reg::component_size(unsigned width
) const
514 const unsigned stride
= ((file
!= ARF
&& file
!= FIXED_GRF
) ? this->stride
:
517 return MAX2(width
* stride
, 1) * type_sz(type
);
521 type_size_scalar(const struct glsl_type
*type
, bool bindless
)
523 unsigned int size
, i
;
525 switch (type
->base_type
) {
528 case GLSL_TYPE_FLOAT
:
530 return type
->components();
531 case GLSL_TYPE_UINT16
:
532 case GLSL_TYPE_INT16
:
533 case GLSL_TYPE_FLOAT16
:
534 return DIV_ROUND_UP(type
->components(), 2);
535 case GLSL_TYPE_UINT8
:
537 return DIV_ROUND_UP(type
->components(), 4);
538 case GLSL_TYPE_DOUBLE
:
539 case GLSL_TYPE_UINT64
:
540 case GLSL_TYPE_INT64
:
541 return type
->components() * 2;
542 case GLSL_TYPE_ARRAY
:
543 return type_size_scalar(type
->fields
.array
, bindless
) * type
->length
;
544 case GLSL_TYPE_STRUCT
:
545 case GLSL_TYPE_INTERFACE
:
547 for (i
= 0; i
< type
->length
; i
++) {
548 size
+= type_size_scalar(type
->fields
.structure
[i
].type
, bindless
);
551 case GLSL_TYPE_SAMPLER
:
552 case GLSL_TYPE_IMAGE
:
554 return type
->components() * 2;
555 case GLSL_TYPE_ATOMIC_UINT
:
556 /* Samplers, atomics, and images take up no register space, since
557 * they're baked in at link time.
560 case GLSL_TYPE_SUBROUTINE
:
563 case GLSL_TYPE_ERROR
:
564 case GLSL_TYPE_FUNCTION
:
565 unreachable("not reached");
572 * Create a MOV to read the timestamp register.
574 * The caller is responsible for emitting the MOV. The return value is
575 * the destination of the MOV, with extra parameters set.
578 fs_visitor::get_timestamp(const fs_builder
&bld
)
580 assert(devinfo
->gen
>= 7);
582 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
585 BRW_REGISTER_TYPE_UD
));
587 fs_reg dst
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
589 /* We want to read the 3 fields we care about even if it's not enabled in
592 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
598 fs_visitor::emit_shader_time_begin()
600 /* We want only the low 32 bits of the timestamp. Since it's running
601 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
602 * which is plenty of time for our purposes. It is identical across the
603 * EUs, but since it's tracking GPU core speed it will increment at a
604 * varying rate as render P-states change.
606 shader_start_time
= component(
607 get_timestamp(bld
.annotate("shader time start")), 0);
611 fs_visitor::emit_shader_time_end()
613 /* Insert our code just before the final SEND with EOT. */
614 exec_node
*end
= this->instructions
.get_tail();
615 assert(end
&& ((fs_inst
*) end
)->eot
);
616 const fs_builder ibld
= bld
.annotate("shader time end")
617 .exec_all().at(NULL
, end
);
618 const fs_reg timestamp
= get_timestamp(ibld
);
620 /* We only use the low 32 bits of the timestamp - see
621 * emit_shader_time_begin()).
623 * We could also check if render P-states have changed (or anything
624 * else that might disrupt timing) by setting smear to 2 and checking if
625 * that field is != 0.
627 const fs_reg shader_end_time
= component(timestamp
, 0);
629 /* Check that there weren't any timestamp reset events (assuming these
630 * were the only two timestamp reads that happened).
632 const fs_reg reset
= component(timestamp
, 2);
633 set_condmod(BRW_CONDITIONAL_Z
,
634 ibld
.AND(ibld
.null_reg_ud(), reset
, brw_imm_ud(1u)));
635 ibld
.IF(BRW_PREDICATE_NORMAL
);
637 fs_reg start
= shader_start_time
;
639 const fs_reg diff
= component(fs_reg(VGRF
, alloc
.allocate(1),
640 BRW_REGISTER_TYPE_UD
),
642 const fs_builder cbld
= ibld
.group(1, 0);
643 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
645 /* If there were no instructions between the two timestamp gets, the diff
646 * is 2 cycles. Remove that overhead, so I can forget about that when
647 * trying to determine the time taken for single instructions.
649 cbld
.ADD(diff
, diff
, brw_imm_ud(-2u));
650 SHADER_TIME_ADD(cbld
, 0, diff
);
651 SHADER_TIME_ADD(cbld
, 1, brw_imm_ud(1u));
652 ibld
.emit(BRW_OPCODE_ELSE
);
653 SHADER_TIME_ADD(cbld
, 2, brw_imm_ud(1u));
654 ibld
.emit(BRW_OPCODE_ENDIF
);
658 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
659 int shader_time_subindex
,
662 int index
= shader_time_index
* 3 + shader_time_subindex
;
663 struct brw_reg offset
= brw_imm_d(index
* BRW_SHADER_TIME_STRIDE
);
666 if (dispatch_width
== 8)
667 payload
= vgrf(glsl_type::uvec2_type
);
669 payload
= vgrf(glsl_type::uint_type
);
671 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
675 fs_visitor::vfail(const char *format
, va_list va
)
684 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
685 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
687 this->fail_msg
= msg
;
690 fprintf(stderr
, "%s", msg
);
695 fs_visitor::fail(const char *format
, ...)
699 va_start(va
, format
);
705 * Mark this program as impossible to compile with dispatch width greater
708 * During the SIMD8 compile (which happens first), we can detect and flag
709 * things that are unsupported in SIMD16+ mode, so the compiler can skip the
710 * SIMD16+ compile altogether.
712 * During a compile of dispatch width greater than n (if one happens anyway),
713 * this just calls fail().
716 fs_visitor::limit_dispatch_width(unsigned n
, const char *msg
)
718 if (dispatch_width
> n
) {
721 max_dispatch_width
= n
;
722 compiler
->shader_perf_log(log_data
,
723 "Shader dispatch width limited to SIMD%d: %s",
729 * Returns true if the instruction has a flag that means it won't
730 * update an entire destination register.
732 * For example, dead code elimination and live variable analysis want to know
733 * when a write to a variable screens off any preceding values that were in
737 fs_inst::is_partial_write() const
739 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
740 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
741 !this->dst
.is_contiguous() ||
742 this->dst
.offset
% REG_SIZE
!= 0);
746 fs_inst::components_read(unsigned i
) const
748 /* Return zero if the source is not present. */
749 if (src
[i
].file
== BAD_FILE
)
753 case FS_OPCODE_LINTERP
:
759 case FS_OPCODE_PIXEL_X
:
760 case FS_OPCODE_PIXEL_Y
:
764 case FS_OPCODE_FB_WRITE_LOGICAL
:
765 assert(src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
766 /* First/second FB write color. */
768 return src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
772 case SHADER_OPCODE_TEX_LOGICAL
:
773 case SHADER_OPCODE_TXD_LOGICAL
:
774 case SHADER_OPCODE_TXF_LOGICAL
:
775 case SHADER_OPCODE_TXL_LOGICAL
:
776 case SHADER_OPCODE_TXS_LOGICAL
:
777 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
778 case FS_OPCODE_TXB_LOGICAL
:
779 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
780 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
781 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
782 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
783 case SHADER_OPCODE_LOD_LOGICAL
:
784 case SHADER_OPCODE_TG4_LOGICAL
:
785 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
786 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
787 assert(src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
&&
788 src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
789 /* Texture coordinates. */
790 if (i
== TEX_LOGICAL_SRC_COORDINATE
)
791 return src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
792 /* Texture derivatives. */
793 else if ((i
== TEX_LOGICAL_SRC_LOD
|| i
== TEX_LOGICAL_SRC_LOD2
) &&
794 opcode
== SHADER_OPCODE_TXD_LOGICAL
)
795 return src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
796 /* Texture offset. */
797 else if (i
== TEX_LOGICAL_SRC_TG4_OFFSET
)
800 else if (i
== TEX_LOGICAL_SRC_MCS
&& opcode
== SHADER_OPCODE_TXF_CMS_W_LOGICAL
)
805 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
806 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
807 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
);
808 /* Surface coordinates. */
809 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
810 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
811 /* Surface operation source (ignored for reads). */
812 else if (i
== SURFACE_LOGICAL_SRC_DATA
)
817 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
818 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
819 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
820 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
821 /* Surface coordinates. */
822 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
823 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
824 /* Surface operation source. */
825 else if (i
== SURFACE_LOGICAL_SRC_DATA
)
826 return src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
830 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
831 assert(src
[2].file
== IMM
);
834 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
835 assert(src
[2].file
== IMM
);
836 return i
== 1 ? src
[2].ud
: 1;
838 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
839 assert(src
[2].file
== IMM
);
842 const unsigned op
= src
[2].ud
;
857 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
858 assert(src
[2].file
== IMM
);
861 const unsigned op
= src
[2].ud
;
862 return op
== BRW_AOP_FCMPWR
? 2 : 1;
867 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
868 /* Scattered logical opcodes use the following params:
869 * src[0] Surface coordinates
870 * src[1] Surface operation source (ignored for reads)
872 * src[3] IMM with always 1 dimension.
873 * src[4] IMM with arg bitsize for scattered read/write 8, 16, 32
875 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
876 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
877 return i
== SURFACE_LOGICAL_SRC_DATA
? 0 : 1;
879 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
880 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
881 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
884 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
885 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
886 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
887 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
888 const unsigned op
= src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
889 /* Surface coordinates. */
890 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
891 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
892 /* Surface operation source. */
893 else if (i
== SURFACE_LOGICAL_SRC_DATA
&& op
== BRW_AOP_CMPWR
)
895 else if (i
== SURFACE_LOGICAL_SRC_DATA
&&
896 (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
|| op
== BRW_AOP_PREDEC
))
901 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
902 return (i
== 0 ? 2 : 1);
904 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
: {
905 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
906 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
907 const unsigned op
= src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
908 /* Surface coordinates. */
909 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
910 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
911 /* Surface operation source. */
912 else if (i
== SURFACE_LOGICAL_SRC_DATA
&& op
== BRW_AOP_FCMPWR
)
924 fs_inst::size_read(int arg
) const
927 case SHADER_OPCODE_SEND
:
929 return mlen
* REG_SIZE
;
930 } else if (arg
== 3) {
931 return ex_mlen
* REG_SIZE
;
935 case FS_OPCODE_FB_WRITE
:
936 case FS_OPCODE_REP_FB_WRITE
:
939 return src
[0].file
== BAD_FILE
? 0 : 2 * REG_SIZE
;
941 return mlen
* REG_SIZE
;
945 case FS_OPCODE_FB_READ
:
946 case SHADER_OPCODE_URB_WRITE_SIMD8
:
947 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
948 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
949 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
950 case SHADER_OPCODE_URB_READ_SIMD8
:
951 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
952 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
953 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
955 return mlen
* REG_SIZE
;
958 case FS_OPCODE_SET_SAMPLE_ID
:
963 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
964 /* The payload is actually stored in src1 */
966 return mlen
* REG_SIZE
;
969 case FS_OPCODE_LINTERP
:
974 case SHADER_OPCODE_LOAD_PAYLOAD
:
975 if (arg
< this->header_size
)
979 case CS_OPCODE_CS_TERMINATE
:
980 case SHADER_OPCODE_BARRIER
:
983 case SHADER_OPCODE_MOV_INDIRECT
:
985 assert(src
[2].file
== IMM
);
991 if (is_tex() && arg
== 0 && src
[0].file
== VGRF
)
992 return mlen
* REG_SIZE
;
996 switch (src
[arg
].file
) {
999 return components_read(arg
) * type_sz(src
[arg
].type
);
1005 return components_read(arg
) * src
[arg
].component_size(exec_size
);
1007 unreachable("MRF registers are not allowed as sources");
1013 /* Return the subset of flag registers that an instruction could
1014 * potentially read or write based on the execution controls and flag
1015 * subregister number of the instruction.
1018 flag_mask(const fs_inst
*inst
)
1020 const unsigned start
= inst
->flag_subreg
* 16 + inst
->group
;
1021 const unsigned end
= start
+ inst
->exec_size
;
1022 return ((1 << DIV_ROUND_UP(end
, 8)) - 1) & ~((1 << (start
/ 8)) - 1);
1026 bit_mask(unsigned n
)
1028 return (n
>= CHAR_BIT
* sizeof(bit_mask(n
)) ? ~0u : (1u << n
) - 1);
1032 flag_mask(const fs_reg
&r
, unsigned sz
)
1034 if (r
.file
== ARF
) {
1035 const unsigned start
= (r
.nr
- BRW_ARF_FLAG
) * 4 + r
.subnr
;
1036 const unsigned end
= start
+ sz
;
1037 return bit_mask(end
) & ~bit_mask(start
);
1045 fs_inst::flags_read(const gen_device_info
*devinfo
) const
1047 if (predicate
== BRW_PREDICATE_ALIGN1_ANYV
||
1048 predicate
== BRW_PREDICATE_ALIGN1_ALLV
) {
1049 /* The vertical predication modes combine corresponding bits from
1050 * f0.0 and f1.0 on Gen7+, and f0.0 and f0.1 on older hardware.
1052 const unsigned shift
= devinfo
->gen
>= 7 ? 4 : 2;
1053 return flag_mask(this) << shift
| flag_mask(this);
1054 } else if (predicate
) {
1055 return flag_mask(this);
1058 for (int i
= 0; i
< sources
; i
++) {
1059 mask
|= flag_mask(src
[i
], size_read(i
));
1066 fs_inst::flags_written() const
1068 if ((conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
1069 opcode
!= BRW_OPCODE_CSEL
&&
1070 opcode
!= BRW_OPCODE_IF
&&
1071 opcode
!= BRW_OPCODE_WHILE
)) ||
1072 opcode
== SHADER_OPCODE_FIND_LIVE_CHANNEL
||
1073 opcode
== FS_OPCODE_FB_WRITE
) {
1074 return flag_mask(this);
1076 return flag_mask(dst
, size_written
);
1081 * Returns how many MRFs an FS opcode will write over.
1083 * Note that this is not the 0 or 1 implied writes in an actual gen
1084 * instruction -- the FS opcodes often generate MOVs in addition.
1087 fs_visitor::implied_mrf_writes(fs_inst
*inst
) const
1089 if (inst
->mlen
== 0)
1092 if (inst
->base_mrf
== -1)
1095 switch (inst
->opcode
) {
1096 case SHADER_OPCODE_RCP
:
1097 case SHADER_OPCODE_RSQ
:
1098 case SHADER_OPCODE_SQRT
:
1099 case SHADER_OPCODE_EXP2
:
1100 case SHADER_OPCODE_LOG2
:
1101 case SHADER_OPCODE_SIN
:
1102 case SHADER_OPCODE_COS
:
1103 return 1 * dispatch_width
/ 8;
1104 case SHADER_OPCODE_POW
:
1105 case SHADER_OPCODE_INT_QUOTIENT
:
1106 case SHADER_OPCODE_INT_REMAINDER
:
1107 return 2 * dispatch_width
/ 8;
1108 case SHADER_OPCODE_TEX
:
1110 case SHADER_OPCODE_TXD
:
1111 case SHADER_OPCODE_TXF
:
1112 case SHADER_OPCODE_TXF_CMS
:
1113 case SHADER_OPCODE_TXF_MCS
:
1114 case SHADER_OPCODE_TG4
:
1115 case SHADER_OPCODE_TG4_OFFSET
:
1116 case SHADER_OPCODE_TXL
:
1117 case SHADER_OPCODE_TXS
:
1118 case SHADER_OPCODE_LOD
:
1119 case SHADER_OPCODE_SAMPLEINFO
:
1121 case FS_OPCODE_FB_WRITE
:
1122 case FS_OPCODE_REP_FB_WRITE
:
1123 return inst
->src
[0].file
== BAD_FILE
? 0 : 2;
1124 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
1125 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
1127 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
:
1129 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
1132 unreachable("not reached");
1137 fs_visitor::vgrf(const glsl_type
*const type
)
1139 int reg_width
= dispatch_width
/ 8;
1141 alloc
.allocate(type_size_scalar(type
, false) * reg_width
),
1142 brw_type_for_base_type(type
));
1145 fs_reg::fs_reg(enum brw_reg_file file
, int nr
)
1150 this->type
= BRW_REGISTER_TYPE_F
;
1151 this->stride
= (file
== UNIFORM
? 0 : 1);
1154 fs_reg::fs_reg(enum brw_reg_file file
, int nr
, enum brw_reg_type type
)
1160 this->stride
= (file
== UNIFORM
? 0 : 1);
1163 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
1164 * This brings in those uniform definitions
1167 fs_visitor::import_uniforms(fs_visitor
*v
)
1169 this->push_constant_loc
= v
->push_constant_loc
;
1170 this->pull_constant_loc
= v
->pull_constant_loc
;
1171 this->uniforms
= v
->uniforms
;
1172 this->subgroup_id
= v
->subgroup_id
;
1176 fs_visitor::emit_fragcoord_interpolation(fs_reg wpos
)
1178 assert(stage
== MESA_SHADER_FRAGMENT
);
1180 /* gl_FragCoord.x */
1181 bld
.MOV(wpos
, this->pixel_x
);
1182 wpos
= offset(wpos
, bld
, 1);
1184 /* gl_FragCoord.y */
1185 bld
.MOV(wpos
, this->pixel_y
);
1186 wpos
= offset(wpos
, bld
, 1);
1188 /* gl_FragCoord.z */
1189 if (devinfo
->gen
>= 6) {
1190 bld
.MOV(wpos
, fetch_payload_reg(bld
, payload
.source_depth_reg
));
1192 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
1193 this->delta_xy
[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL
],
1194 component(interp_reg(VARYING_SLOT_POS
, 2), 0));
1196 wpos
= offset(wpos
, bld
, 1);
1198 /* gl_FragCoord.w: Already set up in emit_interpolation */
1199 bld
.MOV(wpos
, this->wpos_w
);
1202 enum brw_barycentric_mode
1203 brw_barycentric_mode(enum glsl_interp_mode mode
, nir_intrinsic_op op
)
1205 /* Barycentric modes don't make sense for flat inputs. */
1206 assert(mode
!= INTERP_MODE_FLAT
);
1210 case nir_intrinsic_load_barycentric_pixel
:
1211 case nir_intrinsic_load_barycentric_at_offset
:
1212 bary
= BRW_BARYCENTRIC_PERSPECTIVE_PIXEL
;
1214 case nir_intrinsic_load_barycentric_centroid
:
1215 bary
= BRW_BARYCENTRIC_PERSPECTIVE_CENTROID
;
1217 case nir_intrinsic_load_barycentric_sample
:
1218 case nir_intrinsic_load_barycentric_at_sample
:
1219 bary
= BRW_BARYCENTRIC_PERSPECTIVE_SAMPLE
;
1222 unreachable("invalid intrinsic");
1225 if (mode
== INTERP_MODE_NOPERSPECTIVE
)
1228 return (enum brw_barycentric_mode
) bary
;
1232 * Turn one of the two CENTROID barycentric modes into PIXEL mode.
1234 static enum brw_barycentric_mode
1235 centroid_to_pixel(enum brw_barycentric_mode bary
)
1237 assert(bary
== BRW_BARYCENTRIC_PERSPECTIVE_CENTROID
||
1238 bary
== BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID
);
1239 return (enum brw_barycentric_mode
) ((unsigned) bary
- 1);
1243 fs_visitor::emit_frontfacing_interpolation()
1245 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1247 if (devinfo
->gen
>= 6) {
1248 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1249 * a boolean result from this (~0/true or 0/false).
1251 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1252 * this task in only one instruction:
1253 * - a negation source modifier will flip the bit; and
1254 * - a W -> D type conversion will sign extend the bit into the high
1255 * word of the destination.
1257 * An ASR 15 fills the low word of the destination.
1259 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1262 bld
.ASR(*reg
, g0
, brw_imm_d(15));
1264 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1265 * a boolean result from this (1/true or 0/false).
1267 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1268 * the negation source modifier to flip it. Unfortunately the SHR
1269 * instruction only operates on UD (or D with an abs source modifier)
1270 * sources without negation.
1272 * Instead, use ASR (which will give ~0/true or 0/false).
1274 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1277 bld
.ASR(*reg
, g1_6
, brw_imm_d(31));
1284 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1286 assert(stage
== MESA_SHADER_FRAGMENT
);
1287 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
1288 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1290 if (wm_prog_data
->persample_dispatch
) {
1291 /* Convert int_sample_pos to floating point */
1292 bld
.MOV(dst
, int_sample_pos
);
1293 /* Scale to the range [0, 1] */
1294 bld
.MUL(dst
, dst
, brw_imm_f(1 / 16.0f
));
1297 /* From ARB_sample_shading specification:
1298 * "When rendering to a non-multisample buffer, or if multisample
1299 * rasterization is disabled, gl_SamplePosition will always be
1302 bld
.MOV(dst
, brw_imm_f(0.5f
));
1307 fs_visitor::emit_samplepos_setup()
1309 assert(devinfo
->gen
>= 6);
1311 const fs_builder abld
= bld
.annotate("compute sample position");
1312 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1314 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1315 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1317 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1318 * mode will be enabled.
1320 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1321 * R31.1:0 Position Offset X/Y for Slot[3:0]
1322 * R31.3:2 Position Offset X/Y for Slot[7:4]
1325 * The X, Y sample positions come in as bytes in thread payload. So, read
1326 * the positions using vstride=16, width=8, hstride=2.
1328 const fs_reg sample_pos_reg
=
1329 fetch_payload_reg(abld
, payload
.sample_pos_reg
, BRW_REGISTER_TYPE_W
);
1331 /* Compute gl_SamplePosition.x */
1332 abld
.MOV(int_sample_x
, subscript(sample_pos_reg
, BRW_REGISTER_TYPE_B
, 0));
1333 compute_sample_position(offset(pos
, abld
, 0), int_sample_x
);
1335 /* Compute gl_SamplePosition.y */
1336 abld
.MOV(int_sample_y
, subscript(sample_pos_reg
, BRW_REGISTER_TYPE_B
, 1));
1337 compute_sample_position(offset(pos
, abld
, 1), int_sample_y
);
1342 fs_visitor::emit_sampleid_setup()
1344 assert(stage
== MESA_SHADER_FRAGMENT
);
1345 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1346 assert(devinfo
->gen
>= 6);
1348 const fs_builder abld
= bld
.annotate("compute sample id");
1349 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uint_type
));
1351 if (!key
->multisample_fbo
) {
1352 /* As per GL_ARB_sample_shading specification:
1353 * "When rendering to a non-multisample buffer, or if multisample
1354 * rasterization is disabled, gl_SampleID will always be zero."
1356 abld
.MOV(*reg
, brw_imm_d(0));
1357 } else if (devinfo
->gen
>= 8) {
1358 /* Sample ID comes in as 4-bit numbers in g1.0:
1360 * 15:12 Slot 3 SampleID (only used in SIMD16)
1361 * 11:8 Slot 2 SampleID (only used in SIMD16)
1362 * 7:4 Slot 1 SampleID
1363 * 3:0 Slot 0 SampleID
1365 * Each slot corresponds to four channels, so we want to replicate each
1366 * half-byte value to 4 channels in a row:
1368 * dst+0: .7 .6 .5 .4 .3 .2 .1 .0
1369 * 7:4 7:4 7:4 7:4 3:0 3:0 3:0 3:0
1371 * dst+1: .7 .6 .5 .4 .3 .2 .1 .0 (if SIMD16)
1372 * 15:12 15:12 15:12 15:12 11:8 11:8 11:8 11:8
1374 * First, we read g1.0 with a <1,8,0>UB region, causing the first 8
1375 * channels to read the first byte (7:0), and the second group of 8
1376 * channels to read the second byte (15:8). Then, we shift right by
1377 * a vector immediate of <4, 4, 4, 4, 0, 0, 0, 0>, moving the slot 1 / 3
1378 * values into place. Finally, we AND with 0xf to keep the low nibble.
1380 * shr(16) tmp<1>W g1.0<1,8,0>B 0x44440000:V
1381 * and(16) dst<1>D tmp<8,8,1>W 0xf:W
1383 * TODO: These payload bits exist on Gen7 too, but they appear to always
1384 * be zero, so this code fails to work. We should find out why.
1386 const fs_reg tmp
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
1388 for (unsigned i
= 0; i
< DIV_ROUND_UP(dispatch_width
, 16); i
++) {
1389 const fs_builder hbld
= abld
.group(MIN2(16, dispatch_width
), i
);
1390 hbld
.SHR(offset(tmp
, hbld
, i
),
1391 stride(retype(brw_vec1_grf(1 + i
, 0), BRW_REGISTER_TYPE_UB
),
1393 brw_imm_v(0x44440000));
1396 abld
.AND(*reg
, tmp
, brw_imm_w(0xf));
1398 const fs_reg t1
= component(abld
.vgrf(BRW_REGISTER_TYPE_UD
), 0);
1399 const fs_reg t2
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
1401 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1402 * 8x multisampling, subspan 0 will represent sample N (where N
1403 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1404 * 7. We can find the value of N by looking at R0.0 bits 7:6
1405 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1406 * (since samples are always delivered in pairs). That is, we
1407 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1408 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1409 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1410 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1411 * populating a temporary variable with the sequence (0, 1, 2, 3),
1412 * and then reading from it using vstride=1, width=4, hstride=0.
1413 * These computations hold good for 4x multisampling as well.
1415 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1416 * the first four slots are sample 0 of subspan 0; the next four
1417 * are sample 1 of subspan 0; the third group is sample 0 of
1418 * subspan 1, and finally sample 1 of subspan 1.
1421 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1422 * accomodate 16x MSAA.
1424 abld
.exec_all().group(1, 0)
1425 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
1427 abld
.exec_all().group(1, 0).SHR(t1
, t1
, brw_imm_d(5));
1429 /* This works for SIMD8-SIMD16. It also works for SIMD32 but only if we
1430 * can assume 4x MSAA. Disallow it on IVB+
1432 * FINISHME: One day, we could come up with a way to do this that
1433 * actually works on gen7.
1435 if (devinfo
->gen
>= 7)
1436 limit_dispatch_width(16, "gl_SampleId is unsupported in SIMD32 on gen7");
1437 abld
.exec_all().group(8, 0).MOV(t2
, brw_imm_v(0x32103210));
1439 /* This special instruction takes care of setting vstride=1,
1440 * width=4, hstride=0 of t2 during an ADD instruction.
1442 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1449 fs_visitor::emit_samplemaskin_setup()
1451 assert(stage
== MESA_SHADER_FRAGMENT
);
1452 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
1453 assert(devinfo
->gen
>= 6);
1455 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1457 fs_reg coverage_mask
=
1458 fetch_payload_reg(bld
, payload
.sample_mask_in_reg
, BRW_REGISTER_TYPE_D
);
1460 if (wm_prog_data
->persample_dispatch
) {
1461 /* gl_SampleMaskIn[] comes from two sources: the input coverage mask,
1462 * and a mask representing which sample is being processed by the
1463 * current shader invocation.
1465 * From the OES_sample_variables specification:
1466 * "When per-sample shading is active due to the use of a fragment input
1467 * qualified by "sample" or due to the use of the gl_SampleID or
1468 * gl_SamplePosition variables, only the bit for the current sample is
1469 * set in gl_SampleMaskIn."
1471 const fs_builder abld
= bld
.annotate("compute gl_SampleMaskIn");
1473 if (nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
].file
== BAD_FILE
)
1474 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
] = *emit_sampleid_setup();
1476 fs_reg one
= vgrf(glsl_type::int_type
);
1477 fs_reg enabled_mask
= vgrf(glsl_type::int_type
);
1478 abld
.MOV(one
, brw_imm_d(1));
1479 abld
.SHL(enabled_mask
, one
, nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
]);
1480 abld
.AND(*reg
, enabled_mask
, coverage_mask
);
1482 /* In per-pixel mode, the coverage mask is sufficient. */
1483 *reg
= coverage_mask
;
1489 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1491 if (!src
.abs
&& !src
.negate
)
1494 fs_reg temp
= bld
.vgrf(src
.type
);
1501 fs_visitor::emit_discard_jump()
1503 assert(brw_wm_prog_data(this->prog_data
)->uses_kill
);
1505 /* For performance, after a discard, jump to the end of the
1506 * shader if all relevant channels have been discarded.
1508 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1509 discard_jump
->flag_subreg
= 1;
1511 discard_jump
->predicate
= BRW_PREDICATE_ALIGN1_ANY4H
;
1512 discard_jump
->predicate_inverse
= true;
1516 fs_visitor::emit_gs_thread_end()
1518 assert(stage
== MESA_SHADER_GEOMETRY
);
1520 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1522 if (gs_compile
->control_data_header_size_bits
> 0) {
1523 emit_gs_control_data_bits(this->final_gs_vertex_count
);
1526 const fs_builder abld
= bld
.annotate("thread end");
1529 if (gs_prog_data
->static_vertex_count
!= -1) {
1530 foreach_in_list_reverse(fs_inst
, prev
, &this->instructions
) {
1531 if (prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
||
1532 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
1533 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
1534 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
) {
1537 /* Delete now dead instructions. */
1538 foreach_in_list_reverse_safe(exec_node
, dead
, &this->instructions
) {
1544 } else if (prev
->is_control_flow() || prev
->has_side_effects()) {
1548 fs_reg hdr
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1549 abld
.MOV(hdr
, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
)));
1550 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, hdr
);
1553 fs_reg payload
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1554 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
1555 sources
[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1556 sources
[1] = this->final_gs_vertex_count
;
1557 abld
.LOAD_PAYLOAD(payload
, sources
, 2, 2);
1558 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1566 fs_visitor::assign_curb_setup()
1568 unsigned uniform_push_length
= DIV_ROUND_UP(stage_prog_data
->nr_params
, 8);
1570 unsigned ubo_push_length
= 0;
1571 unsigned ubo_push_start
[4];
1572 for (int i
= 0; i
< 4; i
++) {
1573 ubo_push_start
[i
] = 8 * (ubo_push_length
+ uniform_push_length
);
1574 ubo_push_length
+= stage_prog_data
->ubo_ranges
[i
].length
;
1577 prog_data
->curb_read_length
= uniform_push_length
+ ubo_push_length
;
1579 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1580 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1581 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1582 if (inst
->src
[i
].file
== UNIFORM
) {
1583 int uniform_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].offset
/ 4;
1585 if (inst
->src
[i
].nr
>= UBO_START
) {
1586 /* constant_nr is in 32-bit units, the rest are in bytes */
1587 constant_nr
= ubo_push_start
[inst
->src
[i
].nr
- UBO_START
] +
1588 inst
->src
[i
].offset
/ 4;
1589 } else if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1590 constant_nr
= push_constant_loc
[uniform_nr
];
1592 /* Section 5.11 of the OpenGL 4.1 spec says:
1593 * "Out-of-bounds reads return undefined values, which include
1594 * values from other variables of the active program or zero."
1595 * Just return the first push constant.
1600 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1603 brw_reg
.abs
= inst
->src
[i
].abs
;
1604 brw_reg
.negate
= inst
->src
[i
].negate
;
1606 assert(inst
->src
[i
].stride
== 0);
1607 inst
->src
[i
] = byte_offset(
1608 retype(brw_reg
, inst
->src
[i
].type
),
1609 inst
->src
[i
].offset
% 4);
1614 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1615 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1619 fs_visitor::calculate_urb_setup()
1621 assert(stage
== MESA_SHADER_FRAGMENT
);
1622 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
1623 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1625 memset(prog_data
->urb_setup
, -1,
1626 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1629 /* Figure out where each of the incoming setup attributes lands. */
1630 if (devinfo
->gen
>= 6) {
1631 if (util_bitcount64(nir
->info
.inputs_read
&
1632 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1633 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1634 * first 16 varying inputs, so we can put them wherever we want.
1635 * Just put them in order.
1637 * This is useful because it means that (a) inputs not used by the
1638 * fragment shader won't take up valuable register space, and (b) we
1639 * won't have to recompile the fragment shader if it gets paired with
1640 * a different vertex (or geometry) shader.
1642 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1643 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1644 BITFIELD64_BIT(i
)) {
1645 prog_data
->urb_setup
[i
] = urb_next
++;
1649 /* We have enough input varyings that the SF/SBE pipeline stage can't
1650 * arbitrarily rearrange them to suit our whim; we have to put them
1651 * in an order that matches the output of the previous pipeline stage
1652 * (geometry or vertex shader).
1654 struct brw_vue_map prev_stage_vue_map
;
1655 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1656 key
->input_slots_valid
,
1657 nir
->info
.separate_shader
);
1660 brw_compute_first_urb_slot_required(nir
->info
.inputs_read
,
1661 &prev_stage_vue_map
);
1663 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1664 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1666 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1667 if (varying
!= BRW_VARYING_SLOT_PAD
&&
1668 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1669 BITFIELD64_BIT(varying
))) {
1670 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1673 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1676 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1677 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1678 /* Point size is packed into the header, not as a general attribute */
1679 if (i
== VARYING_SLOT_PSIZ
)
1682 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1683 /* The back color slot is skipped when the front color is
1684 * also written to. In addition, some slots can be
1685 * written in the vertex shader and not read in the
1686 * fragment shader. So the register number must always be
1687 * incremented, mapped or not.
1689 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1690 prog_data
->urb_setup
[i
] = urb_next
;
1696 * It's a FS only attribute, and we did interpolation for this attribute
1697 * in SF thread. So, count it here, too.
1699 * See compile_sf_prog() for more info.
1701 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1702 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1705 prog_data
->num_varying_inputs
= urb_next
;
1709 fs_visitor::assign_urb_setup()
1711 assert(stage
== MESA_SHADER_FRAGMENT
);
1712 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
1714 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1716 /* Offset all the urb_setup[] index by the actual position of the
1717 * setup regs, now that the location of the constants has been chosen.
1719 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1720 for (int i
= 0; i
< inst
->sources
; i
++) {
1721 if (inst
->src
[i
].file
== ATTR
) {
1722 /* ATTR regs in the FS are in units of logical scalar inputs each
1723 * of which consumes half of a GRF register.
1725 assert(inst
->src
[i
].offset
< REG_SIZE
/ 2);
1726 const unsigned grf
= urb_start
+ inst
->src
[i
].nr
/ 2;
1727 const unsigned offset
= (inst
->src
[i
].nr
% 2) * (REG_SIZE
/ 2) +
1728 inst
->src
[i
].offset
;
1729 const unsigned width
= inst
->src
[i
].stride
== 0 ?
1730 1 : MIN2(inst
->exec_size
, 8);
1731 struct brw_reg reg
= stride(
1732 byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1734 width
* inst
->src
[i
].stride
,
1735 width
, inst
->src
[i
].stride
);
1736 reg
.abs
= inst
->src
[i
].abs
;
1737 reg
.negate
= inst
->src
[i
].negate
;
1743 /* Each attribute is 4 setup channels, each of which is half a reg. */
1744 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1748 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst
*inst
)
1750 for (int i
= 0; i
< inst
->sources
; i
++) {
1751 if (inst
->src
[i
].file
== ATTR
) {
1752 int grf
= payload
.num_regs
+
1753 prog_data
->curb_read_length
+
1755 inst
->src
[i
].offset
/ REG_SIZE
;
1757 /* As explained at brw_reg_from_fs_reg, From the Haswell PRM:
1759 * VertStride must be used to cross GRF register boundaries. This
1760 * rule implies that elements within a 'Width' cannot cross GRF
1763 * So, for registers that are large enough, we have to split the exec
1764 * size in two and trust the compression state to sort it out.
1766 unsigned total_size
= inst
->exec_size
*
1767 inst
->src
[i
].stride
*
1768 type_sz(inst
->src
[i
].type
);
1770 assert(total_size
<= 2 * REG_SIZE
);
1771 const unsigned exec_size
=
1772 (total_size
<= REG_SIZE
) ? inst
->exec_size
: inst
->exec_size
/ 2;
1774 unsigned width
= inst
->src
[i
].stride
== 0 ? 1 : exec_size
;
1775 struct brw_reg reg
=
1776 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1777 inst
->src
[i
].offset
% REG_SIZE
),
1778 exec_size
* inst
->src
[i
].stride
,
1779 width
, inst
->src
[i
].stride
);
1780 reg
.abs
= inst
->src
[i
].abs
;
1781 reg
.negate
= inst
->src
[i
].negate
;
1789 fs_visitor::assign_vs_urb_setup()
1791 struct brw_vs_prog_data
*vs_prog_data
= brw_vs_prog_data(prog_data
);
1793 assert(stage
== MESA_SHADER_VERTEX
);
1795 /* Each attribute is 4 regs. */
1796 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attribute_slots
;
1798 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1800 /* Rewrite all ATTR file references to the hw grf that they land in. */
1801 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1802 convert_attr_sources_to_hw_regs(inst
);
1807 fs_visitor::assign_tcs_single_patch_urb_setup()
1809 assert(stage
== MESA_SHADER_TESS_CTRL
);
1811 /* Rewrite all ATTR file references to HW_REGs. */
1812 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1813 convert_attr_sources_to_hw_regs(inst
);
1818 fs_visitor::assign_tes_urb_setup()
1820 assert(stage
== MESA_SHADER_TESS_EVAL
);
1822 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
1824 first_non_payload_grf
+= 8 * vue_prog_data
->urb_read_length
;
1826 /* Rewrite all ATTR file references to HW_REGs. */
1827 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1828 convert_attr_sources_to_hw_regs(inst
);
1833 fs_visitor::assign_gs_urb_setup()
1835 assert(stage
== MESA_SHADER_GEOMETRY
);
1837 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
1839 first_non_payload_grf
+=
1840 8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
;
1842 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1843 /* Rewrite all ATTR file references to GRFs. */
1844 convert_attr_sources_to_hw_regs(inst
);
1850 * Split large virtual GRFs into separate components if we can.
1852 * This is mostly duplicated with what brw_fs_vector_splitting does,
1853 * but that's really conservative because it's afraid of doing
1854 * splitting that doesn't result in real progress after the rest of
1855 * the optimization phases, which would cause infinite looping in
1856 * optimization. We can do it once here, safely. This also has the
1857 * opportunity to split interpolated values, or maybe even uniforms,
1858 * which we don't have at the IR level.
1860 * We want to split, because virtual GRFs are what we register
1861 * allocate and spill (due to contiguousness requirements for some
1862 * instructions), and they're what we naturally generate in the
1863 * codegen process, but most virtual GRFs don't actually need to be
1864 * contiguous sets of GRFs. If we split, we'll end up with reduced
1865 * live intervals and better dead code elimination and coalescing.
1868 fs_visitor::split_virtual_grfs()
1870 /* Compact the register file so we eliminate dead vgrfs. This
1871 * only defines split points for live registers, so if we have
1872 * too large dead registers they will hit assertions later.
1874 compact_virtual_grfs();
1876 int num_vars
= this->alloc
.count
;
1878 /* Count the total number of registers */
1880 int vgrf_to_reg
[num_vars
];
1881 for (int i
= 0; i
< num_vars
; i
++) {
1882 vgrf_to_reg
[i
] = reg_count
;
1883 reg_count
+= alloc
.sizes
[i
];
1886 /* An array of "split points". For each register slot, this indicates
1887 * if this slot can be separated from the previous slot. Every time an
1888 * instruction uses multiple elements of a register (as a source or
1889 * destination), we mark the used slots as inseparable. Then we go
1890 * through and split the registers into the smallest pieces we can.
1892 bool split_points
[reg_count
];
1893 memset(split_points
, 0, sizeof(split_points
));
1895 /* Mark all used registers as fully splittable */
1896 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1897 if (inst
->dst
.file
== VGRF
) {
1898 int reg
= vgrf_to_reg
[inst
->dst
.nr
];
1899 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.nr
]; j
++)
1900 split_points
[reg
+ j
] = true;
1903 for (int i
= 0; i
< inst
->sources
; i
++) {
1904 if (inst
->src
[i
].file
== VGRF
) {
1905 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
];
1906 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].nr
]; j
++)
1907 split_points
[reg
+ j
] = true;
1912 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1913 if (inst
->dst
.file
== VGRF
) {
1914 int reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.offset
/ REG_SIZE
;
1915 for (unsigned j
= 1; j
< regs_written(inst
); j
++)
1916 split_points
[reg
+ j
] = false;
1918 for (int i
= 0; i
< inst
->sources
; i
++) {
1919 if (inst
->src
[i
].file
== VGRF
) {
1920 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].offset
/ REG_SIZE
;
1921 for (unsigned j
= 1; j
< regs_read(inst
, i
); j
++)
1922 split_points
[reg
+ j
] = false;
1927 int new_virtual_grf
[reg_count
];
1928 int new_reg_offset
[reg_count
];
1931 for (int i
= 0; i
< num_vars
; i
++) {
1932 /* The first one should always be 0 as a quick sanity check. */
1933 assert(split_points
[reg
] == false);
1936 new_reg_offset
[reg
] = 0;
1941 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1942 /* If this is a split point, reset the offset to 0 and allocate a
1943 * new virtual GRF for the previous offset many registers
1945 if (split_points
[reg
]) {
1946 assert(offset
<= MAX_VGRF_SIZE
);
1947 int grf
= alloc
.allocate(offset
);
1948 for (int k
= reg
- offset
; k
< reg
; k
++)
1949 new_virtual_grf
[k
] = grf
;
1952 new_reg_offset
[reg
] = offset
;
1957 /* The last one gets the original register number */
1958 assert(offset
<= MAX_VGRF_SIZE
);
1959 alloc
.sizes
[i
] = offset
;
1960 for (int k
= reg
- offset
; k
< reg
; k
++)
1961 new_virtual_grf
[k
] = i
;
1963 assert(reg
== reg_count
);
1965 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1966 if (inst
->dst
.file
== VGRF
) {
1967 reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.offset
/ REG_SIZE
;
1968 inst
->dst
.nr
= new_virtual_grf
[reg
];
1969 inst
->dst
.offset
= new_reg_offset
[reg
] * REG_SIZE
+
1970 inst
->dst
.offset
% REG_SIZE
;
1971 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1973 for (int i
= 0; i
< inst
->sources
; i
++) {
1974 if (inst
->src
[i
].file
== VGRF
) {
1975 reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].offset
/ REG_SIZE
;
1976 inst
->src
[i
].nr
= new_virtual_grf
[reg
];
1977 inst
->src
[i
].offset
= new_reg_offset
[reg
] * REG_SIZE
+
1978 inst
->src
[i
].offset
% REG_SIZE
;
1979 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1983 invalidate_live_intervals();
1987 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1989 * During code generation, we create tons of temporary variables, many of
1990 * which get immediately killed and are never used again. Yet, in later
1991 * optimization and analysis passes, such as compute_live_intervals, we need
1992 * to loop over all the virtual GRFs. Compacting them can save a lot of
1996 fs_visitor::compact_virtual_grfs()
1998 bool progress
= false;
1999 int remap_table
[this->alloc
.count
];
2000 memset(remap_table
, -1, sizeof(remap_table
));
2002 /* Mark which virtual GRFs are used. */
2003 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
2004 if (inst
->dst
.file
== VGRF
)
2005 remap_table
[inst
->dst
.nr
] = 0;
2007 for (int i
= 0; i
< inst
->sources
; i
++) {
2008 if (inst
->src
[i
].file
== VGRF
)
2009 remap_table
[inst
->src
[i
].nr
] = 0;
2013 /* Compact the GRF arrays. */
2015 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
2016 if (remap_table
[i
] == -1) {
2017 /* We just found an unused register. This means that we are
2018 * actually going to compact something.
2022 remap_table
[i
] = new_index
;
2023 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
2024 invalidate_live_intervals();
2029 this->alloc
.count
= new_index
;
2031 /* Patch all the instructions to use the newly renumbered registers */
2032 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2033 if (inst
->dst
.file
== VGRF
)
2034 inst
->dst
.nr
= remap_table
[inst
->dst
.nr
];
2036 for (int i
= 0; i
< inst
->sources
; i
++) {
2037 if (inst
->src
[i
].file
== VGRF
)
2038 inst
->src
[i
].nr
= remap_table
[inst
->src
[i
].nr
];
2042 /* Patch all the references to delta_xy, since they're used in register
2043 * allocation. If they're unused, switch them to BAD_FILE so we don't
2044 * think some random VGRF is delta_xy.
2046 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2047 if (delta_xy
[i
].file
== VGRF
) {
2048 if (remap_table
[delta_xy
[i
].nr
] != -1) {
2049 delta_xy
[i
].nr
= remap_table
[delta_xy
[i
].nr
];
2051 delta_xy
[i
].file
= BAD_FILE
;
2060 get_subgroup_id_param_index(const brw_stage_prog_data
*prog_data
)
2062 if (prog_data
->nr_params
== 0)
2065 /* The local thread id is always the last parameter in the list */
2066 uint32_t last_param
= prog_data
->param
[prog_data
->nr_params
- 1];
2067 if (last_param
== BRW_PARAM_BUILTIN_SUBGROUP_ID
)
2068 return prog_data
->nr_params
- 1;
2074 * Struct for handling complex alignments.
2076 * A complex alignment is stored as multiplier and an offset. A value is
2077 * considered to be aligned if it is {offset} larger than a multiple of {mul}.
2078 * For instance, with an alignment of {8, 2}, cplx_align_apply would do the
2081 * N | cplx_align_apply({8, 2}, N)
2082 * ----+-----------------------------
2096 #define CPLX_ALIGN_MAX_MUL 8
2099 cplx_align_assert_sane(struct cplx_align a
)
2101 assert(a
.mul
> 0 && util_is_power_of_two_nonzero(a
.mul
));
2102 assert(a
.offset
< a
.mul
);
2106 * Combines two alignments to produce a least multiple of sorts.
2108 * The returned alignment is the smallest (in terms of multiplier) such that
2109 * anything aligned to both a and b will be aligned to the new alignment.
2110 * This function will assert-fail if a and b are not compatible, i.e. if the
2111 * offset parameters are such that no common alignment is possible.
2113 static struct cplx_align
2114 cplx_align_combine(struct cplx_align a
, struct cplx_align b
)
2116 cplx_align_assert_sane(a
);
2117 cplx_align_assert_sane(b
);
2119 /* Assert that the alignments agree. */
2120 assert((a
.offset
& (b
.mul
- 1)) == (b
.offset
& (a
.mul
- 1)));
2122 return a
.mul
> b
.mul
? a
: b
;
2126 * Apply a complex alignment
2128 * This function will return the smallest number greater than or equal to
2129 * offset that is aligned to align.
2132 cplx_align_apply(struct cplx_align align
, unsigned offset
)
2134 return ALIGN(offset
- align
.offset
, align
.mul
) + align
.offset
;
2137 #define UNIFORM_SLOT_SIZE 4
2139 struct uniform_slot_info
{
2140 /** True if the given uniform slot is live */
2143 /** True if this slot and the next slot must remain contiguous */
2144 unsigned contiguous
:1;
2146 struct cplx_align align
;
2150 mark_uniform_slots_read(struct uniform_slot_info
*slots
,
2151 unsigned num_slots
, unsigned alignment
)
2153 assert(alignment
> 0 && util_is_power_of_two_nonzero(alignment
));
2154 assert(alignment
<= CPLX_ALIGN_MAX_MUL
);
2156 /* We can't align a slot to anything less than the slot size */
2157 alignment
= MAX2(alignment
, UNIFORM_SLOT_SIZE
);
2159 struct cplx_align align
= {alignment
, 0};
2160 cplx_align_assert_sane(align
);
2162 for (unsigned i
= 0; i
< num_slots
; i
++) {
2163 slots
[i
].is_live
= true;
2164 if (i
< num_slots
- 1)
2165 slots
[i
].contiguous
= true;
2167 align
.offset
= (i
* UNIFORM_SLOT_SIZE
) & (align
.mul
- 1);
2168 if (slots
[i
].align
.mul
== 0) {
2169 slots
[i
].align
= align
;
2171 slots
[i
].align
= cplx_align_combine(slots
[i
].align
, align
);
2177 * Assign UNIFORM file registers to either push constants or pull constants.
2179 * We allow a fragment shader to have more than the specified minimum
2180 * maximum number of fragment shader uniform components (64). If
2181 * there are too many of these, they'd fill up all of register space.
2182 * So, this will push some of them out to the pull constant buffer and
2183 * update the program to load them.
2186 fs_visitor::assign_constant_locations()
2188 /* Only the first compile gets to decide on locations. */
2189 if (push_constant_loc
) {
2190 assert(pull_constant_loc
);
2194 struct uniform_slot_info slots
[uniforms
];
2195 memset(slots
, 0, sizeof(slots
));
2197 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2198 for (int i
= 0 ; i
< inst
->sources
; i
++) {
2199 if (inst
->src
[i
].file
!= UNIFORM
)
2202 /* NIR tightly packs things so the uniform number might not be
2203 * aligned (if we have a double right after a float, for instance).
2204 * This is fine because the process of re-arranging them will ensure
2205 * that things are properly aligned. The offset into that uniform,
2206 * however, must be aligned.
2208 * In Vulkan, we have explicit offsets but everything is crammed
2209 * into a single "variable" so inst->src[i].nr will always be 0.
2210 * Everything will be properly aligned relative to that one base.
2212 assert(inst
->src
[i
].offset
% type_sz(inst
->src
[i
].type
) == 0);
2214 unsigned u
= inst
->src
[i
].nr
+
2215 inst
->src
[i
].offset
/ UNIFORM_SLOT_SIZE
;
2220 unsigned slots_read
;
2221 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0) {
2222 slots_read
= DIV_ROUND_UP(inst
->src
[2].ud
, UNIFORM_SLOT_SIZE
);
2224 unsigned bytes_read
= inst
->components_read(i
) *
2225 type_sz(inst
->src
[i
].type
);
2226 slots_read
= DIV_ROUND_UP(bytes_read
, UNIFORM_SLOT_SIZE
);
2229 assert(u
+ slots_read
<= uniforms
);
2230 mark_uniform_slots_read(&slots
[u
], slots_read
,
2231 type_sz(inst
->src
[i
].type
));
2235 int subgroup_id_index
= get_subgroup_id_param_index(stage_prog_data
);
2237 /* Only allow 16 registers (128 uniform components) as push constants.
2239 * Just demote the end of the list. We could probably do better
2240 * here, demoting things that are rarely used in the program first.
2242 * If changing this value, note the limitation about total_regs in
2245 unsigned int max_push_components
= 16 * 8;
2246 if (subgroup_id_index
>= 0)
2247 max_push_components
--; /* Save a slot for the thread ID */
2249 /* We push small arrays, but no bigger than 16 floats. This is big enough
2250 * for a vec4 but hopefully not large enough to push out other stuff. We
2251 * should probably use a better heuristic at some point.
2253 const unsigned int max_chunk_size
= 16;
2255 unsigned int num_push_constants
= 0;
2256 unsigned int num_pull_constants
= 0;
2258 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2259 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2261 /* Default to -1 meaning no location */
2262 memset(push_constant_loc
, -1, uniforms
* sizeof(*push_constant_loc
));
2263 memset(pull_constant_loc
, -1, uniforms
* sizeof(*pull_constant_loc
));
2265 int chunk_start
= -1;
2266 struct cplx_align align
;
2267 for (unsigned u
= 0; u
< uniforms
; u
++) {
2268 if (!slots
[u
].is_live
) {
2269 assert(chunk_start
== -1);
2273 /* Skip subgroup_id_index to put it in the last push register. */
2274 if (subgroup_id_index
== (int)u
)
2277 if (chunk_start
== -1) {
2279 align
= slots
[u
].align
;
2281 /* Offset into the chunk */
2282 unsigned chunk_offset
= (u
- chunk_start
) * UNIFORM_SLOT_SIZE
;
2284 /* Shift the slot alignment down by the chunk offset so it is
2285 * comparable with the base chunk alignment.
2287 struct cplx_align slot_align
= slots
[u
].align
;
2289 (slot_align
.offset
- chunk_offset
) & (align
.mul
- 1);
2291 align
= cplx_align_combine(align
, slot_align
);
2294 /* Sanity check the alignment */
2295 cplx_align_assert_sane(align
);
2297 if (slots
[u
].contiguous
)
2300 /* Adjust the alignment to be in terms of slots, not bytes */
2301 assert((align
.mul
& (UNIFORM_SLOT_SIZE
- 1)) == 0);
2302 assert((align
.offset
& (UNIFORM_SLOT_SIZE
- 1)) == 0);
2303 align
.mul
/= UNIFORM_SLOT_SIZE
;
2304 align
.offset
/= UNIFORM_SLOT_SIZE
;
2306 unsigned push_start_align
= cplx_align_apply(align
, num_push_constants
);
2307 unsigned chunk_size
= u
- chunk_start
+ 1;
2308 if ((!compiler
->supports_pull_constants
&& u
< UBO_START
) ||
2309 (chunk_size
< max_chunk_size
&&
2310 push_start_align
+ chunk_size
<= max_push_components
)) {
2311 /* Align up the number of push constants */
2312 num_push_constants
= push_start_align
;
2313 for (unsigned i
= 0; i
< chunk_size
; i
++)
2314 push_constant_loc
[chunk_start
+ i
] = num_push_constants
++;
2316 /* We need to pull this one */
2317 num_pull_constants
= cplx_align_apply(align
, num_pull_constants
);
2318 for (unsigned i
= 0; i
< chunk_size
; i
++)
2319 pull_constant_loc
[chunk_start
+ i
] = num_pull_constants
++;
2322 /* Reset the chunk and start again */
2326 /* Add the CS local thread ID uniform at the end of the push constants */
2327 if (subgroup_id_index
>= 0)
2328 push_constant_loc
[subgroup_id_index
] = num_push_constants
++;
2330 /* As the uniforms are going to be reordered, stash the old array and
2331 * create two new arrays for push/pull params.
2333 uint32_t *param
= stage_prog_data
->param
;
2334 stage_prog_data
->nr_params
= num_push_constants
;
2335 if (num_push_constants
) {
2336 stage_prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t,
2337 num_push_constants
);
2339 stage_prog_data
->param
= NULL
;
2341 assert(stage_prog_data
->nr_pull_params
== 0);
2342 assert(stage_prog_data
->pull_param
== NULL
);
2343 if (num_pull_constants
> 0) {
2344 stage_prog_data
->nr_pull_params
= num_pull_constants
;
2345 stage_prog_data
->pull_param
= rzalloc_array(mem_ctx
, uint32_t,
2346 num_pull_constants
);
2349 /* Now that we know how many regular uniforms we'll push, reduce the
2350 * UBO push ranges so we don't exceed the 3DSTATE_CONSTANT limits.
2352 unsigned push_length
= DIV_ROUND_UP(stage_prog_data
->nr_params
, 8);
2353 for (int i
= 0; i
< 4; i
++) {
2354 struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
2356 if (push_length
+ range
->length
> 64)
2357 range
->length
= 64 - push_length
;
2359 push_length
+= range
->length
;
2361 assert(push_length
<= 64);
2363 /* Up until now, the param[] array has been indexed by reg + offset
2364 * of UNIFORM registers. Move pull constants into pull_param[] and
2365 * condense param[] to only contain the uniforms we chose to push.
2367 * NOTE: Because we are condensing the params[] array, we know that
2368 * push_constant_loc[i] <= i and we can do it in one smooth loop without
2369 * having to make a copy.
2371 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2372 uint32_t value
= param
[i
];
2373 if (pull_constant_loc
[i
] != -1) {
2374 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
2375 } else if (push_constant_loc
[i
] != -1) {
2376 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
2383 fs_visitor::get_pull_locs(const fs_reg
&src
,
2384 unsigned *out_surf_index
,
2385 unsigned *out_pull_index
)
2387 assert(src
.file
== UNIFORM
);
2389 if (src
.nr
>= UBO_START
) {
2390 const struct brw_ubo_range
*range
=
2391 &prog_data
->ubo_ranges
[src
.nr
- UBO_START
];
2393 /* If this access is in our (reduced) range, use the push data. */
2394 if (src
.offset
/ 32 < range
->length
)
2397 *out_surf_index
= prog_data
->binding_table
.ubo_start
+ range
->block
;
2398 *out_pull_index
= (32 * range
->start
+ src
.offset
) / 4;
2402 const unsigned location
= src
.nr
+ src
.offset
/ 4;
2404 if (location
< uniforms
&& pull_constant_loc
[location
] != -1) {
2405 /* A regular uniform push constant */
2406 *out_surf_index
= stage_prog_data
->binding_table
.pull_constants_start
;
2407 *out_pull_index
= pull_constant_loc
[location
];
2415 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2416 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2419 fs_visitor::lower_constant_loads()
2421 unsigned index
, pull_index
;
2423 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2424 /* Set up the annotation tracking for new generated instructions. */
2425 const fs_builder
ibld(this, block
, inst
);
2427 for (int i
= 0; i
< inst
->sources
; i
++) {
2428 if (inst
->src
[i
].file
!= UNIFORM
)
2431 /* We'll handle this case later */
2432 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0)
2435 if (!get_pull_locs(inst
->src
[i
], &index
, &pull_index
))
2438 assert(inst
->src
[i
].stride
== 0);
2440 const unsigned block_sz
= 64; /* Fetch one cacheline at a time. */
2441 const fs_builder ubld
= ibld
.exec_all().group(block_sz
/ 4, 0);
2442 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
2443 const unsigned base
= pull_index
* 4;
2445 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
2446 dst
, brw_imm_ud(index
), brw_imm_ud(base
& ~(block_sz
- 1)));
2448 /* Rewrite the instruction to use the temporary VGRF. */
2449 inst
->src
[i
].file
= VGRF
;
2450 inst
->src
[i
].nr
= dst
.nr
;
2451 inst
->src
[i
].offset
= (base
& (block_sz
- 1)) +
2452 inst
->src
[i
].offset
% 4;
2455 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&&
2456 inst
->src
[0].file
== UNIFORM
) {
2458 if (!get_pull_locs(inst
->src
[0], &index
, &pull_index
))
2461 VARYING_PULL_CONSTANT_LOAD(ibld
, inst
->dst
,
2465 inst
->remove(block
);
2468 invalidate_live_intervals();
2472 fs_visitor::opt_algebraic()
2474 bool progress
= false;
2476 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2477 switch (inst
->opcode
) {
2478 case BRW_OPCODE_MOV
:
2479 if (!devinfo
->has_64bit_types
&&
2480 (inst
->dst
.type
== BRW_REGISTER_TYPE_DF
||
2481 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
||
2482 inst
->dst
.type
== BRW_REGISTER_TYPE_Q
)) {
2483 assert(inst
->dst
.type
== inst
->src
[0].type
);
2484 assert(!inst
->saturate
);
2485 assert(!inst
->src
[0].abs
);
2486 assert(!inst
->src
[0].negate
);
2487 const brw::fs_builder
ibld(this, block
, inst
);
2489 if (inst
->src
[0].file
== IMM
) {
2490 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2491 brw_imm_ud(inst
->src
[0].u64
>> 32));
2492 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2493 brw_imm_ud(inst
->src
[0].u64
));
2495 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2496 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1));
2497 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2498 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0));
2501 inst
->remove(block
);
2505 if ((inst
->conditional_mod
== BRW_CONDITIONAL_Z
||
2506 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) &&
2507 inst
->dst
.is_null() &&
2508 (inst
->src
[0].abs
|| inst
->src
[0].negate
)) {
2509 inst
->src
[0].abs
= false;
2510 inst
->src
[0].negate
= false;
2515 if (inst
->src
[0].file
!= IMM
)
2518 if (inst
->saturate
) {
2519 /* Full mixed-type saturates don't happen. However, we can end up
2522 * mov.sat(8) g21<1>DF -1F
2524 * Other mixed-size-but-same-base-type cases may also be possible.
2526 if (inst
->dst
.type
!= inst
->src
[0].type
&&
2527 inst
->dst
.type
!= BRW_REGISTER_TYPE_DF
&&
2528 inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
)
2529 assert(!"unimplemented: saturate mixed types");
2531 if (brw_saturate_immediate(inst
->src
[0].type
,
2532 &inst
->src
[0].as_brw_reg())) {
2533 inst
->saturate
= false;
2539 case BRW_OPCODE_MUL
:
2540 if (inst
->src
[1].file
!= IMM
)
2544 if (inst
->src
[1].is_one()) {
2545 inst
->opcode
= BRW_OPCODE_MOV
;
2546 inst
->src
[1] = reg_undef
;
2552 if (inst
->src
[1].is_negative_one()) {
2553 inst
->opcode
= BRW_OPCODE_MOV
;
2554 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2555 inst
->src
[1] = reg_undef
;
2561 if (inst
->src
[1].is_zero()) {
2562 inst
->opcode
= BRW_OPCODE_MOV
;
2563 inst
->src
[0] = inst
->src
[1];
2564 inst
->src
[1] = reg_undef
;
2569 if (inst
->src
[0].file
== IMM
) {
2570 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2571 inst
->opcode
= BRW_OPCODE_MOV
;
2572 inst
->src
[0].f
*= inst
->src
[1].f
;
2573 inst
->src
[1] = reg_undef
;
2578 case BRW_OPCODE_ADD
:
2579 if (inst
->src
[1].file
!= IMM
)
2583 if (inst
->src
[1].is_zero()) {
2584 inst
->opcode
= BRW_OPCODE_MOV
;
2585 inst
->src
[1] = reg_undef
;
2590 if (inst
->src
[0].file
== IMM
) {
2591 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2592 inst
->opcode
= BRW_OPCODE_MOV
;
2593 inst
->src
[0].f
+= inst
->src
[1].f
;
2594 inst
->src
[1] = reg_undef
;
2600 if (inst
->src
[0].equals(inst
->src
[1]) ||
2601 inst
->src
[1].is_zero()) {
2602 /* On Gen8+, the OR instruction can have a source modifier that
2603 * performs logical not on the operand. Cases of 'OR r0, ~r1, 0'
2604 * or 'OR r0, ~r1, ~r1' should become a NOT instead of a MOV.
2606 if (inst
->src
[0].negate
) {
2607 inst
->opcode
= BRW_OPCODE_NOT
;
2608 inst
->src
[0].negate
= false;
2610 inst
->opcode
= BRW_OPCODE_MOV
;
2612 inst
->src
[1] = reg_undef
;
2617 case BRW_OPCODE_LRP
:
2618 if (inst
->src
[1].equals(inst
->src
[2])) {
2619 inst
->opcode
= BRW_OPCODE_MOV
;
2620 inst
->src
[0] = inst
->src
[1];
2621 inst
->src
[1] = reg_undef
;
2622 inst
->src
[2] = reg_undef
;
2627 case BRW_OPCODE_CMP
:
2628 if ((inst
->conditional_mod
== BRW_CONDITIONAL_Z
||
2629 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) &&
2630 inst
->src
[1].is_zero() &&
2631 (inst
->src
[0].abs
|| inst
->src
[0].negate
)) {
2632 inst
->src
[0].abs
= false;
2633 inst
->src
[0].negate
= false;
2638 case BRW_OPCODE_SEL
:
2639 if (!devinfo
->has_64bit_types
&&
2640 (inst
->dst
.type
== BRW_REGISTER_TYPE_DF
||
2641 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
||
2642 inst
->dst
.type
== BRW_REGISTER_TYPE_Q
)) {
2643 assert(inst
->dst
.type
== inst
->src
[0].type
);
2644 assert(!inst
->saturate
);
2645 assert(!inst
->src
[0].abs
&& !inst
->src
[0].negate
);
2646 assert(!inst
->src
[1].abs
&& !inst
->src
[1].negate
);
2647 const brw::fs_builder
ibld(this, block
, inst
);
2649 set_predicate(inst
->predicate
,
2650 ibld
.SEL(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2651 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
2652 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 0)));
2653 set_predicate(inst
->predicate
,
2654 ibld
.SEL(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2655 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1),
2656 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 1)));
2658 inst
->remove(block
);
2661 if (inst
->src
[0].equals(inst
->src
[1])) {
2662 inst
->opcode
= BRW_OPCODE_MOV
;
2663 inst
->src
[1] = reg_undef
;
2664 inst
->predicate
= BRW_PREDICATE_NONE
;
2665 inst
->predicate_inverse
= false;
2667 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2668 switch (inst
->conditional_mod
) {
2669 case BRW_CONDITIONAL_LE
:
2670 case BRW_CONDITIONAL_L
:
2671 switch (inst
->src
[1].type
) {
2672 case BRW_REGISTER_TYPE_F
:
2673 if (inst
->src
[1].f
>= 1.0f
) {
2674 inst
->opcode
= BRW_OPCODE_MOV
;
2675 inst
->src
[1] = reg_undef
;
2676 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2684 case BRW_CONDITIONAL_GE
:
2685 case BRW_CONDITIONAL_G
:
2686 switch (inst
->src
[1].type
) {
2687 case BRW_REGISTER_TYPE_F
:
2688 if (inst
->src
[1].f
<= 0.0f
) {
2689 inst
->opcode
= BRW_OPCODE_MOV
;
2690 inst
->src
[1] = reg_undef
;
2691 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2703 case BRW_OPCODE_MAD
:
2704 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2705 inst
->opcode
= BRW_OPCODE_MOV
;
2706 inst
->src
[1] = reg_undef
;
2707 inst
->src
[2] = reg_undef
;
2709 } else if (inst
->src
[0].is_zero()) {
2710 inst
->opcode
= BRW_OPCODE_MUL
;
2711 inst
->src
[0] = inst
->src
[2];
2712 inst
->src
[2] = reg_undef
;
2714 } else if (inst
->src
[1].is_one()) {
2715 inst
->opcode
= BRW_OPCODE_ADD
;
2716 inst
->src
[1] = inst
->src
[2];
2717 inst
->src
[2] = reg_undef
;
2719 } else if (inst
->src
[2].is_one()) {
2720 inst
->opcode
= BRW_OPCODE_ADD
;
2721 inst
->src
[2] = reg_undef
;
2725 case SHADER_OPCODE_BROADCAST
:
2726 if (is_uniform(inst
->src
[0])) {
2727 inst
->opcode
= BRW_OPCODE_MOV
;
2729 inst
->force_writemask_all
= true;
2731 } else if (inst
->src
[1].file
== IMM
) {
2732 inst
->opcode
= BRW_OPCODE_MOV
;
2733 /* It's possible that the selected component will be too large and
2734 * overflow the register. This can happen if someone does a
2735 * readInvocation() from GLSL or SPIR-V and provides an OOB
2736 * invocationIndex. If this happens and we some how manage
2737 * to constant fold it in and get here, then component() may cause
2738 * us to start reading outside of the VGRF which will lead to an
2739 * assert later. Instead, just let it wrap around if it goes over
2742 const unsigned comp
= inst
->src
[1].ud
& (inst
->exec_size
- 1);
2743 inst
->src
[0] = component(inst
->src
[0], comp
);
2745 inst
->force_writemask_all
= true;
2750 case SHADER_OPCODE_SHUFFLE
:
2751 if (is_uniform(inst
->src
[0])) {
2752 inst
->opcode
= BRW_OPCODE_MOV
;
2755 } else if (inst
->src
[1].file
== IMM
) {
2756 inst
->opcode
= BRW_OPCODE_MOV
;
2757 inst
->src
[0] = component(inst
->src
[0],
2768 /* Swap if src[0] is immediate. */
2769 if (progress
&& inst
->is_commutative()) {
2770 if (inst
->src
[0].file
== IMM
) {
2771 fs_reg tmp
= inst
->src
[1];
2772 inst
->src
[1] = inst
->src
[0];
2781 * Optimize sample messages that have constant zero values for the trailing
2782 * texture coordinates. We can just reduce the message length for these
2783 * instructions instead of reserving a register for it. Trailing parameters
2784 * that aren't sent default to zero anyway. This will cause the dead code
2785 * eliminator to remove the MOV instruction that would otherwise be emitted to
2786 * set up the zero value.
2789 fs_visitor::opt_zero_samples()
2791 /* Gen4 infers the texturing opcode based on the message length so we can't
2794 if (devinfo
->gen
< 5)
2797 bool progress
= false;
2799 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2800 if (!inst
->is_tex())
2803 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2805 if (load_payload
->is_head_sentinel() ||
2806 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2809 /* We don't want to remove the message header or the first parameter.
2810 * Removing the first parameter is not allowed, see the Haswell PRM
2811 * volume 7, page 149:
2813 * "Parameter 0 is required except for the sampleinfo message, which
2814 * has no parameter 0"
2816 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2817 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2818 (inst
->exec_size
/ 8) +
2819 inst
->header_size
- 1].is_zero()) {
2820 inst
->mlen
-= inst
->exec_size
/ 8;
2826 invalidate_live_intervals();
2832 * Optimize sample messages which are followed by the final RT write.
2834 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2835 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2836 * final texturing results copied to the framebuffer write payload and modify
2837 * them to write to the framebuffer directly.
2840 fs_visitor::opt_sampler_eot()
2842 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2844 if (stage
!= MESA_SHADER_FRAGMENT
|| dispatch_width
> 16)
2847 if (devinfo
->gen
!= 9 && !devinfo
->is_cherryview
)
2850 /* FINISHME: It should be possible to implement this optimization when there
2851 * are multiple drawbuffers.
2853 if (key
->nr_color_regions
!= 1)
2856 /* Requires emitting a bunch of saturating MOV instructions during logical
2857 * send lowering to clamp the color payload, which the sampler unit isn't
2858 * going to do for us.
2860 if (key
->clamp_fragment_color
)
2863 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2864 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2865 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2866 assert(fb_write
->eot
);
2867 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE_LOGICAL
);
2869 /* There wasn't one; nothing to do. */
2870 if (unlikely(fb_write
->prev
->is_head_sentinel()))
2873 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2875 /* 3D Sampler » Messages » Message Format
2877 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2878 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2880 if (tex_inst
->opcode
!= SHADER_OPCODE_TEX_LOGICAL
&&
2881 tex_inst
->opcode
!= SHADER_OPCODE_TXD_LOGICAL
&&
2882 tex_inst
->opcode
!= SHADER_OPCODE_TXF_LOGICAL
&&
2883 tex_inst
->opcode
!= SHADER_OPCODE_TXL_LOGICAL
&&
2884 tex_inst
->opcode
!= FS_OPCODE_TXB_LOGICAL
&&
2885 tex_inst
->opcode
!= SHADER_OPCODE_TXF_CMS_LOGICAL
&&
2886 tex_inst
->opcode
!= SHADER_OPCODE_TXF_CMS_W_LOGICAL
&&
2887 tex_inst
->opcode
!= SHADER_OPCODE_TXF_UMS_LOGICAL
)
2890 /* XXX - This shouldn't be necessary. */
2891 if (tex_inst
->prev
->is_head_sentinel())
2894 /* Check that the FB write sources are fully initialized by the single
2895 * texturing instruction.
2897 for (unsigned i
= 0; i
< FB_WRITE_LOGICAL_NUM_SRCS
; i
++) {
2898 if (i
== FB_WRITE_LOGICAL_SRC_COLOR0
) {
2899 if (!fb_write
->src
[i
].equals(tex_inst
->dst
) ||
2900 fb_write
->size_read(i
) != tex_inst
->size_written
)
2902 } else if (i
!= FB_WRITE_LOGICAL_SRC_COMPONENTS
) {
2903 if (fb_write
->src
[i
].file
!= BAD_FILE
)
2908 assert(!tex_inst
->eot
); /* We can't get here twice */
2909 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2911 const fs_builder
ibld(this, block
, tex_inst
);
2913 tex_inst
->offset
|= fb_write
->target
<< 24;
2914 tex_inst
->eot
= true;
2915 tex_inst
->dst
= ibld
.null_reg_ud();
2916 tex_inst
->size_written
= 0;
2917 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2919 /* Marking EOT is sufficient, lower_logical_sends() will notice the EOT
2920 * flag and submit a header together with the sampler message as required
2923 invalidate_live_intervals();
2928 fs_visitor::opt_register_renaming()
2930 bool progress
= false;
2933 unsigned remap
[alloc
.count
];
2934 memset(remap
, ~0u, sizeof(unsigned) * alloc
.count
);
2936 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2937 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2939 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2940 inst
->opcode
== BRW_OPCODE_WHILE
) {
2944 /* Rewrite instruction sources. */
2945 for (int i
= 0; i
< inst
->sources
; i
++) {
2946 if (inst
->src
[i
].file
== VGRF
&&
2947 remap
[inst
->src
[i
].nr
] != ~0u &&
2948 remap
[inst
->src
[i
].nr
] != inst
->src
[i
].nr
) {
2949 inst
->src
[i
].nr
= remap
[inst
->src
[i
].nr
];
2954 const unsigned dst
= inst
->dst
.nr
;
2957 inst
->dst
.file
== VGRF
&&
2958 alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
== inst
->size_written
&&
2959 !inst
->is_partial_write()) {
2960 if (remap
[dst
] == ~0u) {
2963 remap
[dst
] = alloc
.allocate(regs_written(inst
));
2964 inst
->dst
.nr
= remap
[dst
];
2967 } else if (inst
->dst
.file
== VGRF
&&
2968 remap
[dst
] != ~0u &&
2969 remap
[dst
] != dst
) {
2970 inst
->dst
.nr
= remap
[dst
];
2976 invalidate_live_intervals();
2978 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2979 if (delta_xy
[i
].file
== VGRF
&& remap
[delta_xy
[i
].nr
] != ~0u) {
2980 delta_xy
[i
].nr
= remap
[delta_xy
[i
].nr
];
2989 * Remove redundant or useless discard jumps.
2991 * For example, we can eliminate jumps in the following sequence:
2993 * discard-jump (redundant with the next jump)
2994 * discard-jump (useless; jumps to the next instruction)
2998 fs_visitor::opt_redundant_discard_jumps()
3000 bool progress
= false;
3002 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
3004 fs_inst
*placeholder_halt
= NULL
;
3005 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
3006 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
3007 placeholder_halt
= inst
;
3012 if (!placeholder_halt
)
3015 /* Delete any HALTs immediately before the placeholder halt. */
3016 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
3017 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
3018 prev
= (fs_inst
*) placeholder_halt
->prev
) {
3019 prev
->remove(last_bblock
);
3024 invalidate_live_intervals();
3030 * Compute a bitmask with GRF granularity with a bit set for each GRF starting
3031 * from \p r.offset which overlaps the region starting at \p s.offset and
3032 * spanning \p ds bytes.
3034 static inline unsigned
3035 mask_relative_to(const fs_reg
&r
, const fs_reg
&s
, unsigned ds
)
3037 const int rel_offset
= reg_offset(s
) - reg_offset(r
);
3038 const int shift
= rel_offset
/ REG_SIZE
;
3039 const unsigned n
= DIV_ROUND_UP(rel_offset
% REG_SIZE
+ ds
, REG_SIZE
);
3040 assert(reg_space(r
) == reg_space(s
) &&
3041 shift
>= 0 && shift
< int(8 * sizeof(unsigned)));
3042 return ((1 << n
) - 1) << shift
;
3046 fs_visitor::opt_peephole_csel()
3048 if (devinfo
->gen
< 8)
3051 bool progress
= false;
3053 foreach_block_reverse(block
, cfg
) {
3054 int ip
= block
->end_ip
+ 1;
3056 foreach_inst_in_block_reverse_safe(fs_inst
, inst
, block
) {
3059 if (inst
->opcode
!= BRW_OPCODE_SEL
||
3060 inst
->predicate
!= BRW_PREDICATE_NORMAL
||
3061 (inst
->dst
.type
!= BRW_REGISTER_TYPE_F
&&
3062 inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3063 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3066 /* Because it is a 3-src instruction, CSEL cannot have an immediate
3067 * value as a source, but we can sometimes handle zero.
3069 if ((inst
->src
[0].file
!= VGRF
&& inst
->src
[0].file
!= ATTR
&&
3070 inst
->src
[0].file
!= UNIFORM
) ||
3071 (inst
->src
[1].file
!= VGRF
&& inst
->src
[1].file
!= ATTR
&&
3072 inst
->src
[1].file
!= UNIFORM
&& !inst
->src
[1].is_zero()))
3075 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3076 if (!scan_inst
->flags_written())
3079 if ((scan_inst
->opcode
!= BRW_OPCODE_CMP
&&
3080 scan_inst
->opcode
!= BRW_OPCODE_MOV
) ||
3081 scan_inst
->predicate
!= BRW_PREDICATE_NONE
||
3082 (scan_inst
->src
[0].file
!= VGRF
&&
3083 scan_inst
->src
[0].file
!= ATTR
&&
3084 scan_inst
->src
[0].file
!= UNIFORM
) ||
3085 scan_inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
)
3088 if (scan_inst
->opcode
== BRW_OPCODE_CMP
&& !scan_inst
->src
[1].is_zero())
3091 const brw::fs_builder
ibld(this, block
, inst
);
3093 const enum brw_conditional_mod cond
=
3094 inst
->predicate_inverse
3095 ? brw_negate_cmod(scan_inst
->conditional_mod
)
3096 : scan_inst
->conditional_mod
;
3098 fs_inst
*csel_inst
= NULL
;
3100 if (inst
->src
[1].file
!= IMM
) {
3101 csel_inst
= ibld
.CSEL(inst
->dst
,
3106 } else if (cond
== BRW_CONDITIONAL_NZ
) {
3107 /* Consider the sequence
3109 * cmp.nz.f0 null<1>F g3<8,8,1>F 0F
3110 * (+f0) sel g124<1>UD g2<8,8,1>UD 0x00000000UD
3112 * The sel will pick the immediate value 0 if r0 is ±0.0.
3113 * Therefore, this sequence is equivalent:
3115 * cmp.nz.f0 null<1>F g3<8,8,1>F 0F
3116 * (+f0) sel g124<1>F g2<8,8,1>F (abs)g3<8,8,1>F
3118 * The abs is ensures that the result is 0UD when g3 is -0.0F.
3119 * By normal cmp-sel merging, this is also equivalent:
3121 * csel.nz g124<1>F g2<4,4,1>F (abs)g3<4,4,1>F g3<4,4,1>F
3123 csel_inst
= ibld
.CSEL(inst
->dst
,
3129 csel_inst
->src
[1].abs
= true;
3132 if (csel_inst
!= NULL
) {
3134 csel_inst
->saturate
= inst
->saturate
;
3135 inst
->remove(block
);
3147 fs_visitor::compute_to_mrf()
3149 bool progress
= false;
3152 /* No MRFs on Gen >= 7. */
3153 if (devinfo
->gen
>= 7)
3156 calculate_live_intervals();
3158 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3162 if (inst
->opcode
!= BRW_OPCODE_MOV
||
3163 inst
->is_partial_write() ||
3164 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= VGRF
||
3165 inst
->dst
.type
!= inst
->src
[0].type
||
3166 inst
->src
[0].abs
|| inst
->src
[0].negate
||
3167 !inst
->src
[0].is_contiguous() ||
3168 inst
->src
[0].offset
% REG_SIZE
!= 0)
3171 /* Can't compute-to-MRF this GRF if someone else was going to
3174 if (this->virtual_grf_end
[inst
->src
[0].nr
] > ip
)
3177 /* Found a move of a GRF to a MRF. Let's see if we can go rewrite the
3178 * things that computed the value of all GRFs of the source region. The
3179 * regs_left bitset keeps track of the registers we haven't yet found a
3180 * generating instruction for.
3182 unsigned regs_left
= (1 << regs_read(inst
, 0)) - 1;
3184 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3185 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3186 inst
->src
[0], inst
->size_read(0))) {
3187 /* Found the last thing to write our reg we want to turn
3188 * into a compute-to-MRF.
3191 /* If this one instruction didn't populate all the
3192 * channels, bail. We might be able to rewrite everything
3193 * that writes that reg, but it would require smarter
3196 if (scan_inst
->is_partial_write())
3199 /* Handling things not fully contained in the source of the copy
3200 * would need us to understand coalescing out more than one MOV at
3203 if (!region_contained_in(scan_inst
->dst
, scan_inst
->size_written
,
3204 inst
->src
[0], inst
->size_read(0)))
3207 /* SEND instructions can't have MRF as a destination. */
3208 if (scan_inst
->mlen
)
3211 if (devinfo
->gen
== 6) {
3212 /* gen6 math instructions must have the destination be
3213 * GRF, so no compute-to-MRF for them.
3215 if (scan_inst
->is_math()) {
3220 /* Clear the bits for any registers this instruction overwrites. */
3221 regs_left
&= ~mask_relative_to(
3222 inst
->src
[0], scan_inst
->dst
, scan_inst
->size_written
);
3227 /* We don't handle control flow here. Most computation of
3228 * values that end up in MRFs are shortly before the MRF
3231 if (block
->start() == scan_inst
)
3234 /* You can't read from an MRF, so if someone else reads our
3235 * MRF's source GRF that we wanted to rewrite, that stops us.
3237 bool interfered
= false;
3238 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
3239 if (regions_overlap(scan_inst
->src
[i
], scan_inst
->size_read(i
),
3240 inst
->src
[0], inst
->size_read(0))) {
3247 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3248 inst
->dst
, inst
->size_written
)) {
3249 /* If somebody else writes our MRF here, we can't
3250 * compute-to-MRF before that.
3255 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1 &&
3256 regions_overlap(fs_reg(MRF
, scan_inst
->base_mrf
), scan_inst
->mlen
* REG_SIZE
,
3257 inst
->dst
, inst
->size_written
)) {
3258 /* Found a SEND instruction, which means that there are
3259 * live values in MRFs from base_mrf to base_mrf +
3260 * scan_inst->mlen - 1. Don't go pushing our MRF write up
3270 /* Found all generating instructions of our MRF's source value, so it
3271 * should be safe to rewrite them to point to the MRF directly.
3273 regs_left
= (1 << regs_read(inst
, 0)) - 1;
3275 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3276 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3277 inst
->src
[0], inst
->size_read(0))) {
3278 /* Clear the bits for any registers this instruction overwrites. */
3279 regs_left
&= ~mask_relative_to(
3280 inst
->src
[0], scan_inst
->dst
, scan_inst
->size_written
);
3282 const unsigned rel_offset
= reg_offset(scan_inst
->dst
) -
3283 reg_offset(inst
->src
[0]);
3285 if (inst
->dst
.nr
& BRW_MRF_COMPR4
) {
3286 /* Apply the same address transformation done by the hardware
3287 * for COMPR4 MRF writes.
3289 assert(rel_offset
< 2 * REG_SIZE
);
3290 scan_inst
->dst
.nr
= inst
->dst
.nr
+ rel_offset
/ REG_SIZE
* 4;
3292 /* Clear the COMPR4 bit if the generating instruction is not
3295 if (scan_inst
->size_written
< 2 * REG_SIZE
)
3296 scan_inst
->dst
.nr
&= ~BRW_MRF_COMPR4
;
3299 /* Calculate the MRF number the result of this instruction is
3300 * ultimately written to.
3302 scan_inst
->dst
.nr
= inst
->dst
.nr
+ rel_offset
/ REG_SIZE
;
3305 scan_inst
->dst
.file
= MRF
;
3306 scan_inst
->dst
.offset
= inst
->dst
.offset
+ rel_offset
% REG_SIZE
;
3307 scan_inst
->saturate
|= inst
->saturate
;
3314 inst
->remove(block
);
3319 invalidate_live_intervals();
3325 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
3326 * flow. We could probably do better here with some form of divergence
3330 fs_visitor::eliminate_find_live_channel()
3332 bool progress
= false;
3335 if (!brw_stage_has_packed_dispatch(devinfo
, stage
, stage_prog_data
)) {
3336 /* The optimization below assumes that channel zero is live on thread
3337 * dispatch, which may not be the case if the fixed function dispatches
3343 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3344 switch (inst
->opcode
) {
3350 case BRW_OPCODE_ENDIF
:
3351 case BRW_OPCODE_WHILE
:
3355 case FS_OPCODE_DISCARD_JUMP
:
3356 /* This can potentially make control flow non-uniform until the end
3361 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
3363 inst
->opcode
= BRW_OPCODE_MOV
;
3364 inst
->src
[0] = brw_imm_ud(0u);
3366 inst
->force_writemask_all
= true;
3380 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
3381 * instructions to FS_OPCODE_REP_FB_WRITE.
3384 fs_visitor::emit_repclear_shader()
3386 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3388 int color_mrf
= base_mrf
+ 2;
3392 mov
= bld
.exec_all().group(4, 0)
3393 .MOV(brw_message_reg(color_mrf
),
3394 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
3396 struct brw_reg reg
=
3397 brw_reg(BRW_GENERAL_REGISTER_FILE
, 2, 3, 0, 0, BRW_REGISTER_TYPE_F
,
3398 BRW_VERTICAL_STRIDE_8
, BRW_WIDTH_2
, BRW_HORIZONTAL_STRIDE_4
,
3399 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
3401 mov
= bld
.exec_all().group(4, 0)
3402 .MOV(vec4(brw_message_reg(color_mrf
)), fs_reg(reg
));
3405 fs_inst
*write
= NULL
;
3406 if (key
->nr_color_regions
== 1) {
3407 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
3408 write
->saturate
= key
->clamp_fragment_color
;
3409 write
->base_mrf
= color_mrf
;
3411 write
->header_size
= 0;
3414 assume(key
->nr_color_regions
> 0);
3416 struct brw_reg header
=
3417 retype(brw_message_reg(base_mrf
), BRW_REGISTER_TYPE_UD
);
3418 bld
.exec_all().group(16, 0)
3419 .MOV(header
, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
3421 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
3423 bld
.exec_all().group(1, 0)
3424 .MOV(component(header
, 2), brw_imm_ud(i
));
3427 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
3428 write
->saturate
= key
->clamp_fragment_color
;
3429 write
->base_mrf
= base_mrf
;
3431 write
->header_size
= 2;
3436 write
->last_rt
= true;
3440 assign_constant_locations();
3441 assign_curb_setup();
3443 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
3445 assert(mov
->src
[0].file
== FIXED_GRF
);
3446 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].nr
, 0);
3451 * Walks through basic blocks, looking for repeated MRF writes and
3452 * removing the later ones.
3455 fs_visitor::remove_duplicate_mrf_writes()
3457 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
3458 bool progress
= false;
3460 /* Need to update the MRF tracking for compressed instructions. */
3461 if (dispatch_width
>= 16)
3464 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3466 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3467 if (inst
->is_control_flow()) {
3468 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3471 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3472 inst
->dst
.file
== MRF
) {
3473 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.nr
];
3474 if (prev_inst
&& prev_inst
->opcode
== BRW_OPCODE_MOV
&&
3475 inst
->dst
.equals(prev_inst
->dst
) &&
3476 inst
->src
[0].equals(prev_inst
->src
[0]) &&
3477 inst
->saturate
== prev_inst
->saturate
&&
3478 inst
->predicate
== prev_inst
->predicate
&&
3479 inst
->conditional_mod
== prev_inst
->conditional_mod
&&
3480 inst
->exec_size
== prev_inst
->exec_size
) {
3481 inst
->remove(block
);
3487 /* Clear out the last-write records for MRFs that were overwritten. */
3488 if (inst
->dst
.file
== MRF
) {
3489 last_mrf_move
[inst
->dst
.nr
] = NULL
;
3492 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
3493 /* Found a SEND instruction, which will include two or fewer
3494 * implied MRF writes. We could do better here.
3496 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
3497 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
3501 /* Clear out any MRF move records whose sources got overwritten. */
3502 for (unsigned i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
3503 if (last_mrf_move
[i
] &&
3504 regions_overlap(inst
->dst
, inst
->size_written
,
3505 last_mrf_move
[i
]->src
[0],
3506 last_mrf_move
[i
]->size_read(0))) {
3507 last_mrf_move
[i
] = NULL
;
3511 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3512 inst
->dst
.file
== MRF
&&
3513 inst
->src
[0].file
!= ARF
&&
3514 !inst
->is_partial_write()) {
3515 last_mrf_move
[inst
->dst
.nr
] = inst
;
3520 invalidate_live_intervals();
3526 * Rounding modes for conversion instructions are included for each
3527 * conversion, but right now it is a state. So once it is set,
3528 * we don't need to call it again for subsequent calls.
3530 * This is useful for vector/matrices conversions, as setting the
3531 * mode once is enough for the full vector/matrix
3534 fs_visitor::remove_extra_rounding_modes()
3536 bool progress
= false;
3538 foreach_block (block
, cfg
) {
3539 brw_rnd_mode prev_mode
= BRW_RND_MODE_UNSPECIFIED
;
3541 foreach_inst_in_block_safe (fs_inst
, inst
, block
) {
3542 if (inst
->opcode
== SHADER_OPCODE_RND_MODE
) {
3543 assert(inst
->src
[0].file
== BRW_IMMEDIATE_VALUE
);
3544 const brw_rnd_mode mode
= (brw_rnd_mode
) inst
->src
[0].d
;
3545 if (mode
== prev_mode
) {
3546 inst
->remove(block
);
3556 invalidate_live_intervals();
3562 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
3564 /* Clear the flag for registers that actually got read (as expected). */
3565 for (int i
= 0; i
< inst
->sources
; i
++) {
3567 if (inst
->src
[i
].file
== VGRF
|| inst
->src
[i
].file
== FIXED_GRF
) {
3568 grf
= inst
->src
[i
].nr
;
3573 if (grf
>= first_grf
&&
3574 grf
< first_grf
+ grf_len
) {
3575 deps
[grf
- first_grf
] = false;
3576 if (inst
->exec_size
== 16)
3577 deps
[grf
- first_grf
+ 1] = false;
3583 * Implements this workaround for the original 965:
3585 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
3586 * check for post destination dependencies on this instruction, software
3587 * must ensure that there is no destination hazard for the case of ‘write
3588 * followed by a posted write’ shown in the following example.
3591 * 2. send r3.xy <rest of send instruction>
3594 * Due to no post-destination dependency check on the ‘send’, the above
3595 * code sequence could have two instructions (1 and 2) in flight at the
3596 * same time that both consider ‘r3’ as the target of their final writes.
3599 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
3602 int write_len
= regs_written(inst
);
3603 int first_write_grf
= inst
->dst
.nr
;
3604 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3605 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3607 memset(needs_dep
, false, sizeof(needs_dep
));
3608 memset(needs_dep
, true, write_len
);
3610 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
3612 /* Walk backwards looking for writes to registers we're writing which
3613 * aren't read since being written. If we hit the start of the program,
3614 * we assume that there are no outstanding dependencies on entry to the
3617 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3618 /* If we hit control flow, assume that there *are* outstanding
3619 * dependencies, and force their cleanup before our instruction.
3621 if (block
->start() == scan_inst
&& block
->num
!= 0) {
3622 for (int i
= 0; i
< write_len
; i
++) {
3624 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
3625 first_write_grf
+ i
);
3630 /* We insert our reads as late as possible on the assumption that any
3631 * instruction but a MOV that might have left us an outstanding
3632 * dependency has more latency than a MOV.
3634 if (scan_inst
->dst
.file
== VGRF
) {
3635 for (unsigned i
= 0; i
< regs_written(scan_inst
); i
++) {
3636 int reg
= scan_inst
->dst
.nr
+ i
;
3638 if (reg
>= first_write_grf
&&
3639 reg
< first_write_grf
+ write_len
&&
3640 needs_dep
[reg
- first_write_grf
]) {
3641 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
3642 needs_dep
[reg
- first_write_grf
] = false;
3643 if (scan_inst
->exec_size
== 16)
3644 needs_dep
[reg
- first_write_grf
+ 1] = false;
3649 /* Clear the flag for registers that actually got read (as expected). */
3650 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3652 /* Continue the loop only if we haven't resolved all the dependencies */
3654 for (i
= 0; i
< write_len
; i
++) {
3664 * Implements this workaround for the original 965:
3666 * "[DevBW, DevCL] Errata: A destination register from a send can not be
3667 * used as a destination register until after it has been sourced by an
3668 * instruction with a different destination register.
3671 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
3673 int write_len
= regs_written(inst
);
3674 unsigned first_write_grf
= inst
->dst
.nr
;
3675 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3676 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3678 memset(needs_dep
, false, sizeof(needs_dep
));
3679 memset(needs_dep
, true, write_len
);
3680 /* Walk forwards looking for writes to registers we're writing which aren't
3681 * read before being written.
3683 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
) {
3684 /* If we hit control flow, force resolve all remaining dependencies. */
3685 if (block
->end() == scan_inst
&& block
->num
!= cfg
->num_blocks
- 1) {
3686 for (int i
= 0; i
< write_len
; i
++) {
3688 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3689 first_write_grf
+ i
);
3694 /* Clear the flag for registers that actually got read (as expected). */
3695 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3697 /* We insert our reads as late as possible since they're reading the
3698 * result of a SEND, which has massive latency.
3700 if (scan_inst
->dst
.file
== VGRF
&&
3701 scan_inst
->dst
.nr
>= first_write_grf
&&
3702 scan_inst
->dst
.nr
< first_write_grf
+ write_len
&&
3703 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
]) {
3704 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3706 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
] = false;
3709 /* Continue the loop only if we haven't resolved all the dependencies */
3711 for (i
= 0; i
< write_len
; i
++) {
3721 fs_visitor::insert_gen4_send_dependency_workarounds()
3723 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
3726 bool progress
= false;
3728 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3729 if (inst
->mlen
!= 0 && inst
->dst
.file
== VGRF
) {
3730 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3731 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3737 invalidate_live_intervals();
3741 * Turns the generic expression-style uniform pull constant load instruction
3742 * into a hardware-specific series of instructions for loading a pull
3745 * The expression style allows the CSE pass before this to optimize out
3746 * repeated loads from the same offset, and gives the pre-register-allocation
3747 * scheduling full flexibility, while the conversion to native instructions
3748 * allows the post-register-allocation scheduler the best information
3751 * Note that execution masking for setting up pull constant loads is special:
3752 * the channels that need to be written are unrelated to the current execution
3753 * mask, since a later instruction will use one of the result channels as a
3754 * source operand for all 8 or 16 of its channels.
3757 fs_visitor::lower_uniform_pull_constant_loads()
3759 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3760 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3763 if (devinfo
->gen
>= 7) {
3764 const fs_builder ubld
= fs_builder(this, block
, inst
).exec_all();
3765 const fs_reg payload
= ubld
.group(8, 0).vgrf(BRW_REGISTER_TYPE_UD
);
3767 ubld
.group(8, 0).MOV(payload
,
3768 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
3769 ubld
.group(1, 0).MOV(component(payload
, 2),
3770 brw_imm_ud(inst
->src
[1].ud
/ 16));
3772 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3773 inst
->src
[1] = payload
;
3774 inst
->header_size
= 1;
3777 invalidate_live_intervals();
3779 /* Before register allocation, we didn't tell the scheduler about the
3780 * MRF we use. We know it's safe to use this MRF because nothing
3781 * else does except for register spill/unspill, which generates and
3782 * uses its MRF within a single IR instruction.
3784 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
3791 fs_visitor::lower_load_payload()
3793 bool progress
= false;
3795 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3796 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3799 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== VGRF
);
3800 assert(inst
->saturate
== false);
3801 fs_reg dst
= inst
->dst
;
3803 /* Get rid of COMPR4. We'll add it back in if we need it */
3804 if (dst
.file
== MRF
)
3805 dst
.nr
= dst
.nr
& ~BRW_MRF_COMPR4
;
3807 const fs_builder
ibld(this, block
, inst
);
3808 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3810 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3811 if (inst
->src
[i
].file
!= BAD_FILE
) {
3812 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3813 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3814 hbld
.MOV(mov_dst
, mov_src
);
3816 dst
= offset(dst
, hbld
, 1);
3819 if (inst
->dst
.file
== MRF
&& (inst
->dst
.nr
& BRW_MRF_COMPR4
) &&
3820 inst
->exec_size
> 8) {
3821 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3822 * a straightforward copy. Instead, the result of the
3823 * LOAD_PAYLOAD is treated as interleaved and the first four
3824 * non-header sources are unpacked as:
3835 * This is used for gen <= 5 fb writes.
3837 assert(inst
->exec_size
== 16);
3838 assert(inst
->header_size
+ 4 <= inst
->sources
);
3839 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3840 if (inst
->src
[i
].file
!= BAD_FILE
) {
3841 if (devinfo
->has_compr4
) {
3842 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3843 compr4_dst
.nr
|= BRW_MRF_COMPR4
;
3844 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3846 /* Platform doesn't have COMPR4. We have to fake it */
3847 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3848 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3850 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3857 /* The loop above only ever incremented us through the first set
3858 * of 4 registers. However, thanks to the magic of COMPR4, we
3859 * actually wrote to the first 8 registers, so we need to take
3860 * that into account now.
3864 /* The COMPR4 code took care of the first 4 sources. We'll let
3865 * the regular path handle any remaining sources. Yes, we are
3866 * modifying the instruction but we're about to delete it so
3867 * this really doesn't hurt anything.
3869 inst
->header_size
+= 4;
3872 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3873 if (inst
->src
[i
].file
!= BAD_FILE
) {
3874 dst
.type
= inst
->src
[i
].type
;
3875 ibld
.MOV(dst
, inst
->src
[i
]);
3877 dst
.type
= BRW_REGISTER_TYPE_UD
;
3879 dst
= offset(dst
, ibld
, 1);
3882 inst
->remove(block
);
3887 invalidate_live_intervals();
3893 fs_visitor::lower_integer_multiplication()
3895 bool progress
= false;
3897 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3898 const fs_builder
ibld(this, block
, inst
);
3900 if (inst
->opcode
== BRW_OPCODE_MUL
) {
3901 if (inst
->dst
.is_accumulator() ||
3902 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3903 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3906 if (devinfo
->has_integer_dword_mul
)
3909 if (inst
->src
[1].file
== IMM
&&
3910 inst
->src
[1].ud
< (1 << 16)) {
3911 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3912 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3915 * If multiplying by an immediate value that fits in 16-bits, do a
3916 * single MUL instruction with that value in the proper location.
3918 if (devinfo
->gen
< 7) {
3919 fs_reg
imm(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3921 ibld
.MOV(imm
, inst
->src
[1]);
3922 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3924 const bool ud
= (inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3925 ibld
.MUL(inst
->dst
, inst
->src
[0],
3926 ud
? brw_imm_uw(inst
->src
[1].ud
)
3927 : brw_imm_w(inst
->src
[1].d
));
3930 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3931 * do 32-bit integer multiplication in one instruction, but instead
3932 * must do a sequence (which actually calculates a 64-bit result):
3934 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3935 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3936 * mov(8) g2<1>D acc0<8,8,1>D
3938 * But on Gen > 6, the ability to use second accumulator register
3939 * (acc1) for non-float data types was removed, preventing a simple
3940 * implementation in SIMD16. A 16-channel result can be calculated by
3941 * executing the three instructions twice in SIMD8, once with quarter
3942 * control of 1Q for the first eight channels and again with 2Q for
3943 * the second eight channels.
3945 * Which accumulator register is implicitly accessed (by AccWrEnable
3946 * for instance) is determined by the quarter control. Unfortunately
3947 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3948 * implicit accumulator access by an instruction with 2Q will access
3949 * acc1 regardless of whether the data type is usable in acc1.
3951 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3952 * integer data types.
3954 * Since we only want the low 32-bits of the result, we can do two
3955 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3956 * adjust the high result and add them (like the mach is doing):
3958 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3959 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3960 * shl(8) g9<1>D g8<8,8,1>D 16D
3961 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3963 * We avoid the shl instruction by realizing that we only want to add
3964 * the low 16-bits of the "high" result to the high 16-bits of the
3965 * "low" result and using proper regioning on the add:
3967 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3968 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3969 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3971 * Since it does not use the (single) accumulator register, we can
3972 * schedule multi-component multiplications much better.
3975 bool needs_mov
= false;
3976 fs_reg orig_dst
= inst
->dst
;
3978 /* Get a new VGRF for the "low" 32x16-bit multiplication result if
3979 * reusing the original destination is impossible due to hardware
3980 * restrictions, source/destination overlap, or it being the null
3983 fs_reg low
= inst
->dst
;
3984 if (orig_dst
.is_null() || orig_dst
.file
== MRF
||
3985 regions_overlap(inst
->dst
, inst
->size_written
,
3986 inst
->src
[0], inst
->size_read(0)) ||
3987 regions_overlap(inst
->dst
, inst
->size_written
,
3988 inst
->src
[1], inst
->size_read(1)) ||
3989 inst
->dst
.stride
>= 4) {
3991 low
= fs_reg(VGRF
, alloc
.allocate(regs_written(inst
)),
3995 /* Get a new VGRF but keep the same stride as inst->dst */
3996 fs_reg
high(VGRF
, alloc
.allocate(regs_written(inst
)),
3998 high
.stride
= inst
->dst
.stride
;
3999 high
.offset
= inst
->dst
.offset
% REG_SIZE
;
4001 if (devinfo
->gen
>= 7) {
4002 if (inst
->src
[1].abs
)
4003 lower_src_modifiers(this, block
, inst
, 1);
4005 if (inst
->src
[1].file
== IMM
) {
4006 ibld
.MUL(low
, inst
->src
[0],
4007 brw_imm_uw(inst
->src
[1].ud
& 0xffff));
4008 ibld
.MUL(high
, inst
->src
[0],
4009 brw_imm_uw(inst
->src
[1].ud
>> 16));
4011 ibld
.MUL(low
, inst
->src
[0],
4012 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 0));
4013 ibld
.MUL(high
, inst
->src
[0],
4014 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 1));
4017 if (inst
->src
[0].abs
)
4018 lower_src_modifiers(this, block
, inst
, 0);
4020 ibld
.MUL(low
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UW
, 0),
4022 ibld
.MUL(high
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UW
, 1),
4026 ibld
.ADD(subscript(low
, BRW_REGISTER_TYPE_UW
, 1),
4027 subscript(low
, BRW_REGISTER_TYPE_UW
, 1),
4028 subscript(high
, BRW_REGISTER_TYPE_UW
, 0));
4030 if (needs_mov
|| inst
->conditional_mod
) {
4031 set_condmod(inst
->conditional_mod
,
4032 ibld
.MOV(orig_dst
, low
));
4036 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
4037 /* According to the BDW+ BSpec page for the "Multiply Accumulate
4038 * High" instruction:
4040 * "An added preliminary mov is required for source modification on
4042 * mov (8) r3.0<1>:d -r3<8;8,1>:d
4043 * mul (8) acc0:d r2.0<8;8,1>:d r3.0<16;8,2>:uw
4044 * mach (8) r5.0<1>:d r2.0<8;8,1>:d r3.0<8;8,1>:d"
4046 if (devinfo
->gen
>= 8 && (inst
->src
[1].negate
|| inst
->src
[1].abs
))
4047 lower_src_modifiers(this, block
, inst
, 1);
4049 /* Should have been lowered to 8-wide. */
4050 assert(inst
->exec_size
<= get_lowered_simd_width(devinfo
, inst
));
4051 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
),
4053 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
4054 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
4056 if (devinfo
->gen
>= 8) {
4057 /* Until Gen8, integer multiplies read 32-bits from one source,
4058 * and 16-bits from the other, and relying on the MACH instruction
4059 * to generate the high bits of the result.
4061 * On Gen8, the multiply instruction does a full 32x32-bit
4062 * multiply, but in order to do a 64-bit multiply we can simulate
4063 * the previous behavior and then use a MACH instruction.
4065 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
4066 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
4067 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
4068 mul
->src
[1].stride
*= 2;
4070 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
4072 /* Among other things the quarter control bits influence which
4073 * accumulator register is used by the hardware for instructions
4074 * that access the accumulator implicitly (e.g. MACH). A
4075 * second-half instruction would normally map to acc1, which
4076 * doesn't exist on Gen7 and up (the hardware does emulate it for
4077 * floating-point instructions *only* by taking advantage of the
4078 * extra precision of acc0 not normally used for floating point
4081 * HSW and up are careful enough not to try to access an
4082 * accumulator register that doesn't exist, but on earlier Gen7
4083 * hardware we need to make sure that the quarter control bits are
4084 * zero to avoid non-deterministic behaviour and emit an extra MOV
4085 * to get the result masked correctly according to the current
4089 mach
->force_writemask_all
= true;
4090 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
4091 ibld
.MOV(inst
->dst
, mach
->dst
);
4097 inst
->remove(block
);
4102 invalidate_live_intervals();
4108 fs_visitor::lower_minmax()
4110 assert(devinfo
->gen
< 6);
4112 bool progress
= false;
4114 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4115 const fs_builder
ibld(this, block
, inst
);
4117 if (inst
->opcode
== BRW_OPCODE_SEL
&&
4118 inst
->predicate
== BRW_PREDICATE_NONE
) {
4119 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
4120 * the original SEL.L/GE instruction
4122 ibld
.CMP(ibld
.null_reg_d(), inst
->src
[0], inst
->src
[1],
4123 inst
->conditional_mod
);
4124 inst
->predicate
= BRW_PREDICATE_NORMAL
;
4125 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
4132 invalidate_live_intervals();
4138 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
4139 fs_reg
*dst
, fs_reg color
, unsigned components
)
4141 if (key
->clamp_fragment_color
) {
4142 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
4143 assert(color
.type
== BRW_REGISTER_TYPE_F
);
4145 for (unsigned i
= 0; i
< components
; i
++)
4147 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
4152 for (unsigned i
= 0; i
< components
; i
++)
4153 dst
[i
] = offset(color
, bld
, i
);
4157 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
4158 const struct brw_wm_prog_data
*prog_data
,
4159 const brw_wm_prog_key
*key
,
4160 const fs_visitor::thread_payload
&payload
)
4162 assert(inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
4163 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4164 const fs_reg
&color0
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR0
];
4165 const fs_reg
&color1
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
];
4166 const fs_reg
&src0_alpha
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA
];
4167 const fs_reg
&src_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
];
4168 const fs_reg
&dst_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_DST_DEPTH
];
4169 const fs_reg
&src_stencil
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_STENCIL
];
4170 fs_reg sample_mask
= inst
->src
[FB_WRITE_LOGICAL_SRC_OMASK
];
4171 const unsigned components
=
4172 inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
4174 /* We can potentially have a message length of up to 15, so we have to set
4175 * base_mrf to either 0 or 1 in order to fit in m0..m15.
4178 int header_size
= 2, payload_header_size
;
4179 unsigned length
= 0;
4181 if (devinfo
->gen
< 6) {
4182 /* TODO: Support SIMD32 on gen4-5 */
4183 assert(bld
.group() < 16);
4185 /* For gen4-5, we always have a header consisting of g0 and g1. We have
4186 * an implied MOV from g0,g1 to the start of the message. The MOV from
4187 * g0 is handled by the hardware and the MOV from g1 is provided by the
4188 * generator. This is required because, on gen4-5, the generator may
4189 * generate two write messages with different message lengths in order
4190 * to handle AA data properly.
4192 * Also, since the pixel mask goes in the g0 portion of the message and
4193 * since render target writes are the last thing in the shader, we write
4194 * the pixel mask directly into g0 and it will get copied as part of the
4197 if (prog_data
->uses_kill
) {
4198 bld
.exec_all().group(1, 0)
4199 .MOV(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
),
4200 brw_flag_reg(0, 1));
4203 assert(length
== 0);
4205 } else if ((devinfo
->gen
<= 7 && !devinfo
->is_haswell
&&
4206 prog_data
->uses_kill
) ||
4207 color1
.file
!= BAD_FILE
||
4208 key
->nr_color_regions
> 1) {
4209 /* From the Sandy Bridge PRM, volume 4, page 198:
4211 * "Dispatched Pixel Enables. One bit per pixel indicating
4212 * which pixels were originally enabled when the thread was
4213 * dispatched. This field is only required for the end-of-
4214 * thread message and on all dual-source messages."
4216 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4218 fs_reg header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
4219 if (bld
.group() < 16) {
4220 /* The header starts off as g0 and g1 for the first half */
4221 ubld
.group(16, 0).MOV(header
, retype(brw_vec8_grf(0, 0),
4222 BRW_REGISTER_TYPE_UD
));
4224 /* The header starts off as g0 and g2 for the second half */
4225 assert(bld
.group() < 32);
4226 const fs_reg header_sources
[2] = {
4227 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
),
4228 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD
),
4230 ubld
.LOAD_PAYLOAD(header
, header_sources
, 2, 0);
4233 uint32_t g00_bits
= 0;
4235 /* Set "Source0 Alpha Present to RenderTarget" bit in message
4238 if (inst
->target
> 0 && prog_data
->replicate_alpha
)
4239 g00_bits
|= 1 << 11;
4241 /* Set computes stencil to render target */
4242 if (prog_data
->computed_stencil
)
4243 g00_bits
|= 1 << 14;
4246 /* OR extra bits into g0.0 */
4247 ubld
.group(1, 0).OR(component(header
, 0),
4248 retype(brw_vec1_grf(0, 0),
4249 BRW_REGISTER_TYPE_UD
),
4250 brw_imm_ud(g00_bits
));
4253 /* Set the render target index for choosing BLEND_STATE. */
4254 if (inst
->target
> 0) {
4255 ubld
.group(1, 0).MOV(component(header
, 2), brw_imm_ud(inst
->target
));
4258 if (prog_data
->uses_kill
) {
4259 assert(bld
.group() < 16);
4260 ubld
.group(1, 0).MOV(retype(component(header
, 15),
4261 BRW_REGISTER_TYPE_UW
),
4262 brw_flag_reg(0, 1));
4265 assert(length
== 0);
4266 sources
[0] = header
;
4267 sources
[1] = horiz_offset(header
, 8);
4270 assert(length
== 0 || length
== 2);
4271 header_size
= length
;
4273 if (payload
.aa_dest_stencil_reg
[0]) {
4274 assert(inst
->group
< 16);
4275 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1));
4276 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
4277 .MOV(sources
[length
],
4278 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
[0], 0)));
4282 if (src0_alpha
.file
!= BAD_FILE
) {
4283 for (unsigned i
= 0; i
< bld
.dispatch_width() / 8; i
++) {
4284 const fs_builder
&ubld
= bld
.exec_all().group(8, i
)
4285 .annotate("FB write src0 alpha");
4286 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_F
);
4287 ubld
.MOV(tmp
, horiz_offset(src0_alpha
, i
* 8));
4288 setup_color_payload(ubld
, key
, &sources
[length
], tmp
, 1);
4291 } else if (prog_data
->replicate_alpha
&& inst
->target
!= 0) {
4292 /* Handle the case when fragment shader doesn't write to draw buffer
4293 * zero. No need to call setup_color_payload() for src0_alpha because
4294 * alpha value will be undefined.
4296 length
+= bld
.dispatch_width() / 8;
4299 if (sample_mask
.file
!= BAD_FILE
) {
4300 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1),
4301 BRW_REGISTER_TYPE_UD
);
4303 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
4304 * relevant. Since it's unsigned single words one vgrf is always
4305 * 16-wide, but only the lower or higher 8 channels will be used by the
4306 * hardware when doing a SIMD8 write depending on whether we have
4307 * selected the subspans for the first or second half respectively.
4309 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
4310 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
4311 sample_mask
.stride
*= 2;
4313 bld
.exec_all().annotate("FB write oMask")
4314 .MOV(horiz_offset(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
4320 payload_header_size
= length
;
4322 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
4325 if (color1
.file
!= BAD_FILE
) {
4326 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
4330 if (src_depth
.file
!= BAD_FILE
) {
4331 sources
[length
] = src_depth
;
4335 if (dst_depth
.file
!= BAD_FILE
) {
4336 sources
[length
] = dst_depth
;
4340 if (src_stencil
.file
!= BAD_FILE
) {
4341 assert(devinfo
->gen
>= 9);
4342 assert(bld
.dispatch_width() == 8);
4344 /* XXX: src_stencil is only available on gen9+. dst_depth is never
4345 * available on gen9+. As such it's impossible to have both enabled at the
4346 * same time and therefore length cannot overrun the array.
4348 assert(length
< 15);
4350 sources
[length
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4351 bld
.exec_all().annotate("FB write OS")
4352 .MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UB
),
4353 subscript(src_stencil
, BRW_REGISTER_TYPE_UB
, 0));
4358 if (devinfo
->gen
>= 7) {
4359 /* Send from the GRF */
4360 fs_reg payload
= fs_reg(VGRF
, -1, BRW_REGISTER_TYPE_F
);
4361 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
4362 payload
.nr
= bld
.shader
->alloc
.allocate(regs_written(load
));
4363 load
->dst
= payload
;
4365 inst
->src
[0] = payload
;
4366 inst
->resize_sources(1);
4368 /* Send from the MRF */
4369 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
4370 sources
, length
, payload_header_size
);
4372 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
4373 * will do this for us if we just give it a COMPR4 destination.
4375 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
4376 load
->dst
.nr
|= BRW_MRF_COMPR4
;
4378 if (devinfo
->gen
< 6) {
4379 /* Set up src[0] for the implied MOV from grf0-1 */
4380 inst
->resize_sources(1);
4381 inst
->src
[0] = brw_vec8_grf(0, 0);
4383 inst
->resize_sources(0);
4388 inst
->opcode
= FS_OPCODE_FB_WRITE
;
4389 inst
->mlen
= regs_written(load
);
4390 inst
->header_size
= header_size
;
4394 lower_fb_read_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
4396 const fs_builder
&ubld
= bld
.exec_all().group(8, 0);
4397 const unsigned length
= 2;
4398 const fs_reg header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, length
);
4400 if (bld
.group() < 16) {
4401 ubld
.group(16, 0).MOV(header
, retype(brw_vec8_grf(0, 0),
4402 BRW_REGISTER_TYPE_UD
));
4404 assert(bld
.group() < 32);
4405 const fs_reg header_sources
[] = {
4406 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
),
4407 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD
)
4409 ubld
.LOAD_PAYLOAD(header
, header_sources
, ARRAY_SIZE(header_sources
), 0);
4412 inst
->resize_sources(1);
4413 inst
->src
[0] = header
;
4414 inst
->opcode
= FS_OPCODE_FB_READ
;
4415 inst
->mlen
= length
;
4416 inst
->header_size
= length
;
4420 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4421 const fs_reg
&coordinate
,
4422 const fs_reg
&shadow_c
,
4423 const fs_reg
&lod
, const fs_reg
&lod2
,
4424 const fs_reg
&surface
,
4425 const fs_reg
&sampler
,
4426 unsigned coord_components
,
4427 unsigned grad_components
)
4429 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
4430 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
4431 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
4432 fs_reg msg_end
= msg_begin
;
4435 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
4437 for (unsigned i
= 0; i
< coord_components
; i
++)
4438 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
4439 offset(coordinate
, bld
, i
));
4441 msg_end
= offset(msg_end
, bld
, coord_components
);
4443 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
4444 * require all three components to be present and zero if they are unused.
4446 if (coord_components
> 0 &&
4447 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
4448 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
4449 for (unsigned i
= coord_components
; i
< 3; i
++)
4450 bld
.MOV(offset(msg_end
, bld
, i
), brw_imm_f(0.0f
));
4452 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
4455 if (op
== SHADER_OPCODE_TXD
) {
4456 /* TXD unsupported in SIMD16 mode. */
4457 assert(bld
.dispatch_width() == 8);
4459 /* the slots for u and v are always present, but r is optional */
4460 if (coord_components
< 2)
4461 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
4464 * dPdx = dudx, dvdx, drdx
4465 * dPdy = dudy, dvdy, drdy
4467 * 1-arg: Does not exist.
4469 * 2-arg: dudx dvdx dudy dvdy
4470 * dPdx.x dPdx.y dPdy.x dPdy.y
4473 * 3-arg: dudx dvdx drdx dudy dvdy drdy
4474 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
4475 * m5 m6 m7 m8 m9 m10
4477 for (unsigned i
= 0; i
< grad_components
; i
++)
4478 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
4480 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
4482 for (unsigned i
= 0; i
< grad_components
; i
++)
4483 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
4485 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
4489 /* Bias/LOD with shadow comparator is unsupported in SIMD16 -- *Without*
4490 * shadow comparator (including RESINFO) it's unsupported in SIMD8 mode.
4492 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
4493 bld
.dispatch_width() == 16);
4495 const brw_reg_type type
=
4496 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
4497 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
4498 bld
.MOV(retype(msg_end
, type
), lod
);
4499 msg_end
= offset(msg_end
, bld
, 1);
4502 if (shadow_c
.file
!= BAD_FILE
) {
4503 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
4504 /* There's no plain shadow compare message, so we use shadow
4505 * compare with a bias of 0.0.
4507 bld
.MOV(msg_end
, brw_imm_f(0.0f
));
4508 msg_end
= offset(msg_end
, bld
, 1);
4511 bld
.MOV(msg_end
, shadow_c
);
4512 msg_end
= offset(msg_end
, bld
, 1);
4516 inst
->src
[0] = reg_undef
;
4517 inst
->src
[1] = surface
;
4518 inst
->src
[2] = sampler
;
4519 inst
->resize_sources(3);
4520 inst
->base_mrf
= msg_begin
.nr
;
4521 inst
->mlen
= msg_end
.nr
- msg_begin
.nr
;
4522 inst
->header_size
= 1;
4526 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4527 const fs_reg
&coordinate
,
4528 const fs_reg
&shadow_c
,
4529 const fs_reg
&lod
, const fs_reg
&lod2
,
4530 const fs_reg
&sample_index
,
4531 const fs_reg
&surface
,
4532 const fs_reg
&sampler
,
4533 unsigned coord_components
,
4534 unsigned grad_components
)
4536 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
4537 fs_reg msg_coords
= message
;
4538 unsigned header_size
= 0;
4540 if (inst
->offset
!= 0) {
4541 /* The offsets set up by the visitor are in the m1 header, so we can't
4548 for (unsigned i
= 0; i
< coord_components
; i
++)
4549 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
),
4550 offset(coordinate
, bld
, i
));
4552 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
4553 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
4555 if (shadow_c
.file
!= BAD_FILE
) {
4556 fs_reg msg_shadow
= msg_lod
;
4557 bld
.MOV(msg_shadow
, shadow_c
);
4558 msg_lod
= offset(msg_shadow
, bld
, 1);
4563 case SHADER_OPCODE_TXL
:
4565 bld
.MOV(msg_lod
, lod
);
4566 msg_end
= offset(msg_lod
, bld
, 1);
4568 case SHADER_OPCODE_TXD
:
4571 * dPdx = dudx, dvdx, drdx
4572 * dPdy = dudy, dvdy, drdy
4574 * Load up these values:
4575 * - dudx dudy dvdx dvdy drdx drdy
4576 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
4579 for (unsigned i
= 0; i
< grad_components
; i
++) {
4580 bld
.MOV(msg_end
, offset(lod
, bld
, i
));
4581 msg_end
= offset(msg_end
, bld
, 1);
4583 bld
.MOV(msg_end
, offset(lod2
, bld
, i
));
4584 msg_end
= offset(msg_end
, bld
, 1);
4587 case SHADER_OPCODE_TXS
:
4588 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
4589 bld
.MOV(msg_lod
, lod
);
4590 msg_end
= offset(msg_lod
, bld
, 1);
4592 case SHADER_OPCODE_TXF
:
4593 msg_lod
= offset(msg_coords
, bld
, 3);
4594 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
4595 msg_end
= offset(msg_lod
, bld
, 1);
4597 case SHADER_OPCODE_TXF_CMS
:
4598 msg_lod
= offset(msg_coords
, bld
, 3);
4600 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), brw_imm_ud(0u));
4602 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
4603 msg_end
= offset(msg_lod
, bld
, 2);
4610 inst
->src
[0] = reg_undef
;
4611 inst
->src
[1] = surface
;
4612 inst
->src
[2] = sampler
;
4613 inst
->resize_sources(3);
4614 inst
->base_mrf
= message
.nr
;
4615 inst
->mlen
= msg_end
.nr
- message
.nr
;
4616 inst
->header_size
= header_size
;
4618 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4619 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
4623 is_high_sampler(const struct gen_device_info
*devinfo
, const fs_reg
&sampler
)
4625 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
4628 return sampler
.file
!= IMM
|| sampler
.ud
>= 16;
4632 sampler_msg_type(const gen_device_info
*devinfo
,
4633 opcode opcode
, bool shadow_compare
)
4635 assert(devinfo
->gen
>= 5);
4637 case SHADER_OPCODE_TEX
:
4638 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
:
4639 GEN5_SAMPLER_MESSAGE_SAMPLE
;
4641 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
:
4642 GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
4643 case SHADER_OPCODE_TXL
:
4644 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
:
4645 GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
4646 case SHADER_OPCODE_TXL_LZ
:
4647 return shadow_compare
? GEN9_SAMPLER_MESSAGE_SAMPLE_C_LZ
:
4648 GEN9_SAMPLER_MESSAGE_SAMPLE_LZ
;
4649 case SHADER_OPCODE_TXS
:
4650 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4651 return GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
4652 case SHADER_OPCODE_TXD
:
4653 assert(!shadow_compare
|| devinfo
->gen
>= 8 || devinfo
->is_haswell
);
4654 return shadow_compare
? HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE
:
4655 GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
4656 case SHADER_OPCODE_TXF
:
4657 return GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
4658 case SHADER_OPCODE_TXF_LZ
:
4659 assert(devinfo
->gen
>= 9);
4660 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD_LZ
;
4661 case SHADER_OPCODE_TXF_CMS_W
:
4662 assert(devinfo
->gen
>= 9);
4663 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD2DMS_W
;
4664 case SHADER_OPCODE_TXF_CMS
:
4665 return devinfo
->gen
>= 7 ? GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
:
4666 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
4667 case SHADER_OPCODE_TXF_UMS
:
4668 assert(devinfo
->gen
>= 7);
4669 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
;
4670 case SHADER_OPCODE_TXF_MCS
:
4671 assert(devinfo
->gen
>= 7);
4672 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
;
4673 case SHADER_OPCODE_LOD
:
4674 return GEN5_SAMPLER_MESSAGE_LOD
;
4675 case SHADER_OPCODE_TG4
:
4676 assert(devinfo
->gen
>= 7);
4677 return shadow_compare
? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C
:
4678 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4
;
4680 case SHADER_OPCODE_TG4_OFFSET
:
4681 assert(devinfo
->gen
>= 7);
4682 return shadow_compare
? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C
:
4683 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO
;
4684 case SHADER_OPCODE_SAMPLEINFO
:
4685 return GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO
;
4687 unreachable("not reached");
4692 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4693 const fs_reg
&coordinate
,
4694 const fs_reg
&shadow_c
,
4695 fs_reg lod
, const fs_reg
&lod2
,
4696 const fs_reg
&min_lod
,
4697 const fs_reg
&sample_index
,
4699 const fs_reg
&surface
,
4700 const fs_reg
&sampler
,
4701 const fs_reg
&tg4_offset
,
4702 unsigned coord_components
,
4703 unsigned grad_components
)
4705 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4706 const brw_stage_prog_data
*prog_data
= bld
.shader
->stage_prog_data
;
4707 unsigned reg_width
= bld
.dispatch_width() / 8;
4708 unsigned header_size
= 0, length
= 0;
4709 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
4710 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
4711 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
4713 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
4714 inst
->offset
!= 0 || inst
->eot
||
4715 op
== SHADER_OPCODE_SAMPLEINFO
||
4716 is_high_sampler(devinfo
, sampler
)) {
4717 /* For general texture offsets (no txf workaround), we need a header to
4720 * TG4 needs to place its channel select in the header, for interaction
4721 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
4722 * larger sampler numbers we need to offset the Sampler State Pointer in
4725 fs_reg header
= retype(sources
[0], BRW_REGISTER_TYPE_UD
);
4729 /* If we're requesting fewer than four channels worth of response,
4730 * and we have an explicit header, we need to set up the sampler
4731 * writemask. It's reversed from normal: 1 means "don't write".
4733 if (!inst
->eot
&& regs_written(inst
) != 4 * reg_width
) {
4734 assert(regs_written(inst
) % reg_width
== 0);
4735 unsigned mask
= ~((1 << (regs_written(inst
) / reg_width
)) - 1) & 0xf;
4736 inst
->offset
|= mask
<< 12;
4739 /* Build the actual header */
4740 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4741 const fs_builder ubld1
= ubld
.group(1, 0);
4742 ubld
.MOV(header
, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
4744 ubld1
.MOV(component(header
, 2), brw_imm_ud(inst
->offset
));
4745 } else if (bld
.shader
->stage
!= MESA_SHADER_VERTEX
&&
4746 bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
) {
4747 /* The vertex and fragment stages have g0.2 set to 0, so
4748 * header0.2 is 0 when g0 is copied. Other stages may not, so we
4749 * must set it to 0 to avoid setting undesirable bits in the
4752 ubld1
.MOV(component(header
, 2), brw_imm_ud(0));
4755 if (is_high_sampler(devinfo
, sampler
)) {
4756 if (sampler
.file
== BRW_IMMEDIATE_VALUE
) {
4757 assert(sampler
.ud
>= 16);
4758 const int sampler_state_size
= 16; /* 16 bytes */
4760 ubld1
.ADD(component(header
, 3),
4761 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD
),
4762 brw_imm_ud(16 * (sampler
.ud
/ 16) * sampler_state_size
));
4764 fs_reg tmp
= ubld1
.vgrf(BRW_REGISTER_TYPE_UD
);
4765 ubld1
.AND(tmp
, sampler
, brw_imm_ud(0x0f0));
4766 ubld1
.SHL(tmp
, tmp
, brw_imm_ud(4));
4767 ubld1
.ADD(component(header
, 3),
4768 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD
),
4774 if (shadow_c
.file
!= BAD_FILE
) {
4775 bld
.MOV(sources
[length
], shadow_c
);
4779 bool coordinate_done
= false;
4781 /* Set up the LOD info */
4784 case SHADER_OPCODE_TXL
:
4785 if (devinfo
->gen
>= 9 && op
== SHADER_OPCODE_TXL
&& lod
.is_zero()) {
4786 op
= SHADER_OPCODE_TXL_LZ
;
4789 bld
.MOV(sources
[length
], lod
);
4792 case SHADER_OPCODE_TXD
:
4793 /* TXD should have been lowered in SIMD16 mode. */
4794 assert(bld
.dispatch_width() == 8);
4796 /* Load dPdx and the coordinate together:
4797 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
4799 for (unsigned i
= 0; i
< coord_components
; i
++) {
4800 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
4802 /* For cube map array, the coordinate is (u,v,r,ai) but there are
4803 * only derivatives for (u, v, r).
4805 if (i
< grad_components
) {
4806 bld
.MOV(sources
[length
++], offset(lod
, bld
, i
));
4807 bld
.MOV(sources
[length
++], offset(lod2
, bld
, i
));
4811 coordinate_done
= true;
4813 case SHADER_OPCODE_TXS
:
4814 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
4817 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4818 /* We need an LOD; just use 0 */
4819 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), brw_imm_ud(0));
4822 case SHADER_OPCODE_TXF
:
4823 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
4824 * On Gen9 they are u, v, lod, r
4826 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
), coordinate
);
4828 if (devinfo
->gen
>= 9) {
4829 if (coord_components
>= 2) {
4830 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
),
4831 offset(coordinate
, bld
, 1));
4833 sources
[length
] = brw_imm_d(0);
4838 if (devinfo
->gen
>= 9 && lod
.is_zero()) {
4839 op
= SHADER_OPCODE_TXF_LZ
;
4841 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
4845 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++)
4846 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
4847 offset(coordinate
, bld
, i
));
4849 coordinate_done
= true;
4852 case SHADER_OPCODE_TXF_CMS
:
4853 case SHADER_OPCODE_TXF_CMS_W
:
4854 case SHADER_OPCODE_TXF_UMS
:
4855 case SHADER_OPCODE_TXF_MCS
:
4856 if (op
== SHADER_OPCODE_TXF_UMS
||
4857 op
== SHADER_OPCODE_TXF_CMS
||
4858 op
== SHADER_OPCODE_TXF_CMS_W
) {
4859 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
4863 if (op
== SHADER_OPCODE_TXF_CMS
|| op
== SHADER_OPCODE_TXF_CMS_W
) {
4864 /* Data from the multisample control surface. */
4865 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
4868 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
4871 if (op
== SHADER_OPCODE_TXF_CMS_W
) {
4872 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
),
4875 offset(mcs
, bld
, 1));
4880 /* There is no offsetting for this message; just copy in the integer
4881 * texture coordinates.
4883 for (unsigned i
= 0; i
< coord_components
; i
++)
4884 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
4885 offset(coordinate
, bld
, i
));
4887 coordinate_done
= true;
4889 case SHADER_OPCODE_TG4_OFFSET
:
4890 /* More crazy intermixing */
4891 for (unsigned i
= 0; i
< 2; i
++) /* u, v */
4892 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
4894 for (unsigned i
= 0; i
< 2; i
++) /* offu, offv */
4895 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
4896 offset(tg4_offset
, bld
, i
));
4898 if (coord_components
== 3) /* r if present */
4899 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, 2));
4901 coordinate_done
= true;
4907 /* Set up the coordinate (except for cases where it was done above) */
4908 if (!coordinate_done
) {
4909 for (unsigned i
= 0; i
< coord_components
; i
++)
4910 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
4913 if (min_lod
.file
!= BAD_FILE
) {
4914 /* Account for all of the missing coordinate sources */
4915 length
+= 4 - coord_components
;
4916 if (op
== SHADER_OPCODE_TXD
)
4917 length
+= (3 - grad_components
) * 2;
4919 bld
.MOV(sources
[length
++], min_lod
);
4924 mlen
= length
* reg_width
- header_size
;
4926 mlen
= length
* reg_width
;
4928 const fs_reg src_payload
= fs_reg(VGRF
, bld
.shader
->alloc
.allocate(mlen
),
4929 BRW_REGISTER_TYPE_F
);
4930 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
4932 /* Generate the SEND. */
4933 inst
->opcode
= SHADER_OPCODE_SEND
;
4935 inst
->header_size
= header_size
;
4937 const unsigned msg_type
=
4938 sampler_msg_type(devinfo
, op
, inst
->shadow_compare
);
4939 const unsigned simd_mode
=
4940 inst
->exec_size
<= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8
:
4941 BRW_SAMPLER_SIMD_MODE_SIMD16
;
4943 uint32_t base_binding_table_index
;
4945 case SHADER_OPCODE_TG4
:
4946 case SHADER_OPCODE_TG4_OFFSET
:
4947 base_binding_table_index
= prog_data
->binding_table
.gather_texture_start
;
4949 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4950 base_binding_table_index
= prog_data
->binding_table
.image_start
;
4953 base_binding_table_index
= prog_data
->binding_table
.texture_start
;
4957 inst
->sfid
= BRW_SFID_SAMPLER
;
4958 if (surface
.file
== IMM
&& sampler
.file
== IMM
) {
4959 inst
->desc
= brw_sampler_desc(devinfo
,
4960 surface
.ud
+ base_binding_table_index
,
4964 0 /* return_format unused on gen7+ */);
4965 inst
->src
[0] = brw_imm_ud(0);
4967 /* Immediate portion of the descriptor */
4968 inst
->desc
= brw_sampler_desc(devinfo
,
4973 0 /* return_format unused on gen7+ */);
4974 const fs_builder ubld
= bld
.group(1, 0).exec_all();
4975 fs_reg desc
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4976 if (surface
.equals(sampler
)) {
4977 /* This case is common in GL */
4978 ubld
.MUL(desc
, surface
, brw_imm_ud(0x101));
4980 if (sampler
.file
== IMM
) {
4981 ubld
.OR(desc
, surface
, brw_imm_ud(sampler
.ud
<< 8));
4983 ubld
.SHL(desc
, sampler
, brw_imm_ud(8));
4984 ubld
.OR(desc
, desc
, surface
);
4987 if (base_binding_table_index
)
4988 ubld
.ADD(desc
, desc
, brw_imm_ud(base_binding_table_index
));
4989 ubld
.AND(desc
, desc
, brw_imm_ud(0xfff));
4991 inst
->src
[0] = component(desc
, 0);
4993 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
4995 inst
->src
[2] = src_payload
;
4996 inst
->resize_sources(3);
4999 /* EOT sampler messages don't make sense to split because it would
5000 * involve ending half of the thread early.
5002 assert(inst
->group
== 0);
5003 /* We need to use SENDC for EOT sampler messages */
5004 inst
->check_tdr
= true;
5005 inst
->send_has_side_effects
= true;
5008 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
5009 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
5013 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
5015 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5016 const fs_reg
&coordinate
= inst
->src
[TEX_LOGICAL_SRC_COORDINATE
];
5017 const fs_reg
&shadow_c
= inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
];
5018 const fs_reg
&lod
= inst
->src
[TEX_LOGICAL_SRC_LOD
];
5019 const fs_reg
&lod2
= inst
->src
[TEX_LOGICAL_SRC_LOD2
];
5020 const fs_reg
&min_lod
= inst
->src
[TEX_LOGICAL_SRC_MIN_LOD
];
5021 const fs_reg
&sample_index
= inst
->src
[TEX_LOGICAL_SRC_SAMPLE_INDEX
];
5022 const fs_reg
&mcs
= inst
->src
[TEX_LOGICAL_SRC_MCS
];
5023 const fs_reg
&surface
= inst
->src
[TEX_LOGICAL_SRC_SURFACE
];
5024 const fs_reg
&sampler
= inst
->src
[TEX_LOGICAL_SRC_SAMPLER
];
5025 const fs_reg
&tg4_offset
= inst
->src
[TEX_LOGICAL_SRC_TG4_OFFSET
];
5026 assert(inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
);
5027 const unsigned coord_components
= inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
5028 assert(inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
5029 const unsigned grad_components
= inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
5031 if (devinfo
->gen
>= 7) {
5032 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
5033 shadow_c
, lod
, lod2
, min_lod
,
5035 mcs
, surface
, sampler
, tg4_offset
,
5036 coord_components
, grad_components
);
5037 } else if (devinfo
->gen
>= 5) {
5038 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
5039 shadow_c
, lod
, lod2
, sample_index
,
5041 coord_components
, grad_components
);
5043 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
5044 shadow_c
, lod
, lod2
,
5046 coord_components
, grad_components
);
5051 * Initialize the header present in some typed and untyped surface
5055 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
5057 fs_builder ubld
= bld
.exec_all().group(8, 0);
5058 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5059 ubld
.MOV(dst
, brw_imm_d(0));
5060 ubld
.group(1, 0).MOV(component(dst
, 7), sample_mask
);
5065 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5067 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5069 /* Get the logical send arguments. */
5070 const fs_reg
&addr
= inst
->src
[SURFACE_LOGICAL_SRC_ADDRESS
];
5071 const fs_reg
&src
= inst
->src
[SURFACE_LOGICAL_SRC_DATA
];
5072 const fs_reg
&surface
= inst
->src
[SURFACE_LOGICAL_SRC_SURFACE
];
5073 const UNUSED fs_reg
&dims
= inst
->src
[SURFACE_LOGICAL_SRC_IMM_DIMS
];
5074 const fs_reg
&arg
= inst
->src
[SURFACE_LOGICAL_SRC_IMM_ARG
];
5075 assert(arg
.file
== IMM
);
5077 /* Calculate the total number of components of the payload. */
5078 const unsigned addr_sz
= inst
->components_read(SURFACE_LOGICAL_SRC_ADDRESS
);
5079 const unsigned src_sz
= inst
->components_read(SURFACE_LOGICAL_SRC_DATA
);
5081 const bool is_typed_access
=
5082 inst
->opcode
== SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
||
5083 inst
->opcode
== SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
||
5084 inst
->opcode
== SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
;
5086 /* From the BDW PRM Volume 7, page 147:
5088 * "For the Data Cache Data Port*, the header must be present for the
5089 * following message types: [...] Typed read/write/atomics"
5091 * Earlier generations have a similar wording. Because of this restriction
5092 * we don't attempt to implement sample masks via predication for such
5093 * messages prior to Gen9, since we have to provide a header anyway. On
5094 * Gen11+ the header has been removed so we can only use predication.
5096 const unsigned header_sz
= devinfo
->gen
< 9 && is_typed_access
? 1 : 0;
5098 const bool has_side_effects
= inst
->has_side_effects();
5099 fs_reg sample_mask
= has_side_effects
? bld
.sample_mask_reg() :
5100 fs_reg(brw_imm_d(0xffff));
5102 fs_reg payload
, payload2
;
5103 unsigned mlen
, ex_mlen
= 0;
5104 if (devinfo
->gen
>= 9) {
5105 /* We have split sends on gen9 and above */
5106 assert(header_sz
== 0);
5107 payload
= bld
.move_to_vgrf(addr
, addr_sz
);
5108 payload2
= bld
.move_to_vgrf(src
, src_sz
);
5109 mlen
= addr_sz
* (inst
->exec_size
/ 8);
5110 ex_mlen
= src_sz
* (inst
->exec_size
/ 8);
5112 /* Allocate space for the payload. */
5113 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
5114 payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
5115 fs_reg
*const components
= new fs_reg
[sz
];
5118 /* Construct the payload. */
5120 components
[n
++] = emit_surface_header(bld
, sample_mask
);
5122 for (unsigned i
= 0; i
< addr_sz
; i
++)
5123 components
[n
++] = offset(addr
, bld
, i
);
5125 for (unsigned i
= 0; i
< src_sz
; i
++)
5126 components
[n
++] = offset(src
, bld
, i
);
5128 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
5129 mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
5131 delete[] components
;
5134 /* Predicate the instruction on the sample mask if no header is
5137 if (!header_sz
&& sample_mask
.file
!= BAD_FILE
&&
5138 sample_mask
.file
!= IMM
) {
5139 const fs_builder ubld
= bld
.group(1, 0).exec_all();
5140 if (inst
->predicate
) {
5141 assert(inst
->predicate
== BRW_PREDICATE_NORMAL
);
5142 assert(!inst
->predicate_inverse
);
5143 assert(inst
->flag_subreg
< 2);
5144 /* Combine the sample mask with the existing predicate by using a
5145 * vertical predication mode.
5147 inst
->predicate
= BRW_PREDICATE_ALIGN1_ALLV
;
5148 ubld
.MOV(retype(brw_flag_subreg(inst
->flag_subreg
+ 2),
5152 inst
->flag_subreg
= 2;
5153 inst
->predicate
= BRW_PREDICATE_NORMAL
;
5154 inst
->predicate_inverse
= false;
5155 ubld
.MOV(retype(brw_flag_subreg(inst
->flag_subreg
), sample_mask
.type
),
5161 switch (inst
->opcode
) {
5162 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5163 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5164 /* Byte scattered opcodes go through the normal data cache */
5165 sfid
= GEN7_SFID_DATAPORT_DATA_CACHE
;
5168 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5169 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5170 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5171 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5172 /* Untyped Surface messages go through the data cache but the SFID value
5173 * changed on Haswell.
5175 sfid
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
?
5176 HSW_SFID_DATAPORT_DATA_CACHE_1
:
5177 GEN7_SFID_DATAPORT_DATA_CACHE
);
5180 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5181 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5182 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5183 /* Typed surface messages go through the render cache on IVB and the
5184 * data cache on HSW+.
5186 sfid
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
?
5187 HSW_SFID_DATAPORT_DATA_CACHE_1
:
5188 GEN6_SFID_DATAPORT_RENDER_CACHE
);
5192 unreachable("Unsupported surface opcode");
5196 switch (inst
->opcode
) {
5197 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5198 desc
= brw_dp_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5199 arg
.ud
, /* num_channels */
5203 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5204 desc
= brw_dp_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5205 arg
.ud
, /* num_channels */
5209 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5210 desc
= brw_dp_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5211 arg
.ud
, /* bit_size */
5215 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5216 desc
= brw_dp_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5217 arg
.ud
, /* bit_size */
5221 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5222 desc
= brw_dp_untyped_atomic_desc(devinfo
, inst
->exec_size
,
5223 arg
.ud
, /* atomic_op */
5224 !inst
->dst
.is_null());
5227 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5228 desc
= brw_dp_untyped_atomic_float_desc(devinfo
, inst
->exec_size
,
5229 arg
.ud
, /* atomic_op */
5230 !inst
->dst
.is_null());
5233 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5234 desc
= brw_dp_typed_surface_rw_desc(devinfo
, inst
->exec_size
, inst
->group
,
5235 arg
.ud
, /* num_channels */
5239 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5240 desc
= brw_dp_typed_surface_rw_desc(devinfo
, inst
->exec_size
, inst
->group
,
5241 arg
.ud
, /* num_channels */
5245 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5246 desc
= brw_dp_typed_atomic_desc(devinfo
, inst
->exec_size
, inst
->group
,
5247 arg
.ud
, /* atomic_op */
5248 !inst
->dst
.is_null());
5252 unreachable("Unknown surface logical instruction");
5255 /* Update the original instruction. */
5256 inst
->opcode
= SHADER_OPCODE_SEND
;
5258 inst
->ex_mlen
= ex_mlen
;
5259 inst
->header_size
= header_sz
;
5260 inst
->send_has_side_effects
= has_side_effects
;
5261 inst
->send_is_volatile
= !has_side_effects
;
5263 /* Set up SFID and descriptors */
5266 if (surface
.file
== IMM
) {
5267 inst
->desc
|= surface
.ud
& 0xff;
5268 inst
->src
[0] = brw_imm_ud(0);
5270 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5271 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5272 ubld
.AND(tmp
, surface
, brw_imm_ud(0xff));
5273 inst
->src
[0] = component(tmp
, 0);
5275 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5277 /* Finally, the payload */
5278 inst
->src
[2] = payload
;
5279 inst
->src
[3] = payload2
;
5281 inst
->resize_sources(4);
5285 lower_a64_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5287 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5289 const fs_reg
&addr
= inst
->src
[0];
5290 const fs_reg
&src
= inst
->src
[1];
5291 const unsigned src_comps
= inst
->components_read(1);
5292 assert(inst
->src
[2].file
== IMM
);
5293 const unsigned arg
= inst
->src
[2].ud
;
5294 const bool has_side_effects
= inst
->has_side_effects();
5296 /* If the surface message has side effects and we're a fragment shader, we
5297 * have to predicate with the sample mask to avoid helper invocations.
5299 if (has_side_effects
&& bld
.shader
->stage
== MESA_SHADER_FRAGMENT
) {
5300 inst
->flag_subreg
= 2;
5301 inst
->predicate
= BRW_PREDICATE_NORMAL
;
5302 inst
->predicate_inverse
= false;
5304 fs_reg sample_mask
= bld
.sample_mask_reg();
5305 const fs_builder ubld
= bld
.group(1, 0).exec_all();
5306 ubld
.MOV(retype(brw_flag_subreg(inst
->flag_subreg
), sample_mask
.type
),
5310 fs_reg payload
, payload2
;
5311 unsigned mlen
, ex_mlen
= 0;
5312 if (devinfo
->gen
>= 9) {
5313 /* On Skylake and above, we have SENDS */
5314 mlen
= 2 * (inst
->exec_size
/ 8);
5315 ex_mlen
= src_comps
* (inst
->exec_size
/ 8);
5316 payload
= retype(bld
.move_to_vgrf(addr
, 1), BRW_REGISTER_TYPE_UD
);
5317 payload2
= retype(bld
.move_to_vgrf(src
, src_comps
),
5318 BRW_REGISTER_TYPE_UD
);
5320 /* Add two because the address is 64-bit */
5321 const unsigned dwords
= 2 + src_comps
;
5322 mlen
= dwords
* (inst
->exec_size
/ 8);
5328 for (unsigned i
= 0; i
< src_comps
; i
++)
5329 sources
[1 + i
] = offset(src
, bld
, i
);
5331 payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, dwords
);
5332 bld
.LOAD_PAYLOAD(payload
, sources
, 1 + src_comps
, 0);
5336 switch (inst
->opcode
) {
5337 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
5338 desc
= brw_dp_a64_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5339 arg
, /* num_channels */
5343 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
5344 desc
= brw_dp_a64_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5345 arg
, /* num_channels */
5349 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
5350 desc
= brw_dp_a64_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5355 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
5356 desc
= brw_dp_a64_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5361 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
5362 desc
= brw_dp_a64_untyped_atomic_desc(devinfo
, inst
->exec_size
, 32,
5363 arg
, /* atomic_op */
5364 !inst
->dst
.is_null());
5367 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5368 desc
= brw_dp_a64_untyped_atomic_float_desc(devinfo
, inst
->exec_size
,
5369 arg
, /* atomic_op */
5370 !inst
->dst
.is_null());
5374 unreachable("Unknown A64 logical instruction");
5377 /* Update the original instruction. */
5378 inst
->opcode
= SHADER_OPCODE_SEND
;
5380 inst
->ex_mlen
= ex_mlen
;
5381 inst
->header_size
= 0;
5382 inst
->send_has_side_effects
= has_side_effects
;
5383 inst
->send_is_volatile
= !has_side_effects
;
5385 /* Set up SFID and descriptors */
5386 inst
->sfid
= HSW_SFID_DATAPORT_DATA_CACHE_1
;
5388 inst
->resize_sources(4);
5389 inst
->src
[0] = brw_imm_ud(0); /* desc */
5390 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5391 inst
->src
[2] = payload
;
5392 inst
->src
[3] = payload2
;
5396 lower_varying_pull_constant_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5398 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5400 if (devinfo
->gen
>= 7) {
5401 fs_reg index
= inst
->src
[0];
5402 /* We are switching the instruction from an ALU-like instruction to a
5403 * send-from-grf instruction. Since sends can't handle strides or
5404 * source modifiers, we have to make a copy of the offset source.
5406 fs_reg offset
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
5407 bld
.MOV(offset
, inst
->src
[1]);
5409 const unsigned simd_mode
=
5410 inst
->exec_size
<= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8
:
5411 BRW_SAMPLER_SIMD_MODE_SIMD16
;
5413 inst
->opcode
= SHADER_OPCODE_SEND
;
5414 inst
->mlen
= inst
->exec_size
/ 8;
5415 inst
->resize_sources(3);
5417 inst
->sfid
= BRW_SFID_SAMPLER
;
5418 inst
->desc
= brw_sampler_desc(devinfo
, 0, 0,
5419 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
5421 if (index
.file
== IMM
) {
5422 inst
->desc
|= index
.ud
& 0xff;
5423 inst
->src
[0] = brw_imm_ud(0);
5425 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5426 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5427 ubld
.AND(tmp
, index
, brw_imm_ud(0xff));
5428 inst
->src
[0] = component(tmp
, 0);
5430 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5431 inst
->src
[2] = offset
; /* payload */
5433 const fs_reg
payload(MRF
, FIRST_PULL_LOAD_MRF(devinfo
->gen
),
5434 BRW_REGISTER_TYPE_UD
);
5436 bld
.MOV(byte_offset(payload
, REG_SIZE
), inst
->src
[1]);
5438 inst
->opcode
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
;
5439 inst
->resize_sources(1);
5440 inst
->base_mrf
= payload
.nr
;
5441 inst
->header_size
= 1;
5442 inst
->mlen
= 1 + inst
->exec_size
/ 8;
5447 lower_math_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5449 assert(bld
.shader
->devinfo
->gen
< 6);
5452 inst
->mlen
= inst
->sources
* inst
->exec_size
/ 8;
5454 if (inst
->sources
> 1) {
5455 /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
5456 * "Message Payload":
5458 * "Operand0[7]. For the INT DIV functions, this operand is the
5461 * "Operand1[7]. For the INT DIV functions, this operand is the
5464 const bool is_int_div
= inst
->opcode
!= SHADER_OPCODE_POW
;
5465 const fs_reg src0
= is_int_div
? inst
->src
[1] : inst
->src
[0];
5466 const fs_reg src1
= is_int_div
? inst
->src
[0] : inst
->src
[1];
5468 inst
->resize_sources(1);
5469 inst
->src
[0] = src0
;
5471 assert(inst
->exec_size
== 8);
5472 bld
.MOV(fs_reg(MRF
, inst
->base_mrf
+ 1, src1
.type
), src1
);
5477 fs_visitor::lower_logical_sends()
5479 bool progress
= false;
5481 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
5482 const fs_builder
ibld(this, block
, inst
);
5484 switch (inst
->opcode
) {
5485 case FS_OPCODE_FB_WRITE_LOGICAL
:
5486 assert(stage
== MESA_SHADER_FRAGMENT
);
5487 lower_fb_write_logical_send(ibld
, inst
,
5488 brw_wm_prog_data(prog_data
),
5489 (const brw_wm_prog_key
*)key
,
5493 case FS_OPCODE_FB_READ_LOGICAL
:
5494 lower_fb_read_logical_send(ibld
, inst
);
5497 case SHADER_OPCODE_TEX_LOGICAL
:
5498 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
5501 case SHADER_OPCODE_TXD_LOGICAL
:
5502 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
5505 case SHADER_OPCODE_TXF_LOGICAL
:
5506 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
5509 case SHADER_OPCODE_TXL_LOGICAL
:
5510 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
5513 case SHADER_OPCODE_TXS_LOGICAL
:
5514 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
5517 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
5518 lower_sampler_logical_send(ibld
, inst
,
5519 SHADER_OPCODE_IMAGE_SIZE_LOGICAL
);
5522 case FS_OPCODE_TXB_LOGICAL
:
5523 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
5526 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
5527 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
5530 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
5531 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS_W
);
5534 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
5535 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
5538 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
5539 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
5542 case SHADER_OPCODE_LOD_LOGICAL
:
5543 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
5546 case SHADER_OPCODE_TG4_LOGICAL
:
5547 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
5550 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
5551 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
5554 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
5555 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_SAMPLEINFO
);
5558 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5559 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5560 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5561 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5562 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5563 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5564 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5565 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5566 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5567 lower_surface_logical_send(ibld
, inst
);
5570 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
5571 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
5572 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
5573 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
5574 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
5575 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5576 lower_a64_logical_send(ibld
, inst
);
5579 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
:
5580 lower_varying_pull_constant_logical_send(ibld
, inst
);
5583 case SHADER_OPCODE_RCP
:
5584 case SHADER_OPCODE_RSQ
:
5585 case SHADER_OPCODE_SQRT
:
5586 case SHADER_OPCODE_EXP2
:
5587 case SHADER_OPCODE_LOG2
:
5588 case SHADER_OPCODE_SIN
:
5589 case SHADER_OPCODE_COS
:
5590 case SHADER_OPCODE_POW
:
5591 case SHADER_OPCODE_INT_QUOTIENT
:
5592 case SHADER_OPCODE_INT_REMAINDER
:
5593 /* The math opcodes are overloaded for the send-like and
5594 * expression-like instructions which seems kind of icky. Gen6+ has
5595 * a native (but rather quirky) MATH instruction so we don't need to
5596 * do anything here. On Gen4-5 we'll have to lower the Gen6-like
5597 * logical instructions (which we can easily recognize because they
5598 * have mlen = 0) into send-like virtual instructions.
5600 if (devinfo
->gen
< 6 && inst
->mlen
== 0) {
5601 lower_math_logical_send(ibld
, inst
);
5616 invalidate_live_intervals();
5622 * Get the closest allowed SIMD width for instruction \p inst accounting for
5623 * some common regioning and execution control restrictions that apply to FPU
5624 * instructions. These restrictions don't necessarily have any relevance to
5625 * instructions not executed by the FPU pipeline like extended math, control
5626 * flow or send message instructions.
5628 * For virtual opcodes it's really up to the instruction -- In some cases
5629 * (e.g. where a virtual instruction unrolls into a simple sequence of FPU
5630 * instructions) it may simplify virtual instruction lowering if we can
5631 * enforce FPU-like regioning restrictions already on the virtual instruction,
5632 * in other cases (e.g. virtual send-like instructions) this may be
5633 * excessively restrictive.
5636 get_fpu_lowered_simd_width(const struct gen_device_info
*devinfo
,
5637 const fs_inst
*inst
)
5639 /* Maximum execution size representable in the instruction controls. */
5640 unsigned max_width
= MIN2(32, inst
->exec_size
);
5642 /* According to the PRMs:
5643 * "A. In Direct Addressing mode, a source cannot span more than 2
5644 * adjacent GRF registers.
5645 * B. A destination cannot span more than 2 adjacent GRF registers."
5647 * Look for the source or destination with the largest register region
5648 * which is the one that is going to limit the overall execution size of
5649 * the instruction due to this rule.
5651 unsigned reg_count
= DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
5653 for (unsigned i
= 0; i
< inst
->sources
; i
++)
5654 reg_count
= MAX2(reg_count
, DIV_ROUND_UP(inst
->size_read(i
), REG_SIZE
));
5656 /* Calculate the maximum execution size of the instruction based on the
5657 * factor by which it goes over the hardware limit of 2 GRFs.
5660 max_width
= MIN2(max_width
, inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2));
5662 /* According to the IVB PRMs:
5663 * "When destination spans two registers, the source MUST span two
5664 * registers. The exception to the above rule:
5666 * - When source is scalar, the source registers are not incremented.
5667 * - When source is packed integer Word and destination is packed
5668 * integer DWord, the source register is not incremented but the
5669 * source sub register is incremented."
5671 * The hardware specs from Gen4 to Gen7.5 mention similar regioning
5672 * restrictions. The code below intentionally doesn't check whether the
5673 * destination type is integer because empirically the hardware doesn't
5674 * seem to care what the actual type is as long as it's dword-aligned.
5676 if (devinfo
->gen
< 8) {
5677 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
5678 /* IVB implements DF scalars as <0;2,1> regions. */
5679 const bool is_scalar_exception
= is_uniform(inst
->src
[i
]) &&
5680 (devinfo
->is_haswell
|| type_sz(inst
->src
[i
].type
) != 8);
5681 const bool is_packed_word_exception
=
5682 type_sz(inst
->dst
.type
) == 4 && inst
->dst
.stride
== 1 &&
5683 type_sz(inst
->src
[i
].type
) == 2 && inst
->src
[i
].stride
== 1;
5685 /* We check size_read(i) against size_written instead of REG_SIZE
5686 * because we want to properly handle SIMD32. In SIMD32, you can end
5687 * up with writes to 4 registers and a source that reads 2 registers
5688 * and we may still need to lower all the way to SIMD8 in that case.
5690 if (inst
->size_written
> REG_SIZE
&&
5691 inst
->size_read(i
) != 0 &&
5692 inst
->size_read(i
) < inst
->size_written
&&
5693 !is_scalar_exception
&& !is_packed_word_exception
) {
5694 const unsigned reg_count
= DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
5695 max_width
= MIN2(max_width
, inst
->exec_size
/ reg_count
);
5700 if (devinfo
->gen
< 6) {
5701 /* From the G45 PRM, Volume 4 Page 361:
5703 * "Operand Alignment Rule: With the exceptions listed below, a
5704 * source/destination operand in general should be aligned to even
5705 * 256-bit physical register with a region size equal to two 256-bit
5706 * physical registers."
5708 * Normally we enforce this by allocating virtual registers to the
5709 * even-aligned class. But we need to handle payload registers.
5711 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
5712 if (inst
->src
[i
].file
== FIXED_GRF
&& (inst
->src
[i
].nr
& 1) &&
5713 inst
->size_read(i
) > REG_SIZE
) {
5714 max_width
= MIN2(max_width
, 8);
5719 /* From the IVB PRMs:
5720 * "When an instruction is SIMD32, the low 16 bits of the execution mask
5721 * are applied for both halves of the SIMD32 instruction. If different
5722 * execution mask channels are required, split the instruction into two
5723 * SIMD16 instructions."
5725 * There is similar text in the HSW PRMs. Gen4-6 don't even implement
5726 * 32-wide control flow support in hardware and will behave similarly.
5728 if (devinfo
->gen
< 8 && !inst
->force_writemask_all
)
5729 max_width
= MIN2(max_width
, 16);
5731 /* From the IVB PRMs (applies to HSW too):
5732 * "Instructions with condition modifiers must not use SIMD32."
5734 * From the BDW PRMs (applies to later hardware too):
5735 * "Ternary instruction with condition modifiers must not use SIMD32."
5737 if (inst
->conditional_mod
&& (devinfo
->gen
< 8 || inst
->is_3src(devinfo
)))
5738 max_width
= MIN2(max_width
, 16);
5740 /* From the IVB PRMs (applies to other devices that don't have the
5741 * gen_device_info::supports_simd16_3src flag set):
5742 * "In Align16 access mode, SIMD16 is not allowed for DW operations and
5743 * SIMD8 is not allowed for DF operations."
5745 if (inst
->is_3src(devinfo
) && !devinfo
->supports_simd16_3src
)
5746 max_width
= MIN2(max_width
, inst
->exec_size
/ reg_count
);
5748 /* Pre-Gen8 EUs are hardwired to use the QtrCtrl+1 (where QtrCtrl is
5749 * the 8-bit quarter of the execution mask signals specified in the
5750 * instruction control fields) for the second compressed half of any
5751 * single-precision instruction (for double-precision instructions
5752 * it's hardwired to use NibCtrl+1, at least on HSW), which means that
5753 * the EU will apply the wrong execution controls for the second
5754 * sequential GRF write if the number of channels per GRF is not exactly
5755 * eight in single-precision mode (or four in double-float mode).
5757 * In this situation we calculate the maximum size of the split
5758 * instructions so they only ever write to a single register.
5760 if (devinfo
->gen
< 8 && inst
->size_written
> REG_SIZE
&&
5761 !inst
->force_writemask_all
) {
5762 const unsigned channels_per_grf
= inst
->exec_size
/
5763 DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
5764 const unsigned exec_type_size
= get_exec_type_size(inst
);
5765 assert(exec_type_size
);
5767 /* The hardware shifts exactly 8 channels per compressed half of the
5768 * instruction in single-precision mode and exactly 4 in double-precision.
5770 if (channels_per_grf
!= (exec_type_size
== 8 ? 4 : 8))
5771 max_width
= MIN2(max_width
, channels_per_grf
);
5773 /* Lower all non-force_writemask_all DF instructions to SIMD4 on IVB/BYT
5774 * because HW applies the same channel enable signals to both halves of
5775 * the compressed instruction which will be just wrong under
5776 * non-uniform control flow.
5778 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
5779 (exec_type_size
== 8 || type_sz(inst
->dst
.type
) == 8))
5780 max_width
= MIN2(max_width
, 4);
5783 /* Only power-of-two execution sizes are representable in the instruction
5786 return 1 << _mesa_logbase2(max_width
);
5790 * Get the maximum allowed SIMD width for instruction \p inst accounting for
5791 * various payload size restrictions that apply to sampler message
5794 * This is only intended to provide a maximum theoretical bound for the
5795 * execution size of the message based on the number of argument components
5796 * alone, which in most cases will determine whether the SIMD8 or SIMD16
5797 * variant of the message can be used, though some messages may have
5798 * additional restrictions not accounted for here (e.g. pre-ILK hardware uses
5799 * the message length to determine the exact SIMD width and argument count,
5800 * which makes a number of sampler message combinations impossible to
5804 get_sampler_lowered_simd_width(const struct gen_device_info
*devinfo
,
5805 const fs_inst
*inst
)
5807 /* If we have a min_lod parameter on anything other than a simple sample
5808 * message, it will push it over 5 arguments and we have to fall back to
5811 if (inst
->opcode
!= SHADER_OPCODE_TEX
&&
5812 inst
->components_read(TEX_LOGICAL_SRC_MIN_LOD
))
5815 /* Calculate the number of coordinate components that have to be present
5816 * assuming that additional arguments follow the texel coordinates in the
5817 * message payload. On IVB+ there is no need for padding, on ILK-SNB we
5818 * need to pad to four or three components depending on the message,
5819 * pre-ILK we need to pad to at most three components.
5821 const unsigned req_coord_components
=
5822 (devinfo
->gen
>= 7 ||
5823 !inst
->components_read(TEX_LOGICAL_SRC_COORDINATE
)) ? 0 :
5824 (devinfo
->gen
>= 5 && inst
->opcode
!= SHADER_OPCODE_TXF_LOGICAL
&&
5825 inst
->opcode
!= SHADER_OPCODE_TXF_CMS_LOGICAL
) ? 4 :
5828 /* On Gen9+ the LOD argument is for free if we're able to use the LZ
5829 * variant of the TXL or TXF message.
5831 const bool implicit_lod
= devinfo
->gen
>= 9 &&
5832 (inst
->opcode
== SHADER_OPCODE_TXL
||
5833 inst
->opcode
== SHADER_OPCODE_TXF
) &&
5834 inst
->src
[TEX_LOGICAL_SRC_LOD
].is_zero();
5836 /* Calculate the total number of argument components that need to be passed
5837 * to the sampler unit.
5839 const unsigned num_payload_components
=
5840 MAX2(inst
->components_read(TEX_LOGICAL_SRC_COORDINATE
),
5841 req_coord_components
) +
5842 inst
->components_read(TEX_LOGICAL_SRC_SHADOW_C
) +
5843 (implicit_lod
? 0 : inst
->components_read(TEX_LOGICAL_SRC_LOD
)) +
5844 inst
->components_read(TEX_LOGICAL_SRC_LOD2
) +
5845 inst
->components_read(TEX_LOGICAL_SRC_SAMPLE_INDEX
) +
5846 (inst
->opcode
== SHADER_OPCODE_TG4_OFFSET_LOGICAL
?
5847 inst
->components_read(TEX_LOGICAL_SRC_TG4_OFFSET
) : 0) +
5848 inst
->components_read(TEX_LOGICAL_SRC_MCS
);
5850 /* SIMD16 messages with more than five arguments exceed the maximum message
5851 * size supported by the sampler, regardless of whether a header is
5854 return MIN2(inst
->exec_size
,
5855 num_payload_components
> MAX_SAMPLER_MESSAGE_SIZE
/ 2 ? 8 : 16);
5859 * Get the closest native SIMD width supported by the hardware for instruction
5860 * \p inst. The instruction will be left untouched by
5861 * fs_visitor::lower_simd_width() if the returned value is equal to the
5862 * original execution size.
5865 get_lowered_simd_width(const struct gen_device_info
*devinfo
,
5866 const fs_inst
*inst
)
5868 switch (inst
->opcode
) {
5869 case BRW_OPCODE_MOV
:
5870 case BRW_OPCODE_SEL
:
5871 case BRW_OPCODE_NOT
:
5872 case BRW_OPCODE_AND
:
5874 case BRW_OPCODE_XOR
:
5875 case BRW_OPCODE_SHR
:
5876 case BRW_OPCODE_SHL
:
5877 case BRW_OPCODE_ASR
:
5878 case BRW_OPCODE_CMPN
:
5879 case BRW_OPCODE_CSEL
:
5880 case BRW_OPCODE_F32TO16
:
5881 case BRW_OPCODE_F16TO32
:
5882 case BRW_OPCODE_BFREV
:
5883 case BRW_OPCODE_BFE
:
5884 case BRW_OPCODE_ADD
:
5885 case BRW_OPCODE_MUL
:
5886 case BRW_OPCODE_AVG
:
5887 case BRW_OPCODE_FRC
:
5888 case BRW_OPCODE_RNDU
:
5889 case BRW_OPCODE_RNDD
:
5890 case BRW_OPCODE_RNDE
:
5891 case BRW_OPCODE_RNDZ
:
5892 case BRW_OPCODE_LZD
:
5893 case BRW_OPCODE_FBH
:
5894 case BRW_OPCODE_FBL
:
5895 case BRW_OPCODE_CBIT
:
5896 case BRW_OPCODE_SAD2
:
5897 case BRW_OPCODE_MAD
:
5898 case BRW_OPCODE_LRP
:
5899 case FS_OPCODE_PACK
:
5900 case SHADER_OPCODE_SEL_EXEC
:
5901 case SHADER_OPCODE_CLUSTER_BROADCAST
:
5902 return get_fpu_lowered_simd_width(devinfo
, inst
);
5904 case BRW_OPCODE_CMP
: {
5905 /* The Ivybridge/BayTrail WaCMPInstFlagDepClearedEarly workaround says that
5906 * when the destination is a GRF the dependency-clear bit on the flag
5907 * register is cleared early.
5909 * Suggested workarounds are to disable coissuing CMP instructions
5910 * or to split CMP(16) instructions into two CMP(8) instructions.
5912 * We choose to split into CMP(8) instructions since disabling
5913 * coissuing would affect CMP instructions not otherwise affected by
5916 const unsigned max_width
= (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
5917 !inst
->dst
.is_null() ? 8 : ~0);
5918 return MIN2(max_width
, get_fpu_lowered_simd_width(devinfo
, inst
));
5920 case BRW_OPCODE_BFI1
:
5921 case BRW_OPCODE_BFI2
:
5922 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
5924 * "Force BFI instructions to be executed always in SIMD8."
5926 return MIN2(devinfo
->is_haswell
? 8 : ~0u,
5927 get_fpu_lowered_simd_width(devinfo
, inst
));
5930 assert(inst
->src
[0].file
== BAD_FILE
|| inst
->exec_size
<= 16);
5931 return inst
->exec_size
;
5933 case SHADER_OPCODE_RCP
:
5934 case SHADER_OPCODE_RSQ
:
5935 case SHADER_OPCODE_SQRT
:
5936 case SHADER_OPCODE_EXP2
:
5937 case SHADER_OPCODE_LOG2
:
5938 case SHADER_OPCODE_SIN
:
5939 case SHADER_OPCODE_COS
:
5940 /* Unary extended math instructions are limited to SIMD8 on Gen4 and
5943 return (devinfo
->gen
>= 7 ? MIN2(16, inst
->exec_size
) :
5944 devinfo
->gen
== 5 || devinfo
->is_g4x
? MIN2(16, inst
->exec_size
) :
5945 MIN2(8, inst
->exec_size
));
5947 case SHADER_OPCODE_POW
:
5948 /* SIMD16 is only allowed on Gen7+. */
5949 return (devinfo
->gen
>= 7 ? MIN2(16, inst
->exec_size
) :
5950 MIN2(8, inst
->exec_size
));
5952 case SHADER_OPCODE_INT_QUOTIENT
:
5953 case SHADER_OPCODE_INT_REMAINDER
:
5954 /* Integer division is limited to SIMD8 on all generations. */
5955 return MIN2(8, inst
->exec_size
);
5957 case FS_OPCODE_LINTERP
:
5958 case SHADER_OPCODE_GET_BUFFER_SIZE
:
5959 case FS_OPCODE_DDX_COARSE
:
5960 case FS_OPCODE_DDX_FINE
:
5961 case FS_OPCODE_DDY_COARSE
:
5962 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
5963 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
5964 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
5965 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
5966 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
5967 return MIN2(16, inst
->exec_size
);
5969 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
:
5970 /* Pre-ILK hardware doesn't have a SIMD8 variant of the texel fetch
5971 * message used to implement varying pull constant loads, so expand it
5972 * to SIMD16. An alternative with longer message payload length but
5973 * shorter return payload would be to use the SIMD8 sampler message that
5974 * takes (header, u, v, r) as parameters instead of (header, u).
5976 return (devinfo
->gen
== 4 ? 16 : MIN2(16, inst
->exec_size
));
5978 case FS_OPCODE_DDY_FINE
:
5979 /* The implementation of this virtual opcode may require emitting
5980 * compressed Align16 instructions, which are severely limited on some
5983 * From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
5984 * Region Restrictions):
5986 * "In Align16 access mode, SIMD16 is not allowed for DW operations
5987 * and SIMD8 is not allowed for DF operations."
5989 * In this context, "DW operations" means "operations acting on 32-bit
5990 * values", so it includes operations on floats.
5992 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
5993 * (Instruction Compression -> Rules and Restrictions):
5995 * "A compressed instruction must be in Align1 access mode. Align16
5996 * mode instructions cannot be compressed."
5998 * Similar text exists in the g45 PRM.
6000 * Empirically, compressed align16 instructions using odd register
6001 * numbers don't appear to work on Sandybridge either.
6003 return (devinfo
->gen
== 4 || devinfo
->gen
== 6 ||
6004 (devinfo
->gen
== 7 && !devinfo
->is_haswell
) ?
6005 MIN2(8, inst
->exec_size
) : MIN2(16, inst
->exec_size
));
6007 case SHADER_OPCODE_MULH
:
6008 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
6009 * is 8-wide on Gen7+.
6011 return (devinfo
->gen
>= 7 ? 8 :
6012 get_fpu_lowered_simd_width(devinfo
, inst
));
6014 case FS_OPCODE_FB_WRITE_LOGICAL
:
6015 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
6018 assert(devinfo
->gen
!= 6 ||
6019 inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
].file
== BAD_FILE
||
6020 inst
->exec_size
== 8);
6021 /* Dual-source FB writes are unsupported in SIMD16 mode. */
6022 return (inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
].file
!= BAD_FILE
?
6023 8 : MIN2(16, inst
->exec_size
));
6025 case FS_OPCODE_FB_READ_LOGICAL
:
6026 return MIN2(16, inst
->exec_size
);
6028 case SHADER_OPCODE_TEX_LOGICAL
:
6029 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
6030 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
6031 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
6032 case SHADER_OPCODE_LOD_LOGICAL
:
6033 case SHADER_OPCODE_TG4_LOGICAL
:
6034 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
6035 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
6036 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
6037 return get_sampler_lowered_simd_width(devinfo
, inst
);
6039 case SHADER_OPCODE_TXD_LOGICAL
:
6040 /* TXD is unsupported in SIMD16 mode. */
6043 case SHADER_OPCODE_TXL_LOGICAL
:
6044 case FS_OPCODE_TXB_LOGICAL
:
6045 /* Only one execution size is representable pre-ILK depending on whether
6046 * the shadow reference argument is present.
6048 if (devinfo
->gen
== 4)
6049 return inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
].file
== BAD_FILE
? 16 : 8;
6051 return get_sampler_lowered_simd_width(devinfo
, inst
);
6053 case SHADER_OPCODE_TXF_LOGICAL
:
6054 case SHADER_OPCODE_TXS_LOGICAL
:
6055 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
6056 * messages. Use SIMD16 instead.
6058 if (devinfo
->gen
== 4)
6061 return get_sampler_lowered_simd_width(devinfo
, inst
);
6063 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
6064 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
6065 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
6068 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
6069 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
6070 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
6071 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
6072 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
6073 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
6074 return MIN2(16, inst
->exec_size
);
6076 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
6077 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
6078 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
6079 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
6080 return devinfo
->gen
<= 8 ? 8 : MIN2(16, inst
->exec_size
);
6082 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
6083 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
6086 case SHADER_OPCODE_URB_READ_SIMD8
:
6087 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
6088 case SHADER_OPCODE_URB_WRITE_SIMD8
:
6089 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
6090 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
6091 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
6092 return MIN2(8, inst
->exec_size
);
6094 case SHADER_OPCODE_QUAD_SWIZZLE
: {
6095 const unsigned swiz
= inst
->src
[1].ud
;
6096 return (is_uniform(inst
->src
[0]) ?
6097 get_fpu_lowered_simd_width(devinfo
, inst
) :
6098 devinfo
->gen
< 11 && type_sz(inst
->src
[0].type
) == 4 ? 8 :
6099 swiz
== BRW_SWIZZLE_XYXY
|| swiz
== BRW_SWIZZLE_ZWZW
? 4 :
6100 get_fpu_lowered_simd_width(devinfo
, inst
));
6102 case SHADER_OPCODE_MOV_INDIRECT
: {
6103 /* From IVB and HSW PRMs:
6105 * "2.When the destination requires two registers and the sources are
6106 * indirect, the sources must use 1x1 regioning mode.
6108 * In case of DF instructions in HSW/IVB, the exec_size is limited by
6109 * the EU decompression logic not handling VxH indirect addressing
6112 const unsigned max_size
= (devinfo
->gen
>= 8 ? 2 : 1) * REG_SIZE
;
6113 /* Prior to Broadwell, we only have 8 address subregisters. */
6114 return MIN3(devinfo
->gen
>= 8 ? 16 : 8,
6115 max_size
/ (inst
->dst
.stride
* type_sz(inst
->dst
.type
)),
6119 case SHADER_OPCODE_LOAD_PAYLOAD
: {
6120 const unsigned reg_count
=
6121 DIV_ROUND_UP(inst
->dst
.component_size(inst
->exec_size
), REG_SIZE
);
6123 if (reg_count
> 2) {
6124 /* Only LOAD_PAYLOAD instructions with per-channel destination region
6125 * can be easily lowered (which excludes headers and heterogeneous
6128 assert(!inst
->header_size
);
6129 for (unsigned i
= 0; i
< inst
->sources
; i
++)
6130 assert(type_sz(inst
->dst
.type
) == type_sz(inst
->src
[i
].type
) ||
6131 inst
->src
[i
].file
== BAD_FILE
);
6133 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
6135 return inst
->exec_size
;
6139 return inst
->exec_size
;
6144 * Return true if splitting out the group of channels of instruction \p inst
6145 * given by lbld.group() requires allocating a temporary for the i-th source
6146 * of the lowered instruction.
6149 needs_src_copy(const fs_builder
&lbld
, const fs_inst
*inst
, unsigned i
)
6151 return !(is_periodic(inst
->src
[i
], lbld
.dispatch_width()) ||
6152 (inst
->components_read(i
) == 1 &&
6153 lbld
.dispatch_width() <= inst
->exec_size
)) ||
6154 (inst
->flags_written() &
6155 flag_mask(inst
->src
[i
], type_sz(inst
->src
[i
].type
)));
6159 * Extract the data that would be consumed by the channel group given by
6160 * lbld.group() from the i-th source region of instruction \p inst and return
6161 * it as result in packed form.
6164 emit_unzip(const fs_builder
&lbld
, fs_inst
*inst
, unsigned i
)
6166 assert(lbld
.group() >= inst
->group
);
6168 /* Specified channel group from the source region. */
6169 const fs_reg src
= horiz_offset(inst
->src
[i
], lbld
.group() - inst
->group
);
6171 if (needs_src_copy(lbld
, inst
, i
)) {
6172 /* Builder of the right width to perform the copy avoiding uninitialized
6173 * data if the lowered execution size is greater than the original
6174 * execution size of the instruction.
6176 const fs_builder cbld
= lbld
.group(MIN2(lbld
.dispatch_width(),
6177 inst
->exec_size
), 0);
6178 const fs_reg tmp
= lbld
.vgrf(inst
->src
[i
].type
, inst
->components_read(i
));
6180 for (unsigned k
= 0; k
< inst
->components_read(i
); ++k
)
6181 cbld
.MOV(offset(tmp
, lbld
, k
), offset(src
, inst
->exec_size
, k
));
6185 } else if (is_periodic(inst
->src
[i
], lbld
.dispatch_width())) {
6186 /* The source is invariant for all dispatch_width-wide groups of the
6189 return inst
->src
[i
];
6192 /* We can just point the lowered instruction at the right channel group
6193 * from the original region.
6200 * Return true if splitting out the group of channels of instruction \p inst
6201 * given by lbld.group() requires allocating a temporary for the destination
6202 * of the lowered instruction and copying the data back to the original
6203 * destination region.
6206 needs_dst_copy(const fs_builder
&lbld
, const fs_inst
*inst
)
6208 /* If the instruction writes more than one component we'll have to shuffle
6209 * the results of multiple lowered instructions in order to make sure that
6210 * they end up arranged correctly in the original destination region.
6212 if (inst
->size_written
> inst
->dst
.component_size(inst
->exec_size
))
6215 /* If the lowered execution size is larger than the original the result of
6216 * the instruction won't fit in the original destination, so we'll have to
6217 * allocate a temporary in any case.
6219 if (lbld
.dispatch_width() > inst
->exec_size
)
6222 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
6223 /* If we already made a copy of the source for other reasons there won't
6224 * be any overlap with the destination.
6226 if (needs_src_copy(lbld
, inst
, i
))
6229 /* In order to keep the logic simple we emit a copy whenever the
6230 * destination region doesn't exactly match an overlapping source, which
6231 * may point at the source and destination not being aligned group by
6232 * group which could cause one of the lowered instructions to overwrite
6233 * the data read from the same source by other lowered instructions.
6235 if (regions_overlap(inst
->dst
, inst
->size_written
,
6236 inst
->src
[i
], inst
->size_read(i
)) &&
6237 !inst
->dst
.equals(inst
->src
[i
]))
6245 * Insert data from a packed temporary into the channel group given by
6246 * lbld.group() of the destination region of instruction \p inst and return
6247 * the temporary as result. Any copy instructions that are required for
6248 * unzipping the previous value (in the case of partial writes) will be
6249 * inserted using \p lbld_before and any copy instructions required for
6250 * zipping up the destination of \p inst will be inserted using \p lbld_after.
6253 emit_zip(const fs_builder
&lbld_before
, const fs_builder
&lbld_after
,
6256 assert(lbld_before
.dispatch_width() == lbld_after
.dispatch_width());
6257 assert(lbld_before
.group() == lbld_after
.group());
6258 assert(lbld_after
.group() >= inst
->group
);
6260 /* Specified channel group from the destination region. */
6261 const fs_reg dst
= horiz_offset(inst
->dst
, lbld_after
.group() - inst
->group
);
6262 const unsigned dst_size
= inst
->size_written
/
6263 inst
->dst
.component_size(inst
->exec_size
);
6265 if (needs_dst_copy(lbld_after
, inst
)) {
6266 const fs_reg tmp
= lbld_after
.vgrf(inst
->dst
.type
, dst_size
);
6268 if (inst
->predicate
) {
6269 /* Handle predication by copying the original contents of
6270 * the destination into the temporary before emitting the
6271 * lowered instruction.
6273 const fs_builder gbld_before
=
6274 lbld_before
.group(MIN2(lbld_before
.dispatch_width(),
6275 inst
->exec_size
), 0);
6276 for (unsigned k
= 0; k
< dst_size
; ++k
) {
6277 gbld_before
.MOV(offset(tmp
, lbld_before
, k
),
6278 offset(dst
, inst
->exec_size
, k
));
6282 const fs_builder gbld_after
=
6283 lbld_after
.group(MIN2(lbld_after
.dispatch_width(),
6284 inst
->exec_size
), 0);
6285 for (unsigned k
= 0; k
< dst_size
; ++k
) {
6286 /* Use a builder of the right width to perform the copy avoiding
6287 * uninitialized data if the lowered execution size is greater than
6288 * the original execution size of the instruction.
6290 gbld_after
.MOV(offset(dst
, inst
->exec_size
, k
),
6291 offset(tmp
, lbld_after
, k
));
6297 /* No need to allocate a temporary for the lowered instruction, just
6298 * take the right group of channels from the original region.
6305 fs_visitor::lower_simd_width()
6307 bool progress
= false;
6309 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
6310 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
6312 if (lower_width
!= inst
->exec_size
) {
6313 /* Builder matching the original instruction. We may also need to
6314 * emit an instruction of width larger than the original, set the
6315 * execution size of the builder to the highest of both for now so
6316 * we're sure that both cases can be handled.
6318 const unsigned max_width
= MAX2(inst
->exec_size
, lower_width
);
6319 const fs_builder ibld
= bld
.at(block
, inst
)
6320 .exec_all(inst
->force_writemask_all
)
6321 .group(max_width
, inst
->group
/ max_width
);
6323 /* Split the copies in chunks of the execution width of either the
6324 * original or the lowered instruction, whichever is lower.
6326 const unsigned n
= DIV_ROUND_UP(inst
->exec_size
, lower_width
);
6327 const unsigned dst_size
= inst
->size_written
/
6328 inst
->dst
.component_size(inst
->exec_size
);
6330 assert(!inst
->writes_accumulator
&& !inst
->mlen
);
6332 /* Inserting the zip, unzip, and duplicated instructions in all of
6333 * the right spots is somewhat tricky. All of the unzip and any
6334 * instructions from the zip which unzip the destination prior to
6335 * writing need to happen before all of the per-group instructions
6336 * and the zip instructions need to happen after. In order to sort
6337 * this all out, we insert the unzip instructions before \p inst,
6338 * insert the per-group instructions after \p inst (i.e. before
6339 * inst->next), and insert the zip instructions before the
6340 * instruction after \p inst. Since we are inserting instructions
6341 * after \p inst, inst->next is a moving target and we need to save
6342 * it off here so that we insert the zip instructions in the right
6345 * Since we're inserting split instructions after after_inst, the
6346 * instructions will end up in the reverse order that we insert them.
6347 * However, certain render target writes require that the low group
6348 * instructions come before the high group. From the Ivy Bridge PRM
6349 * Vol. 4, Pt. 1, Section 3.9.11:
6351 * "If multiple SIMD8 Dual Source messages are delivered by the
6352 * pixel shader thread, each SIMD8_DUALSRC_LO message must be
6353 * issued before the SIMD8_DUALSRC_HI message with the same Slot
6354 * Group Select setting."
6356 * And, from Section 3.9.11.1 of the same PRM:
6358 * "When SIMD32 or SIMD16 PS threads send render target writes
6359 * with multiple SIMD8 and SIMD16 messages, the following must
6362 * All the slots (as described above) must have a corresponding
6363 * render target write irrespective of the slot's validity. A slot
6364 * is considered valid when at least one sample is enabled. For
6365 * example, a SIMD16 PS thread must send two SIMD8 render target
6366 * writes to cover all the slots.
6368 * PS thread must send SIMD render target write messages with
6369 * increasing slot numbers. For example, SIMD16 thread has
6370 * Slot[15:0] and if two SIMD8 render target writes are used, the
6371 * first SIMD8 render target write must send Slot[7:0] and the
6372 * next one must send Slot[15:8]."
6374 * In order to make low group instructions come before high group
6375 * instructions (this is required for some render target writes), we
6376 * split from the highest group to lowest.
6378 exec_node
*const after_inst
= inst
->next
;
6379 for (int i
= n
- 1; i
>= 0; i
--) {
6380 /* Emit a copy of the original instruction with the lowered width.
6381 * If the EOT flag was set throw it away except for the last
6382 * instruction to avoid killing the thread prematurely.
6384 fs_inst split_inst
= *inst
;
6385 split_inst
.exec_size
= lower_width
;
6386 split_inst
.eot
= inst
->eot
&& i
== int(n
- 1);
6388 /* Select the correct channel enables for the i-th group, then
6389 * transform the sources and destination and emit the lowered
6392 const fs_builder lbld
= ibld
.group(lower_width
, i
);
6394 for (unsigned j
= 0; j
< inst
->sources
; j
++)
6395 split_inst
.src
[j
] = emit_unzip(lbld
.at(block
, inst
), inst
, j
);
6397 split_inst
.dst
= emit_zip(lbld
.at(block
, inst
),
6398 lbld
.at(block
, after_inst
), inst
);
6399 split_inst
.size_written
=
6400 split_inst
.dst
.component_size(lower_width
) * dst_size
;
6402 lbld
.at(block
, inst
->next
).emit(split_inst
);
6405 inst
->remove(block
);
6411 invalidate_live_intervals();
6417 fs_visitor::dump_instructions()
6419 dump_instructions(NULL
);
6423 fs_visitor::dump_instructions(const char *name
)
6425 FILE *file
= stderr
;
6426 if (name
&& geteuid() != 0) {
6427 file
= fopen(name
, "w");
6433 calculate_register_pressure();
6434 int ip
= 0, max_pressure
= 0;
6435 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
6436 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
6437 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
6438 dump_instruction(inst
, file
);
6441 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
6444 foreach_in_list(backend_instruction
, inst
, &instructions
) {
6445 fprintf(file
, "%4d: ", ip
++);
6446 dump_instruction(inst
, file
);
6450 if (file
!= stderr
) {
6456 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
6458 dump_instruction(be_inst
, stderr
);
6462 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
6464 fs_inst
*inst
= (fs_inst
*)be_inst
;
6466 if (inst
->predicate
) {
6467 fprintf(file
, "(%cf%d.%d) ",
6468 inst
->predicate_inverse
? '-' : '+',
6469 inst
->flag_subreg
/ 2,
6470 inst
->flag_subreg
% 2);
6473 fprintf(file
, "%s", brw_instruction_name(devinfo
, inst
->opcode
));
6475 fprintf(file
, ".sat");
6476 if (inst
->conditional_mod
) {
6477 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
6478 if (!inst
->predicate
&&
6479 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
6480 inst
->opcode
!= BRW_OPCODE_CSEL
&&
6481 inst
->opcode
!= BRW_OPCODE_IF
&&
6482 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
6483 fprintf(file
, ".f%d.%d", inst
->flag_subreg
/ 2,
6484 inst
->flag_subreg
% 2);
6487 fprintf(file
, "(%d) ", inst
->exec_size
);
6490 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
6493 if (inst
->ex_mlen
) {
6494 fprintf(file
, "(ex_mlen: %d) ", inst
->ex_mlen
);
6498 fprintf(file
, "(EOT) ");
6501 switch (inst
->dst
.file
) {
6503 fprintf(file
, "vgrf%d", inst
->dst
.nr
);
6506 fprintf(file
, "g%d", inst
->dst
.nr
);
6509 fprintf(file
, "m%d", inst
->dst
.nr
);
6512 fprintf(file
, "(null)");
6515 fprintf(file
, "***u%d***", inst
->dst
.nr
);
6518 fprintf(file
, "***attr%d***", inst
->dst
.nr
);
6521 switch (inst
->dst
.nr
) {
6523 fprintf(file
, "null");
6525 case BRW_ARF_ADDRESS
:
6526 fprintf(file
, "a0.%d", inst
->dst
.subnr
);
6528 case BRW_ARF_ACCUMULATOR
:
6529 fprintf(file
, "acc%d", inst
->dst
.subnr
);
6532 fprintf(file
, "f%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
6535 fprintf(file
, "arf%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
6540 unreachable("not reached");
6543 if (inst
->dst
.offset
||
6544 (inst
->dst
.file
== VGRF
&&
6545 alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
!= inst
->size_written
)) {
6546 const unsigned reg_size
= (inst
->dst
.file
== UNIFORM
? 4 : REG_SIZE
);
6547 fprintf(file
, "+%d.%d", inst
->dst
.offset
/ reg_size
,
6548 inst
->dst
.offset
% reg_size
);
6551 if (inst
->dst
.stride
!= 1)
6552 fprintf(file
, "<%u>", inst
->dst
.stride
);
6553 fprintf(file
, ":%s, ", brw_reg_type_to_letters(inst
->dst
.type
));
6555 for (int i
= 0; i
< inst
->sources
; i
++) {
6556 if (inst
->src
[i
].negate
)
6558 if (inst
->src
[i
].abs
)
6560 switch (inst
->src
[i
].file
) {
6562 fprintf(file
, "vgrf%d", inst
->src
[i
].nr
);
6565 fprintf(file
, "g%d", inst
->src
[i
].nr
);
6568 fprintf(file
, "***m%d***", inst
->src
[i
].nr
);
6571 fprintf(file
, "attr%d", inst
->src
[i
].nr
);
6574 fprintf(file
, "u%d", inst
->src
[i
].nr
);
6577 fprintf(file
, "(null)");
6580 switch (inst
->src
[i
].type
) {
6581 case BRW_REGISTER_TYPE_F
:
6582 fprintf(file
, "%-gf", inst
->src
[i
].f
);
6584 case BRW_REGISTER_TYPE_DF
:
6585 fprintf(file
, "%fdf", inst
->src
[i
].df
);
6587 case BRW_REGISTER_TYPE_W
:
6588 case BRW_REGISTER_TYPE_D
:
6589 fprintf(file
, "%dd", inst
->src
[i
].d
);
6591 case BRW_REGISTER_TYPE_UW
:
6592 case BRW_REGISTER_TYPE_UD
:
6593 fprintf(file
, "%uu", inst
->src
[i
].ud
);
6595 case BRW_REGISTER_TYPE_Q
:
6596 fprintf(file
, "%" PRId64
"q", inst
->src
[i
].d64
);
6598 case BRW_REGISTER_TYPE_UQ
:
6599 fprintf(file
, "%" PRIu64
"uq", inst
->src
[i
].u64
);
6601 case BRW_REGISTER_TYPE_VF
:
6602 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
6603 brw_vf_to_float((inst
->src
[i
].ud
>> 0) & 0xff),
6604 brw_vf_to_float((inst
->src
[i
].ud
>> 8) & 0xff),
6605 brw_vf_to_float((inst
->src
[i
].ud
>> 16) & 0xff),
6606 brw_vf_to_float((inst
->src
[i
].ud
>> 24) & 0xff));
6608 case BRW_REGISTER_TYPE_V
:
6609 case BRW_REGISTER_TYPE_UV
:
6610 fprintf(file
, "%08x%s", inst
->src
[i
].ud
,
6611 inst
->src
[i
].type
== BRW_REGISTER_TYPE_V
? "V" : "UV");
6614 fprintf(file
, "???");
6619 switch (inst
->src
[i
].nr
) {
6621 fprintf(file
, "null");
6623 case BRW_ARF_ADDRESS
:
6624 fprintf(file
, "a0.%d", inst
->src
[i
].subnr
);
6626 case BRW_ARF_ACCUMULATOR
:
6627 fprintf(file
, "acc%d", inst
->src
[i
].subnr
);
6630 fprintf(file
, "f%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
6633 fprintf(file
, "arf%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
6639 if (inst
->src
[i
].offset
||
6640 (inst
->src
[i
].file
== VGRF
&&
6641 alloc
.sizes
[inst
->src
[i
].nr
] * REG_SIZE
!= inst
->size_read(i
))) {
6642 const unsigned reg_size
= (inst
->src
[i
].file
== UNIFORM
? 4 : REG_SIZE
);
6643 fprintf(file
, "+%d.%d", inst
->src
[i
].offset
/ reg_size
,
6644 inst
->src
[i
].offset
% reg_size
);
6647 if (inst
->src
[i
].abs
)
6650 if (inst
->src
[i
].file
!= IMM
) {
6652 if (inst
->src
[i
].file
== ARF
|| inst
->src
[i
].file
== FIXED_GRF
) {
6653 unsigned hstride
= inst
->src
[i
].hstride
;
6654 stride
= (hstride
== 0 ? 0 : (1 << (hstride
- 1)));
6656 stride
= inst
->src
[i
].stride
;
6659 fprintf(file
, "<%u>", stride
);
6661 fprintf(file
, ":%s", brw_reg_type_to_letters(inst
->src
[i
].type
));
6664 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
6665 fprintf(file
, ", ");
6670 if (inst
->force_writemask_all
)
6671 fprintf(file
, "NoMask ");
6673 if (inst
->exec_size
!= dispatch_width
)
6674 fprintf(file
, "group%d ", inst
->group
);
6676 fprintf(file
, "\n");
6680 fs_visitor::setup_fs_payload_gen6()
6682 assert(stage
== MESA_SHADER_FRAGMENT
);
6683 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
6684 const unsigned payload_width
= MIN2(16, dispatch_width
);
6685 assert(dispatch_width
% payload_width
== 0);
6686 assert(devinfo
->gen
>= 6);
6688 prog_data
->uses_src_depth
= prog_data
->uses_src_w
=
6689 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
6691 prog_data
->uses_sample_mask
=
6692 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
) != 0;
6694 /* From the Ivy Bridge PRM documentation for 3DSTATE_PS:
6696 * "MSDISPMODE_PERSAMPLE is required in order to select
6699 * So we can only really get sample positions if we are doing real
6700 * per-sample dispatch. If we need gl_SamplePosition and we don't have
6701 * persample dispatch, we hard-code it to 0.5.
6703 prog_data
->uses_pos_offset
= prog_data
->persample_dispatch
&&
6704 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
6706 /* R0: PS thread payload header. */
6709 for (unsigned j
= 0; j
< dispatch_width
/ payload_width
; j
++) {
6710 /* R1: masks, pixel X/Y coordinates. */
6711 payload
.subspan_coord_reg
[j
] = payload
.num_regs
++;
6714 for (unsigned j
= 0; j
< dispatch_width
/ payload_width
; j
++) {
6715 /* R3-26: barycentric interpolation coordinates. These appear in the
6716 * same order that they appear in the brw_barycentric_mode enum. Each
6717 * set of coordinates occupies 2 registers if dispatch width == 8 and 4
6718 * registers if dispatch width == 16. Coordinates only appear if they
6719 * were enabled using the "Barycentric Interpolation Mode" bits in
6722 for (int i
= 0; i
< BRW_BARYCENTRIC_MODE_COUNT
; ++i
) {
6723 if (prog_data
->barycentric_interp_modes
& (1 << i
)) {
6724 payload
.barycentric_coord_reg
[i
][j
] = payload
.num_regs
;
6725 payload
.num_regs
+= payload_width
/ 4;
6729 /* R27-28: interpolated depth if uses source depth */
6730 if (prog_data
->uses_src_depth
) {
6731 payload
.source_depth_reg
[j
] = payload
.num_regs
;
6732 payload
.num_regs
+= payload_width
/ 8;
6735 /* R29-30: interpolated W set if GEN6_WM_USES_SOURCE_W. */
6736 if (prog_data
->uses_src_w
) {
6737 payload
.source_w_reg
[j
] = payload
.num_regs
;
6738 payload
.num_regs
+= payload_width
/ 8;
6741 /* R31: MSAA position offsets. */
6742 if (prog_data
->uses_pos_offset
) {
6743 payload
.sample_pos_reg
[j
] = payload
.num_regs
;
6747 /* R32-33: MSAA input coverage mask */
6748 if (prog_data
->uses_sample_mask
) {
6749 assert(devinfo
->gen
>= 7);
6750 payload
.sample_mask_in_reg
[j
] = payload
.num_regs
;
6751 payload
.num_regs
+= payload_width
/ 8;
6755 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
6756 source_depth_to_render_target
= true;
6761 fs_visitor::setup_vs_payload()
6763 /* R0: thread header, R1: urb handles */
6764 payload
.num_regs
= 2;
6768 fs_visitor::setup_gs_payload()
6770 assert(stage
== MESA_SHADER_GEOMETRY
);
6772 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
6773 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
6775 /* R0: thread header, R1: output URB handles */
6776 payload
.num_regs
= 2;
6778 if (gs_prog_data
->include_primitive_id
) {
6779 /* R2: Primitive ID 0..7 */
6783 /* Always enable VUE handles so we can safely use pull model if needed.
6785 * The push model for a GS uses a ton of register space even for trivial
6786 * scenarios with just a few inputs, so just make things easier and a bit
6787 * safer by always having pull model available.
6789 gs_prog_data
->base
.include_vue_handles
= true;
6791 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
6792 payload
.num_regs
+= nir
->info
.gs
.vertices_in
;
6794 /* Use a maximum of 24 registers for push-model inputs. */
6795 const unsigned max_push_components
= 24;
6797 /* If pushing our inputs would take too many registers, reduce the URB read
6798 * length (which is in HWords, or 8 registers), and resort to pulling.
6800 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
6801 * have to multiply by VerticesIn to obtain the total storage requirement.
6803 if (8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
>
6804 max_push_components
) {
6805 vue_prog_data
->urb_read_length
=
6806 ROUND_DOWN_TO(max_push_components
/ nir
->info
.gs
.vertices_in
, 8) / 8;
6811 fs_visitor::setup_cs_payload()
6813 assert(devinfo
->gen
>= 7);
6814 payload
.num_regs
= 1;
6818 fs_visitor::calculate_register_pressure()
6820 invalidate_live_intervals();
6821 calculate_live_intervals();
6823 unsigned num_instructions
= 0;
6824 foreach_block(block
, cfg
)
6825 num_instructions
+= block
->instructions
.length();
6827 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
6829 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
6830 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
6831 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
6836 fs_visitor::optimize()
6838 /* Start by validating the shader we currently have. */
6841 /* bld is the common builder object pointing at the end of the program we
6842 * used to translate it into i965 IR. For the optimization and lowering
6843 * passes coming next, any code added after the end of the program without
6844 * having explicitly called fs_builder::at() clearly points at a mistake.
6845 * Ideally optimization passes wouldn't be part of the visitor so they
6846 * wouldn't have access to bld at all, but they do, so just in case some
6847 * pass forgets to ask for a location explicitly set it to NULL here to
6848 * make it trip. The dispatch width is initialized to a bogus value to
6849 * make sure that optimizations set the execution controls explicitly to
6850 * match the code they are manipulating instead of relying on the defaults.
6852 bld
= fs_builder(this, 64);
6854 assign_constant_locations();
6855 lower_constant_loads();
6859 split_virtual_grfs();
6862 #define OPT(pass, args...) ({ \
6864 bool this_progress = pass(args); \
6866 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
6867 char filename[64]; \
6868 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
6869 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
6871 backend_shader::dump_instructions(filename); \
6876 progress = progress || this_progress; \
6880 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
6882 snprintf(filename
, 64, "%s%d-%s-00-00-start",
6883 stage_abbrev
, dispatch_width
, nir
->info
.name
);
6885 backend_shader::dump_instructions(filename
);
6888 bool progress
= false;
6892 OPT(remove_extra_rounding_modes
);
6899 OPT(remove_duplicate_mrf_writes
);
6903 OPT(opt_copy_propagation
);
6904 OPT(opt_predicated_break
, this);
6905 OPT(opt_cmod_propagation
);
6906 OPT(dead_code_eliminate
);
6907 OPT(opt_peephole_sel
);
6908 OPT(dead_control_flow_eliminate
, this);
6909 OPT(opt_register_renaming
);
6910 OPT(opt_saturate_propagation
);
6911 OPT(register_coalesce
);
6912 OPT(compute_to_mrf
);
6913 OPT(eliminate_find_live_channel
);
6915 OPT(compact_virtual_grfs
);
6918 /* Do this after cmod propagation has had every possible opportunity to
6919 * propagate results into SEL instructions.
6921 if (OPT(opt_peephole_csel
))
6922 OPT(dead_code_eliminate
);
6927 if (OPT(lower_pack
)) {
6928 OPT(register_coalesce
);
6929 OPT(dead_code_eliminate
);
6932 OPT(lower_simd_width
);
6934 /* After SIMD lowering just in case we had to unroll the EOT send. */
6935 OPT(opt_sampler_eot
);
6937 OPT(lower_logical_sends
);
6940 OPT(opt_copy_propagation
);
6941 /* Only run after logical send lowering because it's easier to implement
6942 * in terms of physical sends.
6944 if (OPT(opt_zero_samples
))
6945 OPT(opt_copy_propagation
);
6946 /* Run after logical send lowering to give it a chance to CSE the
6947 * LOAD_PAYLOAD instructions created to construct the payloads of
6948 * e.g. texturing messages in cases where it wasn't possible to CSE the
6949 * whole logical instruction.
6952 OPT(register_coalesce
);
6953 OPT(compute_to_mrf
);
6954 OPT(dead_code_eliminate
);
6955 OPT(remove_duplicate_mrf_writes
);
6956 OPT(opt_peephole_sel
);
6959 OPT(opt_redundant_discard_jumps
);
6961 if (OPT(lower_load_payload
)) {
6962 split_virtual_grfs();
6963 OPT(register_coalesce
);
6964 OPT(lower_simd_width
);
6965 OPT(compute_to_mrf
);
6966 OPT(dead_code_eliminate
);
6969 OPT(opt_combine_constants
);
6970 OPT(lower_integer_multiplication
);
6972 if (devinfo
->gen
<= 5 && OPT(lower_minmax
)) {
6973 OPT(opt_cmod_propagation
);
6975 OPT(opt_copy_propagation
);
6976 OPT(dead_code_eliminate
);
6979 if (OPT(lower_regioning
)) {
6980 OPT(opt_copy_propagation
);
6981 OPT(dead_code_eliminate
);
6982 OPT(lower_simd_width
);
6985 OPT(fixup_sends_duplicate_payload
);
6987 lower_uniform_pull_constant_loads();
6993 * From the Skylake PRM Vol. 2a docs for sends:
6995 * "It is required that the second block of GRFs does not overlap with the
6998 * There are plenty of cases where we may accidentally violate this due to
6999 * having, for instance, both sources be the constant 0. This little pass
7000 * just adds a new vgrf for the second payload and copies it over.
7003 fs_visitor::fixup_sends_duplicate_payload()
7005 bool progress
= false;
7007 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
7008 if (inst
->opcode
== SHADER_OPCODE_SEND
&& inst
->ex_mlen
> 0 &&
7009 regions_overlap(inst
->src
[2], inst
->mlen
* REG_SIZE
,
7010 inst
->src
[3], inst
->ex_mlen
* REG_SIZE
)) {
7011 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(inst
->ex_mlen
),
7012 BRW_REGISTER_TYPE_UD
);
7013 /* Sadly, we've lost all notion of channels and bit sizes at this
7014 * point. Just WE_all it.
7016 const fs_builder ibld
= bld
.at(block
, inst
).exec_all().group(16, 0);
7017 fs_reg copy_src
= retype(inst
->src
[3], BRW_REGISTER_TYPE_UD
);
7018 fs_reg copy_dst
= tmp
;
7019 for (unsigned i
= 0; i
< inst
->ex_mlen
; i
+= 2) {
7020 if (inst
->ex_mlen
== i
+ 1) {
7021 /* Only one register left; do SIMD8 */
7022 ibld
.group(8, 0).MOV(copy_dst
, copy_src
);
7024 ibld
.MOV(copy_dst
, copy_src
);
7026 copy_src
= offset(copy_src
, ibld
, 1);
7027 copy_dst
= offset(copy_dst
, ibld
, 1);
7035 invalidate_live_intervals();
7041 * Three source instruction must have a GRF/MRF destination register.
7042 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
7045 fs_visitor::fixup_3src_null_dest()
7047 bool progress
= false;
7049 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
7050 if (inst
->is_3src(devinfo
) && inst
->dst
.is_null()) {
7051 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
7058 invalidate_live_intervals();
7062 fs_visitor::allocate_registers(unsigned min_dispatch_width
, bool allow_spilling
)
7064 bool allocated_without_spills
;
7066 static const enum instruction_scheduler_mode pre_modes
[] = {
7068 SCHEDULE_PRE_NON_LIFO
,
7072 bool spill_all
= allow_spilling
&& (INTEL_DEBUG
& DEBUG_SPILL_FS
);
7074 /* Try each scheduling heuristic to see if it can successfully register
7075 * allocate without spilling. They should be ordered by decreasing
7076 * performance but increasing likelihood of allocating.
7078 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
7079 schedule_instructions(pre_modes
[i
]);
7082 assign_regs_trivial();
7083 allocated_without_spills
= true;
7085 allocated_without_spills
= assign_regs(false, spill_all
);
7087 if (allocated_without_spills
)
7091 if (!allocated_without_spills
) {
7092 if (!allow_spilling
)
7093 fail("Failure to register allocate and spilling is not allowed.");
7095 /* We assume that any spilling is worse than just dropping back to
7096 * SIMD8. There's probably actually some intermediate point where
7097 * SIMD16 with a couple of spills is still better.
7099 if (dispatch_width
> min_dispatch_width
) {
7100 fail("Failure to register allocate. Reduce number of "
7101 "live scalar values to avoid this.");
7103 compiler
->shader_perf_log(log_data
,
7104 "%s shader triggered register spilling. "
7105 "Try reducing the number of live scalar "
7106 "values to improve performance.\n",
7110 /* Since we're out of heuristics, just go spill registers until we
7111 * get an allocation.
7113 while (!assign_regs(true, spill_all
)) {
7119 /* This must come after all optimization and register allocation, since
7120 * it inserts dead code that happens to have side effects, and it does
7121 * so based on the actual physical registers in use.
7123 insert_gen4_send_dependency_workarounds();
7128 opt_bank_conflicts();
7130 schedule_instructions(SCHEDULE_POST
);
7132 if (last_scratch
> 0) {
7133 MAYBE_UNUSED
unsigned max_scratch_size
= 2 * 1024 * 1024;
7135 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
7137 if (stage
== MESA_SHADER_COMPUTE
) {
7138 if (devinfo
->is_haswell
) {
7139 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7140 * field documentation, Haswell supports a minimum of 2kB of
7141 * scratch space for compute shaders, unlike every other stage
7144 prog_data
->total_scratch
= MAX2(prog_data
->total_scratch
, 2048);
7145 } else if (devinfo
->gen
<= 7) {
7146 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7147 * field documentation, platforms prior to Haswell measure scratch
7148 * size linearly with a range of [1kB, 12kB] and 1kB granularity.
7150 prog_data
->total_scratch
= ALIGN(last_scratch
, 1024);
7151 max_scratch_size
= 12 * 1024;
7155 /* We currently only support up to 2MB of scratch space. If we
7156 * need to support more eventually, the documentation suggests
7157 * that we could allocate a larger buffer, and partition it out
7158 * ourselves. We'd just have to undo the hardware's address
7159 * calculation by subtracting (FFTID * Per Thread Scratch Space)
7160 * and then add FFTID * (Larger Per Thread Scratch Space).
7162 * See 3D-Media-GPGPU Engine > Media GPGPU Pipeline >
7163 * Thread Group Tracking > Local Memory/Scratch Space.
7165 assert(prog_data
->total_scratch
< max_scratch_size
);
7170 fs_visitor::run_vs()
7172 assert(stage
== MESA_SHADER_VERTEX
);
7176 if (shader_time_index
>= 0)
7177 emit_shader_time_begin();
7184 compute_clip_distance();
7188 if (shader_time_index
>= 0)
7189 emit_shader_time_end();
7195 assign_curb_setup();
7196 assign_vs_urb_setup();
7198 fixup_3src_null_dest();
7199 allocate_registers(8, true);
7205 fs_visitor::run_tcs_single_patch()
7207 assert(stage
== MESA_SHADER_TESS_CTRL
);
7209 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
7211 /* r1-r4 contain the ICP handles. */
7212 payload
.num_regs
= 5;
7214 if (shader_time_index
>= 0)
7215 emit_shader_time_begin();
7217 /* Initialize gl_InvocationID */
7218 fs_reg channels_uw
= bld
.vgrf(BRW_REGISTER_TYPE_UW
);
7219 fs_reg channels_ud
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7220 bld
.MOV(channels_uw
, fs_reg(brw_imm_uv(0x76543210)));
7221 bld
.MOV(channels_ud
, channels_uw
);
7223 if (tcs_prog_data
->instances
== 1) {
7224 invocation_id
= channels_ud
;
7226 const unsigned invocation_id_mask
= devinfo
->gen
>= 11 ?
7227 INTEL_MASK(22, 16) : INTEL_MASK(23, 17);
7228 const unsigned invocation_id_shift
= devinfo
->gen
>= 11 ? 16 : 17;
7230 invocation_id
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7232 /* Get instance number from g0.2 bits 23:17, and multiply it by 8. */
7233 fs_reg t
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7234 fs_reg instance_times_8
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7235 bld
.AND(t
, fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
)),
7236 brw_imm_ud(invocation_id_mask
));
7237 bld
.SHR(instance_times_8
, t
, brw_imm_ud(invocation_id_shift
- 3));
7239 bld
.ADD(invocation_id
, instance_times_8
, channels_ud
);
7242 /* Fix the disptach mask */
7243 if (nir
->info
.tess
.tcs_vertices_out
% 8) {
7244 bld
.CMP(bld
.null_reg_ud(), invocation_id
,
7245 brw_imm_ud(nir
->info
.tess
.tcs_vertices_out
), BRW_CONDITIONAL_L
);
7246 bld
.IF(BRW_PREDICATE_NORMAL
);
7251 if (nir
->info
.tess
.tcs_vertices_out
% 8) {
7252 bld
.emit(BRW_OPCODE_ENDIF
);
7255 /* Emit EOT write; set TR DS Cache bit */
7257 fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
7258 fs_reg(brw_imm_ud(WRITEMASK_X
<< 16)),
7259 fs_reg(brw_imm_ud(0)),
7261 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 3);
7262 bld
.LOAD_PAYLOAD(payload
, srcs
, 3, 2);
7264 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
,
7265 bld
.null_reg_ud(), payload
);
7269 if (shader_time_index
>= 0)
7270 emit_shader_time_end();
7279 assign_curb_setup();
7280 assign_tcs_single_patch_urb_setup();
7282 fixup_3src_null_dest();
7283 allocate_registers(8, true);
7289 fs_visitor::run_tes()
7291 assert(stage
== MESA_SHADER_TESS_EVAL
);
7293 /* R0: thread header, R1-3: gl_TessCoord.xyz, R4: URB handles */
7294 payload
.num_regs
= 5;
7296 if (shader_time_index
>= 0)
7297 emit_shader_time_begin();
7306 if (shader_time_index
>= 0)
7307 emit_shader_time_end();
7313 assign_curb_setup();
7314 assign_tes_urb_setup();
7316 fixup_3src_null_dest();
7317 allocate_registers(8, true);
7323 fs_visitor::run_gs()
7325 assert(stage
== MESA_SHADER_GEOMETRY
);
7329 this->final_gs_vertex_count
= vgrf(glsl_type::uint_type
);
7331 if (gs_compile
->control_data_header_size_bits
> 0) {
7332 /* Create a VGRF to store accumulated control data bits. */
7333 this->control_data_bits
= vgrf(glsl_type::uint_type
);
7335 /* If we're outputting more than 32 control data bits, then EmitVertex()
7336 * will set control_data_bits to 0 after emitting the first vertex.
7337 * Otherwise, we need to initialize it to 0 here.
7339 if (gs_compile
->control_data_header_size_bits
<= 32) {
7340 const fs_builder abld
= bld
.annotate("initialize control data bits");
7341 abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
7345 if (shader_time_index
>= 0)
7346 emit_shader_time_begin();
7350 emit_gs_thread_end();
7352 if (shader_time_index
>= 0)
7353 emit_shader_time_end();
7362 assign_curb_setup();
7363 assign_gs_urb_setup();
7365 fixup_3src_null_dest();
7366 allocate_registers(8, true);
7371 /* From the SKL PRM, Volume 16, Workarounds:
7373 * 0877 3D Pixel Shader Hang possible when pixel shader dispatched with
7374 * only header phases (R0-R2)
7376 * WA: Enable a non-header phase (e.g. push constant) when dispatch would
7377 * have been header only.
7379 * Instead of enabling push constants one can alternatively enable one of the
7380 * inputs. Here one simply chooses "layer" which shouldn't impose much
7384 gen9_ps_header_only_workaround(struct brw_wm_prog_data
*wm_prog_data
)
7386 if (wm_prog_data
->num_varying_inputs
)
7389 if (wm_prog_data
->base
.curb_read_length
)
7392 wm_prog_data
->urb_setup
[VARYING_SLOT_LAYER
] = 0;
7393 wm_prog_data
->num_varying_inputs
= 1;
7397 fs_visitor::run_fs(bool allow_spilling
, bool do_rep_send
)
7399 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
7400 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
7402 assert(stage
== MESA_SHADER_FRAGMENT
);
7404 if (devinfo
->gen
>= 6)
7405 setup_fs_payload_gen6();
7407 setup_fs_payload_gen4();
7411 } else if (do_rep_send
) {
7412 assert(dispatch_width
== 16);
7413 emit_repclear_shader();
7415 if (shader_time_index
>= 0)
7416 emit_shader_time_begin();
7418 calculate_urb_setup();
7419 if (nir
->info
.inputs_read
> 0 ||
7420 (nir
->info
.outputs_read
> 0 && !wm_key
->coherent_fb_fetch
)) {
7421 if (devinfo
->gen
< 6)
7422 emit_interpolation_setup_gen4();
7424 emit_interpolation_setup_gen6();
7427 /* We handle discards by keeping track of the still-live pixels in f0.1.
7428 * Initialize it with the dispatched pixels.
7430 if (wm_prog_data
->uses_kill
) {
7431 const fs_reg dispatch_mask
=
7432 devinfo
->gen
>= 6 ? brw_vec1_grf(1, 7) : brw_vec1_grf(0, 0);
7433 bld
.exec_all().group(1, 0)
7434 .MOV(retype(brw_flag_reg(0, 1), BRW_REGISTER_TYPE_UW
),
7435 retype(dispatch_mask
, BRW_REGISTER_TYPE_UW
));
7443 if (wm_prog_data
->uses_kill
)
7444 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
7446 if (wm_key
->alpha_test_func
)
7451 if (shader_time_index
>= 0)
7452 emit_shader_time_end();
7458 assign_curb_setup();
7460 if (devinfo
->gen
>= 9)
7461 gen9_ps_header_only_workaround(wm_prog_data
);
7465 fixup_3src_null_dest();
7466 allocate_registers(8, allow_spilling
);
7476 fs_visitor::run_cs(unsigned min_dispatch_width
)
7478 assert(stage
== MESA_SHADER_COMPUTE
);
7479 assert(dispatch_width
>= min_dispatch_width
);
7483 if (shader_time_index
>= 0)
7484 emit_shader_time_begin();
7486 if (devinfo
->is_haswell
&& prog_data
->total_shared
> 0) {
7487 /* Move SLM index from g0.0[27:24] to sr0.1[11:8] */
7488 const fs_builder abld
= bld
.exec_all().group(1, 0);
7489 abld
.MOV(retype(brw_sr0_reg(1), BRW_REGISTER_TYPE_UW
),
7490 suboffset(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
), 1));
7498 emit_cs_terminate();
7500 if (shader_time_index
>= 0)
7501 emit_shader_time_end();
7507 assign_curb_setup();
7509 fixup_3src_null_dest();
7510 allocate_registers(min_dispatch_width
, true);
7519 * Return a bitfield where bit n is set if barycentric interpolation mode n
7520 * (see enum brw_barycentric_mode) is needed by the fragment shader.
7522 * We examine the load_barycentric intrinsics rather than looking at input
7523 * variables so that we catch interpolateAtCentroid() messages too, which
7524 * also need the BRW_BARYCENTRIC_[NON]PERSPECTIVE_CENTROID mode set up.
7527 brw_compute_barycentric_interp_modes(const struct gen_device_info
*devinfo
,
7528 const nir_shader
*shader
)
7530 unsigned barycentric_interp_modes
= 0;
7532 nir_foreach_function(f
, shader
) {
7536 nir_foreach_block(block
, f
->impl
) {
7537 nir_foreach_instr(instr
, block
) {
7538 if (instr
->type
!= nir_instr_type_intrinsic
)
7541 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
7542 if (intrin
->intrinsic
!= nir_intrinsic_load_interpolated_input
)
7545 /* Ignore WPOS; it doesn't require interpolation. */
7546 if (nir_intrinsic_base(intrin
) == VARYING_SLOT_POS
)
7549 intrin
= nir_instr_as_intrinsic(intrin
->src
[0].ssa
->parent_instr
);
7550 enum glsl_interp_mode interp
= (enum glsl_interp_mode
)
7551 nir_intrinsic_interp_mode(intrin
);
7552 nir_intrinsic_op bary_op
= intrin
->intrinsic
;
7553 enum brw_barycentric_mode bary
=
7554 brw_barycentric_mode(interp
, bary_op
);
7556 barycentric_interp_modes
|= 1 << bary
;
7558 if (devinfo
->needs_unlit_centroid_workaround
&&
7559 bary_op
== nir_intrinsic_load_barycentric_centroid
)
7560 barycentric_interp_modes
|= 1 << centroid_to_pixel(bary
);
7565 return barycentric_interp_modes
;
7569 brw_compute_flat_inputs(struct brw_wm_prog_data
*prog_data
,
7570 const nir_shader
*shader
)
7572 prog_data
->flat_inputs
= 0;
7574 nir_foreach_variable(var
, &shader
->inputs
) {
7575 unsigned slots
= glsl_count_attribute_slots(var
->type
, false);
7576 for (unsigned s
= 0; s
< slots
; s
++) {
7577 int input_index
= prog_data
->urb_setup
[var
->data
.location
+ s
];
7579 if (input_index
< 0)
7583 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
7584 prog_data
->flat_inputs
|= 1 << input_index
;
7590 computed_depth_mode(const nir_shader
*shader
)
7592 if (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
7593 switch (shader
->info
.fs
.depth_layout
) {
7594 case FRAG_DEPTH_LAYOUT_NONE
:
7595 case FRAG_DEPTH_LAYOUT_ANY
:
7596 return BRW_PSCDEPTH_ON
;
7597 case FRAG_DEPTH_LAYOUT_GREATER
:
7598 return BRW_PSCDEPTH_ON_GE
;
7599 case FRAG_DEPTH_LAYOUT_LESS
:
7600 return BRW_PSCDEPTH_ON_LE
;
7601 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
7602 return BRW_PSCDEPTH_OFF
;
7605 return BRW_PSCDEPTH_OFF
;
7609 * Move load_interpolated_input with simple (payload-based) barycentric modes
7610 * to the top of the program so we don't emit multiple PLNs for the same input.
7612 * This works around CSE not being able to handle non-dominating cases
7618 * interpolate the same exact input
7621 * This should be replaced by global value numbering someday.
7624 move_interpolation_to_top(nir_shader
*nir
)
7626 bool progress
= false;
7628 nir_foreach_function(f
, nir
) {
7632 nir_block
*top
= nir_start_block(f
->impl
);
7633 exec_node
*cursor_node
= NULL
;
7635 nir_foreach_block(block
, f
->impl
) {
7639 nir_foreach_instr_safe(instr
, block
) {
7640 if (instr
->type
!= nir_instr_type_intrinsic
)
7643 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
7644 if (intrin
->intrinsic
!= nir_intrinsic_load_interpolated_input
)
7646 nir_intrinsic_instr
*bary_intrinsic
=
7647 nir_instr_as_intrinsic(intrin
->src
[0].ssa
->parent_instr
);
7648 nir_intrinsic_op op
= bary_intrinsic
->intrinsic
;
7650 /* Leave interpolateAtSample/Offset() where they are. */
7651 if (op
== nir_intrinsic_load_barycentric_at_sample
||
7652 op
== nir_intrinsic_load_barycentric_at_offset
)
7655 nir_instr
*move
[3] = {
7656 &bary_intrinsic
->instr
,
7657 intrin
->src
[1].ssa
->parent_instr
,
7661 for (unsigned i
= 0; i
< ARRAY_SIZE(move
); i
++) {
7662 if (move
[i
]->block
!= top
) {
7663 move
[i
]->block
= top
;
7664 exec_node_remove(&move
[i
]->node
);
7666 exec_node_insert_after(cursor_node
, &move
[i
]->node
);
7668 exec_list_push_head(&top
->instr_list
, &move
[i
]->node
);
7670 cursor_node
= &move
[i
]->node
;
7676 nir_metadata_preserve(f
->impl
, (nir_metadata
)
7677 ((unsigned) nir_metadata_block_index
|
7678 (unsigned) nir_metadata_dominance
));
7685 * Demote per-sample barycentric intrinsics to centroid.
7687 * Useful when rendering to a non-multisampled buffer.
7690 demote_sample_qualifiers(nir_shader
*nir
)
7692 bool progress
= true;
7694 nir_foreach_function(f
, nir
) {
7699 nir_builder_init(&b
, f
->impl
);
7701 nir_foreach_block(block
, f
->impl
) {
7702 nir_foreach_instr_safe(instr
, block
) {
7703 if (instr
->type
!= nir_instr_type_intrinsic
)
7706 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
7707 if (intrin
->intrinsic
!= nir_intrinsic_load_barycentric_sample
&&
7708 intrin
->intrinsic
!= nir_intrinsic_load_barycentric_at_sample
)
7711 b
.cursor
= nir_before_instr(instr
);
7712 nir_ssa_def
*centroid
=
7713 nir_load_barycentric(&b
, nir_intrinsic_load_barycentric_centroid
,
7714 nir_intrinsic_interp_mode(intrin
));
7715 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
7716 nir_src_for_ssa(centroid
));
7717 nir_instr_remove(instr
);
7722 nir_metadata_preserve(f
->impl
, (nir_metadata
)
7723 ((unsigned) nir_metadata_block_index
|
7724 (unsigned) nir_metadata_dominance
));
7731 * Pre-gen6, the register file of the EUs was shared between threads,
7732 * and each thread used some subset allocated on a 16-register block
7733 * granularity. The unit states wanted these block counts.
7736 brw_register_blocks(int reg_count
)
7738 return ALIGN(reg_count
, 16) / 16 - 1;
7742 brw_compile_fs(const struct brw_compiler
*compiler
, void *log_data
,
7744 const struct brw_wm_prog_key
*key
,
7745 struct brw_wm_prog_data
*prog_data
,
7747 struct gl_program
*prog
,
7748 int shader_time_index8
, int shader_time_index16
,
7749 int shader_time_index32
, bool allow_spilling
,
7750 bool use_rep_send
, struct brw_vue_map
*vue_map
,
7753 const struct gen_device_info
*devinfo
= compiler
->devinfo
;
7755 shader
= brw_nir_apply_sampler_key(shader
, compiler
, &key
->tex
, true);
7756 brw_nir_lower_fs_inputs(shader
, devinfo
, key
);
7757 brw_nir_lower_fs_outputs(shader
);
7759 if (devinfo
->gen
< 6)
7760 brw_setup_vue_interpolation(vue_map
, shader
, prog_data
);
7762 if (!key
->multisample_fbo
)
7763 NIR_PASS_V(shader
, demote_sample_qualifiers
);
7764 NIR_PASS_V(shader
, move_interpolation_to_top
);
7765 shader
= brw_postprocess_nir(shader
, compiler
, true);
7767 /* key->alpha_test_func means simulating alpha testing via discards,
7768 * so the shader definitely kills pixels.
7770 prog_data
->uses_kill
= shader
->info
.fs
.uses_discard
||
7771 key
->alpha_test_func
;
7772 prog_data
->uses_omask
= key
->multisample_fbo
&&
7773 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
7774 prog_data
->computed_depth_mode
= computed_depth_mode(shader
);
7775 prog_data
->computed_stencil
=
7776 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
7778 prog_data
->persample_dispatch
=
7779 key
->multisample_fbo
&&
7780 (key
->persample_interp
||
7781 (shader
->info
.system_values_read
& (SYSTEM_BIT_SAMPLE_ID
|
7782 SYSTEM_BIT_SAMPLE_POS
)) ||
7783 shader
->info
.fs
.uses_sample_qualifier
||
7784 shader
->info
.outputs_read
);
7786 prog_data
->has_render_target_reads
= shader
->info
.outputs_read
!= 0ull;
7788 prog_data
->early_fragment_tests
= shader
->info
.fs
.early_fragment_tests
;
7789 prog_data
->post_depth_coverage
= shader
->info
.fs
.post_depth_coverage
;
7790 prog_data
->inner_coverage
= shader
->info
.fs
.inner_coverage
;
7792 prog_data
->barycentric_interp_modes
=
7793 brw_compute_barycentric_interp_modes(compiler
->devinfo
, shader
);
7795 cfg_t
*simd8_cfg
= NULL
, *simd16_cfg
= NULL
, *simd32_cfg
= NULL
;
7797 fs_visitor
v8(compiler
, log_data
, mem_ctx
, key
,
7798 &prog_data
->base
, prog
, shader
, 8,
7799 shader_time_index8
);
7800 if (!v8
.run_fs(allow_spilling
, false /* do_rep_send */)) {
7802 *error_str
= ralloc_strdup(mem_ctx
, v8
.fail_msg
);
7805 } else if (likely(!(INTEL_DEBUG
& DEBUG_NO8
))) {
7807 prog_data
->base
.dispatch_grf_start_reg
= v8
.payload
.num_regs
;
7808 prog_data
->reg_blocks_8
= brw_register_blocks(v8
.grf_used
);
7811 if (v8
.max_dispatch_width
>= 16 &&
7812 likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
7813 /* Try a SIMD16 compile */
7814 fs_visitor
v16(compiler
, log_data
, mem_ctx
, key
,
7815 &prog_data
->base
, prog
, shader
, 16,
7816 shader_time_index16
);
7817 v16
.import_uniforms(&v8
);
7818 if (!v16
.run_fs(allow_spilling
, use_rep_send
)) {
7819 compiler
->shader_perf_log(log_data
,
7820 "SIMD16 shader failed to compile: %s",
7823 simd16_cfg
= v16
.cfg
;
7824 prog_data
->dispatch_grf_start_reg_16
= v16
.payload
.num_regs
;
7825 prog_data
->reg_blocks_16
= brw_register_blocks(v16
.grf_used
);
7829 /* Currently, the compiler only supports SIMD32 on SNB+ */
7830 if (v8
.max_dispatch_width
>= 32 && !use_rep_send
&&
7831 compiler
->devinfo
->gen
>= 6 &&
7832 unlikely(INTEL_DEBUG
& DEBUG_DO32
)) {
7833 /* Try a SIMD32 compile */
7834 fs_visitor
v32(compiler
, log_data
, mem_ctx
, key
,
7835 &prog_data
->base
, prog
, shader
, 32,
7836 shader_time_index32
);
7837 v32
.import_uniforms(&v8
);
7838 if (!v32
.run_fs(allow_spilling
, false)) {
7839 compiler
->shader_perf_log(log_data
,
7840 "SIMD32 shader failed to compile: %s",
7843 simd32_cfg
= v32
.cfg
;
7844 prog_data
->dispatch_grf_start_reg_32
= v32
.payload
.num_regs
;
7845 prog_data
->reg_blocks_32
= brw_register_blocks(v32
.grf_used
);
7849 /* When the caller requests a repclear shader, they want SIMD16-only */
7853 /* Prior to Iron Lake, the PS had a single shader offset with a jump table
7854 * at the top to select the shader. We've never implemented that.
7855 * Instead, we just give them exactly one shader and we pick the widest one
7858 if (compiler
->devinfo
->gen
< 5) {
7859 if (simd32_cfg
|| simd16_cfg
)
7865 /* If computed depth is enabled SNB only allows SIMD8. */
7866 if (compiler
->devinfo
->gen
== 6 &&
7867 prog_data
->computed_depth_mode
!= BRW_PSCDEPTH_OFF
)
7868 assert(simd16_cfg
== NULL
&& simd32_cfg
== NULL
);
7870 if (compiler
->devinfo
->gen
<= 5 && !simd8_cfg
) {
7871 /* Iron lake and earlier only have one Dispatch GRF start field. Make
7872 * the data available in the base prog data struct for convenience.
7875 prog_data
->base
.dispatch_grf_start_reg
=
7876 prog_data
->dispatch_grf_start_reg_16
;
7877 } else if (simd32_cfg
) {
7878 prog_data
->base
.dispatch_grf_start_reg
=
7879 prog_data
->dispatch_grf_start_reg_32
;
7883 if (prog_data
->persample_dispatch
) {
7884 /* Starting with SandyBridge (where we first get MSAA), the different
7885 * pixel dispatch combinations are grouped into classifications A
7886 * through F (SNB PRM Vol. 2 Part 1 Section 7.7.1). On all hardware
7887 * generations, the only configurations supporting persample dispatch
7888 * are are this in which only one dispatch width is enabled.
7890 if (simd32_cfg
|| simd16_cfg
)
7896 /* We have to compute the flat inputs after the visitor is finished running
7897 * because it relies on prog_data->urb_setup which is computed in
7898 * fs_visitor::calculate_urb_setup().
7900 brw_compute_flat_inputs(prog_data
, shader
);
7902 fs_generator
g(compiler
, log_data
, mem_ctx
, &prog_data
->base
,
7903 v8
.promoted_constants
, v8
.runtime_check_aads_emit
,
7904 MESA_SHADER_FRAGMENT
);
7906 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
7907 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
7908 shader
->info
.label
?
7909 shader
->info
.label
: "unnamed",
7910 shader
->info
.name
));
7914 prog_data
->dispatch_8
= true;
7915 g
.generate_code(simd8_cfg
, 8);
7919 prog_data
->dispatch_16
= true;
7920 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
7924 prog_data
->dispatch_32
= true;
7925 prog_data
->prog_offset_32
= g
.generate_code(simd32_cfg
, 32);
7928 return g
.get_assembly();
7932 fs_visitor::emit_cs_work_group_id_setup()
7934 assert(stage
== MESA_SHADER_COMPUTE
);
7936 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
7938 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
7939 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
7940 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
7942 bld
.MOV(*reg
, r0_1
);
7943 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
7944 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
7950 fill_push_const_block_info(struct brw_push_const_block
*block
, unsigned dwords
)
7952 block
->dwords
= dwords
;
7953 block
->regs
= DIV_ROUND_UP(dwords
, 8);
7954 block
->size
= block
->regs
* 32;
7958 cs_fill_push_const_info(const struct gen_device_info
*devinfo
,
7959 struct brw_cs_prog_data
*cs_prog_data
)
7961 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
7962 int subgroup_id_index
= get_subgroup_id_param_index(prog_data
);
7963 bool cross_thread_supported
= devinfo
->gen
> 7 || devinfo
->is_haswell
;
7965 /* The thread ID should be stored in the last param dword */
7966 assert(subgroup_id_index
== -1 ||
7967 subgroup_id_index
== (int)prog_data
->nr_params
- 1);
7969 unsigned cross_thread_dwords
, per_thread_dwords
;
7970 if (!cross_thread_supported
) {
7971 cross_thread_dwords
= 0u;
7972 per_thread_dwords
= prog_data
->nr_params
;
7973 } else if (subgroup_id_index
>= 0) {
7974 /* Fill all but the last register with cross-thread payload */
7975 cross_thread_dwords
= 8 * (subgroup_id_index
/ 8);
7976 per_thread_dwords
= prog_data
->nr_params
- cross_thread_dwords
;
7977 assert(per_thread_dwords
> 0 && per_thread_dwords
<= 8);
7979 /* Fill all data using cross-thread payload */
7980 cross_thread_dwords
= prog_data
->nr_params
;
7981 per_thread_dwords
= 0u;
7984 fill_push_const_block_info(&cs_prog_data
->push
.cross_thread
, cross_thread_dwords
);
7985 fill_push_const_block_info(&cs_prog_data
->push
.per_thread
, per_thread_dwords
);
7987 unsigned total_dwords
=
7988 (cs_prog_data
->push
.per_thread
.size
* cs_prog_data
->threads
+
7989 cs_prog_data
->push
.cross_thread
.size
) / 4;
7990 fill_push_const_block_info(&cs_prog_data
->push
.total
, total_dwords
);
7992 assert(cs_prog_data
->push
.cross_thread
.dwords
% 8 == 0 ||
7993 cs_prog_data
->push
.per_thread
.size
== 0);
7994 assert(cs_prog_data
->push
.cross_thread
.dwords
+
7995 cs_prog_data
->push
.per_thread
.dwords
==
7996 prog_data
->nr_params
);
8000 cs_set_simd_size(struct brw_cs_prog_data
*cs_prog_data
, unsigned size
)
8002 cs_prog_data
->simd_size
= size
;
8003 unsigned group_size
= cs_prog_data
->local_size
[0] *
8004 cs_prog_data
->local_size
[1] * cs_prog_data
->local_size
[2];
8005 cs_prog_data
->threads
= (group_size
+ size
- 1) / size
;
8009 compile_cs_to_nir(const struct brw_compiler
*compiler
,
8011 const struct brw_cs_prog_key
*key
,
8012 const nir_shader
*src_shader
,
8013 unsigned dispatch_width
)
8015 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
8016 shader
= brw_nir_apply_sampler_key(shader
, compiler
, &key
->tex
, true);
8018 NIR_PASS_V(shader
, brw_nir_lower_cs_intrinsics
, dispatch_width
);
8020 /* Clean up after the local index and ID calculations. */
8021 NIR_PASS_V(shader
, nir_opt_constant_folding
);
8022 NIR_PASS_V(shader
, nir_opt_dce
);
8024 return brw_postprocess_nir(shader
, compiler
, true);
8028 brw_compile_cs(const struct brw_compiler
*compiler
, void *log_data
,
8030 const struct brw_cs_prog_key
*key
,
8031 struct brw_cs_prog_data
*prog_data
,
8032 const nir_shader
*src_shader
,
8033 int shader_time_index
,
8036 prog_data
->local_size
[0] = src_shader
->info
.cs
.local_size
[0];
8037 prog_data
->local_size
[1] = src_shader
->info
.cs
.local_size
[1];
8038 prog_data
->local_size
[2] = src_shader
->info
.cs
.local_size
[2];
8039 unsigned local_workgroup_size
=
8040 src_shader
->info
.cs
.local_size
[0] * src_shader
->info
.cs
.local_size
[1] *
8041 src_shader
->info
.cs
.local_size
[2];
8043 unsigned min_dispatch_width
=
8044 DIV_ROUND_UP(local_workgroup_size
, compiler
->devinfo
->max_cs_threads
);
8045 min_dispatch_width
= MAX2(8, min_dispatch_width
);
8046 min_dispatch_width
= util_next_power_of_two(min_dispatch_width
);
8047 assert(min_dispatch_width
<= 32);
8049 fs_visitor
*v8
= NULL
, *v16
= NULL
, *v32
= NULL
;
8051 const char *fail_msg
= NULL
;
8052 unsigned promoted_constants
= 0;
8054 /* Now the main event: Visit the shader IR and generate our CS IR for it.
8056 if (min_dispatch_width
<= 8) {
8057 nir_shader
*nir8
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
8059 v8
= new fs_visitor(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
8060 NULL
, /* Never used in core profile */
8061 nir8
, 8, shader_time_index
);
8062 if (!v8
->run_cs(min_dispatch_width
)) {
8063 fail_msg
= v8
->fail_msg
;
8065 /* We should always be able to do SIMD32 for compute shaders */
8066 assert(v8
->max_dispatch_width
>= 32);
8069 cs_set_simd_size(prog_data
, 8);
8070 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
8071 promoted_constants
= v8
->promoted_constants
;
8075 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
8076 !fail_msg
&& min_dispatch_width
<= 16) {
8077 /* Try a SIMD16 compile */
8078 nir_shader
*nir16
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
8080 v16
= new fs_visitor(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
8081 NULL
, /* Never used in core profile */
8082 nir16
, 16, shader_time_index
);
8084 v16
->import_uniforms(v8
);
8086 if (!v16
->run_cs(min_dispatch_width
)) {
8087 compiler
->shader_perf_log(log_data
,
8088 "SIMD16 shader failed to compile: %s",
8092 "Couldn't generate SIMD16 program and not "
8093 "enough threads for SIMD8";
8096 /* We should always be able to do SIMD32 for compute shaders */
8097 assert(v16
->max_dispatch_width
>= 32);
8100 cs_set_simd_size(prog_data
, 16);
8101 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
8102 promoted_constants
= v16
->promoted_constants
;
8106 /* We should always be able to do SIMD32 for compute shaders */
8107 assert(!v16
|| v16
->max_dispatch_width
>= 32);
8109 if (!fail_msg
&& (min_dispatch_width
> 16 || (INTEL_DEBUG
& DEBUG_DO32
))) {
8110 /* Try a SIMD32 compile */
8111 nir_shader
*nir32
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
8113 v32
= new fs_visitor(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
8114 NULL
, /* Never used in core profile */
8115 nir32
, 32, shader_time_index
);
8117 v32
->import_uniforms(v8
);
8119 v32
->import_uniforms(v16
);
8121 if (!v32
->run_cs(min_dispatch_width
)) {
8122 compiler
->shader_perf_log(log_data
,
8123 "SIMD32 shader failed to compile: %s",
8127 "Couldn't generate SIMD32 program and not "
8128 "enough threads for SIMD16";
8132 cs_set_simd_size(prog_data
, 32);
8133 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
8134 promoted_constants
= v32
->promoted_constants
;
8138 const unsigned *ret
= NULL
;
8139 if (unlikely(cfg
== NULL
)) {
8142 *error_str
= ralloc_strdup(mem_ctx
, fail_msg
);
8144 fs_generator
g(compiler
, log_data
, mem_ctx
, &prog_data
->base
,
8145 promoted_constants
, false, MESA_SHADER_COMPUTE
);
8146 if (INTEL_DEBUG
& DEBUG_CS
) {
8147 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
8148 src_shader
->info
.label
?
8149 src_shader
->info
.label
: "unnamed",
8150 src_shader
->info
.name
);
8151 g
.enable_debug(name
);
8154 g
.generate_code(cfg
, prog_data
->simd_size
);
8156 ret
= g
.get_assembly();
8167 * Test the dispatch mask packing assumptions of
8168 * brw_stage_has_packed_dispatch(). Call this from e.g. the top of
8169 * fs_visitor::emit_nir_code() to cause a GPU hang if any shader invocation is
8170 * executed with an unexpected dispatch mask.
8173 brw_fs_test_dispatch_packing(const fs_builder
&bld
)
8175 const gl_shader_stage stage
= bld
.shader
->stage
;
8177 if (brw_stage_has_packed_dispatch(bld
.shader
->devinfo
, stage
,
8178 bld
.shader
->stage_prog_data
)) {
8179 const fs_builder ubld
= bld
.exec_all().group(1, 0);
8180 const fs_reg tmp
= component(bld
.vgrf(BRW_REGISTER_TYPE_UD
), 0);
8181 const fs_reg mask
= (stage
== MESA_SHADER_FRAGMENT
? brw_vmask_reg() :
8184 ubld
.ADD(tmp
, mask
, brw_imm_ud(1));
8185 ubld
.AND(tmp
, mask
, tmp
);
8187 /* This will loop forever if the dispatch mask doesn't have the expected
8188 * form '2^n-1', in which case tmp will be non-zero.
8190 bld
.emit(BRW_OPCODE_DO
);
8191 bld
.CMP(bld
.null_reg_ud(), tmp
, brw_imm_ud(0), BRW_CONDITIONAL_NZ
);
8192 set_predicate(BRW_PREDICATE_NORMAL
, bld
.emit(BRW_OPCODE_WHILE
));