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 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
840 assert(src
[2].file
== IMM
);
843 const unsigned op
= src
[2].ud
;
858 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
859 assert(src
[2].file
== IMM
);
862 const unsigned op
= src
[2].ud
;
863 return op
== BRW_AOP_FCMPWR
? 2 : 1;
868 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
869 /* Scattered logical opcodes use the following params:
870 * src[0] Surface coordinates
871 * src[1] Surface operation source (ignored for reads)
873 * src[3] IMM with always 1 dimension.
874 * src[4] IMM with arg bitsize for scattered read/write 8, 16, 32
876 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
877 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
878 return i
== SURFACE_LOGICAL_SRC_DATA
? 0 : 1;
880 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
881 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
882 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
885 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
886 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
887 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
888 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
889 const unsigned op
= src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
890 /* Surface coordinates. */
891 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
892 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
893 /* Surface operation source. */
894 else if (i
== SURFACE_LOGICAL_SRC_DATA
&& op
== BRW_AOP_CMPWR
)
896 else if (i
== SURFACE_LOGICAL_SRC_DATA
&&
897 (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
|| op
== BRW_AOP_PREDEC
))
902 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
903 return (i
== 0 ? 2 : 1);
905 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
: {
906 assert(src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].file
== IMM
&&
907 src
[SURFACE_LOGICAL_SRC_IMM_ARG
].file
== IMM
);
908 const unsigned op
= src
[SURFACE_LOGICAL_SRC_IMM_ARG
].ud
;
909 /* Surface coordinates. */
910 if (i
== SURFACE_LOGICAL_SRC_ADDRESS
)
911 return src
[SURFACE_LOGICAL_SRC_IMM_DIMS
].ud
;
912 /* Surface operation source. */
913 else if (i
== SURFACE_LOGICAL_SRC_DATA
&& op
== BRW_AOP_FCMPWR
)
925 fs_inst::size_read(int arg
) const
928 case SHADER_OPCODE_SEND
:
930 return mlen
* REG_SIZE
;
931 } else if (arg
== 3) {
932 return ex_mlen
* REG_SIZE
;
936 case FS_OPCODE_FB_WRITE
:
937 case FS_OPCODE_REP_FB_WRITE
:
940 return src
[0].file
== BAD_FILE
? 0 : 2 * REG_SIZE
;
942 return mlen
* REG_SIZE
;
946 case FS_OPCODE_FB_READ
:
947 case SHADER_OPCODE_URB_WRITE_SIMD8
:
948 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
949 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
950 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
951 case SHADER_OPCODE_URB_READ_SIMD8
:
952 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
953 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
954 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
956 return mlen
* REG_SIZE
;
959 case FS_OPCODE_SET_SAMPLE_ID
:
964 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
965 /* The payload is actually stored in src1 */
967 return mlen
* REG_SIZE
;
970 case FS_OPCODE_LINTERP
:
975 case SHADER_OPCODE_LOAD_PAYLOAD
:
976 if (arg
< this->header_size
)
980 case CS_OPCODE_CS_TERMINATE
:
981 case SHADER_OPCODE_BARRIER
:
984 case SHADER_OPCODE_MOV_INDIRECT
:
986 assert(src
[2].file
== IMM
);
992 if (is_tex() && arg
== 0 && src
[0].file
== VGRF
)
993 return mlen
* REG_SIZE
;
997 switch (src
[arg
].file
) {
1000 return components_read(arg
) * type_sz(src
[arg
].type
);
1006 return components_read(arg
) * src
[arg
].component_size(exec_size
);
1008 unreachable("MRF registers are not allowed as sources");
1014 /* Return the subset of flag registers that an instruction could
1015 * potentially read or write based on the execution controls and flag
1016 * subregister number of the instruction.
1019 flag_mask(const fs_inst
*inst
)
1021 const unsigned start
= inst
->flag_subreg
* 16 + inst
->group
;
1022 const unsigned end
= start
+ inst
->exec_size
;
1023 return ((1 << DIV_ROUND_UP(end
, 8)) - 1) & ~((1 << (start
/ 8)) - 1);
1027 bit_mask(unsigned n
)
1029 return (n
>= CHAR_BIT
* sizeof(bit_mask(n
)) ? ~0u : (1u << n
) - 1);
1033 flag_mask(const fs_reg
&r
, unsigned sz
)
1035 if (r
.file
== ARF
) {
1036 const unsigned start
= (r
.nr
- BRW_ARF_FLAG
) * 4 + r
.subnr
;
1037 const unsigned end
= start
+ sz
;
1038 return bit_mask(end
) & ~bit_mask(start
);
1046 fs_inst::flags_read(const gen_device_info
*devinfo
) const
1048 if (predicate
== BRW_PREDICATE_ALIGN1_ANYV
||
1049 predicate
== BRW_PREDICATE_ALIGN1_ALLV
) {
1050 /* The vertical predication modes combine corresponding bits from
1051 * f0.0 and f1.0 on Gen7+, and f0.0 and f0.1 on older hardware.
1053 const unsigned shift
= devinfo
->gen
>= 7 ? 4 : 2;
1054 return flag_mask(this) << shift
| flag_mask(this);
1055 } else if (predicate
) {
1056 return flag_mask(this);
1059 for (int i
= 0; i
< sources
; i
++) {
1060 mask
|= flag_mask(src
[i
], size_read(i
));
1067 fs_inst::flags_written() const
1069 if ((conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
1070 opcode
!= BRW_OPCODE_CSEL
&&
1071 opcode
!= BRW_OPCODE_IF
&&
1072 opcode
!= BRW_OPCODE_WHILE
)) ||
1073 opcode
== SHADER_OPCODE_FIND_LIVE_CHANNEL
||
1074 opcode
== FS_OPCODE_FB_WRITE
) {
1075 return flag_mask(this);
1077 return flag_mask(dst
, size_written
);
1082 * Returns how many MRFs an FS opcode will write over.
1084 * Note that this is not the 0 or 1 implied writes in an actual gen
1085 * instruction -- the FS opcodes often generate MOVs in addition.
1088 fs_visitor::implied_mrf_writes(fs_inst
*inst
) const
1090 if (inst
->mlen
== 0)
1093 if (inst
->base_mrf
== -1)
1096 switch (inst
->opcode
) {
1097 case SHADER_OPCODE_RCP
:
1098 case SHADER_OPCODE_RSQ
:
1099 case SHADER_OPCODE_SQRT
:
1100 case SHADER_OPCODE_EXP2
:
1101 case SHADER_OPCODE_LOG2
:
1102 case SHADER_OPCODE_SIN
:
1103 case SHADER_OPCODE_COS
:
1104 return 1 * dispatch_width
/ 8;
1105 case SHADER_OPCODE_POW
:
1106 case SHADER_OPCODE_INT_QUOTIENT
:
1107 case SHADER_OPCODE_INT_REMAINDER
:
1108 return 2 * dispatch_width
/ 8;
1109 case SHADER_OPCODE_TEX
:
1111 case SHADER_OPCODE_TXD
:
1112 case SHADER_OPCODE_TXF
:
1113 case SHADER_OPCODE_TXF_CMS
:
1114 case SHADER_OPCODE_TXF_MCS
:
1115 case SHADER_OPCODE_TG4
:
1116 case SHADER_OPCODE_TG4_OFFSET
:
1117 case SHADER_OPCODE_TXL
:
1118 case SHADER_OPCODE_TXS
:
1119 case SHADER_OPCODE_LOD
:
1120 case SHADER_OPCODE_SAMPLEINFO
:
1122 case FS_OPCODE_FB_WRITE
:
1123 case FS_OPCODE_REP_FB_WRITE
:
1124 return inst
->src
[0].file
== BAD_FILE
? 0 : 2;
1125 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
1126 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
1128 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
:
1130 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
1133 unreachable("not reached");
1138 fs_visitor::vgrf(const glsl_type
*const type
)
1140 int reg_width
= dispatch_width
/ 8;
1142 alloc
.allocate(type_size_scalar(type
, false) * reg_width
),
1143 brw_type_for_base_type(type
));
1146 fs_reg::fs_reg(enum brw_reg_file file
, int nr
)
1151 this->type
= BRW_REGISTER_TYPE_F
;
1152 this->stride
= (file
== UNIFORM
? 0 : 1);
1155 fs_reg::fs_reg(enum brw_reg_file file
, int nr
, enum brw_reg_type type
)
1161 this->stride
= (file
== UNIFORM
? 0 : 1);
1164 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
1165 * This brings in those uniform definitions
1168 fs_visitor::import_uniforms(fs_visitor
*v
)
1170 this->push_constant_loc
= v
->push_constant_loc
;
1171 this->pull_constant_loc
= v
->pull_constant_loc
;
1172 this->uniforms
= v
->uniforms
;
1173 this->subgroup_id
= v
->subgroup_id
;
1177 fs_visitor::emit_fragcoord_interpolation(fs_reg wpos
)
1179 assert(stage
== MESA_SHADER_FRAGMENT
);
1181 /* gl_FragCoord.x */
1182 bld
.MOV(wpos
, this->pixel_x
);
1183 wpos
= offset(wpos
, bld
, 1);
1185 /* gl_FragCoord.y */
1186 bld
.MOV(wpos
, this->pixel_y
);
1187 wpos
= offset(wpos
, bld
, 1);
1189 /* gl_FragCoord.z */
1190 if (devinfo
->gen
>= 6) {
1191 bld
.MOV(wpos
, fetch_payload_reg(bld
, payload
.source_depth_reg
));
1193 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
1194 this->delta_xy
[BRW_BARYCENTRIC_PERSPECTIVE_PIXEL
],
1195 interp_reg(VARYING_SLOT_POS
, 2));
1197 wpos
= offset(wpos
, bld
, 1);
1199 /* gl_FragCoord.w: Already set up in emit_interpolation */
1200 bld
.MOV(wpos
, this->wpos_w
);
1203 enum brw_barycentric_mode
1204 brw_barycentric_mode(enum glsl_interp_mode mode
, nir_intrinsic_op op
)
1206 /* Barycentric modes don't make sense for flat inputs. */
1207 assert(mode
!= INTERP_MODE_FLAT
);
1211 case nir_intrinsic_load_barycentric_pixel
:
1212 case nir_intrinsic_load_barycentric_at_offset
:
1213 bary
= BRW_BARYCENTRIC_PERSPECTIVE_PIXEL
;
1215 case nir_intrinsic_load_barycentric_centroid
:
1216 bary
= BRW_BARYCENTRIC_PERSPECTIVE_CENTROID
;
1218 case nir_intrinsic_load_barycentric_sample
:
1219 case nir_intrinsic_load_barycentric_at_sample
:
1220 bary
= BRW_BARYCENTRIC_PERSPECTIVE_SAMPLE
;
1223 unreachable("invalid intrinsic");
1226 if (mode
== INTERP_MODE_NOPERSPECTIVE
)
1229 return (enum brw_barycentric_mode
) bary
;
1233 * Turn one of the two CENTROID barycentric modes into PIXEL mode.
1235 static enum brw_barycentric_mode
1236 centroid_to_pixel(enum brw_barycentric_mode bary
)
1238 assert(bary
== BRW_BARYCENTRIC_PERSPECTIVE_CENTROID
||
1239 bary
== BRW_BARYCENTRIC_NONPERSPECTIVE_CENTROID
);
1240 return (enum brw_barycentric_mode
) ((unsigned) bary
- 1);
1244 fs_visitor::emit_frontfacing_interpolation()
1246 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1248 if (devinfo
->gen
>= 6) {
1249 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1250 * a boolean result from this (~0/true or 0/false).
1252 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1253 * this task in only one instruction:
1254 * - a negation source modifier will flip the bit; and
1255 * - a W -> D type conversion will sign extend the bit into the high
1256 * word of the destination.
1258 * An ASR 15 fills the low word of the destination.
1260 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1263 bld
.ASR(*reg
, g0
, brw_imm_d(15));
1265 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1266 * a boolean result from this (1/true or 0/false).
1268 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1269 * the negation source modifier to flip it. Unfortunately the SHR
1270 * instruction only operates on UD (or D with an abs source modifier)
1271 * sources without negation.
1273 * Instead, use ASR (which will give ~0/true or 0/false).
1275 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1278 bld
.ASR(*reg
, g1_6
, brw_imm_d(31));
1285 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1287 assert(stage
== MESA_SHADER_FRAGMENT
);
1288 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
1289 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1291 if (wm_prog_data
->persample_dispatch
) {
1292 /* Convert int_sample_pos to floating point */
1293 bld
.MOV(dst
, int_sample_pos
);
1294 /* Scale to the range [0, 1] */
1295 bld
.MUL(dst
, dst
, brw_imm_f(1 / 16.0f
));
1298 /* From ARB_sample_shading specification:
1299 * "When rendering to a non-multisample buffer, or if multisample
1300 * rasterization is disabled, gl_SamplePosition will always be
1303 bld
.MOV(dst
, brw_imm_f(0.5f
));
1308 fs_visitor::emit_samplepos_setup()
1310 assert(devinfo
->gen
>= 6);
1312 const fs_builder abld
= bld
.annotate("compute sample position");
1313 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1315 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1316 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1318 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1319 * mode will be enabled.
1321 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1322 * R31.1:0 Position Offset X/Y for Slot[3:0]
1323 * R31.3:2 Position Offset X/Y for Slot[7:4]
1326 * The X, Y sample positions come in as bytes in thread payload. So, read
1327 * the positions using vstride=16, width=8, hstride=2.
1329 const fs_reg sample_pos_reg
=
1330 fetch_payload_reg(abld
, payload
.sample_pos_reg
, BRW_REGISTER_TYPE_W
);
1332 /* Compute gl_SamplePosition.x */
1333 abld
.MOV(int_sample_x
, subscript(sample_pos_reg
, BRW_REGISTER_TYPE_B
, 0));
1334 compute_sample_position(offset(pos
, abld
, 0), int_sample_x
);
1336 /* Compute gl_SamplePosition.y */
1337 abld
.MOV(int_sample_y
, subscript(sample_pos_reg
, BRW_REGISTER_TYPE_B
, 1));
1338 compute_sample_position(offset(pos
, abld
, 1), int_sample_y
);
1343 fs_visitor::emit_sampleid_setup()
1345 assert(stage
== MESA_SHADER_FRAGMENT
);
1346 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1347 assert(devinfo
->gen
>= 6);
1349 const fs_builder abld
= bld
.annotate("compute sample id");
1350 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uint_type
));
1352 if (!key
->multisample_fbo
) {
1353 /* As per GL_ARB_sample_shading specification:
1354 * "When rendering to a non-multisample buffer, or if multisample
1355 * rasterization is disabled, gl_SampleID will always be zero."
1357 abld
.MOV(*reg
, brw_imm_d(0));
1358 } else if (devinfo
->gen
>= 8) {
1359 /* Sample ID comes in as 4-bit numbers in g1.0:
1361 * 15:12 Slot 3 SampleID (only used in SIMD16)
1362 * 11:8 Slot 2 SampleID (only used in SIMD16)
1363 * 7:4 Slot 1 SampleID
1364 * 3:0 Slot 0 SampleID
1366 * Each slot corresponds to four channels, so we want to replicate each
1367 * half-byte value to 4 channels in a row:
1369 * dst+0: .7 .6 .5 .4 .3 .2 .1 .0
1370 * 7:4 7:4 7:4 7:4 3:0 3:0 3:0 3:0
1372 * dst+1: .7 .6 .5 .4 .3 .2 .1 .0 (if SIMD16)
1373 * 15:12 15:12 15:12 15:12 11:8 11:8 11:8 11:8
1375 * First, we read g1.0 with a <1,8,0>UB region, causing the first 8
1376 * channels to read the first byte (7:0), and the second group of 8
1377 * channels to read the second byte (15:8). Then, we shift right by
1378 * a vector immediate of <4, 4, 4, 4, 0, 0, 0, 0>, moving the slot 1 / 3
1379 * values into place. Finally, we AND with 0xf to keep the low nibble.
1381 * shr(16) tmp<1>W g1.0<1,8,0>B 0x44440000:V
1382 * and(16) dst<1>D tmp<8,8,1>W 0xf:W
1384 * TODO: These payload bits exist on Gen7 too, but they appear to always
1385 * be zero, so this code fails to work. We should find out why.
1387 const fs_reg tmp
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
1389 for (unsigned i
= 0; i
< DIV_ROUND_UP(dispatch_width
, 16); i
++) {
1390 const fs_builder hbld
= abld
.group(MIN2(16, dispatch_width
), i
);
1391 hbld
.SHR(offset(tmp
, hbld
, i
),
1392 stride(retype(brw_vec1_grf(1 + i
, 0), BRW_REGISTER_TYPE_UB
),
1394 brw_imm_v(0x44440000));
1397 abld
.AND(*reg
, tmp
, brw_imm_w(0xf));
1399 const fs_reg t1
= component(abld
.vgrf(BRW_REGISTER_TYPE_UD
), 0);
1400 const fs_reg t2
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
1402 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1403 * 8x multisampling, subspan 0 will represent sample N (where N
1404 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1405 * 7. We can find the value of N by looking at R0.0 bits 7:6
1406 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1407 * (since samples are always delivered in pairs). That is, we
1408 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1409 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1410 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1411 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1412 * populating a temporary variable with the sequence (0, 1, 2, 3),
1413 * and then reading from it using vstride=1, width=4, hstride=0.
1414 * These computations hold good for 4x multisampling as well.
1416 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1417 * the first four slots are sample 0 of subspan 0; the next four
1418 * are sample 1 of subspan 0; the third group is sample 0 of
1419 * subspan 1, and finally sample 1 of subspan 1.
1422 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1423 * accomodate 16x MSAA.
1425 abld
.exec_all().group(1, 0)
1426 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
1428 abld
.exec_all().group(1, 0).SHR(t1
, t1
, brw_imm_d(5));
1430 /* This works for SIMD8-SIMD16. It also works for SIMD32 but only if we
1431 * can assume 4x MSAA. Disallow it on IVB+
1433 * FINISHME: One day, we could come up with a way to do this that
1434 * actually works on gen7.
1436 if (devinfo
->gen
>= 7)
1437 limit_dispatch_width(16, "gl_SampleId is unsupported in SIMD32 on gen7");
1438 abld
.exec_all().group(8, 0).MOV(t2
, brw_imm_v(0x32103210));
1440 /* This special instruction takes care of setting vstride=1,
1441 * width=4, hstride=0 of t2 during an ADD instruction.
1443 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1450 fs_visitor::emit_samplemaskin_setup()
1452 assert(stage
== MESA_SHADER_FRAGMENT
);
1453 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
1454 assert(devinfo
->gen
>= 6);
1456 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1458 fs_reg coverage_mask
=
1459 fetch_payload_reg(bld
, payload
.sample_mask_in_reg
, BRW_REGISTER_TYPE_D
);
1461 if (wm_prog_data
->persample_dispatch
) {
1462 /* gl_SampleMaskIn[] comes from two sources: the input coverage mask,
1463 * and a mask representing which sample is being processed by the
1464 * current shader invocation.
1466 * From the OES_sample_variables specification:
1467 * "When per-sample shading is active due to the use of a fragment input
1468 * qualified by "sample" or due to the use of the gl_SampleID or
1469 * gl_SamplePosition variables, only the bit for the current sample is
1470 * set in gl_SampleMaskIn."
1472 const fs_builder abld
= bld
.annotate("compute gl_SampleMaskIn");
1474 if (nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
].file
== BAD_FILE
)
1475 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
] = *emit_sampleid_setup();
1477 fs_reg one
= vgrf(glsl_type::int_type
);
1478 fs_reg enabled_mask
= vgrf(glsl_type::int_type
);
1479 abld
.MOV(one
, brw_imm_d(1));
1480 abld
.SHL(enabled_mask
, one
, nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
]);
1481 abld
.AND(*reg
, enabled_mask
, coverage_mask
);
1483 /* In per-pixel mode, the coverage mask is sufficient. */
1484 *reg
= coverage_mask
;
1490 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1492 if (!src
.abs
&& !src
.negate
)
1495 fs_reg temp
= bld
.vgrf(src
.type
);
1502 fs_visitor::emit_discard_jump()
1504 assert(brw_wm_prog_data(this->prog_data
)->uses_kill
);
1506 /* For performance, after a discard, jump to the end of the
1507 * shader if all relevant channels have been discarded.
1509 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1510 discard_jump
->flag_subreg
= 1;
1512 discard_jump
->predicate
= BRW_PREDICATE_ALIGN1_ANY4H
;
1513 discard_jump
->predicate_inverse
= true;
1517 fs_visitor::emit_gs_thread_end()
1519 assert(stage
== MESA_SHADER_GEOMETRY
);
1521 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1523 if (gs_compile
->control_data_header_size_bits
> 0) {
1524 emit_gs_control_data_bits(this->final_gs_vertex_count
);
1527 const fs_builder abld
= bld
.annotate("thread end");
1530 if (gs_prog_data
->static_vertex_count
!= -1) {
1531 foreach_in_list_reverse(fs_inst
, prev
, &this->instructions
) {
1532 if (prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
||
1533 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
1534 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
1535 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
) {
1538 /* Delete now dead instructions. */
1539 foreach_in_list_reverse_safe(exec_node
, dead
, &this->instructions
) {
1545 } else if (prev
->is_control_flow() || prev
->has_side_effects()) {
1549 fs_reg hdr
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1550 abld
.MOV(hdr
, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
)));
1551 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, hdr
);
1554 fs_reg payload
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1555 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
1556 sources
[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1557 sources
[1] = this->final_gs_vertex_count
;
1558 abld
.LOAD_PAYLOAD(payload
, sources
, 2, 2);
1559 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1567 fs_visitor::assign_curb_setup()
1569 unsigned uniform_push_length
= DIV_ROUND_UP(stage_prog_data
->nr_params
, 8);
1571 unsigned ubo_push_length
= 0;
1572 unsigned ubo_push_start
[4];
1573 for (int i
= 0; i
< 4; i
++) {
1574 ubo_push_start
[i
] = 8 * (ubo_push_length
+ uniform_push_length
);
1575 ubo_push_length
+= stage_prog_data
->ubo_ranges
[i
].length
;
1578 prog_data
->curb_read_length
= uniform_push_length
+ ubo_push_length
;
1580 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1581 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1582 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1583 if (inst
->src
[i
].file
== UNIFORM
) {
1584 int uniform_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].offset
/ 4;
1586 if (inst
->src
[i
].nr
>= UBO_START
) {
1587 /* constant_nr is in 32-bit units, the rest are in bytes */
1588 constant_nr
= ubo_push_start
[inst
->src
[i
].nr
- UBO_START
] +
1589 inst
->src
[i
].offset
/ 4;
1590 } else if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1591 constant_nr
= push_constant_loc
[uniform_nr
];
1593 /* Section 5.11 of the OpenGL 4.1 spec says:
1594 * "Out-of-bounds reads return undefined values, which include
1595 * values from other variables of the active program or zero."
1596 * Just return the first push constant.
1601 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1604 brw_reg
.abs
= inst
->src
[i
].abs
;
1605 brw_reg
.negate
= inst
->src
[i
].negate
;
1607 assert(inst
->src
[i
].stride
== 0);
1608 inst
->src
[i
] = byte_offset(
1609 retype(brw_reg
, inst
->src
[i
].type
),
1610 inst
->src
[i
].offset
% 4);
1615 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1616 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1620 fs_visitor::calculate_urb_setup()
1622 assert(stage
== MESA_SHADER_FRAGMENT
);
1623 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
1624 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1626 memset(prog_data
->urb_setup
, -1,
1627 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1630 /* Figure out where each of the incoming setup attributes lands. */
1631 if (devinfo
->gen
>= 6) {
1632 if (util_bitcount64(nir
->info
.inputs_read
&
1633 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1634 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1635 * first 16 varying inputs, so we can put them wherever we want.
1636 * Just put them in order.
1638 * This is useful because it means that (a) inputs not used by the
1639 * fragment shader won't take up valuable register space, and (b) we
1640 * won't have to recompile the fragment shader if it gets paired with
1641 * a different vertex (or geometry) shader.
1643 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1644 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1645 BITFIELD64_BIT(i
)) {
1646 prog_data
->urb_setup
[i
] = urb_next
++;
1650 /* We have enough input varyings that the SF/SBE pipeline stage can't
1651 * arbitrarily rearrange them to suit our whim; we have to put them
1652 * in an order that matches the output of the previous pipeline stage
1653 * (geometry or vertex shader).
1655 struct brw_vue_map prev_stage_vue_map
;
1656 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1657 key
->input_slots_valid
,
1658 nir
->info
.separate_shader
);
1661 brw_compute_first_urb_slot_required(nir
->info
.inputs_read
,
1662 &prev_stage_vue_map
);
1664 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1665 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1667 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1668 if (varying
!= BRW_VARYING_SLOT_PAD
&&
1669 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1670 BITFIELD64_BIT(varying
))) {
1671 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1674 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1677 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1678 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1679 /* Point size is packed into the header, not as a general attribute */
1680 if (i
== VARYING_SLOT_PSIZ
)
1683 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1684 /* The back color slot is skipped when the front color is
1685 * also written to. In addition, some slots can be
1686 * written in the vertex shader and not read in the
1687 * fragment shader. So the register number must always be
1688 * incremented, mapped or not.
1690 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1691 prog_data
->urb_setup
[i
] = urb_next
;
1697 * It's a FS only attribute, and we did interpolation for this attribute
1698 * in SF thread. So, count it here, too.
1700 * See compile_sf_prog() for more info.
1702 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1703 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1706 prog_data
->num_varying_inputs
= urb_next
;
1710 fs_visitor::assign_urb_setup()
1712 assert(stage
== MESA_SHADER_FRAGMENT
);
1713 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
1715 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1717 /* Offset all the urb_setup[] index by the actual position of the
1718 * setup regs, now that the location of the constants has been chosen.
1720 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1721 for (int i
= 0; i
< inst
->sources
; i
++) {
1722 if (inst
->src
[i
].file
== ATTR
) {
1723 /* ATTR regs in the FS are in units of logical scalar inputs each
1724 * of which consumes half of a GRF register.
1726 assert(inst
->src
[i
].offset
< REG_SIZE
/ 2);
1727 const unsigned grf
= urb_start
+ inst
->src
[i
].nr
/ 2;
1728 const unsigned offset
= (inst
->src
[i
].nr
% 2) * (REG_SIZE
/ 2) +
1729 inst
->src
[i
].offset
;
1730 const unsigned width
= inst
->src
[i
].stride
== 0 ?
1731 1 : MIN2(inst
->exec_size
, 8);
1732 struct brw_reg reg
= stride(
1733 byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1735 width
* inst
->src
[i
].stride
,
1736 width
, inst
->src
[i
].stride
);
1737 reg
.abs
= inst
->src
[i
].abs
;
1738 reg
.negate
= inst
->src
[i
].negate
;
1744 /* Each attribute is 4 setup channels, each of which is half a reg. */
1745 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1749 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst
*inst
)
1751 for (int i
= 0; i
< inst
->sources
; i
++) {
1752 if (inst
->src
[i
].file
== ATTR
) {
1753 int grf
= payload
.num_regs
+
1754 prog_data
->curb_read_length
+
1756 inst
->src
[i
].offset
/ REG_SIZE
;
1758 /* As explained at brw_reg_from_fs_reg, From the Haswell PRM:
1760 * VertStride must be used to cross GRF register boundaries. This
1761 * rule implies that elements within a 'Width' cannot cross GRF
1764 * So, for registers that are large enough, we have to split the exec
1765 * size in two and trust the compression state to sort it out.
1767 unsigned total_size
= inst
->exec_size
*
1768 inst
->src
[i
].stride
*
1769 type_sz(inst
->src
[i
].type
);
1771 assert(total_size
<= 2 * REG_SIZE
);
1772 const unsigned exec_size
=
1773 (total_size
<= REG_SIZE
) ? inst
->exec_size
: inst
->exec_size
/ 2;
1775 unsigned width
= inst
->src
[i
].stride
== 0 ? 1 : exec_size
;
1776 struct brw_reg reg
=
1777 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1778 inst
->src
[i
].offset
% REG_SIZE
),
1779 exec_size
* inst
->src
[i
].stride
,
1780 width
, inst
->src
[i
].stride
);
1781 reg
.abs
= inst
->src
[i
].abs
;
1782 reg
.negate
= inst
->src
[i
].negate
;
1790 fs_visitor::assign_vs_urb_setup()
1792 struct brw_vs_prog_data
*vs_prog_data
= brw_vs_prog_data(prog_data
);
1794 assert(stage
== MESA_SHADER_VERTEX
);
1796 /* Each attribute is 4 regs. */
1797 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attribute_slots
;
1799 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1801 /* Rewrite all ATTR file references to the hw grf that they land in. */
1802 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1803 convert_attr_sources_to_hw_regs(inst
);
1808 fs_visitor::assign_tcs_urb_setup()
1810 assert(stage
== MESA_SHADER_TESS_CTRL
);
1812 /* Rewrite all ATTR file references to HW_REGs. */
1813 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1814 convert_attr_sources_to_hw_regs(inst
);
1819 fs_visitor::assign_tes_urb_setup()
1821 assert(stage
== MESA_SHADER_TESS_EVAL
);
1823 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
1825 first_non_payload_grf
+= 8 * vue_prog_data
->urb_read_length
;
1827 /* Rewrite all ATTR file references to HW_REGs. */
1828 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1829 convert_attr_sources_to_hw_regs(inst
);
1834 fs_visitor::assign_gs_urb_setup()
1836 assert(stage
== MESA_SHADER_GEOMETRY
);
1838 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
1840 first_non_payload_grf
+=
1841 8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
;
1843 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1844 /* Rewrite all ATTR file references to GRFs. */
1845 convert_attr_sources_to_hw_regs(inst
);
1851 * Split large virtual GRFs into separate components if we can.
1853 * This is mostly duplicated with what brw_fs_vector_splitting does,
1854 * but that's really conservative because it's afraid of doing
1855 * splitting that doesn't result in real progress after the rest of
1856 * the optimization phases, which would cause infinite looping in
1857 * optimization. We can do it once here, safely. This also has the
1858 * opportunity to split interpolated values, or maybe even uniforms,
1859 * which we don't have at the IR level.
1861 * We want to split, because virtual GRFs are what we register
1862 * allocate and spill (due to contiguousness requirements for some
1863 * instructions), and they're what we naturally generate in the
1864 * codegen process, but most virtual GRFs don't actually need to be
1865 * contiguous sets of GRFs. If we split, we'll end up with reduced
1866 * live intervals and better dead code elimination and coalescing.
1869 fs_visitor::split_virtual_grfs()
1871 /* Compact the register file so we eliminate dead vgrfs. This
1872 * only defines split points for live registers, so if we have
1873 * too large dead registers they will hit assertions later.
1875 compact_virtual_grfs();
1877 int num_vars
= this->alloc
.count
;
1879 /* Count the total number of registers */
1881 int vgrf_to_reg
[num_vars
];
1882 for (int i
= 0; i
< num_vars
; i
++) {
1883 vgrf_to_reg
[i
] = reg_count
;
1884 reg_count
+= alloc
.sizes
[i
];
1887 /* An array of "split points". For each register slot, this indicates
1888 * if this slot can be separated from the previous slot. Every time an
1889 * instruction uses multiple elements of a register (as a source or
1890 * destination), we mark the used slots as inseparable. Then we go
1891 * through and split the registers into the smallest pieces we can.
1893 bool split_points
[reg_count
];
1894 memset(split_points
, 0, sizeof(split_points
));
1896 /* Mark all used registers as fully splittable */
1897 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1898 if (inst
->dst
.file
== VGRF
) {
1899 int reg
= vgrf_to_reg
[inst
->dst
.nr
];
1900 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.nr
]; j
++)
1901 split_points
[reg
+ j
] = true;
1904 for (int i
= 0; i
< inst
->sources
; i
++) {
1905 if (inst
->src
[i
].file
== VGRF
) {
1906 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
];
1907 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].nr
]; j
++)
1908 split_points
[reg
+ j
] = true;
1913 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1914 if (inst
->dst
.file
== VGRF
) {
1915 int reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.offset
/ REG_SIZE
;
1916 for (unsigned j
= 1; j
< regs_written(inst
); j
++)
1917 split_points
[reg
+ j
] = false;
1919 for (int i
= 0; i
< inst
->sources
; i
++) {
1920 if (inst
->src
[i
].file
== VGRF
) {
1921 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].offset
/ REG_SIZE
;
1922 for (unsigned j
= 1; j
< regs_read(inst
, i
); j
++)
1923 split_points
[reg
+ j
] = false;
1928 int new_virtual_grf
[reg_count
];
1929 int new_reg_offset
[reg_count
];
1932 for (int i
= 0; i
< num_vars
; i
++) {
1933 /* The first one should always be 0 as a quick sanity check. */
1934 assert(split_points
[reg
] == false);
1937 new_reg_offset
[reg
] = 0;
1942 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1943 /* If this is a split point, reset the offset to 0 and allocate a
1944 * new virtual GRF for the previous offset many registers
1946 if (split_points
[reg
]) {
1947 assert(offset
<= MAX_VGRF_SIZE
);
1948 int grf
= alloc
.allocate(offset
);
1949 for (int k
= reg
- offset
; k
< reg
; k
++)
1950 new_virtual_grf
[k
] = grf
;
1953 new_reg_offset
[reg
] = offset
;
1958 /* The last one gets the original register number */
1959 assert(offset
<= MAX_VGRF_SIZE
);
1960 alloc
.sizes
[i
] = offset
;
1961 for (int k
= reg
- offset
; k
< reg
; k
++)
1962 new_virtual_grf
[k
] = i
;
1964 assert(reg
== reg_count
);
1966 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1967 if (inst
->dst
.file
== VGRF
) {
1968 reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.offset
/ REG_SIZE
;
1969 inst
->dst
.nr
= new_virtual_grf
[reg
];
1970 inst
->dst
.offset
= new_reg_offset
[reg
] * REG_SIZE
+
1971 inst
->dst
.offset
% REG_SIZE
;
1972 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1974 for (int i
= 0; i
< inst
->sources
; i
++) {
1975 if (inst
->src
[i
].file
== VGRF
) {
1976 reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].offset
/ REG_SIZE
;
1977 inst
->src
[i
].nr
= new_virtual_grf
[reg
];
1978 inst
->src
[i
].offset
= new_reg_offset
[reg
] * REG_SIZE
+
1979 inst
->src
[i
].offset
% REG_SIZE
;
1980 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1984 invalidate_live_intervals();
1988 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1990 * During code generation, we create tons of temporary variables, many of
1991 * which get immediately killed and are never used again. Yet, in later
1992 * optimization and analysis passes, such as compute_live_intervals, we need
1993 * to loop over all the virtual GRFs. Compacting them can save a lot of
1997 fs_visitor::compact_virtual_grfs()
1999 bool progress
= false;
2000 int remap_table
[this->alloc
.count
];
2001 memset(remap_table
, -1, sizeof(remap_table
));
2003 /* Mark which virtual GRFs are used. */
2004 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
2005 if (inst
->dst
.file
== VGRF
)
2006 remap_table
[inst
->dst
.nr
] = 0;
2008 for (int i
= 0; i
< inst
->sources
; i
++) {
2009 if (inst
->src
[i
].file
== VGRF
)
2010 remap_table
[inst
->src
[i
].nr
] = 0;
2014 /* Compact the GRF arrays. */
2016 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
2017 if (remap_table
[i
] == -1) {
2018 /* We just found an unused register. This means that we are
2019 * actually going to compact something.
2023 remap_table
[i
] = new_index
;
2024 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
2025 invalidate_live_intervals();
2030 this->alloc
.count
= new_index
;
2032 /* Patch all the instructions to use the newly renumbered registers */
2033 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2034 if (inst
->dst
.file
== VGRF
)
2035 inst
->dst
.nr
= remap_table
[inst
->dst
.nr
];
2037 for (int i
= 0; i
< inst
->sources
; i
++) {
2038 if (inst
->src
[i
].file
== VGRF
)
2039 inst
->src
[i
].nr
= remap_table
[inst
->src
[i
].nr
];
2043 /* Patch all the references to delta_xy, since they're used in register
2044 * allocation. If they're unused, switch them to BAD_FILE so we don't
2045 * think some random VGRF is delta_xy.
2047 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2048 if (delta_xy
[i
].file
== VGRF
) {
2049 if (remap_table
[delta_xy
[i
].nr
] != -1) {
2050 delta_xy
[i
].nr
= remap_table
[delta_xy
[i
].nr
];
2052 delta_xy
[i
].file
= BAD_FILE
;
2061 get_subgroup_id_param_index(const brw_stage_prog_data
*prog_data
)
2063 if (prog_data
->nr_params
== 0)
2066 /* The local thread id is always the last parameter in the list */
2067 uint32_t last_param
= prog_data
->param
[prog_data
->nr_params
- 1];
2068 if (last_param
== BRW_PARAM_BUILTIN_SUBGROUP_ID
)
2069 return prog_data
->nr_params
- 1;
2075 * Struct for handling complex alignments.
2077 * A complex alignment is stored as multiplier and an offset. A value is
2078 * considered to be aligned if it is {offset} larger than a multiple of {mul}.
2079 * For instance, with an alignment of {8, 2}, cplx_align_apply would do the
2082 * N | cplx_align_apply({8, 2}, N)
2083 * ----+-----------------------------
2097 #define CPLX_ALIGN_MAX_MUL 8
2100 cplx_align_assert_sane(struct cplx_align a
)
2102 assert(a
.mul
> 0 && util_is_power_of_two_nonzero(a
.mul
));
2103 assert(a
.offset
< a
.mul
);
2107 * Combines two alignments to produce a least multiple of sorts.
2109 * The returned alignment is the smallest (in terms of multiplier) such that
2110 * anything aligned to both a and b will be aligned to the new alignment.
2111 * This function will assert-fail if a and b are not compatible, i.e. if the
2112 * offset parameters are such that no common alignment is possible.
2114 static struct cplx_align
2115 cplx_align_combine(struct cplx_align a
, struct cplx_align b
)
2117 cplx_align_assert_sane(a
);
2118 cplx_align_assert_sane(b
);
2120 /* Assert that the alignments agree. */
2121 assert((a
.offset
& (b
.mul
- 1)) == (b
.offset
& (a
.mul
- 1)));
2123 return a
.mul
> b
.mul
? a
: b
;
2127 * Apply a complex alignment
2129 * This function will return the smallest number greater than or equal to
2130 * offset that is aligned to align.
2133 cplx_align_apply(struct cplx_align align
, unsigned offset
)
2135 return ALIGN(offset
- align
.offset
, align
.mul
) + align
.offset
;
2138 #define UNIFORM_SLOT_SIZE 4
2140 struct uniform_slot_info
{
2141 /** True if the given uniform slot is live */
2144 /** True if this slot and the next slot must remain contiguous */
2145 unsigned contiguous
:1;
2147 struct cplx_align align
;
2151 mark_uniform_slots_read(struct uniform_slot_info
*slots
,
2152 unsigned num_slots
, unsigned alignment
)
2154 assert(alignment
> 0 && util_is_power_of_two_nonzero(alignment
));
2155 assert(alignment
<= CPLX_ALIGN_MAX_MUL
);
2157 /* We can't align a slot to anything less than the slot size */
2158 alignment
= MAX2(alignment
, UNIFORM_SLOT_SIZE
);
2160 struct cplx_align align
= {alignment
, 0};
2161 cplx_align_assert_sane(align
);
2163 for (unsigned i
= 0; i
< num_slots
; i
++) {
2164 slots
[i
].is_live
= true;
2165 if (i
< num_slots
- 1)
2166 slots
[i
].contiguous
= true;
2168 align
.offset
= (i
* UNIFORM_SLOT_SIZE
) & (align
.mul
- 1);
2169 if (slots
[i
].align
.mul
== 0) {
2170 slots
[i
].align
= align
;
2172 slots
[i
].align
= cplx_align_combine(slots
[i
].align
, align
);
2178 * Assign UNIFORM file registers to either push constants or pull constants.
2180 * We allow a fragment shader to have more than the specified minimum
2181 * maximum number of fragment shader uniform components (64). If
2182 * there are too many of these, they'd fill up all of register space.
2183 * So, this will push some of them out to the pull constant buffer and
2184 * update the program to load them.
2187 fs_visitor::assign_constant_locations()
2189 /* Only the first compile gets to decide on locations. */
2190 if (push_constant_loc
) {
2191 assert(pull_constant_loc
);
2195 struct uniform_slot_info slots
[uniforms
];
2196 memset(slots
, 0, sizeof(slots
));
2198 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2199 for (int i
= 0 ; i
< inst
->sources
; i
++) {
2200 if (inst
->src
[i
].file
!= UNIFORM
)
2203 /* NIR tightly packs things so the uniform number might not be
2204 * aligned (if we have a double right after a float, for instance).
2205 * This is fine because the process of re-arranging them will ensure
2206 * that things are properly aligned. The offset into that uniform,
2207 * however, must be aligned.
2209 * In Vulkan, we have explicit offsets but everything is crammed
2210 * into a single "variable" so inst->src[i].nr will always be 0.
2211 * Everything will be properly aligned relative to that one base.
2213 assert(inst
->src
[i
].offset
% type_sz(inst
->src
[i
].type
) == 0);
2215 unsigned u
= inst
->src
[i
].nr
+
2216 inst
->src
[i
].offset
/ UNIFORM_SLOT_SIZE
;
2221 unsigned slots_read
;
2222 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0) {
2223 slots_read
= DIV_ROUND_UP(inst
->src
[2].ud
, UNIFORM_SLOT_SIZE
);
2225 unsigned bytes_read
= inst
->components_read(i
) *
2226 type_sz(inst
->src
[i
].type
);
2227 slots_read
= DIV_ROUND_UP(bytes_read
, UNIFORM_SLOT_SIZE
);
2230 assert(u
+ slots_read
<= uniforms
);
2231 mark_uniform_slots_read(&slots
[u
], slots_read
,
2232 type_sz(inst
->src
[i
].type
));
2236 int subgroup_id_index
= get_subgroup_id_param_index(stage_prog_data
);
2238 /* Only allow 16 registers (128 uniform components) as push constants.
2240 * Just demote the end of the list. We could probably do better
2241 * here, demoting things that are rarely used in the program first.
2243 * If changing this value, note the limitation about total_regs in
2246 unsigned int max_push_components
= 16 * 8;
2247 if (subgroup_id_index
>= 0)
2248 max_push_components
--; /* Save a slot for the thread ID */
2250 /* We push small arrays, but no bigger than 16 floats. This is big enough
2251 * for a vec4 but hopefully not large enough to push out other stuff. We
2252 * should probably use a better heuristic at some point.
2254 const unsigned int max_chunk_size
= 16;
2256 unsigned int num_push_constants
= 0;
2257 unsigned int num_pull_constants
= 0;
2259 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2260 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2262 /* Default to -1 meaning no location */
2263 memset(push_constant_loc
, -1, uniforms
* sizeof(*push_constant_loc
));
2264 memset(pull_constant_loc
, -1, uniforms
* sizeof(*pull_constant_loc
));
2266 int chunk_start
= -1;
2267 struct cplx_align align
;
2268 for (unsigned u
= 0; u
< uniforms
; u
++) {
2269 if (!slots
[u
].is_live
) {
2270 assert(chunk_start
== -1);
2274 /* Skip subgroup_id_index to put it in the last push register. */
2275 if (subgroup_id_index
== (int)u
)
2278 if (chunk_start
== -1) {
2280 align
= slots
[u
].align
;
2282 /* Offset into the chunk */
2283 unsigned chunk_offset
= (u
- chunk_start
) * UNIFORM_SLOT_SIZE
;
2285 /* Shift the slot alignment down by the chunk offset so it is
2286 * comparable with the base chunk alignment.
2288 struct cplx_align slot_align
= slots
[u
].align
;
2290 (slot_align
.offset
- chunk_offset
) & (align
.mul
- 1);
2292 align
= cplx_align_combine(align
, slot_align
);
2295 /* Sanity check the alignment */
2296 cplx_align_assert_sane(align
);
2298 if (slots
[u
].contiguous
)
2301 /* Adjust the alignment to be in terms of slots, not bytes */
2302 assert((align
.mul
& (UNIFORM_SLOT_SIZE
- 1)) == 0);
2303 assert((align
.offset
& (UNIFORM_SLOT_SIZE
- 1)) == 0);
2304 align
.mul
/= UNIFORM_SLOT_SIZE
;
2305 align
.offset
/= UNIFORM_SLOT_SIZE
;
2307 unsigned push_start_align
= cplx_align_apply(align
, num_push_constants
);
2308 unsigned chunk_size
= u
- chunk_start
+ 1;
2309 if ((!compiler
->supports_pull_constants
&& u
< UBO_START
) ||
2310 (chunk_size
< max_chunk_size
&&
2311 push_start_align
+ chunk_size
<= max_push_components
)) {
2312 /* Align up the number of push constants */
2313 num_push_constants
= push_start_align
;
2314 for (unsigned i
= 0; i
< chunk_size
; i
++)
2315 push_constant_loc
[chunk_start
+ i
] = num_push_constants
++;
2317 /* We need to pull this one */
2318 num_pull_constants
= cplx_align_apply(align
, num_pull_constants
);
2319 for (unsigned i
= 0; i
< chunk_size
; i
++)
2320 pull_constant_loc
[chunk_start
+ i
] = num_pull_constants
++;
2323 /* Reset the chunk and start again */
2327 /* Add the CS local thread ID uniform at the end of the push constants */
2328 if (subgroup_id_index
>= 0)
2329 push_constant_loc
[subgroup_id_index
] = num_push_constants
++;
2331 /* As the uniforms are going to be reordered, stash the old array and
2332 * create two new arrays for push/pull params.
2334 uint32_t *param
= stage_prog_data
->param
;
2335 stage_prog_data
->nr_params
= num_push_constants
;
2336 if (num_push_constants
) {
2337 stage_prog_data
->param
= rzalloc_array(mem_ctx
, uint32_t,
2338 num_push_constants
);
2340 stage_prog_data
->param
= NULL
;
2342 assert(stage_prog_data
->nr_pull_params
== 0);
2343 assert(stage_prog_data
->pull_param
== NULL
);
2344 if (num_pull_constants
> 0) {
2345 stage_prog_data
->nr_pull_params
= num_pull_constants
;
2346 stage_prog_data
->pull_param
= rzalloc_array(mem_ctx
, uint32_t,
2347 num_pull_constants
);
2350 /* Now that we know how many regular uniforms we'll push, reduce the
2351 * UBO push ranges so we don't exceed the 3DSTATE_CONSTANT limits.
2353 unsigned push_length
= DIV_ROUND_UP(stage_prog_data
->nr_params
, 8);
2354 for (int i
= 0; i
< 4; i
++) {
2355 struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
2357 if (push_length
+ range
->length
> 64)
2358 range
->length
= 64 - push_length
;
2360 push_length
+= range
->length
;
2362 assert(push_length
<= 64);
2364 /* Up until now, the param[] array has been indexed by reg + offset
2365 * of UNIFORM registers. Move pull constants into pull_param[] and
2366 * condense param[] to only contain the uniforms we chose to push.
2368 * NOTE: Because we are condensing the params[] array, we know that
2369 * push_constant_loc[i] <= i and we can do it in one smooth loop without
2370 * having to make a copy.
2372 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2373 uint32_t value
= param
[i
];
2374 if (pull_constant_loc
[i
] != -1) {
2375 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
2376 } else if (push_constant_loc
[i
] != -1) {
2377 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
2384 fs_visitor::get_pull_locs(const fs_reg
&src
,
2385 unsigned *out_surf_index
,
2386 unsigned *out_pull_index
)
2388 assert(src
.file
== UNIFORM
);
2390 if (src
.nr
>= UBO_START
) {
2391 const struct brw_ubo_range
*range
=
2392 &prog_data
->ubo_ranges
[src
.nr
- UBO_START
];
2394 /* If this access is in our (reduced) range, use the push data. */
2395 if (src
.offset
/ 32 < range
->length
)
2398 *out_surf_index
= prog_data
->binding_table
.ubo_start
+ range
->block
;
2399 *out_pull_index
= (32 * range
->start
+ src
.offset
) / 4;
2403 const unsigned location
= src
.nr
+ src
.offset
/ 4;
2405 if (location
< uniforms
&& pull_constant_loc
[location
] != -1) {
2406 /* A regular uniform push constant */
2407 *out_surf_index
= stage_prog_data
->binding_table
.pull_constants_start
;
2408 *out_pull_index
= pull_constant_loc
[location
];
2416 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2417 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2420 fs_visitor::lower_constant_loads()
2422 unsigned index
, pull_index
;
2424 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2425 /* Set up the annotation tracking for new generated instructions. */
2426 const fs_builder
ibld(this, block
, inst
);
2428 for (int i
= 0; i
< inst
->sources
; i
++) {
2429 if (inst
->src
[i
].file
!= UNIFORM
)
2432 /* We'll handle this case later */
2433 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&& i
== 0)
2436 if (!get_pull_locs(inst
->src
[i
], &index
, &pull_index
))
2439 assert(inst
->src
[i
].stride
== 0);
2441 const unsigned block_sz
= 64; /* Fetch one cacheline at a time. */
2442 const fs_builder ubld
= ibld
.exec_all().group(block_sz
/ 4, 0);
2443 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
2444 const unsigned base
= pull_index
* 4;
2446 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
2447 dst
, brw_imm_ud(index
), brw_imm_ud(base
& ~(block_sz
- 1)));
2449 /* Rewrite the instruction to use the temporary VGRF. */
2450 inst
->src
[i
].file
= VGRF
;
2451 inst
->src
[i
].nr
= dst
.nr
;
2452 inst
->src
[i
].offset
= (base
& (block_sz
- 1)) +
2453 inst
->src
[i
].offset
% 4;
2456 if (inst
->opcode
== SHADER_OPCODE_MOV_INDIRECT
&&
2457 inst
->src
[0].file
== UNIFORM
) {
2459 if (!get_pull_locs(inst
->src
[0], &index
, &pull_index
))
2462 VARYING_PULL_CONSTANT_LOAD(ibld
, inst
->dst
,
2466 inst
->remove(block
);
2469 invalidate_live_intervals();
2473 fs_visitor::opt_algebraic()
2475 bool progress
= false;
2477 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2478 switch (inst
->opcode
) {
2479 case BRW_OPCODE_MOV
:
2480 if (!devinfo
->has_64bit_types
&&
2481 (inst
->dst
.type
== BRW_REGISTER_TYPE_DF
||
2482 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
||
2483 inst
->dst
.type
== BRW_REGISTER_TYPE_Q
)) {
2484 assert(inst
->dst
.type
== inst
->src
[0].type
);
2485 assert(!inst
->saturate
);
2486 assert(!inst
->src
[0].abs
);
2487 assert(!inst
->src
[0].negate
);
2488 const brw::fs_builder
ibld(this, block
, inst
);
2490 if (inst
->src
[0].file
== IMM
) {
2491 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2492 brw_imm_ud(inst
->src
[0].u64
>> 32));
2493 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2494 brw_imm_ud(inst
->src
[0].u64
));
2496 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2497 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1));
2498 ibld
.MOV(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2499 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0));
2502 inst
->remove(block
);
2506 if ((inst
->conditional_mod
== BRW_CONDITIONAL_Z
||
2507 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) &&
2508 inst
->dst
.is_null() &&
2509 (inst
->src
[0].abs
|| inst
->src
[0].negate
)) {
2510 inst
->src
[0].abs
= false;
2511 inst
->src
[0].negate
= false;
2516 if (inst
->src
[0].file
!= IMM
)
2519 if (inst
->saturate
) {
2520 /* Full mixed-type saturates don't happen. However, we can end up
2523 * mov.sat(8) g21<1>DF -1F
2525 * Other mixed-size-but-same-base-type cases may also be possible.
2527 if (inst
->dst
.type
!= inst
->src
[0].type
&&
2528 inst
->dst
.type
!= BRW_REGISTER_TYPE_DF
&&
2529 inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
)
2530 assert(!"unimplemented: saturate mixed types");
2532 if (brw_saturate_immediate(inst
->src
[0].type
,
2533 &inst
->src
[0].as_brw_reg())) {
2534 inst
->saturate
= false;
2540 case BRW_OPCODE_MUL
:
2541 if (inst
->src
[1].file
!= IMM
)
2545 if (inst
->src
[1].is_one()) {
2546 inst
->opcode
= BRW_OPCODE_MOV
;
2547 inst
->src
[1] = reg_undef
;
2553 if (inst
->src
[1].is_negative_one()) {
2554 inst
->opcode
= BRW_OPCODE_MOV
;
2555 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2556 inst
->src
[1] = reg_undef
;
2561 if (inst
->src
[0].file
== IMM
) {
2562 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2563 inst
->opcode
= BRW_OPCODE_MOV
;
2564 inst
->src
[0].f
*= inst
->src
[1].f
;
2565 inst
->src
[1] = reg_undef
;
2570 case BRW_OPCODE_ADD
:
2571 if (inst
->src
[1].file
!= IMM
)
2574 if (inst
->src
[0].file
== IMM
) {
2575 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2576 inst
->opcode
= BRW_OPCODE_MOV
;
2577 inst
->src
[0].f
+= inst
->src
[1].f
;
2578 inst
->src
[1] = reg_undef
;
2584 if (inst
->src
[0].equals(inst
->src
[1]) ||
2585 inst
->src
[1].is_zero()) {
2586 /* On Gen8+, the OR instruction can have a source modifier that
2587 * performs logical not on the operand. Cases of 'OR r0, ~r1, 0'
2588 * or 'OR r0, ~r1, ~r1' should become a NOT instead of a MOV.
2590 if (inst
->src
[0].negate
) {
2591 inst
->opcode
= BRW_OPCODE_NOT
;
2592 inst
->src
[0].negate
= false;
2594 inst
->opcode
= BRW_OPCODE_MOV
;
2596 inst
->src
[1] = reg_undef
;
2601 case BRW_OPCODE_CMP
:
2602 if ((inst
->conditional_mod
== BRW_CONDITIONAL_Z
||
2603 inst
->conditional_mod
== BRW_CONDITIONAL_NZ
) &&
2604 inst
->src
[1].is_zero() &&
2605 (inst
->src
[0].abs
|| inst
->src
[0].negate
)) {
2606 inst
->src
[0].abs
= false;
2607 inst
->src
[0].negate
= false;
2612 case BRW_OPCODE_SEL
:
2613 if (!devinfo
->has_64bit_types
&&
2614 (inst
->dst
.type
== BRW_REGISTER_TYPE_DF
||
2615 inst
->dst
.type
== BRW_REGISTER_TYPE_UQ
||
2616 inst
->dst
.type
== BRW_REGISTER_TYPE_Q
)) {
2617 assert(inst
->dst
.type
== inst
->src
[0].type
);
2618 assert(!inst
->saturate
);
2619 assert(!inst
->src
[0].abs
&& !inst
->src
[0].negate
);
2620 assert(!inst
->src
[1].abs
&& !inst
->src
[1].negate
);
2621 const brw::fs_builder
ibld(this, block
, inst
);
2623 set_predicate(inst
->predicate
,
2624 ibld
.SEL(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 0),
2625 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 0),
2626 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 0)));
2627 set_predicate(inst
->predicate
,
2628 ibld
.SEL(subscript(inst
->dst
, BRW_REGISTER_TYPE_UD
, 1),
2629 subscript(inst
->src
[0], BRW_REGISTER_TYPE_UD
, 1),
2630 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UD
, 1)));
2632 inst
->remove(block
);
2635 if (inst
->src
[0].equals(inst
->src
[1])) {
2636 inst
->opcode
= BRW_OPCODE_MOV
;
2637 inst
->src
[1] = reg_undef
;
2638 inst
->predicate
= BRW_PREDICATE_NONE
;
2639 inst
->predicate_inverse
= false;
2641 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2642 switch (inst
->conditional_mod
) {
2643 case BRW_CONDITIONAL_LE
:
2644 case BRW_CONDITIONAL_L
:
2645 switch (inst
->src
[1].type
) {
2646 case BRW_REGISTER_TYPE_F
:
2647 if (inst
->src
[1].f
>= 1.0f
) {
2648 inst
->opcode
= BRW_OPCODE_MOV
;
2649 inst
->src
[1] = reg_undef
;
2650 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2658 case BRW_CONDITIONAL_GE
:
2659 case BRW_CONDITIONAL_G
:
2660 switch (inst
->src
[1].type
) {
2661 case BRW_REGISTER_TYPE_F
:
2662 if (inst
->src
[1].f
<= 0.0f
) {
2663 inst
->opcode
= BRW_OPCODE_MOV
;
2664 inst
->src
[1] = reg_undef
;
2665 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2677 case BRW_OPCODE_MAD
:
2678 if (inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
||
2679 inst
->src
[1].type
!= BRW_REGISTER_TYPE_F
||
2680 inst
->src
[2].type
!= BRW_REGISTER_TYPE_F
)
2682 if (inst
->src
[1].is_one()) {
2683 inst
->opcode
= BRW_OPCODE_ADD
;
2684 inst
->src
[1] = inst
->src
[2];
2685 inst
->src
[2] = reg_undef
;
2687 } else if (inst
->src
[2].is_one()) {
2688 inst
->opcode
= BRW_OPCODE_ADD
;
2689 inst
->src
[2] = reg_undef
;
2693 case SHADER_OPCODE_BROADCAST
:
2694 if (is_uniform(inst
->src
[0])) {
2695 inst
->opcode
= BRW_OPCODE_MOV
;
2697 inst
->force_writemask_all
= true;
2699 } else if (inst
->src
[1].file
== IMM
) {
2700 inst
->opcode
= BRW_OPCODE_MOV
;
2701 /* It's possible that the selected component will be too large and
2702 * overflow the register. This can happen if someone does a
2703 * readInvocation() from GLSL or SPIR-V and provides an OOB
2704 * invocationIndex. If this happens and we some how manage
2705 * to constant fold it in and get here, then component() may cause
2706 * us to start reading outside of the VGRF which will lead to an
2707 * assert later. Instead, just let it wrap around if it goes over
2710 const unsigned comp
= inst
->src
[1].ud
& (inst
->exec_size
- 1);
2711 inst
->src
[0] = component(inst
->src
[0], comp
);
2713 inst
->force_writemask_all
= true;
2718 case SHADER_OPCODE_SHUFFLE
:
2719 if (is_uniform(inst
->src
[0])) {
2720 inst
->opcode
= BRW_OPCODE_MOV
;
2723 } else if (inst
->src
[1].file
== IMM
) {
2724 inst
->opcode
= BRW_OPCODE_MOV
;
2725 inst
->src
[0] = component(inst
->src
[0],
2736 /* Swap if src[0] is immediate. */
2737 if (progress
&& inst
->is_commutative()) {
2738 if (inst
->src
[0].file
== IMM
) {
2739 fs_reg tmp
= inst
->src
[1];
2740 inst
->src
[1] = inst
->src
[0];
2749 * Optimize sample messages that have constant zero values for the trailing
2750 * texture coordinates. We can just reduce the message length for these
2751 * instructions instead of reserving a register for it. Trailing parameters
2752 * that aren't sent default to zero anyway. This will cause the dead code
2753 * eliminator to remove the MOV instruction that would otherwise be emitted to
2754 * set up the zero value.
2757 fs_visitor::opt_zero_samples()
2759 /* Gen4 infers the texturing opcode based on the message length so we can't
2762 if (devinfo
->gen
< 5)
2765 bool progress
= false;
2767 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2768 if (!inst
->is_tex())
2771 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2773 if (load_payload
->is_head_sentinel() ||
2774 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2777 /* We don't want to remove the message header or the first parameter.
2778 * Removing the first parameter is not allowed, see the Haswell PRM
2779 * volume 7, page 149:
2781 * "Parameter 0 is required except for the sampleinfo message, which
2782 * has no parameter 0"
2784 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2785 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2786 (inst
->exec_size
/ 8) +
2787 inst
->header_size
- 1].is_zero()) {
2788 inst
->mlen
-= inst
->exec_size
/ 8;
2794 invalidate_live_intervals();
2800 * Optimize sample messages which are followed by the final RT write.
2802 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2803 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2804 * final texturing results copied to the framebuffer write payload and modify
2805 * them to write to the framebuffer directly.
2808 fs_visitor::opt_sampler_eot()
2810 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2812 if (stage
!= MESA_SHADER_FRAGMENT
|| dispatch_width
> 16)
2815 if (devinfo
->gen
!= 9 && !devinfo
->is_cherryview
)
2818 /* FINISHME: It should be possible to implement this optimization when there
2819 * are multiple drawbuffers.
2821 if (key
->nr_color_regions
!= 1)
2824 /* Requires emitting a bunch of saturating MOV instructions during logical
2825 * send lowering to clamp the color payload, which the sampler unit isn't
2826 * going to do for us.
2828 if (key
->clamp_fragment_color
)
2831 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2832 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2833 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2834 assert(fb_write
->eot
);
2835 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE_LOGICAL
);
2837 /* There wasn't one; nothing to do. */
2838 if (unlikely(fb_write
->prev
->is_head_sentinel()))
2841 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2843 /* 3D Sampler » Messages » Message Format
2845 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2846 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2848 if (tex_inst
->opcode
!= SHADER_OPCODE_TEX_LOGICAL
&&
2849 tex_inst
->opcode
!= SHADER_OPCODE_TXD_LOGICAL
&&
2850 tex_inst
->opcode
!= SHADER_OPCODE_TXF_LOGICAL
&&
2851 tex_inst
->opcode
!= SHADER_OPCODE_TXL_LOGICAL
&&
2852 tex_inst
->opcode
!= FS_OPCODE_TXB_LOGICAL
&&
2853 tex_inst
->opcode
!= SHADER_OPCODE_TXF_CMS_LOGICAL
&&
2854 tex_inst
->opcode
!= SHADER_OPCODE_TXF_CMS_W_LOGICAL
&&
2855 tex_inst
->opcode
!= SHADER_OPCODE_TXF_UMS_LOGICAL
)
2858 /* XXX - This shouldn't be necessary. */
2859 if (tex_inst
->prev
->is_head_sentinel())
2862 /* Check that the FB write sources are fully initialized by the single
2863 * texturing instruction.
2865 for (unsigned i
= 0; i
< FB_WRITE_LOGICAL_NUM_SRCS
; i
++) {
2866 if (i
== FB_WRITE_LOGICAL_SRC_COLOR0
) {
2867 if (!fb_write
->src
[i
].equals(tex_inst
->dst
) ||
2868 fb_write
->size_read(i
) != tex_inst
->size_written
)
2870 } else if (i
!= FB_WRITE_LOGICAL_SRC_COMPONENTS
) {
2871 if (fb_write
->src
[i
].file
!= BAD_FILE
)
2876 assert(!tex_inst
->eot
); /* We can't get here twice */
2877 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2879 const fs_builder
ibld(this, block
, tex_inst
);
2881 tex_inst
->offset
|= fb_write
->target
<< 24;
2882 tex_inst
->eot
= true;
2883 tex_inst
->dst
= ibld
.null_reg_ud();
2884 tex_inst
->size_written
= 0;
2885 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2887 /* Marking EOT is sufficient, lower_logical_sends() will notice the EOT
2888 * flag and submit a header together with the sampler message as required
2891 invalidate_live_intervals();
2896 fs_visitor::opt_register_renaming()
2898 bool progress
= false;
2901 unsigned remap
[alloc
.count
];
2902 memset(remap
, ~0u, sizeof(unsigned) * alloc
.count
);
2904 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2905 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2907 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2908 inst
->opcode
== BRW_OPCODE_WHILE
) {
2912 /* Rewrite instruction sources. */
2913 for (int i
= 0; i
< inst
->sources
; i
++) {
2914 if (inst
->src
[i
].file
== VGRF
&&
2915 remap
[inst
->src
[i
].nr
] != ~0u &&
2916 remap
[inst
->src
[i
].nr
] != inst
->src
[i
].nr
) {
2917 inst
->src
[i
].nr
= remap
[inst
->src
[i
].nr
];
2922 const unsigned dst
= inst
->dst
.nr
;
2925 inst
->dst
.file
== VGRF
&&
2926 alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
== inst
->size_written
&&
2927 !inst
->is_partial_write()) {
2928 if (remap
[dst
] == ~0u) {
2931 remap
[dst
] = alloc
.allocate(regs_written(inst
));
2932 inst
->dst
.nr
= remap
[dst
];
2935 } else if (inst
->dst
.file
== VGRF
&&
2936 remap
[dst
] != ~0u &&
2937 remap
[dst
] != dst
) {
2938 inst
->dst
.nr
= remap
[dst
];
2944 invalidate_live_intervals();
2946 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2947 if (delta_xy
[i
].file
== VGRF
&& remap
[delta_xy
[i
].nr
] != ~0u) {
2948 delta_xy
[i
].nr
= remap
[delta_xy
[i
].nr
];
2957 * Remove redundant or useless discard jumps.
2959 * For example, we can eliminate jumps in the following sequence:
2961 * discard-jump (redundant with the next jump)
2962 * discard-jump (useless; jumps to the next instruction)
2966 fs_visitor::opt_redundant_discard_jumps()
2968 bool progress
= false;
2970 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2972 fs_inst
*placeholder_halt
= NULL
;
2973 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2974 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2975 placeholder_halt
= inst
;
2980 if (!placeholder_halt
)
2983 /* Delete any HALTs immediately before the placeholder halt. */
2984 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2985 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2986 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2987 prev
->remove(last_bblock
);
2992 invalidate_live_intervals();
2998 * Compute a bitmask with GRF granularity with a bit set for each GRF starting
2999 * from \p r.offset which overlaps the region starting at \p s.offset and
3000 * spanning \p ds bytes.
3002 static inline unsigned
3003 mask_relative_to(const fs_reg
&r
, const fs_reg
&s
, unsigned ds
)
3005 const int rel_offset
= reg_offset(s
) - reg_offset(r
);
3006 const int shift
= rel_offset
/ REG_SIZE
;
3007 const unsigned n
= DIV_ROUND_UP(rel_offset
% REG_SIZE
+ ds
, REG_SIZE
);
3008 assert(reg_space(r
) == reg_space(s
) &&
3009 shift
>= 0 && shift
< int(8 * sizeof(unsigned)));
3010 return ((1 << n
) - 1) << shift
;
3014 fs_visitor::opt_peephole_csel()
3016 if (devinfo
->gen
< 8)
3019 bool progress
= false;
3021 foreach_block_reverse(block
, cfg
) {
3022 int ip
= block
->end_ip
+ 1;
3024 foreach_inst_in_block_reverse_safe(fs_inst
, inst
, block
) {
3027 if (inst
->opcode
!= BRW_OPCODE_SEL
||
3028 inst
->predicate
!= BRW_PREDICATE_NORMAL
||
3029 (inst
->dst
.type
!= BRW_REGISTER_TYPE_F
&&
3030 inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3031 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3034 /* Because it is a 3-src instruction, CSEL cannot have an immediate
3035 * value as a source, but we can sometimes handle zero.
3037 if ((inst
->src
[0].file
!= VGRF
&& inst
->src
[0].file
!= ATTR
&&
3038 inst
->src
[0].file
!= UNIFORM
) ||
3039 (inst
->src
[1].file
!= VGRF
&& inst
->src
[1].file
!= ATTR
&&
3040 inst
->src
[1].file
!= UNIFORM
&& !inst
->src
[1].is_zero()))
3043 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3044 if (!scan_inst
->flags_written())
3047 if ((scan_inst
->opcode
!= BRW_OPCODE_CMP
&&
3048 scan_inst
->opcode
!= BRW_OPCODE_MOV
) ||
3049 scan_inst
->predicate
!= BRW_PREDICATE_NONE
||
3050 (scan_inst
->src
[0].file
!= VGRF
&&
3051 scan_inst
->src
[0].file
!= ATTR
&&
3052 scan_inst
->src
[0].file
!= UNIFORM
) ||
3053 scan_inst
->src
[0].type
!= BRW_REGISTER_TYPE_F
)
3056 if (scan_inst
->opcode
== BRW_OPCODE_CMP
&& !scan_inst
->src
[1].is_zero())
3059 const brw::fs_builder
ibld(this, block
, inst
);
3061 const enum brw_conditional_mod cond
=
3062 inst
->predicate_inverse
3063 ? brw_negate_cmod(scan_inst
->conditional_mod
)
3064 : scan_inst
->conditional_mod
;
3066 fs_inst
*csel_inst
= NULL
;
3068 if (inst
->src
[1].file
!= IMM
) {
3069 csel_inst
= ibld
.CSEL(inst
->dst
,
3074 } else if (cond
== BRW_CONDITIONAL_NZ
) {
3075 /* Consider the sequence
3077 * cmp.nz.f0 null<1>F g3<8,8,1>F 0F
3078 * (+f0) sel g124<1>UD g2<8,8,1>UD 0x00000000UD
3080 * The sel will pick the immediate value 0 if r0 is ±0.0.
3081 * Therefore, this sequence is equivalent:
3083 * cmp.nz.f0 null<1>F g3<8,8,1>F 0F
3084 * (+f0) sel g124<1>F g2<8,8,1>F (abs)g3<8,8,1>F
3086 * The abs is ensures that the result is 0UD when g3 is -0.0F.
3087 * By normal cmp-sel merging, this is also equivalent:
3089 * csel.nz g124<1>F g2<4,4,1>F (abs)g3<4,4,1>F g3<4,4,1>F
3091 csel_inst
= ibld
.CSEL(inst
->dst
,
3097 csel_inst
->src
[1].abs
= true;
3100 if (csel_inst
!= NULL
) {
3102 csel_inst
->saturate
= inst
->saturate
;
3103 inst
->remove(block
);
3115 fs_visitor::compute_to_mrf()
3117 bool progress
= false;
3120 /* No MRFs on Gen >= 7. */
3121 if (devinfo
->gen
>= 7)
3124 calculate_live_intervals();
3126 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3130 if (inst
->opcode
!= BRW_OPCODE_MOV
||
3131 inst
->is_partial_write() ||
3132 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= VGRF
||
3133 inst
->dst
.type
!= inst
->src
[0].type
||
3134 inst
->src
[0].abs
|| inst
->src
[0].negate
||
3135 !inst
->src
[0].is_contiguous() ||
3136 inst
->src
[0].offset
% REG_SIZE
!= 0)
3139 /* Can't compute-to-MRF this GRF if someone else was going to
3142 if (this->virtual_grf_end
[inst
->src
[0].nr
] > ip
)
3145 /* Found a move of a GRF to a MRF. Let's see if we can go rewrite the
3146 * things that computed the value of all GRFs of the source region. The
3147 * regs_left bitset keeps track of the registers we haven't yet found a
3148 * generating instruction for.
3150 unsigned regs_left
= (1 << regs_read(inst
, 0)) - 1;
3152 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3153 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3154 inst
->src
[0], inst
->size_read(0))) {
3155 /* Found the last thing to write our reg we want to turn
3156 * into a compute-to-MRF.
3159 /* If this one instruction didn't populate all the
3160 * channels, bail. We might be able to rewrite everything
3161 * that writes that reg, but it would require smarter
3164 if (scan_inst
->is_partial_write())
3167 /* Handling things not fully contained in the source of the copy
3168 * would need us to understand coalescing out more than one MOV at
3171 if (!region_contained_in(scan_inst
->dst
, scan_inst
->size_written
,
3172 inst
->src
[0], inst
->size_read(0)))
3175 /* SEND instructions can't have MRF as a destination. */
3176 if (scan_inst
->mlen
)
3179 if (devinfo
->gen
== 6) {
3180 /* gen6 math instructions must have the destination be
3181 * GRF, so no compute-to-MRF for them.
3183 if (scan_inst
->is_math()) {
3188 /* Clear the bits for any registers this instruction overwrites. */
3189 regs_left
&= ~mask_relative_to(
3190 inst
->src
[0], scan_inst
->dst
, scan_inst
->size_written
);
3195 /* We don't handle control flow here. Most computation of
3196 * values that end up in MRFs are shortly before the MRF
3199 if (block
->start() == scan_inst
)
3202 /* You can't read from an MRF, so if someone else reads our
3203 * MRF's source GRF that we wanted to rewrite, that stops us.
3205 bool interfered
= false;
3206 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
3207 if (regions_overlap(scan_inst
->src
[i
], scan_inst
->size_read(i
),
3208 inst
->src
[0], inst
->size_read(0))) {
3215 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3216 inst
->dst
, inst
->size_written
)) {
3217 /* If somebody else writes our MRF here, we can't
3218 * compute-to-MRF before that.
3223 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1 &&
3224 regions_overlap(fs_reg(MRF
, scan_inst
->base_mrf
), scan_inst
->mlen
* REG_SIZE
,
3225 inst
->dst
, inst
->size_written
)) {
3226 /* Found a SEND instruction, which means that there are
3227 * live values in MRFs from base_mrf to base_mrf +
3228 * scan_inst->mlen - 1. Don't go pushing our MRF write up
3238 /* Found all generating instructions of our MRF's source value, so it
3239 * should be safe to rewrite them to point to the MRF directly.
3241 regs_left
= (1 << regs_read(inst
, 0)) - 1;
3243 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3244 if (regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
3245 inst
->src
[0], inst
->size_read(0))) {
3246 /* Clear the bits for any registers this instruction overwrites. */
3247 regs_left
&= ~mask_relative_to(
3248 inst
->src
[0], scan_inst
->dst
, scan_inst
->size_written
);
3250 const unsigned rel_offset
= reg_offset(scan_inst
->dst
) -
3251 reg_offset(inst
->src
[0]);
3253 if (inst
->dst
.nr
& BRW_MRF_COMPR4
) {
3254 /* Apply the same address transformation done by the hardware
3255 * for COMPR4 MRF writes.
3257 assert(rel_offset
< 2 * REG_SIZE
);
3258 scan_inst
->dst
.nr
= inst
->dst
.nr
+ rel_offset
/ REG_SIZE
* 4;
3260 /* Clear the COMPR4 bit if the generating instruction is not
3263 if (scan_inst
->size_written
< 2 * REG_SIZE
)
3264 scan_inst
->dst
.nr
&= ~BRW_MRF_COMPR4
;
3267 /* Calculate the MRF number the result of this instruction is
3268 * ultimately written to.
3270 scan_inst
->dst
.nr
= inst
->dst
.nr
+ rel_offset
/ REG_SIZE
;
3273 scan_inst
->dst
.file
= MRF
;
3274 scan_inst
->dst
.offset
= inst
->dst
.offset
+ rel_offset
% REG_SIZE
;
3275 scan_inst
->saturate
|= inst
->saturate
;
3282 inst
->remove(block
);
3287 invalidate_live_intervals();
3293 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
3294 * flow. We could probably do better here with some form of divergence
3298 fs_visitor::eliminate_find_live_channel()
3300 bool progress
= false;
3303 if (!brw_stage_has_packed_dispatch(devinfo
, stage
, stage_prog_data
)) {
3304 /* The optimization below assumes that channel zero is live on thread
3305 * dispatch, which may not be the case if the fixed function dispatches
3311 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3312 switch (inst
->opcode
) {
3318 case BRW_OPCODE_ENDIF
:
3319 case BRW_OPCODE_WHILE
:
3323 case FS_OPCODE_DISCARD_JUMP
:
3324 /* This can potentially make control flow non-uniform until the end
3329 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
3331 inst
->opcode
= BRW_OPCODE_MOV
;
3332 inst
->src
[0] = brw_imm_ud(0u);
3334 inst
->force_writemask_all
= true;
3348 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
3349 * instructions to FS_OPCODE_REP_FB_WRITE.
3352 fs_visitor::emit_repclear_shader()
3354 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3356 int color_mrf
= base_mrf
+ 2;
3360 mov
= bld
.exec_all().group(4, 0)
3361 .MOV(brw_message_reg(color_mrf
),
3362 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
3364 struct brw_reg reg
=
3365 brw_reg(BRW_GENERAL_REGISTER_FILE
, 2, 3, 0, 0, BRW_REGISTER_TYPE_F
,
3366 BRW_VERTICAL_STRIDE_8
, BRW_WIDTH_2
, BRW_HORIZONTAL_STRIDE_4
,
3367 BRW_SWIZZLE_XYZW
, WRITEMASK_XYZW
);
3369 mov
= bld
.exec_all().group(4, 0)
3370 .MOV(vec4(brw_message_reg(color_mrf
)), fs_reg(reg
));
3373 fs_inst
*write
= NULL
;
3374 if (key
->nr_color_regions
== 1) {
3375 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
3376 write
->saturate
= key
->clamp_fragment_color
;
3377 write
->base_mrf
= color_mrf
;
3379 write
->header_size
= 0;
3382 assume(key
->nr_color_regions
> 0);
3384 struct brw_reg header
=
3385 retype(brw_message_reg(base_mrf
), BRW_REGISTER_TYPE_UD
);
3386 bld
.exec_all().group(16, 0)
3387 .MOV(header
, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
3389 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
3391 bld
.exec_all().group(1, 0)
3392 .MOV(component(header
, 2), brw_imm_ud(i
));
3395 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
3396 write
->saturate
= key
->clamp_fragment_color
;
3397 write
->base_mrf
= base_mrf
;
3399 write
->header_size
= 2;
3404 write
->last_rt
= true;
3408 assign_constant_locations();
3409 assign_curb_setup();
3411 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
3413 assert(mov
->src
[0].file
== FIXED_GRF
);
3414 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].nr
, 0);
3419 * Walks through basic blocks, looking for repeated MRF writes and
3420 * removing the later ones.
3423 fs_visitor::remove_duplicate_mrf_writes()
3425 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
3426 bool progress
= false;
3428 /* Need to update the MRF tracking for compressed instructions. */
3429 if (dispatch_width
>= 16)
3432 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3434 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3435 if (inst
->is_control_flow()) {
3436 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3439 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3440 inst
->dst
.file
== MRF
) {
3441 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.nr
];
3442 if (prev_inst
&& prev_inst
->opcode
== BRW_OPCODE_MOV
&&
3443 inst
->dst
.equals(prev_inst
->dst
) &&
3444 inst
->src
[0].equals(prev_inst
->src
[0]) &&
3445 inst
->saturate
== prev_inst
->saturate
&&
3446 inst
->predicate
== prev_inst
->predicate
&&
3447 inst
->conditional_mod
== prev_inst
->conditional_mod
&&
3448 inst
->exec_size
== prev_inst
->exec_size
) {
3449 inst
->remove(block
);
3455 /* Clear out the last-write records for MRFs that were overwritten. */
3456 if (inst
->dst
.file
== MRF
) {
3457 last_mrf_move
[inst
->dst
.nr
] = NULL
;
3460 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
3461 /* Found a SEND instruction, which will include two or fewer
3462 * implied MRF writes. We could do better here.
3464 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
3465 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
3469 /* Clear out any MRF move records whose sources got overwritten. */
3470 for (unsigned i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
3471 if (last_mrf_move
[i
] &&
3472 regions_overlap(inst
->dst
, inst
->size_written
,
3473 last_mrf_move
[i
]->src
[0],
3474 last_mrf_move
[i
]->size_read(0))) {
3475 last_mrf_move
[i
] = NULL
;
3479 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3480 inst
->dst
.file
== MRF
&&
3481 inst
->src
[0].file
!= ARF
&&
3482 !inst
->is_partial_write()) {
3483 last_mrf_move
[inst
->dst
.nr
] = inst
;
3488 invalidate_live_intervals();
3494 * Rounding modes for conversion instructions are included for each
3495 * conversion, but right now it is a state. So once it is set,
3496 * we don't need to call it again for subsequent calls.
3498 * This is useful for vector/matrices conversions, as setting the
3499 * mode once is enough for the full vector/matrix
3502 fs_visitor::remove_extra_rounding_modes()
3504 bool progress
= false;
3506 foreach_block (block
, cfg
) {
3507 brw_rnd_mode prev_mode
= BRW_RND_MODE_UNSPECIFIED
;
3509 foreach_inst_in_block_safe (fs_inst
, inst
, block
) {
3510 if (inst
->opcode
== SHADER_OPCODE_RND_MODE
) {
3511 assert(inst
->src
[0].file
== BRW_IMMEDIATE_VALUE
);
3512 const brw_rnd_mode mode
= (brw_rnd_mode
) inst
->src
[0].d
;
3513 if (mode
== prev_mode
) {
3514 inst
->remove(block
);
3524 invalidate_live_intervals();
3530 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
3532 /* Clear the flag for registers that actually got read (as expected). */
3533 for (int i
= 0; i
< inst
->sources
; i
++) {
3535 if (inst
->src
[i
].file
== VGRF
|| inst
->src
[i
].file
== FIXED_GRF
) {
3536 grf
= inst
->src
[i
].nr
;
3541 if (grf
>= first_grf
&&
3542 grf
< first_grf
+ grf_len
) {
3543 deps
[grf
- first_grf
] = false;
3544 if (inst
->exec_size
== 16)
3545 deps
[grf
- first_grf
+ 1] = false;
3551 * Implements this workaround for the original 965:
3553 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
3554 * check for post destination dependencies on this instruction, software
3555 * must ensure that there is no destination hazard for the case of ‘write
3556 * followed by a posted write’ shown in the following example.
3559 * 2. send r3.xy <rest of send instruction>
3562 * Due to no post-destination dependency check on the ‘send’, the above
3563 * code sequence could have two instructions (1 and 2) in flight at the
3564 * same time that both consider ‘r3’ as the target of their final writes.
3567 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
3570 int write_len
= regs_written(inst
);
3571 int first_write_grf
= inst
->dst
.nr
;
3572 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3573 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3575 memset(needs_dep
, false, sizeof(needs_dep
));
3576 memset(needs_dep
, true, write_len
);
3578 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
3580 /* Walk backwards looking for writes to registers we're writing which
3581 * aren't read since being written. If we hit the start of the program,
3582 * we assume that there are no outstanding dependencies on entry to the
3585 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
3586 /* If we hit control flow, assume that there *are* outstanding
3587 * dependencies, and force their cleanup before our instruction.
3589 if (block
->start() == scan_inst
&& block
->num
!= 0) {
3590 for (int i
= 0; i
< write_len
; i
++) {
3592 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
3593 first_write_grf
+ i
);
3598 /* We insert our reads as late as possible on the assumption that any
3599 * instruction but a MOV that might have left us an outstanding
3600 * dependency has more latency than a MOV.
3602 if (scan_inst
->dst
.file
== VGRF
) {
3603 for (unsigned i
= 0; i
< regs_written(scan_inst
); i
++) {
3604 int reg
= scan_inst
->dst
.nr
+ i
;
3606 if (reg
>= first_write_grf
&&
3607 reg
< first_write_grf
+ write_len
&&
3608 needs_dep
[reg
- first_write_grf
]) {
3609 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
3610 needs_dep
[reg
- first_write_grf
] = false;
3611 if (scan_inst
->exec_size
== 16)
3612 needs_dep
[reg
- first_write_grf
+ 1] = false;
3617 /* Clear the flag for registers that actually got read (as expected). */
3618 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3620 /* Continue the loop only if we haven't resolved all the dependencies */
3622 for (i
= 0; i
< write_len
; i
++) {
3632 * Implements this workaround for the original 965:
3634 * "[DevBW, DevCL] Errata: A destination register from a send can not be
3635 * used as a destination register until after it has been sourced by an
3636 * instruction with a different destination register.
3639 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
3641 int write_len
= regs_written(inst
);
3642 unsigned first_write_grf
= inst
->dst
.nr
;
3643 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
3644 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3646 memset(needs_dep
, false, sizeof(needs_dep
));
3647 memset(needs_dep
, true, write_len
);
3648 /* Walk forwards looking for writes to registers we're writing which aren't
3649 * read before being written.
3651 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
) {
3652 /* If we hit control flow, force resolve all remaining dependencies. */
3653 if (block
->end() == scan_inst
&& block
->num
!= cfg
->num_blocks
- 1) {
3654 for (int i
= 0; i
< write_len
; i
++) {
3656 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3657 first_write_grf
+ i
);
3662 /* Clear the flag for registers that actually got read (as expected). */
3663 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3665 /* We insert our reads as late as possible since they're reading the
3666 * result of a SEND, which has massive latency.
3668 if (scan_inst
->dst
.file
== VGRF
&&
3669 scan_inst
->dst
.nr
>= first_write_grf
&&
3670 scan_inst
->dst
.nr
< first_write_grf
+ write_len
&&
3671 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
]) {
3672 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3674 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
] = false;
3677 /* Continue the loop only if we haven't resolved all the dependencies */
3679 for (i
= 0; i
< write_len
; i
++) {
3689 fs_visitor::insert_gen4_send_dependency_workarounds()
3691 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
3694 bool progress
= false;
3696 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3697 if (inst
->mlen
!= 0 && inst
->dst
.file
== VGRF
) {
3698 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3699 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3705 invalidate_live_intervals();
3709 * Turns the generic expression-style uniform pull constant load instruction
3710 * into a hardware-specific series of instructions for loading a pull
3713 * The expression style allows the CSE pass before this to optimize out
3714 * repeated loads from the same offset, and gives the pre-register-allocation
3715 * scheduling full flexibility, while the conversion to native instructions
3716 * allows the post-register-allocation scheduler the best information
3719 * Note that execution masking for setting up pull constant loads is special:
3720 * the channels that need to be written are unrelated to the current execution
3721 * mask, since a later instruction will use one of the result channels as a
3722 * source operand for all 8 or 16 of its channels.
3725 fs_visitor::lower_uniform_pull_constant_loads()
3727 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3728 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3731 if (devinfo
->gen
>= 7) {
3732 const fs_builder ubld
= fs_builder(this, block
, inst
).exec_all();
3733 const fs_reg payload
= ubld
.group(8, 0).vgrf(BRW_REGISTER_TYPE_UD
);
3735 ubld
.group(8, 0).MOV(payload
,
3736 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
3737 ubld
.group(1, 0).MOV(component(payload
, 2),
3738 brw_imm_ud(inst
->src
[1].ud
/ 16));
3740 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3741 inst
->src
[1] = payload
;
3742 inst
->header_size
= 1;
3745 invalidate_live_intervals();
3747 /* Before register allocation, we didn't tell the scheduler about the
3748 * MRF we use. We know it's safe to use this MRF because nothing
3749 * else does except for register spill/unspill, which generates and
3750 * uses its MRF within a single IR instruction.
3752 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
3759 fs_visitor::lower_load_payload()
3761 bool progress
= false;
3763 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3764 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3767 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== VGRF
);
3768 assert(inst
->saturate
== false);
3769 fs_reg dst
= inst
->dst
;
3771 /* Get rid of COMPR4. We'll add it back in if we need it */
3772 if (dst
.file
== MRF
)
3773 dst
.nr
= dst
.nr
& ~BRW_MRF_COMPR4
;
3775 const fs_builder
ibld(this, block
, inst
);
3776 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3778 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3779 if (inst
->src
[i
].file
!= BAD_FILE
) {
3780 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3781 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3782 hbld
.MOV(mov_dst
, mov_src
);
3784 dst
= offset(dst
, hbld
, 1);
3787 if (inst
->dst
.file
== MRF
&& (inst
->dst
.nr
& BRW_MRF_COMPR4
) &&
3788 inst
->exec_size
> 8) {
3789 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3790 * a straightforward copy. Instead, the result of the
3791 * LOAD_PAYLOAD is treated as interleaved and the first four
3792 * non-header sources are unpacked as:
3803 * This is used for gen <= 5 fb writes.
3805 assert(inst
->exec_size
== 16);
3806 assert(inst
->header_size
+ 4 <= inst
->sources
);
3807 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3808 if (inst
->src
[i
].file
!= BAD_FILE
) {
3809 if (devinfo
->has_compr4
) {
3810 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3811 compr4_dst
.nr
|= BRW_MRF_COMPR4
;
3812 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3814 /* Platform doesn't have COMPR4. We have to fake it */
3815 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3816 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3818 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3825 /* The loop above only ever incremented us through the first set
3826 * of 4 registers. However, thanks to the magic of COMPR4, we
3827 * actually wrote to the first 8 registers, so we need to take
3828 * that into account now.
3832 /* The COMPR4 code took care of the first 4 sources. We'll let
3833 * the regular path handle any remaining sources. Yes, we are
3834 * modifying the instruction but we're about to delete it so
3835 * this really doesn't hurt anything.
3837 inst
->header_size
+= 4;
3840 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3841 if (inst
->src
[i
].file
!= BAD_FILE
) {
3842 dst
.type
= inst
->src
[i
].type
;
3843 ibld
.MOV(dst
, inst
->src
[i
]);
3845 dst
.type
= BRW_REGISTER_TYPE_UD
;
3847 dst
= offset(dst
, ibld
, 1);
3850 inst
->remove(block
);
3855 invalidate_live_intervals();
3861 fs_visitor::lower_integer_multiplication()
3863 bool progress
= false;
3865 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3866 const fs_builder
ibld(this, block
, inst
);
3868 if (inst
->opcode
== BRW_OPCODE_MUL
) {
3869 if (inst
->dst
.is_accumulator() ||
3870 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3871 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3874 if (devinfo
->has_integer_dword_mul
)
3877 if (inst
->src
[1].file
== IMM
&&
3878 inst
->src
[1].ud
< (1 << 16)) {
3879 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3880 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3883 * If multiplying by an immediate value that fits in 16-bits, do a
3884 * single MUL instruction with that value in the proper location.
3886 if (devinfo
->gen
< 7) {
3887 fs_reg
imm(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3889 ibld
.MOV(imm
, inst
->src
[1]);
3890 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3892 const bool ud
= (inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3893 ibld
.MUL(inst
->dst
, inst
->src
[0],
3894 ud
? brw_imm_uw(inst
->src
[1].ud
)
3895 : brw_imm_w(inst
->src
[1].d
));
3898 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3899 * do 32-bit integer multiplication in one instruction, but instead
3900 * must do a sequence (which actually calculates a 64-bit result):
3902 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3903 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3904 * mov(8) g2<1>D acc0<8,8,1>D
3906 * But on Gen > 6, the ability to use second accumulator register
3907 * (acc1) for non-float data types was removed, preventing a simple
3908 * implementation in SIMD16. A 16-channel result can be calculated by
3909 * executing the three instructions twice in SIMD8, once with quarter
3910 * control of 1Q for the first eight channels and again with 2Q for
3911 * the second eight channels.
3913 * Which accumulator register is implicitly accessed (by AccWrEnable
3914 * for instance) is determined by the quarter control. Unfortunately
3915 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3916 * implicit accumulator access by an instruction with 2Q will access
3917 * acc1 regardless of whether the data type is usable in acc1.
3919 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3920 * integer data types.
3922 * Since we only want the low 32-bits of the result, we can do two
3923 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3924 * adjust the high result and add them (like the mach is doing):
3926 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3927 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3928 * shl(8) g9<1>D g8<8,8,1>D 16D
3929 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3931 * We avoid the shl instruction by realizing that we only want to add
3932 * the low 16-bits of the "high" result to the high 16-bits of the
3933 * "low" result and using proper regioning on the add:
3935 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3936 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3937 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3939 * Since it does not use the (single) accumulator register, we can
3940 * schedule multi-component multiplications much better.
3943 bool needs_mov
= false;
3944 fs_reg orig_dst
= inst
->dst
;
3946 /* Get a new VGRF for the "low" 32x16-bit multiplication result if
3947 * reusing the original destination is impossible due to hardware
3948 * restrictions, source/destination overlap, or it being the null
3951 fs_reg low
= inst
->dst
;
3952 if (orig_dst
.is_null() || orig_dst
.file
== MRF
||
3953 regions_overlap(inst
->dst
, inst
->size_written
,
3954 inst
->src
[0], inst
->size_read(0)) ||
3955 regions_overlap(inst
->dst
, inst
->size_written
,
3956 inst
->src
[1], inst
->size_read(1)) ||
3957 inst
->dst
.stride
>= 4) {
3959 low
= fs_reg(VGRF
, alloc
.allocate(regs_written(inst
)),
3963 /* Get a new VGRF but keep the same stride as inst->dst */
3964 fs_reg
high(VGRF
, alloc
.allocate(regs_written(inst
)),
3966 high
.stride
= inst
->dst
.stride
;
3967 high
.offset
= inst
->dst
.offset
% REG_SIZE
;
3969 if (devinfo
->gen
>= 7) {
3970 if (inst
->src
[1].abs
)
3971 lower_src_modifiers(this, block
, inst
, 1);
3973 if (inst
->src
[1].file
== IMM
) {
3974 ibld
.MUL(low
, inst
->src
[0],
3975 brw_imm_uw(inst
->src
[1].ud
& 0xffff));
3976 ibld
.MUL(high
, inst
->src
[0],
3977 brw_imm_uw(inst
->src
[1].ud
>> 16));
3979 ibld
.MUL(low
, inst
->src
[0],
3980 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 0));
3981 ibld
.MUL(high
, inst
->src
[0],
3982 subscript(inst
->src
[1], BRW_REGISTER_TYPE_UW
, 1));
3985 if (inst
->src
[0].abs
)
3986 lower_src_modifiers(this, block
, inst
, 0);
3988 ibld
.MUL(low
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UW
, 0),
3990 ibld
.MUL(high
, subscript(inst
->src
[0], BRW_REGISTER_TYPE_UW
, 1),
3994 ibld
.ADD(subscript(low
, BRW_REGISTER_TYPE_UW
, 1),
3995 subscript(low
, BRW_REGISTER_TYPE_UW
, 1),
3996 subscript(high
, BRW_REGISTER_TYPE_UW
, 0));
3998 if (needs_mov
|| inst
->conditional_mod
) {
3999 set_condmod(inst
->conditional_mod
,
4000 ibld
.MOV(orig_dst
, low
));
4004 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
4005 /* According to the BDW+ BSpec page for the "Multiply Accumulate
4006 * High" instruction:
4008 * "An added preliminary mov is required for source modification on
4010 * mov (8) r3.0<1>:d -r3<8;8,1>:d
4011 * mul (8) acc0:d r2.0<8;8,1>:d r3.0<16;8,2>:uw
4012 * mach (8) r5.0<1>:d r2.0<8;8,1>:d r3.0<8;8,1>:d"
4014 if (devinfo
->gen
>= 8 && (inst
->src
[1].negate
|| inst
->src
[1].abs
))
4015 lower_src_modifiers(this, block
, inst
, 1);
4017 /* Should have been lowered to 8-wide. */
4018 assert(inst
->exec_size
<= get_lowered_simd_width(devinfo
, inst
));
4019 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
),
4021 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
4022 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
4024 if (devinfo
->gen
>= 8) {
4025 /* Until Gen8, integer multiplies read 32-bits from one source,
4026 * and 16-bits from the other, and relying on the MACH instruction
4027 * to generate the high bits of the result.
4029 * On Gen8, the multiply instruction does a full 32x32-bit
4030 * multiply, but in order to do a 64-bit multiply we can simulate
4031 * the previous behavior and then use a MACH instruction.
4033 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
4034 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
4035 mul
->src
[1].type
= BRW_REGISTER_TYPE_UW
;
4036 mul
->src
[1].stride
*= 2;
4038 if (mul
->src
[1].file
== IMM
) {
4039 mul
->src
[1] = brw_imm_uw(mul
->src
[1].ud
);
4041 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
4043 /* Among other things the quarter control bits influence which
4044 * accumulator register is used by the hardware for instructions
4045 * that access the accumulator implicitly (e.g. MACH). A
4046 * second-half instruction would normally map to acc1, which
4047 * doesn't exist on Gen7 and up (the hardware does emulate it for
4048 * floating-point instructions *only* by taking advantage of the
4049 * extra precision of acc0 not normally used for floating point
4052 * HSW and up are careful enough not to try to access an
4053 * accumulator register that doesn't exist, but on earlier Gen7
4054 * hardware we need to make sure that the quarter control bits are
4055 * zero to avoid non-deterministic behaviour and emit an extra MOV
4056 * to get the result masked correctly according to the current
4060 mach
->force_writemask_all
= true;
4061 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
4062 ibld
.MOV(inst
->dst
, mach
->dst
);
4068 inst
->remove(block
);
4073 invalidate_live_intervals();
4079 fs_visitor::lower_minmax()
4081 assert(devinfo
->gen
< 6);
4083 bool progress
= false;
4085 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4086 const fs_builder
ibld(this, block
, inst
);
4088 if (inst
->opcode
== BRW_OPCODE_SEL
&&
4089 inst
->predicate
== BRW_PREDICATE_NONE
) {
4090 /* FIXME: Using CMP doesn't preserve the NaN propagation semantics of
4091 * the original SEL.L/GE instruction
4093 ibld
.CMP(ibld
.null_reg_d(), inst
->src
[0], inst
->src
[1],
4094 inst
->conditional_mod
);
4095 inst
->predicate
= BRW_PREDICATE_NORMAL
;
4096 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
4103 invalidate_live_intervals();
4109 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
4110 fs_reg
*dst
, fs_reg color
, unsigned components
)
4112 if (key
->clamp_fragment_color
) {
4113 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
4114 assert(color
.type
== BRW_REGISTER_TYPE_F
);
4116 for (unsigned i
= 0; i
< components
; i
++)
4118 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
4123 for (unsigned i
= 0; i
< components
; i
++)
4124 dst
[i
] = offset(color
, bld
, i
);
4128 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
4129 const struct brw_wm_prog_data
*prog_data
,
4130 const brw_wm_prog_key
*key
,
4131 const fs_visitor::thread_payload
&payload
)
4133 assert(inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
4134 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4135 const fs_reg
&color0
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR0
];
4136 const fs_reg
&color1
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
];
4137 const fs_reg
&src0_alpha
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA
];
4138 const fs_reg
&src_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
];
4139 const fs_reg
&dst_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_DST_DEPTH
];
4140 const fs_reg
&src_stencil
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_STENCIL
];
4141 fs_reg sample_mask
= inst
->src
[FB_WRITE_LOGICAL_SRC_OMASK
];
4142 const unsigned components
=
4143 inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
4145 /* We can potentially have a message length of up to 15, so we have to set
4146 * base_mrf to either 0 or 1 in order to fit in m0..m15.
4149 int header_size
= 2, payload_header_size
;
4150 unsigned length
= 0;
4152 if (devinfo
->gen
< 6) {
4153 /* TODO: Support SIMD32 on gen4-5 */
4154 assert(bld
.group() < 16);
4156 /* For gen4-5, we always have a header consisting of g0 and g1. We have
4157 * an implied MOV from g0,g1 to the start of the message. The MOV from
4158 * g0 is handled by the hardware and the MOV from g1 is provided by the
4159 * generator. This is required because, on gen4-5, the generator may
4160 * generate two write messages with different message lengths in order
4161 * to handle AA data properly.
4163 * Also, since the pixel mask goes in the g0 portion of the message and
4164 * since render target writes are the last thing in the shader, we write
4165 * the pixel mask directly into g0 and it will get copied as part of the
4168 if (prog_data
->uses_kill
) {
4169 bld
.exec_all().group(1, 0)
4170 .MOV(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
),
4171 brw_flag_reg(0, 1));
4174 assert(length
== 0);
4176 } else if ((devinfo
->gen
<= 7 && !devinfo
->is_haswell
&&
4177 prog_data
->uses_kill
) ||
4178 color1
.file
!= BAD_FILE
||
4179 key
->nr_color_regions
> 1) {
4180 /* From the Sandy Bridge PRM, volume 4, page 198:
4182 * "Dispatched Pixel Enables. One bit per pixel indicating
4183 * which pixels were originally enabled when the thread was
4184 * dispatched. This field is only required for the end-of-
4185 * thread message and on all dual-source messages."
4187 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4189 fs_reg header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
4190 if (bld
.group() < 16) {
4191 /* The header starts off as g0 and g1 for the first half */
4192 ubld
.group(16, 0).MOV(header
, retype(brw_vec8_grf(0, 0),
4193 BRW_REGISTER_TYPE_UD
));
4195 /* The header starts off as g0 and g2 for the second half */
4196 assert(bld
.group() < 32);
4197 const fs_reg header_sources
[2] = {
4198 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
),
4199 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD
),
4201 ubld
.LOAD_PAYLOAD(header
, header_sources
, 2, 0);
4204 uint32_t g00_bits
= 0;
4206 /* Set "Source0 Alpha Present to RenderTarget" bit in message
4209 if (inst
->target
> 0 && prog_data
->replicate_alpha
)
4210 g00_bits
|= 1 << 11;
4212 /* Set computes stencil to render target */
4213 if (prog_data
->computed_stencil
)
4214 g00_bits
|= 1 << 14;
4217 /* OR extra bits into g0.0 */
4218 ubld
.group(1, 0).OR(component(header
, 0),
4219 retype(brw_vec1_grf(0, 0),
4220 BRW_REGISTER_TYPE_UD
),
4221 brw_imm_ud(g00_bits
));
4224 /* Set the render target index for choosing BLEND_STATE. */
4225 if (inst
->target
> 0) {
4226 ubld
.group(1, 0).MOV(component(header
, 2), brw_imm_ud(inst
->target
));
4229 if (prog_data
->uses_kill
) {
4230 assert(bld
.group() < 16);
4231 ubld
.group(1, 0).MOV(retype(component(header
, 15),
4232 BRW_REGISTER_TYPE_UW
),
4233 brw_flag_reg(0, 1));
4236 assert(length
== 0);
4237 sources
[0] = header
;
4238 sources
[1] = horiz_offset(header
, 8);
4241 assert(length
== 0 || length
== 2);
4242 header_size
= length
;
4244 if (payload
.aa_dest_stencil_reg
[0]) {
4245 assert(inst
->group
< 16);
4246 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1));
4247 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
4248 .MOV(sources
[length
],
4249 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
[0], 0)));
4253 if (src0_alpha
.file
!= BAD_FILE
) {
4254 for (unsigned i
= 0; i
< bld
.dispatch_width() / 8; i
++) {
4255 const fs_builder
&ubld
= bld
.exec_all().group(8, i
)
4256 .annotate("FB write src0 alpha");
4257 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_F
);
4258 ubld
.MOV(tmp
, horiz_offset(src0_alpha
, i
* 8));
4259 setup_color_payload(ubld
, key
, &sources
[length
], tmp
, 1);
4262 } else if (prog_data
->replicate_alpha
&& inst
->target
!= 0) {
4263 /* Handle the case when fragment shader doesn't write to draw buffer
4264 * zero. No need to call setup_color_payload() for src0_alpha because
4265 * alpha value will be undefined.
4267 length
+= bld
.dispatch_width() / 8;
4270 if (sample_mask
.file
!= BAD_FILE
) {
4271 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1),
4272 BRW_REGISTER_TYPE_UD
);
4274 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
4275 * relevant. Since it's unsigned single words one vgrf is always
4276 * 16-wide, but only the lower or higher 8 channels will be used by the
4277 * hardware when doing a SIMD8 write depending on whether we have
4278 * selected the subspans for the first or second half respectively.
4280 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
4281 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
4282 sample_mask
.stride
*= 2;
4284 bld
.exec_all().annotate("FB write oMask")
4285 .MOV(horiz_offset(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
4291 payload_header_size
= length
;
4293 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
4296 if (color1
.file
!= BAD_FILE
) {
4297 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
4301 if (src_depth
.file
!= BAD_FILE
) {
4302 sources
[length
] = src_depth
;
4306 if (dst_depth
.file
!= BAD_FILE
) {
4307 sources
[length
] = dst_depth
;
4311 if (src_stencil
.file
!= BAD_FILE
) {
4312 assert(devinfo
->gen
>= 9);
4313 assert(bld
.dispatch_width() == 8);
4315 /* XXX: src_stencil is only available on gen9+. dst_depth is never
4316 * available on gen9+. As such it's impossible to have both enabled at the
4317 * same time and therefore length cannot overrun the array.
4319 assert(length
< 15);
4321 sources
[length
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4322 bld
.exec_all().annotate("FB write OS")
4323 .MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UB
),
4324 subscript(src_stencil
, BRW_REGISTER_TYPE_UB
, 0));
4329 if (devinfo
->gen
>= 7) {
4330 /* Send from the GRF */
4331 fs_reg payload
= fs_reg(VGRF
, -1, BRW_REGISTER_TYPE_F
);
4332 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
4333 payload
.nr
= bld
.shader
->alloc
.allocate(regs_written(load
));
4334 load
->dst
= payload
;
4336 inst
->src
[0] = payload
;
4337 inst
->resize_sources(1);
4339 /* Send from the MRF */
4340 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
4341 sources
, length
, payload_header_size
);
4343 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
4344 * will do this for us if we just give it a COMPR4 destination.
4346 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
4347 load
->dst
.nr
|= BRW_MRF_COMPR4
;
4349 if (devinfo
->gen
< 6) {
4350 /* Set up src[0] for the implied MOV from grf0-1 */
4351 inst
->resize_sources(1);
4352 inst
->src
[0] = brw_vec8_grf(0, 0);
4354 inst
->resize_sources(0);
4359 inst
->opcode
= FS_OPCODE_FB_WRITE
;
4360 inst
->mlen
= regs_written(load
);
4361 inst
->header_size
= header_size
;
4365 lower_fb_read_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
4367 const fs_builder
&ubld
= bld
.exec_all().group(8, 0);
4368 const unsigned length
= 2;
4369 const fs_reg header
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, length
);
4371 if (bld
.group() < 16) {
4372 ubld
.group(16, 0).MOV(header
, retype(brw_vec8_grf(0, 0),
4373 BRW_REGISTER_TYPE_UD
));
4375 assert(bld
.group() < 32);
4376 const fs_reg header_sources
[] = {
4377 retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
),
4378 retype(brw_vec8_grf(2, 0), BRW_REGISTER_TYPE_UD
)
4380 ubld
.LOAD_PAYLOAD(header
, header_sources
, ARRAY_SIZE(header_sources
), 0);
4383 inst
->resize_sources(1);
4384 inst
->src
[0] = header
;
4385 inst
->opcode
= FS_OPCODE_FB_READ
;
4386 inst
->mlen
= length
;
4387 inst
->header_size
= length
;
4391 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4392 const fs_reg
&coordinate
,
4393 const fs_reg
&shadow_c
,
4394 const fs_reg
&lod
, const fs_reg
&lod2
,
4395 const fs_reg
&surface
,
4396 const fs_reg
&sampler
,
4397 unsigned coord_components
,
4398 unsigned grad_components
)
4400 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
4401 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
4402 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
4403 fs_reg msg_end
= msg_begin
;
4406 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
4408 for (unsigned i
= 0; i
< coord_components
; i
++)
4409 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
4410 offset(coordinate
, bld
, i
));
4412 msg_end
= offset(msg_end
, bld
, coord_components
);
4414 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
4415 * require all three components to be present and zero if they are unused.
4417 if (coord_components
> 0 &&
4418 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
4419 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
4420 for (unsigned i
= coord_components
; i
< 3; i
++)
4421 bld
.MOV(offset(msg_end
, bld
, i
), brw_imm_f(0.0f
));
4423 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
4426 if (op
== SHADER_OPCODE_TXD
) {
4427 /* TXD unsupported in SIMD16 mode. */
4428 assert(bld
.dispatch_width() == 8);
4430 /* the slots for u and v are always present, but r is optional */
4431 if (coord_components
< 2)
4432 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
4435 * dPdx = dudx, dvdx, drdx
4436 * dPdy = dudy, dvdy, drdy
4438 * 1-arg: Does not exist.
4440 * 2-arg: dudx dvdx dudy dvdy
4441 * dPdx.x dPdx.y dPdy.x dPdy.y
4444 * 3-arg: dudx dvdx drdx dudy dvdy drdy
4445 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
4446 * m5 m6 m7 m8 m9 m10
4448 for (unsigned i
= 0; i
< grad_components
; i
++)
4449 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
4451 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
4453 for (unsigned i
= 0; i
< grad_components
; i
++)
4454 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
4456 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
4460 /* Bias/LOD with shadow comparator is unsupported in SIMD16 -- *Without*
4461 * shadow comparator (including RESINFO) it's unsupported in SIMD8 mode.
4463 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
4464 bld
.dispatch_width() == 16);
4466 const brw_reg_type type
=
4467 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
4468 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
4469 bld
.MOV(retype(msg_end
, type
), lod
);
4470 msg_end
= offset(msg_end
, bld
, 1);
4473 if (shadow_c
.file
!= BAD_FILE
) {
4474 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
4475 /* There's no plain shadow compare message, so we use shadow
4476 * compare with a bias of 0.0.
4478 bld
.MOV(msg_end
, brw_imm_f(0.0f
));
4479 msg_end
= offset(msg_end
, bld
, 1);
4482 bld
.MOV(msg_end
, shadow_c
);
4483 msg_end
= offset(msg_end
, bld
, 1);
4487 inst
->src
[0] = reg_undef
;
4488 inst
->src
[1] = surface
;
4489 inst
->src
[2] = sampler
;
4490 inst
->resize_sources(3);
4491 inst
->base_mrf
= msg_begin
.nr
;
4492 inst
->mlen
= msg_end
.nr
- msg_begin
.nr
;
4493 inst
->header_size
= 1;
4497 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4498 const fs_reg
&coordinate
,
4499 const fs_reg
&shadow_c
,
4500 const fs_reg
&lod
, const fs_reg
&lod2
,
4501 const fs_reg
&sample_index
,
4502 const fs_reg
&surface
,
4503 const fs_reg
&sampler
,
4504 unsigned coord_components
,
4505 unsigned grad_components
)
4507 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
4508 fs_reg msg_coords
= message
;
4509 unsigned header_size
= 0;
4511 if (inst
->offset
!= 0) {
4512 /* The offsets set up by the visitor are in the m1 header, so we can't
4519 for (unsigned i
= 0; i
< coord_components
; i
++)
4520 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
),
4521 offset(coordinate
, bld
, i
));
4523 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
4524 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
4526 if (shadow_c
.file
!= BAD_FILE
) {
4527 fs_reg msg_shadow
= msg_lod
;
4528 bld
.MOV(msg_shadow
, shadow_c
);
4529 msg_lod
= offset(msg_shadow
, bld
, 1);
4534 case SHADER_OPCODE_TXL
:
4536 bld
.MOV(msg_lod
, lod
);
4537 msg_end
= offset(msg_lod
, bld
, 1);
4539 case SHADER_OPCODE_TXD
:
4542 * dPdx = dudx, dvdx, drdx
4543 * dPdy = dudy, dvdy, drdy
4545 * Load up these values:
4546 * - dudx dudy dvdx dvdy drdx drdy
4547 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
4550 for (unsigned i
= 0; i
< grad_components
; i
++) {
4551 bld
.MOV(msg_end
, offset(lod
, bld
, i
));
4552 msg_end
= offset(msg_end
, bld
, 1);
4554 bld
.MOV(msg_end
, offset(lod2
, bld
, i
));
4555 msg_end
= offset(msg_end
, bld
, 1);
4558 case SHADER_OPCODE_TXS
:
4559 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
4560 bld
.MOV(msg_lod
, lod
);
4561 msg_end
= offset(msg_lod
, bld
, 1);
4563 case SHADER_OPCODE_TXF
:
4564 msg_lod
= offset(msg_coords
, bld
, 3);
4565 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
4566 msg_end
= offset(msg_lod
, bld
, 1);
4568 case SHADER_OPCODE_TXF_CMS
:
4569 msg_lod
= offset(msg_coords
, bld
, 3);
4571 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), brw_imm_ud(0u));
4573 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
4574 msg_end
= offset(msg_lod
, bld
, 2);
4581 inst
->src
[0] = reg_undef
;
4582 inst
->src
[1] = surface
;
4583 inst
->src
[2] = sampler
;
4584 inst
->resize_sources(3);
4585 inst
->base_mrf
= message
.nr
;
4586 inst
->mlen
= msg_end
.nr
- message
.nr
;
4587 inst
->header_size
= header_size
;
4589 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4590 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
4594 is_high_sampler(const struct gen_device_info
*devinfo
, const fs_reg
&sampler
)
4596 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
4599 return sampler
.file
!= IMM
|| sampler
.ud
>= 16;
4603 sampler_msg_type(const gen_device_info
*devinfo
,
4604 opcode opcode
, bool shadow_compare
)
4606 assert(devinfo
->gen
>= 5);
4608 case SHADER_OPCODE_TEX
:
4609 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_COMPARE
:
4610 GEN5_SAMPLER_MESSAGE_SAMPLE
;
4612 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS_COMPARE
:
4613 GEN5_SAMPLER_MESSAGE_SAMPLE_BIAS
;
4614 case SHADER_OPCODE_TXL
:
4615 return shadow_compare
? GEN5_SAMPLER_MESSAGE_SAMPLE_LOD_COMPARE
:
4616 GEN5_SAMPLER_MESSAGE_SAMPLE_LOD
;
4617 case SHADER_OPCODE_TXL_LZ
:
4618 return shadow_compare
? GEN9_SAMPLER_MESSAGE_SAMPLE_C_LZ
:
4619 GEN9_SAMPLER_MESSAGE_SAMPLE_LZ
;
4620 case SHADER_OPCODE_TXS
:
4621 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4622 return GEN5_SAMPLER_MESSAGE_SAMPLE_RESINFO
;
4623 case SHADER_OPCODE_TXD
:
4624 assert(!shadow_compare
|| devinfo
->gen
>= 8 || devinfo
->is_haswell
);
4625 return shadow_compare
? HSW_SAMPLER_MESSAGE_SAMPLE_DERIV_COMPARE
:
4626 GEN5_SAMPLER_MESSAGE_SAMPLE_DERIVS
;
4627 case SHADER_OPCODE_TXF
:
4628 return GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
4629 case SHADER_OPCODE_TXF_LZ
:
4630 assert(devinfo
->gen
>= 9);
4631 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD_LZ
;
4632 case SHADER_OPCODE_TXF_CMS_W
:
4633 assert(devinfo
->gen
>= 9);
4634 return GEN9_SAMPLER_MESSAGE_SAMPLE_LD2DMS_W
;
4635 case SHADER_OPCODE_TXF_CMS
:
4636 return devinfo
->gen
>= 7 ? GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DMS
:
4637 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
;
4638 case SHADER_OPCODE_TXF_UMS
:
4639 assert(devinfo
->gen
>= 7);
4640 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD2DSS
;
4641 case SHADER_OPCODE_TXF_MCS
:
4642 assert(devinfo
->gen
>= 7);
4643 return GEN7_SAMPLER_MESSAGE_SAMPLE_LD_MCS
;
4644 case SHADER_OPCODE_LOD
:
4645 return GEN5_SAMPLER_MESSAGE_LOD
;
4646 case SHADER_OPCODE_TG4
:
4647 assert(devinfo
->gen
>= 7);
4648 return shadow_compare
? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_C
:
4649 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4
;
4651 case SHADER_OPCODE_TG4_OFFSET
:
4652 assert(devinfo
->gen
>= 7);
4653 return shadow_compare
? GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO_C
:
4654 GEN7_SAMPLER_MESSAGE_SAMPLE_GATHER4_PO
;
4655 case SHADER_OPCODE_SAMPLEINFO
:
4656 return GEN6_SAMPLER_MESSAGE_SAMPLE_SAMPLEINFO
;
4658 unreachable("not reached");
4663 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4664 const fs_reg
&coordinate
,
4665 const fs_reg
&shadow_c
,
4666 fs_reg lod
, const fs_reg
&lod2
,
4667 const fs_reg
&min_lod
,
4668 const fs_reg
&sample_index
,
4670 const fs_reg
&surface
,
4671 const fs_reg
&sampler
,
4672 const fs_reg
&surface_handle
,
4673 const fs_reg
&sampler_handle
,
4674 const fs_reg
&tg4_offset
,
4675 unsigned coord_components
,
4676 unsigned grad_components
)
4678 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4679 const brw_stage_prog_data
*prog_data
= bld
.shader
->stage_prog_data
;
4680 unsigned reg_width
= bld
.dispatch_width() / 8;
4681 unsigned header_size
= 0, length
= 0;
4682 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
4683 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
4684 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
4686 /* We must have exactly one of surface/sampler and surface/sampler_handle */
4687 assert((surface
.file
== BAD_FILE
) != (surface_handle
.file
== BAD_FILE
));
4688 assert((sampler
.file
== BAD_FILE
) != (sampler_handle
.file
== BAD_FILE
));
4690 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
4691 inst
->offset
!= 0 || inst
->eot
||
4692 op
== SHADER_OPCODE_SAMPLEINFO
||
4693 sampler_handle
.file
!= BAD_FILE
||
4694 is_high_sampler(devinfo
, sampler
)) {
4695 /* For general texture offsets (no txf workaround), we need a header to
4698 * TG4 needs to place its channel select in the header, for interaction
4699 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
4700 * larger sampler numbers we need to offset the Sampler State Pointer in
4703 fs_reg header
= retype(sources
[0], BRW_REGISTER_TYPE_UD
);
4707 /* If we're requesting fewer than four channels worth of response,
4708 * and we have an explicit header, we need to set up the sampler
4709 * writemask. It's reversed from normal: 1 means "don't write".
4711 if (!inst
->eot
&& regs_written(inst
) != 4 * reg_width
) {
4712 assert(regs_written(inst
) % reg_width
== 0);
4713 unsigned mask
= ~((1 << (regs_written(inst
) / reg_width
)) - 1) & 0xf;
4714 inst
->offset
|= mask
<< 12;
4717 /* Build the actual header */
4718 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4719 const fs_builder ubld1
= ubld
.group(1, 0);
4720 ubld
.MOV(header
, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD
));
4722 ubld1
.MOV(component(header
, 2), brw_imm_ud(inst
->offset
));
4723 } else if (bld
.shader
->stage
!= MESA_SHADER_VERTEX
&&
4724 bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
) {
4725 /* The vertex and fragment stages have g0.2 set to 0, so
4726 * header0.2 is 0 when g0 is copied. Other stages may not, so we
4727 * must set it to 0 to avoid setting undesirable bits in the
4730 ubld1
.MOV(component(header
, 2), brw_imm_ud(0));
4733 if (sampler_handle
.file
!= BAD_FILE
) {
4734 /* Bindless sampler handles aren't relative to the sampler state
4735 * pointer passed into the shader through SAMPLER_STATE_POINTERS_*.
4736 * Instead, it's an absolute pointer relative to dynamic state base
4739 * Sampler states are 16 bytes each and the pointer we give here has
4740 * to be 32-byte aligned. In order to avoid more indirect messages
4741 * than required, we assume that all bindless sampler states are
4742 * 32-byte aligned. This sacrifices a bit of general state base
4743 * address space but means we can do something more efficient in the
4746 ubld1
.MOV(component(header
, 3), sampler_handle
);
4747 } else if (is_high_sampler(devinfo
, sampler
)) {
4748 if (sampler
.file
== BRW_IMMEDIATE_VALUE
) {
4749 assert(sampler
.ud
>= 16);
4750 const int sampler_state_size
= 16; /* 16 bytes */
4752 ubld1
.ADD(component(header
, 3),
4753 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD
),
4754 brw_imm_ud(16 * (sampler
.ud
/ 16) * sampler_state_size
));
4756 fs_reg tmp
= ubld1
.vgrf(BRW_REGISTER_TYPE_UD
);
4757 ubld1
.AND(tmp
, sampler
, brw_imm_ud(0x0f0));
4758 ubld1
.SHL(tmp
, tmp
, brw_imm_ud(4));
4759 ubld1
.ADD(component(header
, 3),
4760 retype(brw_vec1_grf(0, 3), BRW_REGISTER_TYPE_UD
),
4766 if (shadow_c
.file
!= BAD_FILE
) {
4767 bld
.MOV(sources
[length
], shadow_c
);
4771 bool coordinate_done
= false;
4773 /* Set up the LOD info */
4776 case SHADER_OPCODE_TXL
:
4777 if (devinfo
->gen
>= 9 && op
== SHADER_OPCODE_TXL
&& lod
.is_zero()) {
4778 op
= SHADER_OPCODE_TXL_LZ
;
4781 bld
.MOV(sources
[length
], lod
);
4784 case SHADER_OPCODE_TXD
:
4785 /* TXD should have been lowered in SIMD16 mode. */
4786 assert(bld
.dispatch_width() == 8);
4788 /* Load dPdx and the coordinate together:
4789 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
4791 for (unsigned i
= 0; i
< coord_components
; i
++) {
4792 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
4794 /* For cube map array, the coordinate is (u,v,r,ai) but there are
4795 * only derivatives for (u, v, r).
4797 if (i
< grad_components
) {
4798 bld
.MOV(sources
[length
++], offset(lod
, bld
, i
));
4799 bld
.MOV(sources
[length
++], offset(lod2
, bld
, i
));
4803 coordinate_done
= true;
4805 case SHADER_OPCODE_TXS
:
4806 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
4809 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4810 /* We need an LOD; just use 0 */
4811 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), brw_imm_ud(0));
4814 case SHADER_OPCODE_TXF
:
4815 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
4816 * On Gen9 they are u, v, lod, r
4818 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
), coordinate
);
4820 if (devinfo
->gen
>= 9) {
4821 if (coord_components
>= 2) {
4822 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
),
4823 offset(coordinate
, bld
, 1));
4825 sources
[length
] = brw_imm_d(0);
4830 if (devinfo
->gen
>= 9 && lod
.is_zero()) {
4831 op
= SHADER_OPCODE_TXF_LZ
;
4833 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
4837 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++)
4838 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
4839 offset(coordinate
, bld
, i
));
4841 coordinate_done
= true;
4844 case SHADER_OPCODE_TXF_CMS
:
4845 case SHADER_OPCODE_TXF_CMS_W
:
4846 case SHADER_OPCODE_TXF_UMS
:
4847 case SHADER_OPCODE_TXF_MCS
:
4848 if (op
== SHADER_OPCODE_TXF_UMS
||
4849 op
== SHADER_OPCODE_TXF_CMS
||
4850 op
== SHADER_OPCODE_TXF_CMS_W
) {
4851 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
4855 if (op
== SHADER_OPCODE_TXF_CMS
|| op
== SHADER_OPCODE_TXF_CMS_W
) {
4856 /* Data from the multisample control surface. */
4857 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
4860 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
4863 if (op
== SHADER_OPCODE_TXF_CMS_W
) {
4864 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
),
4867 offset(mcs
, bld
, 1));
4872 /* There is no offsetting for this message; just copy in the integer
4873 * texture coordinates.
4875 for (unsigned i
= 0; i
< coord_components
; i
++)
4876 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
4877 offset(coordinate
, bld
, i
));
4879 coordinate_done
= true;
4881 case SHADER_OPCODE_TG4_OFFSET
:
4882 /* More crazy intermixing */
4883 for (unsigned i
= 0; i
< 2; i
++) /* u, v */
4884 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
4886 for (unsigned i
= 0; i
< 2; i
++) /* offu, offv */
4887 bld
.MOV(retype(sources
[length
++], BRW_REGISTER_TYPE_D
),
4888 offset(tg4_offset
, bld
, i
));
4890 if (coord_components
== 3) /* r if present */
4891 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, 2));
4893 coordinate_done
= true;
4899 /* Set up the coordinate (except for cases where it was done above) */
4900 if (!coordinate_done
) {
4901 for (unsigned i
= 0; i
< coord_components
; i
++)
4902 bld
.MOV(sources
[length
++], offset(coordinate
, bld
, i
));
4905 if (min_lod
.file
!= BAD_FILE
) {
4906 /* Account for all of the missing coordinate sources */
4907 length
+= 4 - coord_components
;
4908 if (op
== SHADER_OPCODE_TXD
)
4909 length
+= (3 - grad_components
) * 2;
4911 bld
.MOV(sources
[length
++], min_lod
);
4916 mlen
= length
* reg_width
- header_size
;
4918 mlen
= length
* reg_width
;
4920 const fs_reg src_payload
= fs_reg(VGRF
, bld
.shader
->alloc
.allocate(mlen
),
4921 BRW_REGISTER_TYPE_F
);
4922 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
4924 /* Generate the SEND. */
4925 inst
->opcode
= SHADER_OPCODE_SEND
;
4927 inst
->header_size
= header_size
;
4929 const unsigned msg_type
=
4930 sampler_msg_type(devinfo
, op
, inst
->shadow_compare
);
4931 const unsigned simd_mode
=
4932 inst
->exec_size
<= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8
:
4933 BRW_SAMPLER_SIMD_MODE_SIMD16
;
4935 uint32_t base_binding_table_index
;
4937 case SHADER_OPCODE_TG4
:
4938 case SHADER_OPCODE_TG4_OFFSET
:
4939 base_binding_table_index
= prog_data
->binding_table
.gather_texture_start
;
4941 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
4942 base_binding_table_index
= prog_data
->binding_table
.image_start
;
4945 base_binding_table_index
= prog_data
->binding_table
.texture_start
;
4949 inst
->sfid
= BRW_SFID_SAMPLER
;
4950 if (surface
.file
== IMM
&&
4951 (sampler
.file
== IMM
|| sampler_handle
.file
!= BAD_FILE
)) {
4952 inst
->desc
= brw_sampler_desc(devinfo
,
4953 surface
.ud
+ base_binding_table_index
,
4954 sampler
.file
== IMM
? sampler
.ud
% 16 : 0,
4957 0 /* return_format unused on gen7+ */);
4958 inst
->src
[0] = brw_imm_ud(0);
4959 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
4960 } else if (surface_handle
.file
!= BAD_FILE
) {
4961 /* Bindless surface */
4962 assert(devinfo
->gen
>= 9);
4963 inst
->desc
= brw_sampler_desc(devinfo
,
4965 sampler
.file
== IMM
? sampler
.ud
% 16 : 0,
4968 0 /* return_format unused on gen7+ */);
4970 /* For bindless samplers, the entire address is included in the message
4971 * header so we can leave the portion in the message descriptor 0.
4973 if (sampler_handle
.file
!= BAD_FILE
|| sampler
.file
== IMM
) {
4974 inst
->src
[0] = brw_imm_ud(0);
4976 const fs_builder ubld
= bld
.group(1, 0).exec_all();
4977 fs_reg desc
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4978 ubld
.SHL(desc
, sampler
, brw_imm_ud(8));
4979 inst
->src
[0] = desc
;
4982 /* We assume that the driver provided the handle in the top 20 bits so
4983 * we can use the surface handle directly as the extended descriptor.
4985 inst
->src
[1] = retype(surface_handle
, BRW_REGISTER_TYPE_UD
);
4987 /* Immediate portion of the descriptor */
4988 inst
->desc
= brw_sampler_desc(devinfo
,
4993 0 /* return_format unused on gen7+ */);
4994 const fs_builder ubld
= bld
.group(1, 0).exec_all();
4995 fs_reg desc
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4996 if (surface
.equals(sampler
)) {
4997 /* This case is common in GL */
4998 ubld
.MUL(desc
, surface
, brw_imm_ud(0x101));
5000 if (sampler_handle
.file
!= BAD_FILE
) {
5001 ubld
.MOV(desc
, surface
);
5002 } else if (sampler
.file
== IMM
) {
5003 ubld
.OR(desc
, surface
, brw_imm_ud(sampler
.ud
<< 8));
5005 ubld
.SHL(desc
, sampler
, brw_imm_ud(8));
5006 ubld
.OR(desc
, desc
, surface
);
5009 if (base_binding_table_index
)
5010 ubld
.ADD(desc
, desc
, brw_imm_ud(base_binding_table_index
));
5011 ubld
.AND(desc
, desc
, brw_imm_ud(0xfff));
5013 inst
->src
[0] = component(desc
, 0);
5014 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5017 inst
->src
[2] = src_payload
;
5018 inst
->resize_sources(3);
5021 /* EOT sampler messages don't make sense to split because it would
5022 * involve ending half of the thread early.
5024 assert(inst
->group
== 0);
5025 /* We need to use SENDC for EOT sampler messages */
5026 inst
->check_tdr
= true;
5027 inst
->send_has_side_effects
= true;
5030 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
5031 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
5035 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
5037 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5038 const fs_reg
&coordinate
= inst
->src
[TEX_LOGICAL_SRC_COORDINATE
];
5039 const fs_reg
&shadow_c
= inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
];
5040 const fs_reg
&lod
= inst
->src
[TEX_LOGICAL_SRC_LOD
];
5041 const fs_reg
&lod2
= inst
->src
[TEX_LOGICAL_SRC_LOD2
];
5042 const fs_reg
&min_lod
= inst
->src
[TEX_LOGICAL_SRC_MIN_LOD
];
5043 const fs_reg
&sample_index
= inst
->src
[TEX_LOGICAL_SRC_SAMPLE_INDEX
];
5044 const fs_reg
&mcs
= inst
->src
[TEX_LOGICAL_SRC_MCS
];
5045 const fs_reg
&surface
= inst
->src
[TEX_LOGICAL_SRC_SURFACE
];
5046 const fs_reg
&sampler
= inst
->src
[TEX_LOGICAL_SRC_SAMPLER
];
5047 const fs_reg
&surface_handle
= inst
->src
[TEX_LOGICAL_SRC_SURFACE_HANDLE
];
5048 const fs_reg
&sampler_handle
= inst
->src
[TEX_LOGICAL_SRC_SAMPLER_HANDLE
];
5049 const fs_reg
&tg4_offset
= inst
->src
[TEX_LOGICAL_SRC_TG4_OFFSET
];
5050 assert(inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].file
== IMM
);
5051 const unsigned coord_components
= inst
->src
[TEX_LOGICAL_SRC_COORD_COMPONENTS
].ud
;
5052 assert(inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].file
== IMM
);
5053 const unsigned grad_components
= inst
->src
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
].ud
;
5055 if (devinfo
->gen
>= 7) {
5056 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
5057 shadow_c
, lod
, lod2
, min_lod
,
5059 mcs
, surface
, sampler
,
5060 surface_handle
, sampler_handle
,
5062 coord_components
, grad_components
);
5063 } else if (devinfo
->gen
>= 5) {
5064 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
5065 shadow_c
, lod
, lod2
, sample_index
,
5067 coord_components
, grad_components
);
5069 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
5070 shadow_c
, lod
, lod2
,
5072 coord_components
, grad_components
);
5077 * Initialize the header present in some typed and untyped surface
5081 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
5083 fs_builder ubld
= bld
.exec_all().group(8, 0);
5084 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5085 ubld
.MOV(dst
, brw_imm_d(0));
5086 ubld
.group(1, 0).MOV(component(dst
, 7), sample_mask
);
5091 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5093 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5095 /* Get the logical send arguments. */
5096 const fs_reg
&addr
= inst
->src
[SURFACE_LOGICAL_SRC_ADDRESS
];
5097 const fs_reg
&src
= inst
->src
[SURFACE_LOGICAL_SRC_DATA
];
5098 const fs_reg
&surface
= inst
->src
[SURFACE_LOGICAL_SRC_SURFACE
];
5099 const fs_reg
&surface_handle
= inst
->src
[SURFACE_LOGICAL_SRC_SURFACE_HANDLE
];
5100 const UNUSED fs_reg
&dims
= inst
->src
[SURFACE_LOGICAL_SRC_IMM_DIMS
];
5101 const fs_reg
&arg
= inst
->src
[SURFACE_LOGICAL_SRC_IMM_ARG
];
5102 assert(arg
.file
== IMM
);
5104 /* We must have exactly one of surface and surface_handle */
5105 assert((surface
.file
== BAD_FILE
) != (surface_handle
.file
== BAD_FILE
));
5107 /* Calculate the total number of components of the payload. */
5108 const unsigned addr_sz
= inst
->components_read(SURFACE_LOGICAL_SRC_ADDRESS
);
5109 const unsigned src_sz
= inst
->components_read(SURFACE_LOGICAL_SRC_DATA
);
5111 const bool is_typed_access
=
5112 inst
->opcode
== SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
||
5113 inst
->opcode
== SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
||
5114 inst
->opcode
== SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
;
5116 /* From the BDW PRM Volume 7, page 147:
5118 * "For the Data Cache Data Port*, the header must be present for the
5119 * following message types: [...] Typed read/write/atomics"
5121 * Earlier generations have a similar wording. Because of this restriction
5122 * we don't attempt to implement sample masks via predication for such
5123 * messages prior to Gen9, since we have to provide a header anyway. On
5124 * Gen11+ the header has been removed so we can only use predication.
5126 const unsigned header_sz
= devinfo
->gen
< 9 && is_typed_access
? 1 : 0;
5128 const bool has_side_effects
= inst
->has_side_effects();
5129 fs_reg sample_mask
= has_side_effects
? bld
.sample_mask_reg() :
5130 fs_reg(brw_imm_d(0xffff));
5132 fs_reg payload
, payload2
;
5133 unsigned mlen
, ex_mlen
= 0;
5134 if (devinfo
->gen
>= 9) {
5135 /* We have split sends on gen9 and above */
5136 assert(header_sz
== 0);
5137 payload
= bld
.move_to_vgrf(addr
, addr_sz
);
5138 payload2
= bld
.move_to_vgrf(src
, src_sz
);
5139 mlen
= addr_sz
* (inst
->exec_size
/ 8);
5140 ex_mlen
= src_sz
* (inst
->exec_size
/ 8);
5142 /* Allocate space for the payload. */
5143 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
5144 payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
5145 fs_reg
*const components
= new fs_reg
[sz
];
5148 /* Construct the payload. */
5150 components
[n
++] = emit_surface_header(bld
, sample_mask
);
5152 for (unsigned i
= 0; i
< addr_sz
; i
++)
5153 components
[n
++] = offset(addr
, bld
, i
);
5155 for (unsigned i
= 0; i
< src_sz
; i
++)
5156 components
[n
++] = offset(src
, bld
, i
);
5158 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
5159 mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
5161 delete[] components
;
5164 /* Predicate the instruction on the sample mask if no header is
5167 if (!header_sz
&& sample_mask
.file
!= BAD_FILE
&&
5168 sample_mask
.file
!= IMM
) {
5169 const fs_builder ubld
= bld
.group(1, 0).exec_all();
5170 if (inst
->predicate
) {
5171 assert(inst
->predicate
== BRW_PREDICATE_NORMAL
);
5172 assert(!inst
->predicate_inverse
);
5173 assert(inst
->flag_subreg
< 2);
5174 /* Combine the sample mask with the existing predicate by using a
5175 * vertical predication mode.
5177 inst
->predicate
= BRW_PREDICATE_ALIGN1_ALLV
;
5178 ubld
.MOV(retype(brw_flag_subreg(inst
->flag_subreg
+ 2),
5182 inst
->flag_subreg
= 2;
5183 inst
->predicate
= BRW_PREDICATE_NORMAL
;
5184 inst
->predicate_inverse
= false;
5185 ubld
.MOV(retype(brw_flag_subreg(inst
->flag_subreg
), sample_mask
.type
),
5191 switch (inst
->opcode
) {
5192 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5193 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5194 /* Byte scattered opcodes go through the normal data cache */
5195 sfid
= GEN7_SFID_DATAPORT_DATA_CACHE
;
5198 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5199 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5200 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5201 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5202 /* Untyped Surface messages go through the data cache but the SFID value
5203 * changed on Haswell.
5205 sfid
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
?
5206 HSW_SFID_DATAPORT_DATA_CACHE_1
:
5207 GEN7_SFID_DATAPORT_DATA_CACHE
);
5210 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5211 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5212 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5213 /* Typed surface messages go through the render cache on IVB and the
5214 * data cache on HSW+.
5216 sfid
= (devinfo
->gen
>= 8 || devinfo
->is_haswell
?
5217 HSW_SFID_DATAPORT_DATA_CACHE_1
:
5218 GEN6_SFID_DATAPORT_RENDER_CACHE
);
5222 unreachable("Unsupported surface opcode");
5226 switch (inst
->opcode
) {
5227 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5228 desc
= brw_dp_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5229 arg
.ud
, /* num_channels */
5233 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5234 desc
= brw_dp_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5235 arg
.ud
, /* num_channels */
5239 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5240 desc
= brw_dp_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5241 arg
.ud
, /* bit_size */
5245 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5246 desc
= brw_dp_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5247 arg
.ud
, /* bit_size */
5251 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5252 desc
= brw_dp_untyped_atomic_desc(devinfo
, inst
->exec_size
,
5253 arg
.ud
, /* atomic_op */
5254 !inst
->dst
.is_null());
5257 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5258 desc
= brw_dp_untyped_atomic_float_desc(devinfo
, inst
->exec_size
,
5259 arg
.ud
, /* atomic_op */
5260 !inst
->dst
.is_null());
5263 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5264 desc
= brw_dp_typed_surface_rw_desc(devinfo
, inst
->exec_size
, inst
->group
,
5265 arg
.ud
, /* num_channels */
5269 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5270 desc
= brw_dp_typed_surface_rw_desc(devinfo
, inst
->exec_size
, inst
->group
,
5271 arg
.ud
, /* num_channels */
5275 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5276 desc
= brw_dp_typed_atomic_desc(devinfo
, inst
->exec_size
, inst
->group
,
5277 arg
.ud
, /* atomic_op */
5278 !inst
->dst
.is_null());
5282 unreachable("Unknown surface logical instruction");
5285 /* Update the original instruction. */
5286 inst
->opcode
= SHADER_OPCODE_SEND
;
5288 inst
->ex_mlen
= ex_mlen
;
5289 inst
->header_size
= header_sz
;
5290 inst
->send_has_side_effects
= has_side_effects
;
5291 inst
->send_is_volatile
= !has_side_effects
;
5293 /* Set up SFID and descriptors */
5296 if (surface
.file
== IMM
) {
5297 inst
->desc
|= surface
.ud
& 0xff;
5298 inst
->src
[0] = brw_imm_ud(0);
5299 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5300 } else if (surface_handle
.file
!= BAD_FILE
) {
5301 /* Bindless surface */
5302 assert(devinfo
->gen
>= 9);
5303 inst
->desc
|= GEN9_BTI_BINDLESS
;
5304 inst
->src
[0] = brw_imm_ud(0);
5306 /* We assume that the driver provided the handle in the top 20 bits so
5307 * we can use the surface handle directly as the extended descriptor.
5309 inst
->src
[1] = retype(surface_handle
, BRW_REGISTER_TYPE_UD
);
5311 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5312 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5313 ubld
.AND(tmp
, surface
, brw_imm_ud(0xff));
5314 inst
->src
[0] = component(tmp
, 0);
5315 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5318 /* Finally, the payload */
5319 inst
->src
[2] = payload
;
5320 inst
->src
[3] = payload2
;
5322 inst
->resize_sources(4);
5326 lower_a64_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5328 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5330 const fs_reg
&addr
= inst
->src
[0];
5331 const fs_reg
&src
= inst
->src
[1];
5332 const unsigned src_comps
= inst
->components_read(1);
5333 assert(inst
->src
[2].file
== IMM
);
5334 const unsigned arg
= inst
->src
[2].ud
;
5335 const bool has_side_effects
= inst
->has_side_effects();
5337 /* If the surface message has side effects and we're a fragment shader, we
5338 * have to predicate with the sample mask to avoid helper invocations.
5340 if (has_side_effects
&& bld
.shader
->stage
== MESA_SHADER_FRAGMENT
) {
5341 inst
->flag_subreg
= 2;
5342 inst
->predicate
= BRW_PREDICATE_NORMAL
;
5343 inst
->predicate_inverse
= false;
5345 fs_reg sample_mask
= bld
.sample_mask_reg();
5346 const fs_builder ubld
= bld
.group(1, 0).exec_all();
5347 ubld
.MOV(retype(brw_flag_subreg(inst
->flag_subreg
), sample_mask
.type
),
5351 fs_reg payload
, payload2
;
5352 unsigned mlen
, ex_mlen
= 0;
5353 if (devinfo
->gen
>= 9) {
5354 /* On Skylake and above, we have SENDS */
5355 mlen
= 2 * (inst
->exec_size
/ 8);
5356 ex_mlen
= src_comps
* type_sz(src
.type
) * inst
->exec_size
/ REG_SIZE
;
5357 payload
= retype(bld
.move_to_vgrf(addr
, 1), BRW_REGISTER_TYPE_UD
);
5358 payload2
= retype(bld
.move_to_vgrf(src
, src_comps
),
5359 BRW_REGISTER_TYPE_UD
);
5361 /* Add two because the address is 64-bit */
5362 const unsigned dwords
= 2 + src_comps
;
5363 mlen
= dwords
* (inst
->exec_size
/ 8);
5369 for (unsigned i
= 0; i
< src_comps
; i
++)
5370 sources
[1 + i
] = offset(src
, bld
, i
);
5372 payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, dwords
);
5373 bld
.LOAD_PAYLOAD(payload
, sources
, 1 + src_comps
, 0);
5377 switch (inst
->opcode
) {
5378 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
5379 desc
= brw_dp_a64_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5380 arg
, /* num_channels */
5384 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
5385 desc
= brw_dp_a64_untyped_surface_rw_desc(devinfo
, inst
->exec_size
,
5386 arg
, /* num_channels */
5390 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
5391 desc
= brw_dp_a64_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5396 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
5397 desc
= brw_dp_a64_byte_scattered_rw_desc(devinfo
, inst
->exec_size
,
5402 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
5403 desc
= brw_dp_a64_untyped_atomic_desc(devinfo
, inst
->exec_size
, 32,
5404 arg
, /* atomic_op */
5405 !inst
->dst
.is_null());
5408 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
5409 desc
= brw_dp_a64_untyped_atomic_desc(devinfo
, inst
->exec_size
, 64,
5410 arg
, /* atomic_op */
5411 !inst
->dst
.is_null());
5415 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5416 desc
= brw_dp_a64_untyped_atomic_float_desc(devinfo
, inst
->exec_size
,
5417 arg
, /* atomic_op */
5418 !inst
->dst
.is_null());
5422 unreachable("Unknown A64 logical instruction");
5425 /* Update the original instruction. */
5426 inst
->opcode
= SHADER_OPCODE_SEND
;
5428 inst
->ex_mlen
= ex_mlen
;
5429 inst
->header_size
= 0;
5430 inst
->send_has_side_effects
= has_side_effects
;
5431 inst
->send_is_volatile
= !has_side_effects
;
5433 /* Set up SFID and descriptors */
5434 inst
->sfid
= HSW_SFID_DATAPORT_DATA_CACHE_1
;
5436 inst
->resize_sources(4);
5437 inst
->src
[0] = brw_imm_ud(0); /* desc */
5438 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5439 inst
->src
[2] = payload
;
5440 inst
->src
[3] = payload2
;
5444 lower_varying_pull_constant_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5446 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5448 if (devinfo
->gen
>= 7) {
5449 fs_reg index
= inst
->src
[0];
5450 /* We are switching the instruction from an ALU-like instruction to a
5451 * send-from-grf instruction. Since sends can't handle strides or
5452 * source modifiers, we have to make a copy of the offset source.
5454 fs_reg offset
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
5455 bld
.MOV(offset
, inst
->src
[1]);
5457 const unsigned simd_mode
=
5458 inst
->exec_size
<= 8 ? BRW_SAMPLER_SIMD_MODE_SIMD8
:
5459 BRW_SAMPLER_SIMD_MODE_SIMD16
;
5461 inst
->opcode
= SHADER_OPCODE_SEND
;
5462 inst
->mlen
= inst
->exec_size
/ 8;
5463 inst
->resize_sources(3);
5465 inst
->sfid
= BRW_SFID_SAMPLER
;
5466 inst
->desc
= brw_sampler_desc(devinfo
, 0, 0,
5467 GEN5_SAMPLER_MESSAGE_SAMPLE_LD
,
5469 if (index
.file
== IMM
) {
5470 inst
->desc
|= index
.ud
& 0xff;
5471 inst
->src
[0] = brw_imm_ud(0);
5473 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5474 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
5475 ubld
.AND(tmp
, index
, brw_imm_ud(0xff));
5476 inst
->src
[0] = component(tmp
, 0);
5478 inst
->src
[1] = brw_imm_ud(0); /* ex_desc */
5479 inst
->src
[2] = offset
; /* payload */
5481 const fs_reg
payload(MRF
, FIRST_PULL_LOAD_MRF(devinfo
->gen
),
5482 BRW_REGISTER_TYPE_UD
);
5484 bld
.MOV(byte_offset(payload
, REG_SIZE
), inst
->src
[1]);
5486 inst
->opcode
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN4
;
5487 inst
->resize_sources(1);
5488 inst
->base_mrf
= payload
.nr
;
5489 inst
->header_size
= 1;
5490 inst
->mlen
= 1 + inst
->exec_size
/ 8;
5495 lower_math_logical_send(const fs_builder
&bld
, fs_inst
*inst
)
5497 assert(bld
.shader
->devinfo
->gen
< 6);
5500 inst
->mlen
= inst
->sources
* inst
->exec_size
/ 8;
5502 if (inst
->sources
> 1) {
5503 /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
5504 * "Message Payload":
5506 * "Operand0[7]. For the INT DIV functions, this operand is the
5509 * "Operand1[7]. For the INT DIV functions, this operand is the
5512 const bool is_int_div
= inst
->opcode
!= SHADER_OPCODE_POW
;
5513 const fs_reg src0
= is_int_div
? inst
->src
[1] : inst
->src
[0];
5514 const fs_reg src1
= is_int_div
? inst
->src
[0] : inst
->src
[1];
5516 inst
->resize_sources(1);
5517 inst
->src
[0] = src0
;
5519 assert(inst
->exec_size
== 8);
5520 bld
.MOV(fs_reg(MRF
, inst
->base_mrf
+ 1, src1
.type
), src1
);
5525 fs_visitor::lower_logical_sends()
5527 bool progress
= false;
5529 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
5530 const fs_builder
ibld(this, block
, inst
);
5532 switch (inst
->opcode
) {
5533 case FS_OPCODE_FB_WRITE_LOGICAL
:
5534 assert(stage
== MESA_SHADER_FRAGMENT
);
5535 lower_fb_write_logical_send(ibld
, inst
,
5536 brw_wm_prog_data(prog_data
),
5537 (const brw_wm_prog_key
*)key
,
5541 case FS_OPCODE_FB_READ_LOGICAL
:
5542 lower_fb_read_logical_send(ibld
, inst
);
5545 case SHADER_OPCODE_TEX_LOGICAL
:
5546 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
5549 case SHADER_OPCODE_TXD_LOGICAL
:
5550 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
5553 case SHADER_OPCODE_TXF_LOGICAL
:
5554 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
5557 case SHADER_OPCODE_TXL_LOGICAL
:
5558 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
5561 case SHADER_OPCODE_TXS_LOGICAL
:
5562 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
5565 case SHADER_OPCODE_IMAGE_SIZE_LOGICAL
:
5566 lower_sampler_logical_send(ibld
, inst
,
5567 SHADER_OPCODE_IMAGE_SIZE_LOGICAL
);
5570 case FS_OPCODE_TXB_LOGICAL
:
5571 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
5574 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
5575 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
5578 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
5579 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS_W
);
5582 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
5583 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
5586 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
5587 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
5590 case SHADER_OPCODE_LOD_LOGICAL
:
5591 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
5594 case SHADER_OPCODE_TG4_LOGICAL
:
5595 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
5598 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
5599 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
5602 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
5603 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_SAMPLEINFO
);
5606 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
5607 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
5608 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
5609 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
5610 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
5611 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5612 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
5613 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
5614 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
5615 lower_surface_logical_send(ibld
, inst
);
5618 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
5619 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
5620 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
5621 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
5622 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
5623 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
5624 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
5625 lower_a64_logical_send(ibld
, inst
);
5628 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
:
5629 lower_varying_pull_constant_logical_send(ibld
, inst
);
5632 case SHADER_OPCODE_RCP
:
5633 case SHADER_OPCODE_RSQ
:
5634 case SHADER_OPCODE_SQRT
:
5635 case SHADER_OPCODE_EXP2
:
5636 case SHADER_OPCODE_LOG2
:
5637 case SHADER_OPCODE_SIN
:
5638 case SHADER_OPCODE_COS
:
5639 case SHADER_OPCODE_POW
:
5640 case SHADER_OPCODE_INT_QUOTIENT
:
5641 case SHADER_OPCODE_INT_REMAINDER
:
5642 /* The math opcodes are overloaded for the send-like and
5643 * expression-like instructions which seems kind of icky. Gen6+ has
5644 * a native (but rather quirky) MATH instruction so we don't need to
5645 * do anything here. On Gen4-5 we'll have to lower the Gen6-like
5646 * logical instructions (which we can easily recognize because they
5647 * have mlen = 0) into send-like virtual instructions.
5649 if (devinfo
->gen
< 6 && inst
->mlen
== 0) {
5650 lower_math_logical_send(ibld
, inst
);
5665 invalidate_live_intervals();
5671 is_mixed_float_with_fp32_dst(const fs_inst
*inst
)
5673 /* This opcode sometimes uses :W type on the source even if the operand is
5674 * a :HF, because in gen7 there is no support for :HF, and thus it uses :W.
5676 if (inst
->opcode
== BRW_OPCODE_F16TO32
)
5679 if (inst
->dst
.type
!= BRW_REGISTER_TYPE_F
)
5682 for (int i
= 0; i
< inst
->sources
; i
++) {
5683 if (inst
->src
[i
].type
== BRW_REGISTER_TYPE_HF
)
5691 is_mixed_float_with_packed_fp16_dst(const fs_inst
*inst
)
5693 /* This opcode sometimes uses :W type on the destination even if the
5694 * destination is a :HF, because in gen7 there is no support for :HF, and
5697 if (inst
->opcode
== BRW_OPCODE_F32TO16
&&
5698 inst
->dst
.stride
== 1)
5701 if (inst
->dst
.type
!= BRW_REGISTER_TYPE_HF
||
5702 inst
->dst
.stride
!= 1)
5705 for (int i
= 0; i
< inst
->sources
; i
++) {
5706 if (inst
->src
[i
].type
== BRW_REGISTER_TYPE_F
)
5714 * Get the closest allowed SIMD width for instruction \p inst accounting for
5715 * some common regioning and execution control restrictions that apply to FPU
5716 * instructions. These restrictions don't necessarily have any relevance to
5717 * instructions not executed by the FPU pipeline like extended math, control
5718 * flow or send message instructions.
5720 * For virtual opcodes it's really up to the instruction -- In some cases
5721 * (e.g. where a virtual instruction unrolls into a simple sequence of FPU
5722 * instructions) it may simplify virtual instruction lowering if we can
5723 * enforce FPU-like regioning restrictions already on the virtual instruction,
5724 * in other cases (e.g. virtual send-like instructions) this may be
5725 * excessively restrictive.
5728 get_fpu_lowered_simd_width(const struct gen_device_info
*devinfo
,
5729 const fs_inst
*inst
)
5731 /* Maximum execution size representable in the instruction controls. */
5732 unsigned max_width
= MIN2(32, inst
->exec_size
);
5734 /* According to the PRMs:
5735 * "A. In Direct Addressing mode, a source cannot span more than 2
5736 * adjacent GRF registers.
5737 * B. A destination cannot span more than 2 adjacent GRF registers."
5739 * Look for the source or destination with the largest register region
5740 * which is the one that is going to limit the overall execution size of
5741 * the instruction due to this rule.
5743 unsigned reg_count
= DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
5745 for (unsigned i
= 0; i
< inst
->sources
; i
++)
5746 reg_count
= MAX2(reg_count
, DIV_ROUND_UP(inst
->size_read(i
), REG_SIZE
));
5748 /* Calculate the maximum execution size of the instruction based on the
5749 * factor by which it goes over the hardware limit of 2 GRFs.
5752 max_width
= MIN2(max_width
, inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2));
5754 /* According to the IVB PRMs:
5755 * "When destination spans two registers, the source MUST span two
5756 * registers. The exception to the above rule:
5758 * - When source is scalar, the source registers are not incremented.
5759 * - When source is packed integer Word and destination is packed
5760 * integer DWord, the source register is not incremented but the
5761 * source sub register is incremented."
5763 * The hardware specs from Gen4 to Gen7.5 mention similar regioning
5764 * restrictions. The code below intentionally doesn't check whether the
5765 * destination type is integer because empirically the hardware doesn't
5766 * seem to care what the actual type is as long as it's dword-aligned.
5768 if (devinfo
->gen
< 8) {
5769 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
5770 /* IVB implements DF scalars as <0;2,1> regions. */
5771 const bool is_scalar_exception
= is_uniform(inst
->src
[i
]) &&
5772 (devinfo
->is_haswell
|| type_sz(inst
->src
[i
].type
) != 8);
5773 const bool is_packed_word_exception
=
5774 type_sz(inst
->dst
.type
) == 4 && inst
->dst
.stride
== 1 &&
5775 type_sz(inst
->src
[i
].type
) == 2 && inst
->src
[i
].stride
== 1;
5777 /* We check size_read(i) against size_written instead of REG_SIZE
5778 * because we want to properly handle SIMD32. In SIMD32, you can end
5779 * up with writes to 4 registers and a source that reads 2 registers
5780 * and we may still need to lower all the way to SIMD8 in that case.
5782 if (inst
->size_written
> REG_SIZE
&&
5783 inst
->size_read(i
) != 0 &&
5784 inst
->size_read(i
) < inst
->size_written
&&
5785 !is_scalar_exception
&& !is_packed_word_exception
) {
5786 const unsigned reg_count
= DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
5787 max_width
= MIN2(max_width
, inst
->exec_size
/ reg_count
);
5792 if (devinfo
->gen
< 6) {
5793 /* From the G45 PRM, Volume 4 Page 361:
5795 * "Operand Alignment Rule: With the exceptions listed below, a
5796 * source/destination operand in general should be aligned to even
5797 * 256-bit physical register with a region size equal to two 256-bit
5798 * physical registers."
5800 * Normally we enforce this by allocating virtual registers to the
5801 * even-aligned class. But we need to handle payload registers.
5803 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
5804 if (inst
->src
[i
].file
== FIXED_GRF
&& (inst
->src
[i
].nr
& 1) &&
5805 inst
->size_read(i
) > REG_SIZE
) {
5806 max_width
= MIN2(max_width
, 8);
5811 /* From the IVB PRMs:
5812 * "When an instruction is SIMD32, the low 16 bits of the execution mask
5813 * are applied for both halves of the SIMD32 instruction. If different
5814 * execution mask channels are required, split the instruction into two
5815 * SIMD16 instructions."
5817 * There is similar text in the HSW PRMs. Gen4-6 don't even implement
5818 * 32-wide control flow support in hardware and will behave similarly.
5820 if (devinfo
->gen
< 8 && !inst
->force_writemask_all
)
5821 max_width
= MIN2(max_width
, 16);
5823 /* From the IVB PRMs (applies to HSW too):
5824 * "Instructions with condition modifiers must not use SIMD32."
5826 * From the BDW PRMs (applies to later hardware too):
5827 * "Ternary instruction with condition modifiers must not use SIMD32."
5829 if (inst
->conditional_mod
&& (devinfo
->gen
< 8 || inst
->is_3src(devinfo
)))
5830 max_width
= MIN2(max_width
, 16);
5832 /* From the IVB PRMs (applies to other devices that don't have the
5833 * gen_device_info::supports_simd16_3src flag set):
5834 * "In Align16 access mode, SIMD16 is not allowed for DW operations and
5835 * SIMD8 is not allowed for DF operations."
5837 if (inst
->is_3src(devinfo
) && !devinfo
->supports_simd16_3src
)
5838 max_width
= MIN2(max_width
, inst
->exec_size
/ reg_count
);
5840 /* Pre-Gen8 EUs are hardwired to use the QtrCtrl+1 (where QtrCtrl is
5841 * the 8-bit quarter of the execution mask signals specified in the
5842 * instruction control fields) for the second compressed half of any
5843 * single-precision instruction (for double-precision instructions
5844 * it's hardwired to use NibCtrl+1, at least on HSW), which means that
5845 * the EU will apply the wrong execution controls for the second
5846 * sequential GRF write if the number of channels per GRF is not exactly
5847 * eight in single-precision mode (or four in double-float mode).
5849 * In this situation we calculate the maximum size of the split
5850 * instructions so they only ever write to a single register.
5852 if (devinfo
->gen
< 8 && inst
->size_written
> REG_SIZE
&&
5853 !inst
->force_writemask_all
) {
5854 const unsigned channels_per_grf
= inst
->exec_size
/
5855 DIV_ROUND_UP(inst
->size_written
, REG_SIZE
);
5856 const unsigned exec_type_size
= get_exec_type_size(inst
);
5857 assert(exec_type_size
);
5859 /* The hardware shifts exactly 8 channels per compressed half of the
5860 * instruction in single-precision mode and exactly 4 in double-precision.
5862 if (channels_per_grf
!= (exec_type_size
== 8 ? 4 : 8))
5863 max_width
= MIN2(max_width
, channels_per_grf
);
5865 /* Lower all non-force_writemask_all DF instructions to SIMD4 on IVB/BYT
5866 * because HW applies the same channel enable signals to both halves of
5867 * the compressed instruction which will be just wrong under
5868 * non-uniform control flow.
5870 if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
5871 (exec_type_size
== 8 || type_sz(inst
->dst
.type
) == 8))
5872 max_width
= MIN2(max_width
, 4);
5875 /* From the SKL PRM, Special Restrictions for Handling Mixed Mode
5878 * "No SIMD16 in mixed mode when destination is f32. Instruction
5879 * execution size must be no more than 8."
5881 * FIXME: the simulator doesn't seem to complain if we don't do this and
5882 * empirical testing with existing CTS tests show that they pass just fine
5883 * without implementing this, however, since our interpretation of the PRM
5884 * is that conversion MOVs between HF and F are still mixed-float
5885 * instructions (and therefore subject to this restriction) we decided to
5886 * split them to be safe. Might be useful to do additional investigation to
5887 * lift the restriction if we can ensure that it is safe though, since these
5888 * conversions are common when half-float types are involved since many
5889 * instructions do not support HF types and conversions from/to F are
5892 if (is_mixed_float_with_fp32_dst(inst
))
5893 max_width
= MIN2(max_width
, 8);
5895 /* From the SKL PRM, Special Restrictions for Handling Mixed Mode
5898 * "No SIMD16 in mixed mode when destination is packed f16 for both
5899 * Align1 and Align16."
5901 if (is_mixed_float_with_packed_fp16_dst(inst
))
5902 max_width
= MIN2(max_width
, 8);
5904 /* Only power-of-two execution sizes are representable in the instruction
5907 return 1 << _mesa_logbase2(max_width
);
5911 * Get the maximum allowed SIMD width for instruction \p inst accounting for
5912 * various payload size restrictions that apply to sampler message
5915 * This is only intended to provide a maximum theoretical bound for the
5916 * execution size of the message based on the number of argument components
5917 * alone, which in most cases will determine whether the SIMD8 or SIMD16
5918 * variant of the message can be used, though some messages may have
5919 * additional restrictions not accounted for here (e.g. pre-ILK hardware uses
5920 * the message length to determine the exact SIMD width and argument count,
5921 * which makes a number of sampler message combinations impossible to
5925 get_sampler_lowered_simd_width(const struct gen_device_info
*devinfo
,
5926 const fs_inst
*inst
)
5928 /* If we have a min_lod parameter on anything other than a simple sample
5929 * message, it will push it over 5 arguments and we have to fall back to
5932 if (inst
->opcode
!= SHADER_OPCODE_TEX
&&
5933 inst
->components_read(TEX_LOGICAL_SRC_MIN_LOD
))
5936 /* Calculate the number of coordinate components that have to be present
5937 * assuming that additional arguments follow the texel coordinates in the
5938 * message payload. On IVB+ there is no need for padding, on ILK-SNB we
5939 * need to pad to four or three components depending on the message,
5940 * pre-ILK we need to pad to at most three components.
5942 const unsigned req_coord_components
=
5943 (devinfo
->gen
>= 7 ||
5944 !inst
->components_read(TEX_LOGICAL_SRC_COORDINATE
)) ? 0 :
5945 (devinfo
->gen
>= 5 && inst
->opcode
!= SHADER_OPCODE_TXF_LOGICAL
&&
5946 inst
->opcode
!= SHADER_OPCODE_TXF_CMS_LOGICAL
) ? 4 :
5949 /* On Gen9+ the LOD argument is for free if we're able to use the LZ
5950 * variant of the TXL or TXF message.
5952 const bool implicit_lod
= devinfo
->gen
>= 9 &&
5953 (inst
->opcode
== SHADER_OPCODE_TXL
||
5954 inst
->opcode
== SHADER_OPCODE_TXF
) &&
5955 inst
->src
[TEX_LOGICAL_SRC_LOD
].is_zero();
5957 /* Calculate the total number of argument components that need to be passed
5958 * to the sampler unit.
5960 const unsigned num_payload_components
=
5961 MAX2(inst
->components_read(TEX_LOGICAL_SRC_COORDINATE
),
5962 req_coord_components
) +
5963 inst
->components_read(TEX_LOGICAL_SRC_SHADOW_C
) +
5964 (implicit_lod
? 0 : inst
->components_read(TEX_LOGICAL_SRC_LOD
)) +
5965 inst
->components_read(TEX_LOGICAL_SRC_LOD2
) +
5966 inst
->components_read(TEX_LOGICAL_SRC_SAMPLE_INDEX
) +
5967 (inst
->opcode
== SHADER_OPCODE_TG4_OFFSET_LOGICAL
?
5968 inst
->components_read(TEX_LOGICAL_SRC_TG4_OFFSET
) : 0) +
5969 inst
->components_read(TEX_LOGICAL_SRC_MCS
);
5971 /* SIMD16 messages with more than five arguments exceed the maximum message
5972 * size supported by the sampler, regardless of whether a header is
5975 return MIN2(inst
->exec_size
,
5976 num_payload_components
> MAX_SAMPLER_MESSAGE_SIZE
/ 2 ? 8 : 16);
5980 * Get the closest native SIMD width supported by the hardware for instruction
5981 * \p inst. The instruction will be left untouched by
5982 * fs_visitor::lower_simd_width() if the returned value is equal to the
5983 * original execution size.
5986 get_lowered_simd_width(const struct gen_device_info
*devinfo
,
5987 const fs_inst
*inst
)
5989 switch (inst
->opcode
) {
5990 case BRW_OPCODE_MOV
:
5991 case BRW_OPCODE_SEL
:
5992 case BRW_OPCODE_NOT
:
5993 case BRW_OPCODE_AND
:
5995 case BRW_OPCODE_XOR
:
5996 case BRW_OPCODE_SHR
:
5997 case BRW_OPCODE_SHL
:
5998 case BRW_OPCODE_ASR
:
5999 case BRW_OPCODE_CMPN
:
6000 case BRW_OPCODE_CSEL
:
6001 case BRW_OPCODE_F32TO16
:
6002 case BRW_OPCODE_F16TO32
:
6003 case BRW_OPCODE_BFREV
:
6004 case BRW_OPCODE_BFE
:
6005 case BRW_OPCODE_ADD
:
6006 case BRW_OPCODE_MUL
:
6007 case BRW_OPCODE_AVG
:
6008 case BRW_OPCODE_FRC
:
6009 case BRW_OPCODE_RNDU
:
6010 case BRW_OPCODE_RNDD
:
6011 case BRW_OPCODE_RNDE
:
6012 case BRW_OPCODE_RNDZ
:
6013 case BRW_OPCODE_LZD
:
6014 case BRW_OPCODE_FBH
:
6015 case BRW_OPCODE_FBL
:
6016 case BRW_OPCODE_CBIT
:
6017 case BRW_OPCODE_SAD2
:
6018 case BRW_OPCODE_MAD
:
6019 case BRW_OPCODE_LRP
:
6020 case FS_OPCODE_PACK
:
6021 case SHADER_OPCODE_SEL_EXEC
:
6022 case SHADER_OPCODE_CLUSTER_BROADCAST
:
6023 return get_fpu_lowered_simd_width(devinfo
, inst
);
6025 case BRW_OPCODE_CMP
: {
6026 /* The Ivybridge/BayTrail WaCMPInstFlagDepClearedEarly workaround says that
6027 * when the destination is a GRF the dependency-clear bit on the flag
6028 * register is cleared early.
6030 * Suggested workarounds are to disable coissuing CMP instructions
6031 * or to split CMP(16) instructions into two CMP(8) instructions.
6033 * We choose to split into CMP(8) instructions since disabling
6034 * coissuing would affect CMP instructions not otherwise affected by
6037 const unsigned max_width
= (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
6038 !inst
->dst
.is_null() ? 8 : ~0);
6039 return MIN2(max_width
, get_fpu_lowered_simd_width(devinfo
, inst
));
6041 case BRW_OPCODE_BFI1
:
6042 case BRW_OPCODE_BFI2
:
6043 /* The Haswell WaForceSIMD8ForBFIInstruction workaround says that we
6045 * "Force BFI instructions to be executed always in SIMD8."
6047 return MIN2(devinfo
->is_haswell
? 8 : ~0u,
6048 get_fpu_lowered_simd_width(devinfo
, inst
));
6051 assert(inst
->src
[0].file
== BAD_FILE
|| inst
->exec_size
<= 16);
6052 return inst
->exec_size
;
6054 case SHADER_OPCODE_RCP
:
6055 case SHADER_OPCODE_RSQ
:
6056 case SHADER_OPCODE_SQRT
:
6057 case SHADER_OPCODE_EXP2
:
6058 case SHADER_OPCODE_LOG2
:
6059 case SHADER_OPCODE_SIN
:
6060 case SHADER_OPCODE_COS
: {
6061 /* Unary extended math instructions are limited to SIMD8 on Gen4 and
6062 * Gen6. Extended Math Function is limited to SIMD8 with half-float.
6064 if (devinfo
->gen
== 6 || (devinfo
->gen
== 4 && !devinfo
->is_g4x
))
6065 return MIN2(8, inst
->exec_size
);
6066 if (inst
->dst
.type
== BRW_REGISTER_TYPE_HF
)
6067 return MIN2(8, inst
->exec_size
);
6068 return MIN2(16, inst
->exec_size
);
6071 case SHADER_OPCODE_POW
: {
6072 /* SIMD16 is only allowed on Gen7+. Extended Math Function is limited
6073 * to SIMD8 with half-float
6075 if (devinfo
->gen
< 7)
6076 return MIN2(8, inst
->exec_size
);
6077 if (inst
->dst
.type
== BRW_REGISTER_TYPE_HF
)
6078 return MIN2(8, inst
->exec_size
);
6079 return MIN2(16, inst
->exec_size
);
6082 case SHADER_OPCODE_INT_QUOTIENT
:
6083 case SHADER_OPCODE_INT_REMAINDER
:
6084 /* Integer division is limited to SIMD8 on all generations. */
6085 return MIN2(8, inst
->exec_size
);
6087 case FS_OPCODE_LINTERP
:
6088 case SHADER_OPCODE_GET_BUFFER_SIZE
:
6089 case FS_OPCODE_DDX_COARSE
:
6090 case FS_OPCODE_DDX_FINE
:
6091 case FS_OPCODE_DDY_COARSE
:
6092 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
6093 case FS_OPCODE_PACK_HALF_2x16_SPLIT
:
6094 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
6095 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
6096 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
6097 return MIN2(16, inst
->exec_size
);
6099 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL
:
6100 /* Pre-ILK hardware doesn't have a SIMD8 variant of the texel fetch
6101 * message used to implement varying pull constant loads, so expand it
6102 * to SIMD16. An alternative with longer message payload length but
6103 * shorter return payload would be to use the SIMD8 sampler message that
6104 * takes (header, u, v, r) as parameters instead of (header, u).
6106 return (devinfo
->gen
== 4 ? 16 : MIN2(16, inst
->exec_size
));
6108 case FS_OPCODE_DDY_FINE
:
6109 /* The implementation of this virtual opcode may require emitting
6110 * compressed Align16 instructions, which are severely limited on some
6113 * From the Ivy Bridge PRM, volume 4 part 3, section 3.3.9 (Register
6114 * Region Restrictions):
6116 * "In Align16 access mode, SIMD16 is not allowed for DW operations
6117 * and SIMD8 is not allowed for DF operations."
6119 * In this context, "DW operations" means "operations acting on 32-bit
6120 * values", so it includes operations on floats.
6122 * Gen4 has a similar restriction. From the i965 PRM, section 11.5.3
6123 * (Instruction Compression -> Rules and Restrictions):
6125 * "A compressed instruction must be in Align1 access mode. Align16
6126 * mode instructions cannot be compressed."
6128 * Similar text exists in the g45 PRM.
6130 * Empirically, compressed align16 instructions using odd register
6131 * numbers don't appear to work on Sandybridge either.
6133 return (devinfo
->gen
== 4 || devinfo
->gen
== 6 ||
6134 (devinfo
->gen
== 7 && !devinfo
->is_haswell
) ?
6135 MIN2(8, inst
->exec_size
) : MIN2(16, inst
->exec_size
));
6137 case SHADER_OPCODE_MULH
:
6138 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
6139 * is 8-wide on Gen7+.
6141 return (devinfo
->gen
>= 7 ? 8 :
6142 get_fpu_lowered_simd_width(devinfo
, inst
));
6144 case FS_OPCODE_FB_WRITE_LOGICAL
:
6145 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
6148 assert(devinfo
->gen
!= 6 ||
6149 inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
].file
== BAD_FILE
||
6150 inst
->exec_size
== 8);
6151 /* Dual-source FB writes are unsupported in SIMD16 mode. */
6152 return (inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
].file
!= BAD_FILE
?
6153 8 : MIN2(16, inst
->exec_size
));
6155 case FS_OPCODE_FB_READ_LOGICAL
:
6156 return MIN2(16, inst
->exec_size
);
6158 case SHADER_OPCODE_TEX_LOGICAL
:
6159 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
6160 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
6161 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
6162 case SHADER_OPCODE_LOD_LOGICAL
:
6163 case SHADER_OPCODE_TG4_LOGICAL
:
6164 case SHADER_OPCODE_SAMPLEINFO_LOGICAL
:
6165 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
6166 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
6167 return get_sampler_lowered_simd_width(devinfo
, inst
);
6169 case SHADER_OPCODE_TXD_LOGICAL
:
6170 /* TXD is unsupported in SIMD16 mode. */
6173 case SHADER_OPCODE_TXL_LOGICAL
:
6174 case FS_OPCODE_TXB_LOGICAL
:
6175 /* Only one execution size is representable pre-ILK depending on whether
6176 * the shadow reference argument is present.
6178 if (devinfo
->gen
== 4)
6179 return inst
->src
[TEX_LOGICAL_SRC_SHADOW_C
].file
== BAD_FILE
? 16 : 8;
6181 return get_sampler_lowered_simd_width(devinfo
, inst
);
6183 case SHADER_OPCODE_TXF_LOGICAL
:
6184 case SHADER_OPCODE_TXS_LOGICAL
:
6185 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
6186 * messages. Use SIMD16 instead.
6188 if (devinfo
->gen
== 4)
6191 return get_sampler_lowered_simd_width(devinfo
, inst
);
6193 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
6194 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
6195 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
6198 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
6199 case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
6200 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
6201 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
6202 case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
:
6203 case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
:
6204 return MIN2(16, inst
->exec_size
);
6206 case SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
:
6207 case SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
:
6208 case SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
:
6209 case SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
:
6210 return devinfo
->gen
<= 8 ? 8 : MIN2(16, inst
->exec_size
);
6212 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
:
6213 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
:
6214 case SHADER_OPCODE_A64_UNTYPED_ATOMIC_FLOAT_LOGICAL
:
6217 case SHADER_OPCODE_URB_READ_SIMD8
:
6218 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
6219 case SHADER_OPCODE_URB_WRITE_SIMD8
:
6220 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
6221 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
6222 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
6223 return MIN2(8, inst
->exec_size
);
6225 case SHADER_OPCODE_QUAD_SWIZZLE
: {
6226 const unsigned swiz
= inst
->src
[1].ud
;
6227 return (is_uniform(inst
->src
[0]) ?
6228 get_fpu_lowered_simd_width(devinfo
, inst
) :
6229 devinfo
->gen
< 11 && type_sz(inst
->src
[0].type
) == 4 ? 8 :
6230 swiz
== BRW_SWIZZLE_XYXY
|| swiz
== BRW_SWIZZLE_ZWZW
? 4 :
6231 get_fpu_lowered_simd_width(devinfo
, inst
));
6233 case SHADER_OPCODE_MOV_INDIRECT
: {
6234 /* From IVB and HSW PRMs:
6236 * "2.When the destination requires two registers and the sources are
6237 * indirect, the sources must use 1x1 regioning mode.
6239 * In case of DF instructions in HSW/IVB, the exec_size is limited by
6240 * the EU decompression logic not handling VxH indirect addressing
6243 const unsigned max_size
= (devinfo
->gen
>= 8 ? 2 : 1) * REG_SIZE
;
6244 /* Prior to Broadwell, we only have 8 address subregisters. */
6245 return MIN3(devinfo
->gen
>= 8 ? 16 : 8,
6246 max_size
/ (inst
->dst
.stride
* type_sz(inst
->dst
.type
)),
6250 case SHADER_OPCODE_LOAD_PAYLOAD
: {
6251 const unsigned reg_count
=
6252 DIV_ROUND_UP(inst
->dst
.component_size(inst
->exec_size
), REG_SIZE
);
6254 if (reg_count
> 2) {
6255 /* Only LOAD_PAYLOAD instructions with per-channel destination region
6256 * can be easily lowered (which excludes headers and heterogeneous
6259 assert(!inst
->header_size
);
6260 for (unsigned i
= 0; i
< inst
->sources
; i
++)
6261 assert(type_sz(inst
->dst
.type
) == type_sz(inst
->src
[i
].type
) ||
6262 inst
->src
[i
].file
== BAD_FILE
);
6264 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
6266 return inst
->exec_size
;
6270 return inst
->exec_size
;
6275 * Return true if splitting out the group of channels of instruction \p inst
6276 * given by lbld.group() requires allocating a temporary for the i-th source
6277 * of the lowered instruction.
6280 needs_src_copy(const fs_builder
&lbld
, const fs_inst
*inst
, unsigned i
)
6282 return !(is_periodic(inst
->src
[i
], lbld
.dispatch_width()) ||
6283 (inst
->components_read(i
) == 1 &&
6284 lbld
.dispatch_width() <= inst
->exec_size
)) ||
6285 (inst
->flags_written() &
6286 flag_mask(inst
->src
[i
], type_sz(inst
->src
[i
].type
)));
6290 * Extract the data that would be consumed by the channel group given by
6291 * lbld.group() from the i-th source region of instruction \p inst and return
6292 * it as result in packed form.
6295 emit_unzip(const fs_builder
&lbld
, fs_inst
*inst
, unsigned i
)
6297 assert(lbld
.group() >= inst
->group
);
6299 /* Specified channel group from the source region. */
6300 const fs_reg src
= horiz_offset(inst
->src
[i
], lbld
.group() - inst
->group
);
6302 if (needs_src_copy(lbld
, inst
, i
)) {
6303 /* Builder of the right width to perform the copy avoiding uninitialized
6304 * data if the lowered execution size is greater than the original
6305 * execution size of the instruction.
6307 const fs_builder cbld
= lbld
.group(MIN2(lbld
.dispatch_width(),
6308 inst
->exec_size
), 0);
6309 const fs_reg tmp
= lbld
.vgrf(inst
->src
[i
].type
, inst
->components_read(i
));
6311 for (unsigned k
= 0; k
< inst
->components_read(i
); ++k
)
6312 cbld
.MOV(offset(tmp
, lbld
, k
), offset(src
, inst
->exec_size
, k
));
6316 } else if (is_periodic(inst
->src
[i
], lbld
.dispatch_width())) {
6317 /* The source is invariant for all dispatch_width-wide groups of the
6320 return inst
->src
[i
];
6323 /* We can just point the lowered instruction at the right channel group
6324 * from the original region.
6331 * Return true if splitting out the group of channels of instruction \p inst
6332 * given by lbld.group() requires allocating a temporary for the destination
6333 * of the lowered instruction and copying the data back to the original
6334 * destination region.
6337 needs_dst_copy(const fs_builder
&lbld
, const fs_inst
*inst
)
6339 /* If the instruction writes more than one component we'll have to shuffle
6340 * the results of multiple lowered instructions in order to make sure that
6341 * they end up arranged correctly in the original destination region.
6343 if (inst
->size_written
> inst
->dst
.component_size(inst
->exec_size
))
6346 /* If the lowered execution size is larger than the original the result of
6347 * the instruction won't fit in the original destination, so we'll have to
6348 * allocate a temporary in any case.
6350 if (lbld
.dispatch_width() > inst
->exec_size
)
6353 for (unsigned i
= 0; i
< inst
->sources
; i
++) {
6354 /* If we already made a copy of the source for other reasons there won't
6355 * be any overlap with the destination.
6357 if (needs_src_copy(lbld
, inst
, i
))
6360 /* In order to keep the logic simple we emit a copy whenever the
6361 * destination region doesn't exactly match an overlapping source, which
6362 * may point at the source and destination not being aligned group by
6363 * group which could cause one of the lowered instructions to overwrite
6364 * the data read from the same source by other lowered instructions.
6366 if (regions_overlap(inst
->dst
, inst
->size_written
,
6367 inst
->src
[i
], inst
->size_read(i
)) &&
6368 !inst
->dst
.equals(inst
->src
[i
]))
6376 * Insert data from a packed temporary into the channel group given by
6377 * lbld.group() of the destination region of instruction \p inst and return
6378 * the temporary as result. Any copy instructions that are required for
6379 * unzipping the previous value (in the case of partial writes) will be
6380 * inserted using \p lbld_before and any copy instructions required for
6381 * zipping up the destination of \p inst will be inserted using \p lbld_after.
6384 emit_zip(const fs_builder
&lbld_before
, const fs_builder
&lbld_after
,
6387 assert(lbld_before
.dispatch_width() == lbld_after
.dispatch_width());
6388 assert(lbld_before
.group() == lbld_after
.group());
6389 assert(lbld_after
.group() >= inst
->group
);
6391 /* Specified channel group from the destination region. */
6392 const fs_reg dst
= horiz_offset(inst
->dst
, lbld_after
.group() - inst
->group
);
6393 const unsigned dst_size
= inst
->size_written
/
6394 inst
->dst
.component_size(inst
->exec_size
);
6396 if (needs_dst_copy(lbld_after
, inst
)) {
6397 const fs_reg tmp
= lbld_after
.vgrf(inst
->dst
.type
, dst_size
);
6399 if (inst
->predicate
) {
6400 /* Handle predication by copying the original contents of
6401 * the destination into the temporary before emitting the
6402 * lowered instruction.
6404 const fs_builder gbld_before
=
6405 lbld_before
.group(MIN2(lbld_before
.dispatch_width(),
6406 inst
->exec_size
), 0);
6407 for (unsigned k
= 0; k
< dst_size
; ++k
) {
6408 gbld_before
.MOV(offset(tmp
, lbld_before
, k
),
6409 offset(dst
, inst
->exec_size
, k
));
6413 const fs_builder gbld_after
=
6414 lbld_after
.group(MIN2(lbld_after
.dispatch_width(),
6415 inst
->exec_size
), 0);
6416 for (unsigned k
= 0; k
< dst_size
; ++k
) {
6417 /* Use a builder of the right width to perform the copy avoiding
6418 * uninitialized data if the lowered execution size is greater than
6419 * the original execution size of the instruction.
6421 gbld_after
.MOV(offset(dst
, inst
->exec_size
, k
),
6422 offset(tmp
, lbld_after
, k
));
6428 /* No need to allocate a temporary for the lowered instruction, just
6429 * take the right group of channels from the original region.
6436 fs_visitor::lower_simd_width()
6438 bool progress
= false;
6440 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
6441 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
6443 if (lower_width
!= inst
->exec_size
) {
6444 /* Builder matching the original instruction. We may also need to
6445 * emit an instruction of width larger than the original, set the
6446 * execution size of the builder to the highest of both for now so
6447 * we're sure that both cases can be handled.
6449 const unsigned max_width
= MAX2(inst
->exec_size
, lower_width
);
6450 const fs_builder ibld
= bld
.at(block
, inst
)
6451 .exec_all(inst
->force_writemask_all
)
6452 .group(max_width
, inst
->group
/ max_width
);
6454 /* Split the copies in chunks of the execution width of either the
6455 * original or the lowered instruction, whichever is lower.
6457 const unsigned n
= DIV_ROUND_UP(inst
->exec_size
, lower_width
);
6458 const unsigned dst_size
= inst
->size_written
/
6459 inst
->dst
.component_size(inst
->exec_size
);
6461 assert(!inst
->writes_accumulator
&& !inst
->mlen
);
6463 /* Inserting the zip, unzip, and duplicated instructions in all of
6464 * the right spots is somewhat tricky. All of the unzip and any
6465 * instructions from the zip which unzip the destination prior to
6466 * writing need to happen before all of the per-group instructions
6467 * and the zip instructions need to happen after. In order to sort
6468 * this all out, we insert the unzip instructions before \p inst,
6469 * insert the per-group instructions after \p inst (i.e. before
6470 * inst->next), and insert the zip instructions before the
6471 * instruction after \p inst. Since we are inserting instructions
6472 * after \p inst, inst->next is a moving target and we need to save
6473 * it off here so that we insert the zip instructions in the right
6476 * Since we're inserting split instructions after after_inst, the
6477 * instructions will end up in the reverse order that we insert them.
6478 * However, certain render target writes require that the low group
6479 * instructions come before the high group. From the Ivy Bridge PRM
6480 * Vol. 4, Pt. 1, Section 3.9.11:
6482 * "If multiple SIMD8 Dual Source messages are delivered by the
6483 * pixel shader thread, each SIMD8_DUALSRC_LO message must be
6484 * issued before the SIMD8_DUALSRC_HI message with the same Slot
6485 * Group Select setting."
6487 * And, from Section 3.9.11.1 of the same PRM:
6489 * "When SIMD32 or SIMD16 PS threads send render target writes
6490 * with multiple SIMD8 and SIMD16 messages, the following must
6493 * All the slots (as described above) must have a corresponding
6494 * render target write irrespective of the slot's validity. A slot
6495 * is considered valid when at least one sample is enabled. For
6496 * example, a SIMD16 PS thread must send two SIMD8 render target
6497 * writes to cover all the slots.
6499 * PS thread must send SIMD render target write messages with
6500 * increasing slot numbers. For example, SIMD16 thread has
6501 * Slot[15:0] and if two SIMD8 render target writes are used, the
6502 * first SIMD8 render target write must send Slot[7:0] and the
6503 * next one must send Slot[15:8]."
6505 * In order to make low group instructions come before high group
6506 * instructions (this is required for some render target writes), we
6507 * split from the highest group to lowest.
6509 exec_node
*const after_inst
= inst
->next
;
6510 for (int i
= n
- 1; i
>= 0; i
--) {
6511 /* Emit a copy of the original instruction with the lowered width.
6512 * If the EOT flag was set throw it away except for the last
6513 * instruction to avoid killing the thread prematurely.
6515 fs_inst split_inst
= *inst
;
6516 split_inst
.exec_size
= lower_width
;
6517 split_inst
.eot
= inst
->eot
&& i
== int(n
- 1);
6519 /* Select the correct channel enables for the i-th group, then
6520 * transform the sources and destination and emit the lowered
6523 const fs_builder lbld
= ibld
.group(lower_width
, i
);
6525 for (unsigned j
= 0; j
< inst
->sources
; j
++)
6526 split_inst
.src
[j
] = emit_unzip(lbld
.at(block
, inst
), inst
, j
);
6528 split_inst
.dst
= emit_zip(lbld
.at(block
, inst
),
6529 lbld
.at(block
, after_inst
), inst
);
6530 split_inst
.size_written
=
6531 split_inst
.dst
.component_size(lower_width
) * dst_size
;
6533 lbld
.at(block
, inst
->next
).emit(split_inst
);
6536 inst
->remove(block
);
6542 invalidate_live_intervals();
6548 fs_visitor::dump_instructions()
6550 dump_instructions(NULL
);
6554 fs_visitor::dump_instructions(const char *name
)
6556 FILE *file
= stderr
;
6557 if (name
&& geteuid() != 0) {
6558 file
= fopen(name
, "w");
6564 calculate_register_pressure();
6565 int ip
= 0, max_pressure
= 0;
6566 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
6567 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
6568 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
6569 dump_instruction(inst
, file
);
6572 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
6575 foreach_in_list(backend_instruction
, inst
, &instructions
) {
6576 fprintf(file
, "%4d: ", ip
++);
6577 dump_instruction(inst
, file
);
6581 if (file
!= stderr
) {
6587 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
6589 dump_instruction(be_inst
, stderr
);
6593 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
6595 fs_inst
*inst
= (fs_inst
*)be_inst
;
6597 if (inst
->predicate
) {
6598 fprintf(file
, "(%cf%d.%d) ",
6599 inst
->predicate_inverse
? '-' : '+',
6600 inst
->flag_subreg
/ 2,
6601 inst
->flag_subreg
% 2);
6604 fprintf(file
, "%s", brw_instruction_name(devinfo
, inst
->opcode
));
6606 fprintf(file
, ".sat");
6607 if (inst
->conditional_mod
) {
6608 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
6609 if (!inst
->predicate
&&
6610 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
6611 inst
->opcode
!= BRW_OPCODE_CSEL
&&
6612 inst
->opcode
!= BRW_OPCODE_IF
&&
6613 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
6614 fprintf(file
, ".f%d.%d", inst
->flag_subreg
/ 2,
6615 inst
->flag_subreg
% 2);
6618 fprintf(file
, "(%d) ", inst
->exec_size
);
6621 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
6624 if (inst
->ex_mlen
) {
6625 fprintf(file
, "(ex_mlen: %d) ", inst
->ex_mlen
);
6629 fprintf(file
, "(EOT) ");
6632 switch (inst
->dst
.file
) {
6634 fprintf(file
, "vgrf%d", inst
->dst
.nr
);
6637 fprintf(file
, "g%d", inst
->dst
.nr
);
6640 fprintf(file
, "m%d", inst
->dst
.nr
);
6643 fprintf(file
, "(null)");
6646 fprintf(file
, "***u%d***", inst
->dst
.nr
);
6649 fprintf(file
, "***attr%d***", inst
->dst
.nr
);
6652 switch (inst
->dst
.nr
) {
6654 fprintf(file
, "null");
6656 case BRW_ARF_ADDRESS
:
6657 fprintf(file
, "a0.%d", inst
->dst
.subnr
);
6659 case BRW_ARF_ACCUMULATOR
:
6660 fprintf(file
, "acc%d", inst
->dst
.subnr
);
6663 fprintf(file
, "f%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
6666 fprintf(file
, "arf%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
6671 unreachable("not reached");
6674 if (inst
->dst
.offset
||
6675 (inst
->dst
.file
== VGRF
&&
6676 alloc
.sizes
[inst
->dst
.nr
] * REG_SIZE
!= inst
->size_written
)) {
6677 const unsigned reg_size
= (inst
->dst
.file
== UNIFORM
? 4 : REG_SIZE
);
6678 fprintf(file
, "+%d.%d", inst
->dst
.offset
/ reg_size
,
6679 inst
->dst
.offset
% reg_size
);
6682 if (inst
->dst
.stride
!= 1)
6683 fprintf(file
, "<%u>", inst
->dst
.stride
);
6684 fprintf(file
, ":%s, ", brw_reg_type_to_letters(inst
->dst
.type
));
6686 for (int i
= 0; i
< inst
->sources
; i
++) {
6687 if (inst
->src
[i
].negate
)
6689 if (inst
->src
[i
].abs
)
6691 switch (inst
->src
[i
].file
) {
6693 fprintf(file
, "vgrf%d", inst
->src
[i
].nr
);
6696 fprintf(file
, "g%d", inst
->src
[i
].nr
);
6699 fprintf(file
, "***m%d***", inst
->src
[i
].nr
);
6702 fprintf(file
, "attr%d", inst
->src
[i
].nr
);
6705 fprintf(file
, "u%d", inst
->src
[i
].nr
);
6708 fprintf(file
, "(null)");
6711 switch (inst
->src
[i
].type
) {
6712 case BRW_REGISTER_TYPE_F
:
6713 fprintf(file
, "%-gf", inst
->src
[i
].f
);
6715 case BRW_REGISTER_TYPE_DF
:
6716 fprintf(file
, "%fdf", inst
->src
[i
].df
);
6718 case BRW_REGISTER_TYPE_W
:
6719 case BRW_REGISTER_TYPE_D
:
6720 fprintf(file
, "%dd", inst
->src
[i
].d
);
6722 case BRW_REGISTER_TYPE_UW
:
6723 case BRW_REGISTER_TYPE_UD
:
6724 fprintf(file
, "%uu", inst
->src
[i
].ud
);
6726 case BRW_REGISTER_TYPE_Q
:
6727 fprintf(file
, "%" PRId64
"q", inst
->src
[i
].d64
);
6729 case BRW_REGISTER_TYPE_UQ
:
6730 fprintf(file
, "%" PRIu64
"uq", inst
->src
[i
].u64
);
6732 case BRW_REGISTER_TYPE_VF
:
6733 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
6734 brw_vf_to_float((inst
->src
[i
].ud
>> 0) & 0xff),
6735 brw_vf_to_float((inst
->src
[i
].ud
>> 8) & 0xff),
6736 brw_vf_to_float((inst
->src
[i
].ud
>> 16) & 0xff),
6737 brw_vf_to_float((inst
->src
[i
].ud
>> 24) & 0xff));
6739 case BRW_REGISTER_TYPE_V
:
6740 case BRW_REGISTER_TYPE_UV
:
6741 fprintf(file
, "%08x%s", inst
->src
[i
].ud
,
6742 inst
->src
[i
].type
== BRW_REGISTER_TYPE_V
? "V" : "UV");
6745 fprintf(file
, "???");
6750 switch (inst
->src
[i
].nr
) {
6752 fprintf(file
, "null");
6754 case BRW_ARF_ADDRESS
:
6755 fprintf(file
, "a0.%d", inst
->src
[i
].subnr
);
6757 case BRW_ARF_ACCUMULATOR
:
6758 fprintf(file
, "acc%d", inst
->src
[i
].subnr
);
6761 fprintf(file
, "f%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
6764 fprintf(file
, "arf%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
6770 if (inst
->src
[i
].offset
||
6771 (inst
->src
[i
].file
== VGRF
&&
6772 alloc
.sizes
[inst
->src
[i
].nr
] * REG_SIZE
!= inst
->size_read(i
))) {
6773 const unsigned reg_size
= (inst
->src
[i
].file
== UNIFORM
? 4 : REG_SIZE
);
6774 fprintf(file
, "+%d.%d", inst
->src
[i
].offset
/ reg_size
,
6775 inst
->src
[i
].offset
% reg_size
);
6778 if (inst
->src
[i
].abs
)
6781 if (inst
->src
[i
].file
!= IMM
) {
6783 if (inst
->src
[i
].file
== ARF
|| inst
->src
[i
].file
== FIXED_GRF
) {
6784 unsigned hstride
= inst
->src
[i
].hstride
;
6785 stride
= (hstride
== 0 ? 0 : (1 << (hstride
- 1)));
6787 stride
= inst
->src
[i
].stride
;
6790 fprintf(file
, "<%u>", stride
);
6792 fprintf(file
, ":%s", brw_reg_type_to_letters(inst
->src
[i
].type
));
6795 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
6796 fprintf(file
, ", ");
6801 if (inst
->force_writemask_all
)
6802 fprintf(file
, "NoMask ");
6804 if (inst
->exec_size
!= dispatch_width
)
6805 fprintf(file
, "group%d ", inst
->group
);
6807 fprintf(file
, "\n");
6811 fs_visitor::setup_fs_payload_gen6()
6813 assert(stage
== MESA_SHADER_FRAGMENT
);
6814 struct brw_wm_prog_data
*prog_data
= brw_wm_prog_data(this->prog_data
);
6815 const unsigned payload_width
= MIN2(16, dispatch_width
);
6816 assert(dispatch_width
% payload_width
== 0);
6817 assert(devinfo
->gen
>= 6);
6819 prog_data
->uses_src_depth
= prog_data
->uses_src_w
=
6820 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
6822 prog_data
->uses_sample_mask
=
6823 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
) != 0;
6825 /* From the Ivy Bridge PRM documentation for 3DSTATE_PS:
6827 * "MSDISPMODE_PERSAMPLE is required in order to select
6830 * So we can only really get sample positions if we are doing real
6831 * per-sample dispatch. If we need gl_SamplePosition and we don't have
6832 * persample dispatch, we hard-code it to 0.5.
6834 prog_data
->uses_pos_offset
= prog_data
->persample_dispatch
&&
6835 (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_POS
);
6837 /* R0: PS thread payload header. */
6840 for (unsigned j
= 0; j
< dispatch_width
/ payload_width
; j
++) {
6841 /* R1: masks, pixel X/Y coordinates. */
6842 payload
.subspan_coord_reg
[j
] = payload
.num_regs
++;
6845 for (unsigned j
= 0; j
< dispatch_width
/ payload_width
; j
++) {
6846 /* R3-26: barycentric interpolation coordinates. These appear in the
6847 * same order that they appear in the brw_barycentric_mode enum. Each
6848 * set of coordinates occupies 2 registers if dispatch width == 8 and 4
6849 * registers if dispatch width == 16. Coordinates only appear if they
6850 * were enabled using the "Barycentric Interpolation Mode" bits in
6853 for (int i
= 0; i
< BRW_BARYCENTRIC_MODE_COUNT
; ++i
) {
6854 if (prog_data
->barycentric_interp_modes
& (1 << i
)) {
6855 payload
.barycentric_coord_reg
[i
][j
] = payload
.num_regs
;
6856 payload
.num_regs
+= payload_width
/ 4;
6860 /* R27-28: interpolated depth if uses source depth */
6861 if (prog_data
->uses_src_depth
) {
6862 payload
.source_depth_reg
[j
] = payload
.num_regs
;
6863 payload
.num_regs
+= payload_width
/ 8;
6866 /* R29-30: interpolated W set if GEN6_WM_USES_SOURCE_W. */
6867 if (prog_data
->uses_src_w
) {
6868 payload
.source_w_reg
[j
] = payload
.num_regs
;
6869 payload
.num_regs
+= payload_width
/ 8;
6872 /* R31: MSAA position offsets. */
6873 if (prog_data
->uses_pos_offset
) {
6874 payload
.sample_pos_reg
[j
] = payload
.num_regs
;
6878 /* R32-33: MSAA input coverage mask */
6879 if (prog_data
->uses_sample_mask
) {
6880 assert(devinfo
->gen
>= 7);
6881 payload
.sample_mask_in_reg
[j
] = payload
.num_regs
;
6882 payload
.num_regs
+= payload_width
/ 8;
6886 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
6887 source_depth_to_render_target
= true;
6892 fs_visitor::setup_vs_payload()
6894 /* R0: thread header, R1: urb handles */
6895 payload
.num_regs
= 2;
6899 fs_visitor::setup_gs_payload()
6901 assert(stage
== MESA_SHADER_GEOMETRY
);
6903 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
6904 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
6906 /* R0: thread header, R1: output URB handles */
6907 payload
.num_regs
= 2;
6909 if (gs_prog_data
->include_primitive_id
) {
6910 /* R2: Primitive ID 0..7 */
6914 /* Always enable VUE handles so we can safely use pull model if needed.
6916 * The push model for a GS uses a ton of register space even for trivial
6917 * scenarios with just a few inputs, so just make things easier and a bit
6918 * safer by always having pull model available.
6920 gs_prog_data
->base
.include_vue_handles
= true;
6922 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
6923 payload
.num_regs
+= nir
->info
.gs
.vertices_in
;
6925 /* Use a maximum of 24 registers for push-model inputs. */
6926 const unsigned max_push_components
= 24;
6928 /* If pushing our inputs would take too many registers, reduce the URB read
6929 * length (which is in HWords, or 8 registers), and resort to pulling.
6931 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
6932 * have to multiply by VerticesIn to obtain the total storage requirement.
6934 if (8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
>
6935 max_push_components
) {
6936 vue_prog_data
->urb_read_length
=
6937 ROUND_DOWN_TO(max_push_components
/ nir
->info
.gs
.vertices_in
, 8) / 8;
6942 fs_visitor::setup_cs_payload()
6944 assert(devinfo
->gen
>= 7);
6945 payload
.num_regs
= 1;
6949 fs_visitor::calculate_register_pressure()
6951 invalidate_live_intervals();
6952 calculate_live_intervals();
6954 unsigned num_instructions
= 0;
6955 foreach_block(block
, cfg
)
6956 num_instructions
+= block
->instructions
.length();
6958 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
6960 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
6961 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
6962 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
6967 fs_visitor::optimize()
6969 /* Start by validating the shader we currently have. */
6972 /* bld is the common builder object pointing at the end of the program we
6973 * used to translate it into i965 IR. For the optimization and lowering
6974 * passes coming next, any code added after the end of the program without
6975 * having explicitly called fs_builder::at() clearly points at a mistake.
6976 * Ideally optimization passes wouldn't be part of the visitor so they
6977 * wouldn't have access to bld at all, but they do, so just in case some
6978 * pass forgets to ask for a location explicitly set it to NULL here to
6979 * make it trip. The dispatch width is initialized to a bogus value to
6980 * make sure that optimizations set the execution controls explicitly to
6981 * match the code they are manipulating instead of relying on the defaults.
6983 bld
= fs_builder(this, 64);
6985 assign_constant_locations();
6986 lower_constant_loads();
6990 split_virtual_grfs();
6993 #define OPT(pass, args...) ({ \
6995 bool this_progress = pass(args); \
6997 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
6998 char filename[64]; \
6999 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
7000 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
7002 backend_shader::dump_instructions(filename); \
7007 progress = progress || this_progress; \
7011 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
7013 snprintf(filename
, 64, "%s%d-%s-00-00-start",
7014 stage_abbrev
, dispatch_width
, nir
->info
.name
);
7016 backend_shader::dump_instructions(filename
);
7019 bool progress
= false;
7023 /* Before anything else, eliminate dead code. The results of some NIR
7024 * instructions may effectively be calculated twice. Once when the
7025 * instruction is encountered, and again when the user of that result is
7026 * encountered. Wipe those away before algebraic optimizations and
7027 * especially copy propagation can mix things up.
7029 OPT(dead_code_eliminate
);
7031 OPT(remove_extra_rounding_modes
);
7038 OPT(remove_duplicate_mrf_writes
);
7042 OPT(opt_copy_propagation
);
7043 OPT(opt_predicated_break
, this);
7044 OPT(opt_cmod_propagation
);
7045 OPT(dead_code_eliminate
);
7046 OPT(opt_peephole_sel
);
7047 OPT(dead_control_flow_eliminate
, this);
7048 OPT(opt_register_renaming
);
7049 OPT(opt_saturate_propagation
);
7050 OPT(register_coalesce
);
7051 OPT(compute_to_mrf
);
7052 OPT(eliminate_find_live_channel
);
7054 OPT(compact_virtual_grfs
);
7057 /* Do this after cmod propagation has had every possible opportunity to
7058 * propagate results into SEL instructions.
7060 if (OPT(opt_peephole_csel
))
7061 OPT(dead_code_eliminate
);
7066 if (OPT(lower_pack
)) {
7067 OPT(register_coalesce
);
7068 OPT(dead_code_eliminate
);
7071 OPT(lower_simd_width
);
7073 /* After SIMD lowering just in case we had to unroll the EOT send. */
7074 OPT(opt_sampler_eot
);
7076 OPT(lower_logical_sends
);
7079 OPT(opt_copy_propagation
);
7080 /* Only run after logical send lowering because it's easier to implement
7081 * in terms of physical sends.
7083 if (OPT(opt_zero_samples
))
7084 OPT(opt_copy_propagation
);
7085 /* Run after logical send lowering to give it a chance to CSE the
7086 * LOAD_PAYLOAD instructions created to construct the payloads of
7087 * e.g. texturing messages in cases where it wasn't possible to CSE the
7088 * whole logical instruction.
7091 OPT(register_coalesce
);
7092 OPT(compute_to_mrf
);
7093 OPT(dead_code_eliminate
);
7094 OPT(remove_duplicate_mrf_writes
);
7095 OPT(opt_peephole_sel
);
7098 OPT(opt_redundant_discard_jumps
);
7100 if (OPT(lower_load_payload
)) {
7101 split_virtual_grfs();
7102 OPT(register_coalesce
);
7103 OPT(lower_simd_width
);
7104 OPT(compute_to_mrf
);
7105 OPT(dead_code_eliminate
);
7108 OPT(opt_combine_constants
);
7109 OPT(lower_integer_multiplication
);
7111 if (devinfo
->gen
<= 5 && OPT(lower_minmax
)) {
7112 OPT(opt_cmod_propagation
);
7114 OPT(opt_copy_propagation
);
7115 OPT(dead_code_eliminate
);
7118 if (OPT(lower_regioning
)) {
7119 OPT(opt_copy_propagation
);
7120 OPT(dead_code_eliminate
);
7121 OPT(lower_simd_width
);
7124 OPT(fixup_sends_duplicate_payload
);
7126 lower_uniform_pull_constant_loads();
7132 * From the Skylake PRM Vol. 2a docs for sends:
7134 * "It is required that the second block of GRFs does not overlap with the
7137 * There are plenty of cases where we may accidentally violate this due to
7138 * having, for instance, both sources be the constant 0. This little pass
7139 * just adds a new vgrf for the second payload and copies it over.
7142 fs_visitor::fixup_sends_duplicate_payload()
7144 bool progress
= false;
7146 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
7147 if (inst
->opcode
== SHADER_OPCODE_SEND
&& inst
->ex_mlen
> 0 &&
7148 regions_overlap(inst
->src
[2], inst
->mlen
* REG_SIZE
,
7149 inst
->src
[3], inst
->ex_mlen
* REG_SIZE
)) {
7150 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(inst
->ex_mlen
),
7151 BRW_REGISTER_TYPE_UD
);
7152 /* Sadly, we've lost all notion of channels and bit sizes at this
7153 * point. Just WE_all it.
7155 const fs_builder ibld
= bld
.at(block
, inst
).exec_all().group(16, 0);
7156 fs_reg copy_src
= retype(inst
->src
[3], BRW_REGISTER_TYPE_UD
);
7157 fs_reg copy_dst
= tmp
;
7158 for (unsigned i
= 0; i
< inst
->ex_mlen
; i
+= 2) {
7159 if (inst
->ex_mlen
== i
+ 1) {
7160 /* Only one register left; do SIMD8 */
7161 ibld
.group(8, 0).MOV(copy_dst
, copy_src
);
7163 ibld
.MOV(copy_dst
, copy_src
);
7165 copy_src
= offset(copy_src
, ibld
, 1);
7166 copy_dst
= offset(copy_dst
, ibld
, 1);
7174 invalidate_live_intervals();
7180 * Three source instruction must have a GRF/MRF destination register.
7181 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
7184 fs_visitor::fixup_3src_null_dest()
7186 bool progress
= false;
7188 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
7189 if (inst
->is_3src(devinfo
) && inst
->dst
.is_null()) {
7190 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
7197 invalidate_live_intervals();
7201 fs_visitor::allocate_registers(unsigned min_dispatch_width
, bool allow_spilling
)
7205 static const enum instruction_scheduler_mode pre_modes
[] = {
7207 SCHEDULE_PRE_NON_LIFO
,
7211 bool spill_all
= allow_spilling
&& (INTEL_DEBUG
& DEBUG_SPILL_FS
);
7213 /* Try each scheduling heuristic to see if it can successfully register
7214 * allocate without spilling. They should be ordered by decreasing
7215 * performance but increasing likelihood of allocating.
7217 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
7218 schedule_instructions(pre_modes
[i
]);
7221 assign_regs_trivial();
7226 /* We only allow spilling for the last schedule mode and only if the
7227 * allow_spilling parameter and dispatch width work out ok.
7229 bool can_spill
= allow_spilling
&&
7230 (i
== ARRAY_SIZE(pre_modes
) - 1) &&
7231 dispatch_width
== min_dispatch_width
;
7233 /* We should only spill registers on the last scheduling. */
7234 assert(!spilled_any_registers
);
7236 allocated
= assign_regs(can_spill
, spill_all
);
7242 if (!allow_spilling
)
7243 fail("Failure to register allocate and spilling is not allowed.");
7245 /* We assume that any spilling is worse than just dropping back to
7246 * SIMD8. There's probably actually some intermediate point where
7247 * SIMD16 with a couple of spills is still better.
7249 if (dispatch_width
> min_dispatch_width
) {
7250 fail("Failure to register allocate. Reduce number of "
7251 "live scalar values to avoid this.");
7254 /* If we failed to allocate, we must have a reason */
7256 } else if (spilled_any_registers
) {
7257 compiler
->shader_perf_log(log_data
,
7258 "%s shader triggered register spilling. "
7259 "Try reducing the number of live scalar "
7260 "values to improve performance.\n",
7264 /* This must come after all optimization and register allocation, since
7265 * it inserts dead code that happens to have side effects, and it does
7266 * so based on the actual physical registers in use.
7268 insert_gen4_send_dependency_workarounds();
7273 opt_bank_conflicts();
7275 schedule_instructions(SCHEDULE_POST
);
7277 if (last_scratch
> 0) {
7278 MAYBE_UNUSED
unsigned max_scratch_size
= 2 * 1024 * 1024;
7280 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
7282 if (stage
== MESA_SHADER_COMPUTE
) {
7283 if (devinfo
->is_haswell
) {
7284 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7285 * field documentation, Haswell supports a minimum of 2kB of
7286 * scratch space for compute shaders, unlike every other stage
7289 prog_data
->total_scratch
= MAX2(prog_data
->total_scratch
, 2048);
7290 } else if (devinfo
->gen
<= 7) {
7291 /* According to the MEDIA_VFE_STATE's "Per Thread Scratch Space"
7292 * field documentation, platforms prior to Haswell measure scratch
7293 * size linearly with a range of [1kB, 12kB] and 1kB granularity.
7295 prog_data
->total_scratch
= ALIGN(last_scratch
, 1024);
7296 max_scratch_size
= 12 * 1024;
7300 /* We currently only support up to 2MB of scratch space. If we
7301 * need to support more eventually, the documentation suggests
7302 * that we could allocate a larger buffer, and partition it out
7303 * ourselves. We'd just have to undo the hardware's address
7304 * calculation by subtracting (FFTID * Per Thread Scratch Space)
7305 * and then add FFTID * (Larger Per Thread Scratch Space).
7307 * See 3D-Media-GPGPU Engine > Media GPGPU Pipeline >
7308 * Thread Group Tracking > Local Memory/Scratch Space.
7310 assert(prog_data
->total_scratch
< max_scratch_size
);
7315 fs_visitor::run_vs()
7317 assert(stage
== MESA_SHADER_VERTEX
);
7321 if (shader_time_index
>= 0)
7322 emit_shader_time_begin();
7329 compute_clip_distance();
7333 if (shader_time_index
>= 0)
7334 emit_shader_time_end();
7340 assign_curb_setup();
7341 assign_vs_urb_setup();
7343 fixup_3src_null_dest();
7344 allocate_registers(8, true);
7350 fs_visitor::set_tcs_invocation_id()
7352 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
7353 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
7355 const unsigned instance_id_mask
=
7356 devinfo
->gen
>= 11 ? INTEL_MASK(22, 16) : INTEL_MASK(23, 17);
7357 const unsigned instance_id_shift
=
7358 devinfo
->gen
>= 11 ? 16 : 17;
7360 /* Get instance number from g0.2 bits 22:16 or 23:17 */
7361 fs_reg t
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7362 bld
.AND(t
, fs_reg(retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
)),
7363 brw_imm_ud(instance_id_mask
));
7365 invocation_id
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7367 if (vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
) {
7368 /* gl_InvocationID is just the thread number */
7369 bld
.SHR(invocation_id
, t
, brw_imm_ud(instance_id_shift
));
7373 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
);
7375 fs_reg channels_uw
= bld
.vgrf(BRW_REGISTER_TYPE_UW
);
7376 fs_reg channels_ud
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7377 bld
.MOV(channels_uw
, fs_reg(brw_imm_uv(0x76543210)));
7378 bld
.MOV(channels_ud
, channels_uw
);
7380 if (tcs_prog_data
->instances
== 1) {
7381 invocation_id
= channels_ud
;
7383 fs_reg instance_times_8
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
7384 bld
.SHR(instance_times_8
, t
, brw_imm_ud(instance_id_shift
- 3));
7385 bld
.ADD(invocation_id
, instance_times_8
, channels_ud
);
7390 fs_visitor::run_tcs()
7392 assert(stage
== MESA_SHADER_TESS_CTRL
);
7394 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
7395 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
7396 struct brw_tcs_prog_key
*tcs_key
= (struct brw_tcs_prog_key
*) key
;
7398 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
||
7399 vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
);
7401 if (vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
) {
7402 /* r1-r4 contain the ICP handles. */
7403 payload
.num_regs
= 5;
7405 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
);
7406 assert(tcs_key
->input_vertices
> 0);
7407 /* r1 contains output handles, r2 may contain primitive ID, then the
7408 * ICP handles occupy the next 1-32 registers.
7410 payload
.num_regs
= 2 + tcs_prog_data
->include_primitive_id
+
7411 tcs_key
->input_vertices
;
7414 if (shader_time_index
>= 0)
7415 emit_shader_time_begin();
7417 /* Initialize gl_InvocationID */
7418 set_tcs_invocation_id();
7420 const bool fix_dispatch_mask
=
7421 vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
&&
7422 (nir
->info
.tess
.tcs_vertices_out
% 8) != 0;
7424 /* Fix the disptach mask */
7425 if (fix_dispatch_mask
) {
7426 bld
.CMP(bld
.null_reg_ud(), invocation_id
,
7427 brw_imm_ud(nir
->info
.tess
.tcs_vertices_out
), BRW_CONDITIONAL_L
);
7428 bld
.IF(BRW_PREDICATE_NORMAL
);
7433 if (fix_dispatch_mask
) {
7434 bld
.emit(BRW_OPCODE_ENDIF
);
7437 /* Emit EOT write; set TR DS Cache bit */
7439 fs_reg(get_tcs_output_urb_handle()),
7440 fs_reg(brw_imm_ud(WRITEMASK_X
<< 16)),
7441 fs_reg(brw_imm_ud(0)),
7443 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 3);
7444 bld
.LOAD_PAYLOAD(payload
, srcs
, 3, 2);
7446 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
,
7447 bld
.null_reg_ud(), payload
);
7451 if (shader_time_index
>= 0)
7452 emit_shader_time_end();
7461 assign_curb_setup();
7462 assign_tcs_urb_setup();
7464 fixup_3src_null_dest();
7465 allocate_registers(8, true);
7471 fs_visitor::run_tes()
7473 assert(stage
== MESA_SHADER_TESS_EVAL
);
7475 /* R0: thread header, R1-3: gl_TessCoord.xyz, R4: URB handles */
7476 payload
.num_regs
= 5;
7478 if (shader_time_index
>= 0)
7479 emit_shader_time_begin();
7488 if (shader_time_index
>= 0)
7489 emit_shader_time_end();
7495 assign_curb_setup();
7496 assign_tes_urb_setup();
7498 fixup_3src_null_dest();
7499 allocate_registers(8, true);
7505 fs_visitor::run_gs()
7507 assert(stage
== MESA_SHADER_GEOMETRY
);
7511 this->final_gs_vertex_count
= vgrf(glsl_type::uint_type
);
7513 if (gs_compile
->control_data_header_size_bits
> 0) {
7514 /* Create a VGRF to store accumulated control data bits. */
7515 this->control_data_bits
= vgrf(glsl_type::uint_type
);
7517 /* If we're outputting more than 32 control data bits, then EmitVertex()
7518 * will set control_data_bits to 0 after emitting the first vertex.
7519 * Otherwise, we need to initialize it to 0 here.
7521 if (gs_compile
->control_data_header_size_bits
<= 32) {
7522 const fs_builder abld
= bld
.annotate("initialize control data bits");
7523 abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
7527 if (shader_time_index
>= 0)
7528 emit_shader_time_begin();
7532 emit_gs_thread_end();
7534 if (shader_time_index
>= 0)
7535 emit_shader_time_end();
7544 assign_curb_setup();
7545 assign_gs_urb_setup();
7547 fixup_3src_null_dest();
7548 allocate_registers(8, true);
7553 /* From the SKL PRM, Volume 16, Workarounds:
7555 * 0877 3D Pixel Shader Hang possible when pixel shader dispatched with
7556 * only header phases (R0-R2)
7558 * WA: Enable a non-header phase (e.g. push constant) when dispatch would
7559 * have been header only.
7561 * Instead of enabling push constants one can alternatively enable one of the
7562 * inputs. Here one simply chooses "layer" which shouldn't impose much
7566 gen9_ps_header_only_workaround(struct brw_wm_prog_data
*wm_prog_data
)
7568 if (wm_prog_data
->num_varying_inputs
)
7571 if (wm_prog_data
->base
.curb_read_length
)
7574 wm_prog_data
->urb_setup
[VARYING_SLOT_LAYER
] = 0;
7575 wm_prog_data
->num_varying_inputs
= 1;
7579 fs_visitor::run_fs(bool allow_spilling
, bool do_rep_send
)
7581 struct brw_wm_prog_data
*wm_prog_data
= brw_wm_prog_data(this->prog_data
);
7582 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
7584 assert(stage
== MESA_SHADER_FRAGMENT
);
7586 if (devinfo
->gen
>= 6)
7587 setup_fs_payload_gen6();
7589 setup_fs_payload_gen4();
7593 } else if (do_rep_send
) {
7594 assert(dispatch_width
== 16);
7595 emit_repclear_shader();
7597 if (shader_time_index
>= 0)
7598 emit_shader_time_begin();
7600 calculate_urb_setup();
7601 if (nir
->info
.inputs_read
> 0 ||
7602 (nir
->info
.outputs_read
> 0 && !wm_key
->coherent_fb_fetch
)) {
7603 if (devinfo
->gen
< 6)
7604 emit_interpolation_setup_gen4();
7606 emit_interpolation_setup_gen6();
7609 /* We handle discards by keeping track of the still-live pixels in f0.1.
7610 * Initialize it with the dispatched pixels.
7612 if (wm_prog_data
->uses_kill
) {
7613 const fs_reg dispatch_mask
=
7614 devinfo
->gen
>= 6 ? brw_vec1_grf(1, 7) : brw_vec1_grf(0, 0);
7615 bld
.exec_all().group(1, 0)
7616 .MOV(retype(brw_flag_reg(0, 1), BRW_REGISTER_TYPE_UW
),
7617 retype(dispatch_mask
, BRW_REGISTER_TYPE_UW
));
7625 if (wm_prog_data
->uses_kill
)
7626 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
7628 if (wm_key
->alpha_test_func
)
7633 if (shader_time_index
>= 0)
7634 emit_shader_time_end();
7640 assign_curb_setup();
7642 if (devinfo
->gen
>= 9)
7643 gen9_ps_header_only_workaround(wm_prog_data
);
7647 fixup_3src_null_dest();
7648 allocate_registers(8, allow_spilling
);
7658 fs_visitor::run_cs(unsigned min_dispatch_width
)
7660 assert(stage
== MESA_SHADER_COMPUTE
);
7661 assert(dispatch_width
>= min_dispatch_width
);
7665 if (shader_time_index
>= 0)
7666 emit_shader_time_begin();
7668 if (devinfo
->is_haswell
&& prog_data
->total_shared
> 0) {
7669 /* Move SLM index from g0.0[27:24] to sr0.1[11:8] */
7670 const fs_builder abld
= bld
.exec_all().group(1, 0);
7671 abld
.MOV(retype(brw_sr0_reg(1), BRW_REGISTER_TYPE_UW
),
7672 suboffset(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UW
), 1));
7680 emit_cs_terminate();
7682 if (shader_time_index
>= 0)
7683 emit_shader_time_end();
7689 assign_curb_setup();
7691 fixup_3src_null_dest();
7692 allocate_registers(min_dispatch_width
, true);
7701 is_used_in_not_interp_frag_coord(nir_ssa_def
*def
)
7703 nir_foreach_use(src
, def
) {
7704 if (src
->parent_instr
->type
!= nir_instr_type_intrinsic
)
7707 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(src
->parent_instr
);
7708 if (intrin
->intrinsic
!= nir_intrinsic_load_interpolated_input
)
7711 if (nir_intrinsic_base(intrin
) != VARYING_SLOT_POS
)
7715 nir_foreach_if_use(src
, def
)
7722 * Return a bitfield where bit n is set if barycentric interpolation mode n
7723 * (see enum brw_barycentric_mode) is needed by the fragment shader.
7725 * We examine the load_barycentric intrinsics rather than looking at input
7726 * variables so that we catch interpolateAtCentroid() messages too, which
7727 * also need the BRW_BARYCENTRIC_[NON]PERSPECTIVE_CENTROID mode set up.
7730 brw_compute_barycentric_interp_modes(const struct gen_device_info
*devinfo
,
7731 const nir_shader
*shader
)
7733 unsigned barycentric_interp_modes
= 0;
7735 nir_foreach_function(f
, shader
) {
7739 nir_foreach_block(block
, f
->impl
) {
7740 nir_foreach_instr(instr
, block
) {
7741 if (instr
->type
!= nir_instr_type_intrinsic
)
7744 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
7745 switch (intrin
->intrinsic
) {
7746 case nir_intrinsic_load_barycentric_pixel
:
7747 case nir_intrinsic_load_barycentric_centroid
:
7748 case nir_intrinsic_load_barycentric_sample
:
7754 /* Ignore WPOS; it doesn't require interpolation. */
7755 assert(intrin
->dest
.is_ssa
);
7756 if (!is_used_in_not_interp_frag_coord(&intrin
->dest
.ssa
))
7759 enum glsl_interp_mode interp
= (enum glsl_interp_mode
)
7760 nir_intrinsic_interp_mode(intrin
);
7761 nir_intrinsic_op bary_op
= intrin
->intrinsic
;
7762 enum brw_barycentric_mode bary
=
7763 brw_barycentric_mode(interp
, bary_op
);
7765 barycentric_interp_modes
|= 1 << bary
;
7767 if (devinfo
->needs_unlit_centroid_workaround
&&
7768 bary_op
== nir_intrinsic_load_barycentric_centroid
)
7769 barycentric_interp_modes
|= 1 << centroid_to_pixel(bary
);
7774 return barycentric_interp_modes
;
7778 brw_compute_flat_inputs(struct brw_wm_prog_data
*prog_data
,
7779 const nir_shader
*shader
)
7781 prog_data
->flat_inputs
= 0;
7783 nir_foreach_variable(var
, &shader
->inputs
) {
7784 unsigned slots
= glsl_count_attribute_slots(var
->type
, false);
7785 for (unsigned s
= 0; s
< slots
; s
++) {
7786 int input_index
= prog_data
->urb_setup
[var
->data
.location
+ s
];
7788 if (input_index
< 0)
7792 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
7793 prog_data
->flat_inputs
|= 1 << input_index
;
7799 computed_depth_mode(const nir_shader
*shader
)
7801 if (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
7802 switch (shader
->info
.fs
.depth_layout
) {
7803 case FRAG_DEPTH_LAYOUT_NONE
:
7804 case FRAG_DEPTH_LAYOUT_ANY
:
7805 return BRW_PSCDEPTH_ON
;
7806 case FRAG_DEPTH_LAYOUT_GREATER
:
7807 return BRW_PSCDEPTH_ON_GE
;
7808 case FRAG_DEPTH_LAYOUT_LESS
:
7809 return BRW_PSCDEPTH_ON_LE
;
7810 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
7811 return BRW_PSCDEPTH_OFF
;
7814 return BRW_PSCDEPTH_OFF
;
7818 * Move load_interpolated_input with simple (payload-based) barycentric modes
7819 * to the top of the program so we don't emit multiple PLNs for the same input.
7821 * This works around CSE not being able to handle non-dominating cases
7827 * interpolate the same exact input
7830 * This should be replaced by global value numbering someday.
7833 move_interpolation_to_top(nir_shader
*nir
)
7835 bool progress
= false;
7837 nir_foreach_function(f
, nir
) {
7841 nir_block
*top
= nir_start_block(f
->impl
);
7842 exec_node
*cursor_node
= NULL
;
7844 nir_foreach_block(block
, f
->impl
) {
7848 nir_foreach_instr_safe(instr
, block
) {
7849 if (instr
->type
!= nir_instr_type_intrinsic
)
7852 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
7853 if (intrin
->intrinsic
!= nir_intrinsic_load_interpolated_input
)
7855 nir_intrinsic_instr
*bary_intrinsic
=
7856 nir_instr_as_intrinsic(intrin
->src
[0].ssa
->parent_instr
);
7857 nir_intrinsic_op op
= bary_intrinsic
->intrinsic
;
7859 /* Leave interpolateAtSample/Offset() where they are. */
7860 if (op
== nir_intrinsic_load_barycentric_at_sample
||
7861 op
== nir_intrinsic_load_barycentric_at_offset
)
7864 nir_instr
*move
[3] = {
7865 &bary_intrinsic
->instr
,
7866 intrin
->src
[1].ssa
->parent_instr
,
7870 for (unsigned i
= 0; i
< ARRAY_SIZE(move
); i
++) {
7871 if (move
[i
]->block
!= top
) {
7872 move
[i
]->block
= top
;
7873 exec_node_remove(&move
[i
]->node
);
7875 exec_node_insert_after(cursor_node
, &move
[i
]->node
);
7877 exec_list_push_head(&top
->instr_list
, &move
[i
]->node
);
7879 cursor_node
= &move
[i
]->node
;
7885 nir_metadata_preserve(f
->impl
, (nir_metadata
)
7886 ((unsigned) nir_metadata_block_index
|
7887 (unsigned) nir_metadata_dominance
));
7894 * Demote per-sample barycentric intrinsics to centroid.
7896 * Useful when rendering to a non-multisampled buffer.
7899 demote_sample_qualifiers(nir_shader
*nir
)
7901 bool progress
= true;
7903 nir_foreach_function(f
, nir
) {
7908 nir_builder_init(&b
, f
->impl
);
7910 nir_foreach_block(block
, f
->impl
) {
7911 nir_foreach_instr_safe(instr
, block
) {
7912 if (instr
->type
!= nir_instr_type_intrinsic
)
7915 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
7916 if (intrin
->intrinsic
!= nir_intrinsic_load_barycentric_sample
&&
7917 intrin
->intrinsic
!= nir_intrinsic_load_barycentric_at_sample
)
7920 b
.cursor
= nir_before_instr(instr
);
7921 nir_ssa_def
*centroid
=
7922 nir_load_barycentric(&b
, nir_intrinsic_load_barycentric_centroid
,
7923 nir_intrinsic_interp_mode(intrin
));
7924 nir_ssa_def_rewrite_uses(&intrin
->dest
.ssa
,
7925 nir_src_for_ssa(centroid
));
7926 nir_instr_remove(instr
);
7931 nir_metadata_preserve(f
->impl
, (nir_metadata
)
7932 ((unsigned) nir_metadata_block_index
|
7933 (unsigned) nir_metadata_dominance
));
7940 * Pre-gen6, the register file of the EUs was shared between threads,
7941 * and each thread used some subset allocated on a 16-register block
7942 * granularity. The unit states wanted these block counts.
7945 brw_register_blocks(int reg_count
)
7947 return ALIGN(reg_count
, 16) / 16 - 1;
7951 brw_compile_fs(const struct brw_compiler
*compiler
, void *log_data
,
7953 const struct brw_wm_prog_key
*key
,
7954 struct brw_wm_prog_data
*prog_data
,
7956 struct gl_program
*prog
,
7957 int shader_time_index8
, int shader_time_index16
,
7958 int shader_time_index32
, bool allow_spilling
,
7959 bool use_rep_send
, struct brw_vue_map
*vue_map
,
7962 const struct gen_device_info
*devinfo
= compiler
->devinfo
;
7964 brw_nir_apply_sampler_key(shader
, compiler
, &key
->tex
, true);
7965 brw_nir_lower_fs_inputs(shader
, devinfo
, key
);
7966 brw_nir_lower_fs_outputs(shader
);
7968 if (devinfo
->gen
< 6)
7969 brw_setup_vue_interpolation(vue_map
, shader
, prog_data
);
7971 if (!key
->multisample_fbo
)
7972 NIR_PASS_V(shader
, demote_sample_qualifiers
);
7973 NIR_PASS_V(shader
, move_interpolation_to_top
);
7974 brw_postprocess_nir(shader
, compiler
, true);
7976 /* key->alpha_test_func means simulating alpha testing via discards,
7977 * so the shader definitely kills pixels.
7979 prog_data
->uses_kill
= shader
->info
.fs
.uses_discard
||
7980 key
->alpha_test_func
;
7981 prog_data
->uses_omask
= key
->multisample_fbo
&&
7982 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
7983 prog_data
->computed_depth_mode
= computed_depth_mode(shader
);
7984 prog_data
->computed_stencil
=
7985 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
7987 prog_data
->persample_dispatch
=
7988 key
->multisample_fbo
&&
7989 (key
->persample_interp
||
7990 (shader
->info
.system_values_read
& (SYSTEM_BIT_SAMPLE_ID
|
7991 SYSTEM_BIT_SAMPLE_POS
)) ||
7992 shader
->info
.fs
.uses_sample_qualifier
||
7993 shader
->info
.outputs_read
);
7995 prog_data
->has_render_target_reads
= shader
->info
.outputs_read
!= 0ull;
7997 prog_data
->early_fragment_tests
= shader
->info
.fs
.early_fragment_tests
;
7998 prog_data
->post_depth_coverage
= shader
->info
.fs
.post_depth_coverage
;
7999 prog_data
->inner_coverage
= shader
->info
.fs
.inner_coverage
;
8001 prog_data
->barycentric_interp_modes
=
8002 brw_compute_barycentric_interp_modes(compiler
->devinfo
, shader
);
8004 cfg_t
*simd8_cfg
= NULL
, *simd16_cfg
= NULL
, *simd32_cfg
= NULL
;
8006 fs_visitor
v8(compiler
, log_data
, mem_ctx
, key
,
8007 &prog_data
->base
, prog
, shader
, 8,
8008 shader_time_index8
);
8009 if (!v8
.run_fs(allow_spilling
, false /* do_rep_send */)) {
8011 *error_str
= ralloc_strdup(mem_ctx
, v8
.fail_msg
);
8014 } else if (likely(!(INTEL_DEBUG
& DEBUG_NO8
))) {
8016 prog_data
->base
.dispatch_grf_start_reg
= v8
.payload
.num_regs
;
8017 prog_data
->reg_blocks_8
= brw_register_blocks(v8
.grf_used
);
8020 if (v8
.max_dispatch_width
>= 16 &&
8021 likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
8022 /* Try a SIMD16 compile */
8023 fs_visitor
v16(compiler
, log_data
, mem_ctx
, key
,
8024 &prog_data
->base
, prog
, shader
, 16,
8025 shader_time_index16
);
8026 v16
.import_uniforms(&v8
);
8027 if (!v16
.run_fs(allow_spilling
, use_rep_send
)) {
8028 compiler
->shader_perf_log(log_data
,
8029 "SIMD16 shader failed to compile: %s",
8032 simd16_cfg
= v16
.cfg
;
8033 prog_data
->dispatch_grf_start_reg_16
= v16
.payload
.num_regs
;
8034 prog_data
->reg_blocks_16
= brw_register_blocks(v16
.grf_used
);
8038 /* Currently, the compiler only supports SIMD32 on SNB+ */
8039 if (v8
.max_dispatch_width
>= 32 && !use_rep_send
&&
8040 compiler
->devinfo
->gen
>= 6 &&
8041 unlikely(INTEL_DEBUG
& DEBUG_DO32
)) {
8042 /* Try a SIMD32 compile */
8043 fs_visitor
v32(compiler
, log_data
, mem_ctx
, key
,
8044 &prog_data
->base
, prog
, shader
, 32,
8045 shader_time_index32
);
8046 v32
.import_uniforms(&v8
);
8047 if (!v32
.run_fs(allow_spilling
, false)) {
8048 compiler
->shader_perf_log(log_data
,
8049 "SIMD32 shader failed to compile: %s",
8052 simd32_cfg
= v32
.cfg
;
8053 prog_data
->dispatch_grf_start_reg_32
= v32
.payload
.num_regs
;
8054 prog_data
->reg_blocks_32
= brw_register_blocks(v32
.grf_used
);
8058 /* When the caller requests a repclear shader, they want SIMD16-only */
8062 /* Prior to Iron Lake, the PS had a single shader offset with a jump table
8063 * at the top to select the shader. We've never implemented that.
8064 * Instead, we just give them exactly one shader and we pick the widest one
8067 if (compiler
->devinfo
->gen
< 5) {
8068 if (simd32_cfg
|| simd16_cfg
)
8074 /* If computed depth is enabled SNB only allows SIMD8. */
8075 if (compiler
->devinfo
->gen
== 6 &&
8076 prog_data
->computed_depth_mode
!= BRW_PSCDEPTH_OFF
)
8077 assert(simd16_cfg
== NULL
&& simd32_cfg
== NULL
);
8079 if (compiler
->devinfo
->gen
<= 5 && !simd8_cfg
) {
8080 /* Iron lake and earlier only have one Dispatch GRF start field. Make
8081 * the data available in the base prog data struct for convenience.
8084 prog_data
->base
.dispatch_grf_start_reg
=
8085 prog_data
->dispatch_grf_start_reg_16
;
8086 } else if (simd32_cfg
) {
8087 prog_data
->base
.dispatch_grf_start_reg
=
8088 prog_data
->dispatch_grf_start_reg_32
;
8092 if (prog_data
->persample_dispatch
) {
8093 /* Starting with SandyBridge (where we first get MSAA), the different
8094 * pixel dispatch combinations are grouped into classifications A
8095 * through F (SNB PRM Vol. 2 Part 1 Section 7.7.1). On all hardware
8096 * generations, the only configurations supporting persample dispatch
8097 * are are this in which only one dispatch width is enabled.
8099 if (simd32_cfg
|| simd16_cfg
)
8105 /* We have to compute the flat inputs after the visitor is finished running
8106 * because it relies on prog_data->urb_setup which is computed in
8107 * fs_visitor::calculate_urb_setup().
8109 brw_compute_flat_inputs(prog_data
, shader
);
8111 fs_generator
g(compiler
, log_data
, mem_ctx
, &prog_data
->base
,
8112 v8
.promoted_constants
, v8
.runtime_check_aads_emit
,
8113 MESA_SHADER_FRAGMENT
);
8115 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
8116 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
8117 shader
->info
.label
?
8118 shader
->info
.label
: "unnamed",
8119 shader
->info
.name
));
8123 prog_data
->dispatch_8
= true;
8124 g
.generate_code(simd8_cfg
, 8);
8128 prog_data
->dispatch_16
= true;
8129 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
8133 prog_data
->dispatch_32
= true;
8134 prog_data
->prog_offset_32
= g
.generate_code(simd32_cfg
, 32);
8137 return g
.get_assembly();
8141 fs_visitor::emit_cs_work_group_id_setup()
8143 assert(stage
== MESA_SHADER_COMPUTE
);
8145 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
8147 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
8148 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
8149 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
8151 bld
.MOV(*reg
, r0_1
);
8152 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
8153 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
8159 fill_push_const_block_info(struct brw_push_const_block
*block
, unsigned dwords
)
8161 block
->dwords
= dwords
;
8162 block
->regs
= DIV_ROUND_UP(dwords
, 8);
8163 block
->size
= block
->regs
* 32;
8167 cs_fill_push_const_info(const struct gen_device_info
*devinfo
,
8168 struct brw_cs_prog_data
*cs_prog_data
)
8170 const struct brw_stage_prog_data
*prog_data
= &cs_prog_data
->base
;
8171 int subgroup_id_index
= get_subgroup_id_param_index(prog_data
);
8172 bool cross_thread_supported
= devinfo
->gen
> 7 || devinfo
->is_haswell
;
8174 /* The thread ID should be stored in the last param dword */
8175 assert(subgroup_id_index
== -1 ||
8176 subgroup_id_index
== (int)prog_data
->nr_params
- 1);
8178 unsigned cross_thread_dwords
, per_thread_dwords
;
8179 if (!cross_thread_supported
) {
8180 cross_thread_dwords
= 0u;
8181 per_thread_dwords
= prog_data
->nr_params
;
8182 } else if (subgroup_id_index
>= 0) {
8183 /* Fill all but the last register with cross-thread payload */
8184 cross_thread_dwords
= 8 * (subgroup_id_index
/ 8);
8185 per_thread_dwords
= prog_data
->nr_params
- cross_thread_dwords
;
8186 assert(per_thread_dwords
> 0 && per_thread_dwords
<= 8);
8188 /* Fill all data using cross-thread payload */
8189 cross_thread_dwords
= prog_data
->nr_params
;
8190 per_thread_dwords
= 0u;
8193 fill_push_const_block_info(&cs_prog_data
->push
.cross_thread
, cross_thread_dwords
);
8194 fill_push_const_block_info(&cs_prog_data
->push
.per_thread
, per_thread_dwords
);
8196 unsigned total_dwords
=
8197 (cs_prog_data
->push
.per_thread
.size
* cs_prog_data
->threads
+
8198 cs_prog_data
->push
.cross_thread
.size
) / 4;
8199 fill_push_const_block_info(&cs_prog_data
->push
.total
, total_dwords
);
8201 assert(cs_prog_data
->push
.cross_thread
.dwords
% 8 == 0 ||
8202 cs_prog_data
->push
.per_thread
.size
== 0);
8203 assert(cs_prog_data
->push
.cross_thread
.dwords
+
8204 cs_prog_data
->push
.per_thread
.dwords
==
8205 prog_data
->nr_params
);
8209 cs_set_simd_size(struct brw_cs_prog_data
*cs_prog_data
, unsigned size
)
8211 cs_prog_data
->simd_size
= size
;
8212 unsigned group_size
= cs_prog_data
->local_size
[0] *
8213 cs_prog_data
->local_size
[1] * cs_prog_data
->local_size
[2];
8214 cs_prog_data
->threads
= (group_size
+ size
- 1) / size
;
8218 compile_cs_to_nir(const struct brw_compiler
*compiler
,
8220 const struct brw_cs_prog_key
*key
,
8221 const nir_shader
*src_shader
,
8222 unsigned dispatch_width
)
8224 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
8225 brw_nir_apply_sampler_key(shader
, compiler
, &key
->tex
, true);
8227 NIR_PASS_V(shader
, brw_nir_lower_cs_intrinsics
, dispatch_width
);
8229 /* Clean up after the local index and ID calculations. */
8230 NIR_PASS_V(shader
, nir_opt_constant_folding
);
8231 NIR_PASS_V(shader
, nir_opt_dce
);
8233 brw_postprocess_nir(shader
, compiler
, true);
8239 brw_compile_cs(const struct brw_compiler
*compiler
, void *log_data
,
8241 const struct brw_cs_prog_key
*key
,
8242 struct brw_cs_prog_data
*prog_data
,
8243 const nir_shader
*src_shader
,
8244 int shader_time_index
,
8247 prog_data
->local_size
[0] = src_shader
->info
.cs
.local_size
[0];
8248 prog_data
->local_size
[1] = src_shader
->info
.cs
.local_size
[1];
8249 prog_data
->local_size
[2] = src_shader
->info
.cs
.local_size
[2];
8250 unsigned local_workgroup_size
=
8251 src_shader
->info
.cs
.local_size
[0] * src_shader
->info
.cs
.local_size
[1] *
8252 src_shader
->info
.cs
.local_size
[2];
8254 unsigned min_dispatch_width
=
8255 DIV_ROUND_UP(local_workgroup_size
, compiler
->devinfo
->max_cs_threads
);
8256 min_dispatch_width
= MAX2(8, min_dispatch_width
);
8257 min_dispatch_width
= util_next_power_of_two(min_dispatch_width
);
8258 assert(min_dispatch_width
<= 32);
8260 fs_visitor
*v8
= NULL
, *v16
= NULL
, *v32
= NULL
;
8262 const char *fail_msg
= NULL
;
8263 unsigned promoted_constants
= 0;
8265 /* Now the main event: Visit the shader IR and generate our CS IR for it.
8267 if (min_dispatch_width
<= 8) {
8268 nir_shader
*nir8
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
8270 v8
= new fs_visitor(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
8271 NULL
, /* Never used in core profile */
8272 nir8
, 8, shader_time_index
);
8273 if (!v8
->run_cs(min_dispatch_width
)) {
8274 fail_msg
= v8
->fail_msg
;
8276 /* We should always be able to do SIMD32 for compute shaders */
8277 assert(v8
->max_dispatch_width
>= 32);
8280 cs_set_simd_size(prog_data
, 8);
8281 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
8282 promoted_constants
= v8
->promoted_constants
;
8286 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
8287 !fail_msg
&& min_dispatch_width
<= 16) {
8288 /* Try a SIMD16 compile */
8289 nir_shader
*nir16
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
8291 v16
= new fs_visitor(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
8292 NULL
, /* Never used in core profile */
8293 nir16
, 16, shader_time_index
);
8295 v16
->import_uniforms(v8
);
8297 if (!v16
->run_cs(min_dispatch_width
)) {
8298 compiler
->shader_perf_log(log_data
,
8299 "SIMD16 shader failed to compile: %s",
8303 "Couldn't generate SIMD16 program and not "
8304 "enough threads for SIMD8";
8307 /* We should always be able to do SIMD32 for compute shaders */
8308 assert(v16
->max_dispatch_width
>= 32);
8311 cs_set_simd_size(prog_data
, 16);
8312 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
8313 promoted_constants
= v16
->promoted_constants
;
8317 /* We should always be able to do SIMD32 for compute shaders */
8318 assert(!v16
|| v16
->max_dispatch_width
>= 32);
8320 if (!fail_msg
&& (min_dispatch_width
> 16 || (INTEL_DEBUG
& DEBUG_DO32
))) {
8321 /* Try a SIMD32 compile */
8322 nir_shader
*nir32
= compile_cs_to_nir(compiler
, mem_ctx
, key
,
8324 v32
= new fs_visitor(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
8325 NULL
, /* Never used in core profile */
8326 nir32
, 32, shader_time_index
);
8328 v32
->import_uniforms(v8
);
8330 v32
->import_uniforms(v16
);
8332 if (!v32
->run_cs(min_dispatch_width
)) {
8333 compiler
->shader_perf_log(log_data
,
8334 "SIMD32 shader failed to compile: %s",
8338 "Couldn't generate SIMD32 program and not "
8339 "enough threads for SIMD16";
8343 cs_set_simd_size(prog_data
, 32);
8344 cs_fill_push_const_info(compiler
->devinfo
, prog_data
);
8345 promoted_constants
= v32
->promoted_constants
;
8349 const unsigned *ret
= NULL
;
8350 if (unlikely(cfg
== NULL
)) {
8353 *error_str
= ralloc_strdup(mem_ctx
, fail_msg
);
8355 fs_generator
g(compiler
, log_data
, mem_ctx
, &prog_data
->base
,
8356 promoted_constants
, false, MESA_SHADER_COMPUTE
);
8357 if (INTEL_DEBUG
& DEBUG_CS
) {
8358 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
8359 src_shader
->info
.label
?
8360 src_shader
->info
.label
: "unnamed",
8361 src_shader
->info
.name
);
8362 g
.enable_debug(name
);
8365 g
.generate_code(cfg
, prog_data
->simd_size
);
8367 ret
= g
.get_assembly();
8378 * Test the dispatch mask packing assumptions of
8379 * brw_stage_has_packed_dispatch(). Call this from e.g. the top of
8380 * fs_visitor::emit_nir_code() to cause a GPU hang if any shader invocation is
8381 * executed with an unexpected dispatch mask.
8384 brw_fs_test_dispatch_packing(const fs_builder
&bld
)
8386 const gl_shader_stage stage
= bld
.shader
->stage
;
8388 if (brw_stage_has_packed_dispatch(bld
.shader
->devinfo
, stage
,
8389 bld
.shader
->stage_prog_data
)) {
8390 const fs_builder ubld
= bld
.exec_all().group(1, 0);
8391 const fs_reg tmp
= component(bld
.vgrf(BRW_REGISTER_TYPE_UD
), 0);
8392 const fs_reg mask
= (stage
== MESA_SHADER_FRAGMENT
? brw_vmask_reg() :
8395 ubld
.ADD(tmp
, mask
, brw_imm_ud(1));
8396 ubld
.AND(tmp
, mask
, tmp
);
8398 /* This will loop forever if the dispatch mask doesn't have the expected
8399 * form '2^n-1', in which case tmp will be non-zero.
8401 bld
.emit(BRW_OPCODE_DO
);
8402 bld
.CMP(bld
.null_reg_ud(), tmp
, brw_imm_ud(0), BRW_CONDITIONAL_NZ
);
8403 set_predicate(BRW_PREDICATE_NORMAL
, bld
.emit(BRW_OPCODE_WHILE
));