2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * This file drives the GLSL IR -> LIR translation, contains the
27 * optimizations on the LIR, and drives the generation of native code
31 #include <sys/types.h>
33 #include "util/hash_table.h"
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/fbobject.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "util/register_allocate.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
47 #include "brw_vec4_gs_visitor.h"
49 #include "brw_dead_control_flow.h"
50 #include "main/uniforms.h"
51 #include "brw_fs_live_variables.h"
52 #include "glsl/nir/glsl_types.h"
53 #include "program/sampler.h"
58 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
59 const fs_reg
*src
, unsigned sources
)
61 memset(this, 0, sizeof(*this));
63 this->src
= new fs_reg
[MAX2(sources
, 3)];
64 for (unsigned i
= 0; i
< sources
; i
++)
65 this->src
[i
] = src
[i
];
67 this->opcode
= opcode
;
69 this->sources
= sources
;
70 this->exec_size
= exec_size
;
72 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
74 assert(this->exec_size
!= 0);
76 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
78 /* This will be the case for almost all instructions. */
85 this->regs_written
= DIV_ROUND_UP(dst
.component_size(exec_size
),
89 this->regs_written
= 0;
93 unreachable("Invalid destination register file");
96 this->writes_accumulator
= false;
101 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
104 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
106 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
109 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
111 init(opcode
, exec_size
, dst
, NULL
, 0);
114 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
117 const fs_reg src
[1] = { src0
};
118 init(opcode
, exec_size
, dst
, src
, 1);
121 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
122 const fs_reg
&src0
, const fs_reg
&src1
)
124 const fs_reg src
[2] = { src0
, src1
};
125 init(opcode
, exec_size
, dst
, src
, 2);
128 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
129 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
131 const fs_reg src
[3] = { src0
, src1
, src2
};
132 init(opcode
, exec_size
, dst
, src
, 3);
135 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
136 const fs_reg src
[], unsigned sources
)
138 init(opcode
, exec_width
, dst
, src
, sources
);
141 fs_inst::fs_inst(const fs_inst
&that
)
143 memcpy(this, &that
, sizeof(that
));
145 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
147 for (unsigned i
= 0; i
< that
.sources
; i
++)
148 this->src
[i
] = that
.src
[i
];
157 fs_inst::resize_sources(uint8_t num_sources
)
159 if (this->sources
!= num_sources
) {
160 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
162 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
163 src
[i
] = this->src
[i
];
167 this->sources
= num_sources
;
172 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
174 const fs_reg
&surf_index
,
175 const fs_reg
&varying_offset
,
176 uint32_t const_offset
)
178 /* We have our constant surface use a pitch of 4 bytes, so our index can
179 * be any component of a vector, and then we load 4 contiguous
180 * components starting from that.
182 * We break down the const_offset to a portion added to the variable
183 * offset and a portion done using reg_offset, which means that if you
184 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
185 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
186 * CSE can later notice that those loads are all the same and eliminate
187 * the redundant ones.
189 fs_reg vec4_offset
= vgrf(glsl_type::int_type
);
190 bld
.ADD(vec4_offset
, varying_offset
, brw_imm_ud(const_offset
& ~3));
193 if (devinfo
->gen
== 4 && bld
.dispatch_width() == 8) {
194 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
195 * u, v, r) as parameters, or we can just use the SIMD16 message
196 * consisting of (header, u). We choose the second, at the cost of a
197 * longer return length.
203 if (devinfo
->gen
>= 7)
204 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
;
206 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
;
208 int regs_written
= 4 * (bld
.dispatch_width() / 8) * scale
;
209 fs_reg vec4_result
= fs_reg(VGRF
, alloc
.allocate(regs_written
), dst
.type
);
210 fs_inst
*inst
= bld
.emit(op
, vec4_result
, surf_index
, vec4_offset
);
211 inst
->regs_written
= regs_written
;
213 if (devinfo
->gen
< 7) {
214 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
);
215 inst
->header_size
= 1;
216 if (devinfo
->gen
== 4)
219 inst
->mlen
= 1 + bld
.dispatch_width() / 8;
222 bld
.MOV(dst
, offset(vec4_result
, bld
, (const_offset
& 3) * scale
));
226 * A helper for MOV generation for fixing up broken hardware SEND dependency
230 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
232 /* The caller always wants uncompressed to emit the minimal extra
233 * dependencies, and to avoid having to deal with aligning its regs to 2.
235 const fs_builder ubld
= bld
.annotate("send dependency resolve")
238 ubld
.MOV(ubld
.null_reg_f(), fs_reg(VGRF
, grf
, BRW_REGISTER_TYPE_F
));
242 fs_inst::equals(fs_inst
*inst
) const
244 return (opcode
== inst
->opcode
&&
245 dst
.equals(inst
->dst
) &&
246 src
[0].equals(inst
->src
[0]) &&
247 src
[1].equals(inst
->src
[1]) &&
248 src
[2].equals(inst
->src
[2]) &&
249 saturate
== inst
->saturate
&&
250 predicate
== inst
->predicate
&&
251 conditional_mod
== inst
->conditional_mod
&&
252 mlen
== inst
->mlen
&&
253 base_mrf
== inst
->base_mrf
&&
254 target
== inst
->target
&&
256 header_size
== inst
->header_size
&&
257 shadow_compare
== inst
->shadow_compare
&&
258 exec_size
== inst
->exec_size
&&
259 offset
== inst
->offset
);
263 fs_inst::overwrites_reg(const fs_reg
®
) const
265 return reg
.in_range(dst
, regs_written
);
269 fs_inst::is_send_from_grf() const
272 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
273 case SHADER_OPCODE_SHADER_TIME_ADD
:
274 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
275 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
276 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
277 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
278 case SHADER_OPCODE_UNTYPED_ATOMIC
:
279 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
280 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
281 case SHADER_OPCODE_TYPED_ATOMIC
:
282 case SHADER_OPCODE_TYPED_SURFACE_READ
:
283 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
284 case SHADER_OPCODE_URB_WRITE_SIMD8
:
285 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
286 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
287 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
288 case SHADER_OPCODE_URB_READ_SIMD8
:
289 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
291 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
292 return src
[1].file
== VGRF
;
293 case FS_OPCODE_FB_WRITE
:
294 return src
[0].file
== VGRF
;
297 return src
[0].file
== VGRF
;
304 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
306 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
309 fs_reg reg
= this->src
[0];
310 if (reg
.file
!= VGRF
|| reg
.reg_offset
!= 0 || reg
.stride
== 0)
313 if (grf_alloc
.sizes
[reg
.nr
] != this->regs_written
)
316 for (int i
= 0; i
< this->sources
; i
++) {
317 reg
.type
= this->src
[i
].type
;
318 if (!this->src
[i
].equals(reg
))
321 if (i
< this->header_size
) {
324 reg
.reg_offset
+= this->exec_size
/ 8;
332 fs_inst::can_do_source_mods(const struct brw_device_info
*devinfo
)
334 if (devinfo
->gen
== 6 && is_math())
337 if (is_send_from_grf())
340 if (!backend_instruction::can_do_source_mods())
347 fs_inst::can_change_types() const
349 return dst
.type
== src
[0].type
&&
350 !src
[0].abs
&& !src
[0].negate
&& !saturate
&&
351 (opcode
== BRW_OPCODE_MOV
||
352 (opcode
== BRW_OPCODE_SEL
&&
353 dst
.type
== src
[1].type
&&
354 predicate
!= BRW_PREDICATE_NONE
&&
355 !src
[1].abs
&& !src
[1].negate
));
359 fs_inst::has_side_effects() const
361 return this->eot
|| backend_instruction::has_side_effects();
367 memset(this, 0, sizeof(*this));
371 /** Generic unset register constructor. */
375 this->file
= BAD_FILE
;
378 fs_reg::fs_reg(struct ::brw_reg reg
) :
381 this->reg_offset
= 0;
382 this->subreg_offset
= 0;
383 this->reladdr
= NULL
;
385 if (this->file
== IMM
&&
386 (this->type
!= BRW_REGISTER_TYPE_V
&&
387 this->type
!= BRW_REGISTER_TYPE_UV
&&
388 this->type
!= BRW_REGISTER_TYPE_VF
)) {
394 fs_reg::equals(const fs_reg
&r
) const
396 return (this->backend_reg::equals(r
) &&
397 subreg_offset
== r
.subreg_offset
&&
398 !reladdr
&& !r
.reladdr
&&
403 fs_reg::set_smear(unsigned subreg
)
405 assert(file
!= ARF
&& file
!= FIXED_GRF
&& file
!= IMM
);
406 subreg_offset
= subreg
* type_sz(type
);
412 fs_reg::is_contiguous() const
418 fs_reg::component_size(unsigned width
) const
420 const unsigned stride
= ((file
!= ARF
&& file
!= FIXED_GRF
) ? this->stride
:
423 return MAX2(width
* stride
, 1) * type_sz(type
);
427 type_size_scalar(const struct glsl_type
*type
)
429 unsigned int size
, i
;
431 switch (type
->base_type
) {
434 case GLSL_TYPE_FLOAT
:
436 return type
->components();
437 case GLSL_TYPE_ARRAY
:
438 return type_size_scalar(type
->fields
.array
) * type
->length
;
439 case GLSL_TYPE_STRUCT
:
441 for (i
= 0; i
< type
->length
; i
++) {
442 size
+= type_size_scalar(type
->fields
.structure
[i
].type
);
445 case GLSL_TYPE_SAMPLER
:
446 /* Samplers take up no register space, since they're baked in at
450 case GLSL_TYPE_ATOMIC_UINT
:
452 case GLSL_TYPE_SUBROUTINE
:
454 case GLSL_TYPE_IMAGE
:
455 return BRW_IMAGE_PARAM_SIZE
;
457 case GLSL_TYPE_ERROR
:
458 case GLSL_TYPE_INTERFACE
:
459 case GLSL_TYPE_DOUBLE
:
460 unreachable("not reached");
467 * Returns the number of scalar components needed to store type, assuming
468 * that vectors are padded out to vec4.
470 * This has the packing rules of type_size_vec4(), but counts components
471 * similar to type_size_scalar().
474 type_size_vec4_times_4(const struct glsl_type
*type
)
476 return 4 * type_size_vec4(type
);
480 * Create a MOV to read the timestamp register.
482 * The caller is responsible for emitting the MOV. The return value is
483 * the destination of the MOV, with extra parameters set.
486 fs_visitor::get_timestamp(const fs_builder
&bld
)
488 assert(devinfo
->gen
>= 7);
490 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
493 BRW_REGISTER_TYPE_UD
));
495 fs_reg dst
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
497 /* We want to read the 3 fields we care about even if it's not enabled in
500 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
506 fs_visitor::emit_shader_time_begin()
508 shader_start_time
= get_timestamp(bld
.annotate("shader time start"));
510 /* We want only the low 32 bits of the timestamp. Since it's running
511 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
512 * which is plenty of time for our purposes. It is identical across the
513 * EUs, but since it's tracking GPU core speed it will increment at a
514 * varying rate as render P-states change.
516 shader_start_time
.set_smear(0);
520 fs_visitor::emit_shader_time_end()
522 /* Insert our code just before the final SEND with EOT. */
523 exec_node
*end
= this->instructions
.get_tail();
524 assert(end
&& ((fs_inst
*) end
)->eot
);
525 const fs_builder ibld
= bld
.annotate("shader time end")
526 .exec_all().at(NULL
, end
);
528 fs_reg shader_end_time
= get_timestamp(ibld
);
530 /* We only use the low 32 bits of the timestamp - see
531 * emit_shader_time_begin()).
533 * We could also check if render P-states have changed (or anything
534 * else that might disrupt timing) by setting smear to 2 and checking if
535 * that field is != 0.
537 shader_end_time
.set_smear(0);
539 /* Check that there weren't any timestamp reset events (assuming these
540 * were the only two timestamp reads that happened).
542 fs_reg reset
= shader_end_time
;
544 set_condmod(BRW_CONDITIONAL_Z
,
545 ibld
.AND(ibld
.null_reg_ud(), reset
, brw_imm_ud(1u)));
546 ibld
.IF(BRW_PREDICATE_NORMAL
);
548 fs_reg start
= shader_start_time
;
550 fs_reg diff
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
553 const fs_builder cbld
= ibld
.group(1, 0);
554 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
556 /* If there were no instructions between the two timestamp gets, the diff
557 * is 2 cycles. Remove that overhead, so I can forget about that when
558 * trying to determine the time taken for single instructions.
560 cbld
.ADD(diff
, diff
, brw_imm_ud(-2u));
561 SHADER_TIME_ADD(cbld
, 0, diff
);
562 SHADER_TIME_ADD(cbld
, 1, brw_imm_ud(1u));
563 ibld
.emit(BRW_OPCODE_ELSE
);
564 SHADER_TIME_ADD(cbld
, 2, brw_imm_ud(1u));
565 ibld
.emit(BRW_OPCODE_ENDIF
);
569 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
570 int shader_time_subindex
,
573 int index
= shader_time_index
* 3 + shader_time_subindex
;
574 struct brw_reg offset
= brw_imm_d(index
* SHADER_TIME_STRIDE
);
577 if (dispatch_width
== 8)
578 payload
= vgrf(glsl_type::uvec2_type
);
580 payload
= vgrf(glsl_type::uint_type
);
582 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
586 fs_visitor::vfail(const char *format
, va_list va
)
595 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
596 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
598 this->fail_msg
= msg
;
601 fprintf(stderr
, "%s", msg
);
606 fs_visitor::fail(const char *format
, ...)
610 va_start(va
, format
);
616 * Mark this program as impossible to compile in SIMD16 mode.
618 * During the SIMD8 compile (which happens first), we can detect and flag
619 * things that are unsupported in SIMD16 mode, so the compiler can skip
620 * the SIMD16 compile altogether.
622 * During a SIMD16 compile (if one happens anyway), this just calls fail().
625 fs_visitor::no16(const char *msg
)
627 if (dispatch_width
== 16) {
630 simd16_unsupported
= true;
632 compiler
->shader_perf_log(log_data
,
633 "SIMD16 shader failed to compile: %s", msg
);
638 * Returns true if the instruction has a flag that means it won't
639 * update an entire destination register.
641 * For example, dead code elimination and live variable analysis want to know
642 * when a write to a variable screens off any preceding values that were in
646 fs_inst::is_partial_write() const
648 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
649 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
650 !this->dst
.is_contiguous());
654 fs_inst::components_read(unsigned i
) const
657 case FS_OPCODE_LINTERP
:
663 case FS_OPCODE_PIXEL_X
:
664 case FS_OPCODE_PIXEL_Y
:
668 case FS_OPCODE_FB_WRITE_LOGICAL
:
669 assert(src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
670 /* First/second FB write color. */
672 return src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
676 case SHADER_OPCODE_TEX_LOGICAL
:
677 case SHADER_OPCODE_TXD_LOGICAL
:
678 case SHADER_OPCODE_TXF_LOGICAL
:
679 case SHADER_OPCODE_TXL_LOGICAL
:
680 case SHADER_OPCODE_TXS_LOGICAL
:
681 case FS_OPCODE_TXB_LOGICAL
:
682 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
683 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
684 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
685 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
686 case SHADER_OPCODE_LOD_LOGICAL
:
687 case SHADER_OPCODE_TG4_LOGICAL
:
688 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
689 assert(src
[8].file
== IMM
&& src
[9].file
== IMM
);
690 /* Texture coordinates. */
693 /* Texture derivatives. */
694 else if ((i
== 2 || i
== 3) && opcode
== SHADER_OPCODE_TXD_LOGICAL
)
696 /* Texture offset. */
700 else if (i
== 5 && opcode
== SHADER_OPCODE_TXF_CMS_W_LOGICAL
)
705 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
706 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
707 assert(src
[3].file
== IMM
);
708 /* Surface coordinates. */
711 /* Surface operation source (ignored for reads). */
717 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
718 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
719 assert(src
[3].file
== IMM
&&
721 /* Surface coordinates. */
724 /* Surface operation source. */
730 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
731 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
732 assert(src
[3].file
== IMM
&&
734 const unsigned op
= src
[4].ud
;
735 /* Surface coordinates. */
738 /* Surface operation source. */
739 else if (i
== 1 && op
== BRW_AOP_CMPWR
)
741 else if (i
== 1 && (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
||
742 op
== BRW_AOP_PREDEC
))
754 fs_inst::regs_read(int arg
) const
757 case FS_OPCODE_FB_WRITE
:
758 case SHADER_OPCODE_URB_WRITE_SIMD8
:
759 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
760 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
761 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
762 case SHADER_OPCODE_URB_READ_SIMD8
:
763 case SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
:
764 case SHADER_OPCODE_UNTYPED_ATOMIC
:
765 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
766 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
767 case SHADER_OPCODE_TYPED_ATOMIC
:
768 case SHADER_OPCODE_TYPED_SURFACE_READ
:
769 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
770 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
775 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
776 /* The payload is actually stored in src1 */
781 case FS_OPCODE_LINTERP
:
786 case SHADER_OPCODE_LOAD_PAYLOAD
:
787 if (arg
< this->header_size
)
791 case CS_OPCODE_CS_TERMINATE
:
792 case SHADER_OPCODE_BARRIER
:
795 case SHADER_OPCODE_MOV_INDIRECT
:
797 assert(src
[2].file
== IMM
);
798 unsigned region_length
= src
[2].ud
;
800 if (src
[0].file
== FIXED_GRF
) {
801 /* If the start of the region is not register aligned, then
802 * there's some portion of the register that's technically
803 * unread at the beginning.
805 * However, the register allocator works in terms of whole
806 * registers, and does not use subnr. It assumes that the
807 * read starts at the beginning of the register, and extends
808 * regs_read() whole registers beyond that.
810 * To compensate, we extend the region length to include this
811 * unread portion at the beginning.
814 region_length
+= src
[0].subnr
* type_sz(src
[0].type
);
816 return DIV_ROUND_UP(region_length
, REG_SIZE
);
818 assert(!"Invalid register file");
824 if (is_tex() && arg
== 0 && src
[0].file
== VGRF
)
829 switch (src
[arg
].file
) {
839 return DIV_ROUND_UP(components_read(arg
) *
840 src
[arg
].component_size(exec_size
),
843 unreachable("MRF registers are not allowed as sources");
849 fs_inst::reads_flag() const
855 fs_inst::writes_flag() const
857 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
858 opcode
!= BRW_OPCODE_IF
&&
859 opcode
!= BRW_OPCODE_WHILE
)) ||
860 opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
;
864 * Returns how many MRFs an FS opcode will write over.
866 * Note that this is not the 0 or 1 implied writes in an actual gen
867 * instruction -- the FS opcodes often generate MOVs in addition.
870 fs_visitor::implied_mrf_writes(fs_inst
*inst
)
875 if (inst
->base_mrf
== -1)
878 switch (inst
->opcode
) {
879 case SHADER_OPCODE_RCP
:
880 case SHADER_OPCODE_RSQ
:
881 case SHADER_OPCODE_SQRT
:
882 case SHADER_OPCODE_EXP2
:
883 case SHADER_OPCODE_LOG2
:
884 case SHADER_OPCODE_SIN
:
885 case SHADER_OPCODE_COS
:
886 return 1 * dispatch_width
/ 8;
887 case SHADER_OPCODE_POW
:
888 case SHADER_OPCODE_INT_QUOTIENT
:
889 case SHADER_OPCODE_INT_REMAINDER
:
890 return 2 * dispatch_width
/ 8;
891 case SHADER_OPCODE_TEX
:
893 case SHADER_OPCODE_TXD
:
894 case SHADER_OPCODE_TXF
:
895 case SHADER_OPCODE_TXF_CMS
:
896 case SHADER_OPCODE_TXF_CMS_W
:
897 case SHADER_OPCODE_TXF_MCS
:
898 case SHADER_OPCODE_TG4
:
899 case SHADER_OPCODE_TG4_OFFSET
:
900 case SHADER_OPCODE_TXL
:
901 case SHADER_OPCODE_TXS
:
902 case SHADER_OPCODE_LOD
:
903 case SHADER_OPCODE_SAMPLEINFO
:
905 case FS_OPCODE_FB_WRITE
:
907 case FS_OPCODE_GET_BUFFER_SIZE
:
908 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
909 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
911 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
913 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
915 case SHADER_OPCODE_UNTYPED_ATOMIC
:
916 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
917 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
918 case SHADER_OPCODE_TYPED_ATOMIC
:
919 case SHADER_OPCODE_TYPED_SURFACE_READ
:
920 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
921 case SHADER_OPCODE_URB_WRITE_SIMD8
:
922 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
923 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
924 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
925 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
926 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
927 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
928 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
931 unreachable("not reached");
936 fs_visitor::vgrf(const glsl_type
*const type
)
938 int reg_width
= dispatch_width
/ 8;
939 return fs_reg(VGRF
, alloc
.allocate(type_size_scalar(type
) * reg_width
),
940 brw_type_for_base_type(type
));
943 fs_reg::fs_reg(enum brw_reg_file file
, int nr
)
948 this->type
= BRW_REGISTER_TYPE_F
;
949 this->stride
= (file
== UNIFORM
? 0 : 1);
952 fs_reg::fs_reg(enum brw_reg_file file
, int nr
, enum brw_reg_type type
)
958 this->stride
= (file
== UNIFORM
? 0 : 1);
961 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
962 * This brings in those uniform definitions
965 fs_visitor::import_uniforms(fs_visitor
*v
)
967 this->push_constant_loc
= v
->push_constant_loc
;
968 this->pull_constant_loc
= v
->pull_constant_loc
;
969 this->uniforms
= v
->uniforms
;
970 this->param_size
= v
->param_size
;
974 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer
,
975 bool origin_upper_left
)
977 assert(stage
== MESA_SHADER_FRAGMENT
);
978 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
979 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec4_type
));
981 bool flip
= !origin_upper_left
^ key
->render_to_fbo
;
984 if (pixel_center_integer
) {
985 bld
.MOV(wpos
, this->pixel_x
);
987 bld
.ADD(wpos
, this->pixel_x
, brw_imm_f(0.5f
));
989 wpos
= offset(wpos
, bld
, 1);
992 if (!flip
&& pixel_center_integer
) {
993 bld
.MOV(wpos
, this->pixel_y
);
995 fs_reg pixel_y
= this->pixel_y
;
996 float offset
= (pixel_center_integer
? 0.0f
: 0.5f
);
999 pixel_y
.negate
= true;
1000 offset
+= key
->drawable_height
- 1.0f
;
1003 bld
.ADD(wpos
, pixel_y
, brw_imm_f(offset
));
1005 wpos
= offset(wpos
, bld
, 1);
1007 /* gl_FragCoord.z */
1008 if (devinfo
->gen
>= 6) {
1009 bld
.MOV(wpos
, fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0)));
1011 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
1012 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
1013 interp_reg(VARYING_SLOT_POS
, 2));
1015 wpos
= offset(wpos
, bld
, 1);
1017 /* gl_FragCoord.w: Already set up in emit_interpolation */
1018 bld
.MOV(wpos
, this->wpos_w
);
1024 fs_visitor::emit_linterp(const fs_reg
&attr
, const fs_reg
&interp
,
1025 glsl_interp_qualifier interpolation_mode
,
1026 bool is_centroid
, bool is_sample
)
1028 brw_wm_barycentric_interp_mode barycoord_mode
;
1029 if (devinfo
->gen
>= 6) {
1031 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1032 barycoord_mode
= BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
1034 barycoord_mode
= BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
1035 } else if (is_sample
) {
1036 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1037 barycoord_mode
= BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
1039 barycoord_mode
= BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
1041 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1042 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1044 barycoord_mode
= BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
1047 /* On Ironlake and below, there is only one interpolation mode.
1048 * Centroid interpolation doesn't mean anything on this hardware --
1049 * there is no multisampling.
1051 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1053 return bld
.emit(FS_OPCODE_LINTERP
, attr
,
1054 this->delta_xy
[barycoord_mode
], interp
);
1058 fs_visitor::emit_general_interpolation(fs_reg attr
, const char *name
,
1059 const glsl_type
*type
,
1060 glsl_interp_qualifier interpolation_mode
,
1061 int location
, bool mod_centroid
,
1064 attr
.type
= brw_type_for_base_type(type
->get_scalar_type());
1066 assert(stage
== MESA_SHADER_FRAGMENT
);
1067 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1068 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1070 unsigned int array_elements
;
1072 if (type
->is_array()) {
1073 array_elements
= type
->arrays_of_arrays_size();
1074 if (array_elements
== 0) {
1075 fail("dereferenced array '%s' has length 0\n", name
);
1077 type
= type
->without_array();
1082 if (interpolation_mode
== INTERP_QUALIFIER_NONE
) {
1084 location
== VARYING_SLOT_COL0
|| location
== VARYING_SLOT_COL1
;
1085 if (key
->flat_shade
&& is_gl_Color
) {
1086 interpolation_mode
= INTERP_QUALIFIER_FLAT
;
1088 interpolation_mode
= INTERP_QUALIFIER_SMOOTH
;
1092 for (unsigned int i
= 0; i
< array_elements
; i
++) {
1093 for (unsigned int j
= 0; j
< type
->matrix_columns
; j
++) {
1094 if (prog_data
->urb_setup
[location
] == -1) {
1095 /* If there's no incoming setup data for this slot, don't
1096 * emit interpolation for it.
1098 attr
= offset(attr
, bld
, type
->vector_elements
);
1103 if (interpolation_mode
== INTERP_QUALIFIER_FLAT
) {
1104 /* Constant interpolation (flat shading) case. The SF has
1105 * handed us defined values in only the constant offset
1106 * field of the setup reg.
1108 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1109 struct brw_reg interp
= interp_reg(location
, k
);
1110 interp
= suboffset(interp
, 3);
1111 interp
.type
= attr
.type
;
1112 bld
.emit(FS_OPCODE_CINTERP
, attr
, fs_reg(interp
));
1113 attr
= offset(attr
, bld
, 1);
1116 /* Smooth/noperspective interpolation case. */
1117 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1118 struct brw_reg interp
= interp_reg(location
, k
);
1119 if (devinfo
->needs_unlit_centroid_workaround
&& mod_centroid
) {
1120 /* Get the pixel/sample mask into f0 so that we know
1121 * which pixels are lit. Then, for each channel that is
1122 * unlit, replace the centroid data with non-centroid
1125 bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
1128 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1130 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1131 inst
->predicate_inverse
= true;
1132 if (devinfo
->has_pln
)
1133 inst
->no_dd_clear
= true;
1135 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1136 mod_centroid
&& !key
->persample_shading
,
1137 mod_sample
|| key
->persample_shading
);
1138 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1139 inst
->predicate_inverse
= false;
1140 if (devinfo
->has_pln
)
1141 inst
->no_dd_check
= true;
1144 emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1145 mod_centroid
&& !key
->persample_shading
,
1146 mod_sample
|| key
->persample_shading
);
1148 if (devinfo
->gen
< 6 && interpolation_mode
== INTERP_QUALIFIER_SMOOTH
) {
1149 bld
.MUL(attr
, attr
, this->pixel_w
);
1151 attr
= offset(attr
, bld
, 1);
1161 fs_visitor::emit_frontfacing_interpolation()
1163 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1165 if (devinfo
->gen
>= 6) {
1166 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1167 * a boolean result from this (~0/true or 0/false).
1169 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1170 * this task in only one instruction:
1171 * - a negation source modifier will flip the bit; and
1172 * - a W -> D type conversion will sign extend the bit into the high
1173 * word of the destination.
1175 * An ASR 15 fills the low word of the destination.
1177 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1180 bld
.ASR(*reg
, g0
, brw_imm_d(15));
1182 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1183 * a boolean result from this (1/true or 0/false).
1185 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1186 * the negation source modifier to flip it. Unfortunately the SHR
1187 * instruction only operates on UD (or D with an abs source modifier)
1188 * sources without negation.
1190 * Instead, use ASR (which will give ~0/true or 0/false).
1192 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1195 bld
.ASR(*reg
, g1_6
, brw_imm_d(31));
1202 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1204 assert(stage
== MESA_SHADER_FRAGMENT
);
1205 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1206 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1208 if (key
->compute_pos_offset
) {
1209 /* Convert int_sample_pos to floating point */
1210 bld
.MOV(dst
, int_sample_pos
);
1211 /* Scale to the range [0, 1] */
1212 bld
.MUL(dst
, dst
, brw_imm_f(1 / 16.0f
));
1215 /* From ARB_sample_shading specification:
1216 * "When rendering to a non-multisample buffer, or if multisample
1217 * rasterization is disabled, gl_SamplePosition will always be
1220 bld
.MOV(dst
, brw_imm_f(0.5f
));
1225 fs_visitor::emit_samplepos_setup()
1227 assert(devinfo
->gen
>= 6);
1229 const fs_builder abld
= bld
.annotate("compute sample position");
1230 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1232 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1233 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1235 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1236 * mode will be enabled.
1238 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1239 * R31.1:0 Position Offset X/Y for Slot[3:0]
1240 * R31.3:2 Position Offset X/Y for Slot[7:4]
1243 * The X, Y sample positions come in as bytes in thread payload. So, read
1244 * the positions using vstride=16, width=8, hstride=2.
1246 struct brw_reg sample_pos_reg
=
1247 stride(retype(brw_vec1_grf(payload
.sample_pos_reg
, 0),
1248 BRW_REGISTER_TYPE_B
), 16, 8, 2);
1250 if (dispatch_width
== 8) {
1251 abld
.MOV(int_sample_x
, fs_reg(sample_pos_reg
));
1253 abld
.half(0).MOV(half(int_sample_x
, 0), fs_reg(sample_pos_reg
));
1254 abld
.half(1).MOV(half(int_sample_x
, 1),
1255 fs_reg(suboffset(sample_pos_reg
, 16)));
1257 /* Compute gl_SamplePosition.x */
1258 compute_sample_position(pos
, int_sample_x
);
1259 pos
= offset(pos
, abld
, 1);
1260 if (dispatch_width
== 8) {
1261 abld
.MOV(int_sample_y
, fs_reg(suboffset(sample_pos_reg
, 1)));
1263 abld
.half(0).MOV(half(int_sample_y
, 0),
1264 fs_reg(suboffset(sample_pos_reg
, 1)));
1265 abld
.half(1).MOV(half(int_sample_y
, 1),
1266 fs_reg(suboffset(sample_pos_reg
, 17)));
1268 /* Compute gl_SamplePosition.y */
1269 compute_sample_position(pos
, int_sample_y
);
1274 fs_visitor::emit_sampleid_setup()
1276 assert(stage
== MESA_SHADER_FRAGMENT
);
1277 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1278 assert(devinfo
->gen
>= 6);
1280 const fs_builder abld
= bld
.annotate("compute sample id");
1281 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1283 if (key
->compute_sample_id
) {
1284 fs_reg
t1(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_D
);
1286 fs_reg
t2(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_W
);
1288 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1289 * 8x multisampling, subspan 0 will represent sample N (where N
1290 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1291 * 7. We can find the value of N by looking at R0.0 bits 7:6
1292 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1293 * (since samples are always delivered in pairs). That is, we
1294 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1295 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1296 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1297 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1298 * populating a temporary variable with the sequence (0, 1, 2, 3),
1299 * and then reading from it using vstride=1, width=4, hstride=0.
1300 * These computations hold good for 4x multisampling as well.
1302 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1303 * the first four slots are sample 0 of subspan 0; the next four
1304 * are sample 1 of subspan 0; the third group is sample 0 of
1305 * subspan 1, and finally sample 1 of subspan 1.
1308 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1309 * accomodate 16x MSAA.
1311 unsigned sspi_mask
= devinfo
->gen
>= 9 ? 0x1c0 : 0xc0;
1313 abld
.exec_all().group(1, 0)
1314 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_D
)),
1315 brw_imm_ud(sspi_mask
));
1316 abld
.exec_all().group(1, 0).SHR(t1
, t1
, brw_imm_d(5));
1318 /* This works for both SIMD8 and SIMD16 */
1319 abld
.exec_all().group(4, 0)
1320 .MOV(t2
, brw_imm_v(key
->persample_2x
? 0x1010 : 0x3210));
1322 /* This special instruction takes care of setting vstride=1,
1323 * width=4, hstride=0 of t2 during an ADD instruction.
1325 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1327 /* As per GL_ARB_sample_shading specification:
1328 * "When rendering to a non-multisample buffer, or if multisample
1329 * rasterization is disabled, gl_SampleID will always be zero."
1331 abld
.MOV(*reg
, brw_imm_d(0));
1338 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1340 if (!src
.abs
&& !src
.negate
)
1343 fs_reg temp
= bld
.vgrf(src
.type
);
1350 fs_visitor::emit_discard_jump()
1352 assert(((brw_wm_prog_data
*) this->prog_data
)->uses_kill
);
1354 /* For performance, after a discard, jump to the end of the
1355 * shader if all relevant channels have been discarded.
1357 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1358 discard_jump
->flag_subreg
= 1;
1360 discard_jump
->predicate
= (dispatch_width
== 8)
1361 ? BRW_PREDICATE_ALIGN1_ANY8H
1362 : BRW_PREDICATE_ALIGN1_ANY16H
;
1363 discard_jump
->predicate_inverse
= true;
1367 fs_visitor::emit_gs_thread_end()
1369 assert(stage
== MESA_SHADER_GEOMETRY
);
1371 struct brw_gs_prog_data
*gs_prog_data
=
1372 (struct brw_gs_prog_data
*) prog_data
;
1374 if (gs_compile
->control_data_header_size_bits
> 0) {
1375 emit_gs_control_data_bits(this->final_gs_vertex_count
);
1378 const fs_builder abld
= bld
.annotate("thread end");
1381 if (gs_prog_data
->static_vertex_count
!= -1) {
1382 foreach_in_list_reverse(fs_inst
, prev
, &this->instructions
) {
1383 if (prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
||
1384 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
1385 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
1386 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
) {
1389 /* Delete now dead instructions. */
1390 foreach_in_list_reverse_safe(exec_node
, dead
, &this->instructions
) {
1396 } else if (prev
->is_control_flow() || prev
->has_side_effects()) {
1400 fs_reg hdr
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1401 abld
.MOV(hdr
, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
)));
1402 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, hdr
);
1405 fs_reg payload
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1406 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
1407 sources
[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1408 sources
[1] = this->final_gs_vertex_count
;
1409 abld
.LOAD_PAYLOAD(payload
, sources
, 2, 2);
1410 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1418 fs_visitor::assign_curb_setup()
1420 if (dispatch_width
== 8) {
1421 prog_data
->dispatch_grf_start_reg
= payload
.num_regs
;
1423 if (stage
== MESA_SHADER_FRAGMENT
) {
1424 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1425 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1426 } else if (stage
== MESA_SHADER_COMPUTE
) {
1427 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
1428 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1430 unreachable("Unsupported shader type!");
1434 prog_data
->curb_read_length
= ALIGN(stage_prog_data
->nr_params
, 8) / 8;
1436 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1437 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1438 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1439 if (inst
->src
[i
].file
== UNIFORM
) {
1440 int uniform_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1442 if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1443 constant_nr
= push_constant_loc
[uniform_nr
];
1445 /* Section 5.11 of the OpenGL 4.1 spec says:
1446 * "Out-of-bounds reads return undefined values, which include
1447 * values from other variables of the active program or zero."
1448 * Just return the first push constant.
1453 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1456 brw_reg
.abs
= inst
->src
[i
].abs
;
1457 brw_reg
.negate
= inst
->src
[i
].negate
;
1459 assert(inst
->src
[i
].stride
== 0);
1460 inst
->src
[i
] = byte_offset(
1461 retype(brw_reg
, inst
->src
[i
].type
),
1462 inst
->src
[i
].subreg_offset
);
1467 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1468 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1472 fs_visitor::calculate_urb_setup()
1474 assert(stage
== MESA_SHADER_FRAGMENT
);
1475 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1476 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1478 memset(prog_data
->urb_setup
, -1,
1479 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1482 /* Figure out where each of the incoming setup attributes lands. */
1483 if (devinfo
->gen
>= 6) {
1484 if (_mesa_bitcount_64(nir
->info
.inputs_read
&
1485 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1486 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1487 * first 16 varying inputs, so we can put them wherever we want.
1488 * Just put them in order.
1490 * This is useful because it means that (a) inputs not used by the
1491 * fragment shader won't take up valuable register space, and (b) we
1492 * won't have to recompile the fragment shader if it gets paired with
1493 * a different vertex (or geometry) shader.
1495 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1496 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1497 BITFIELD64_BIT(i
)) {
1498 prog_data
->urb_setup
[i
] = urb_next
++;
1502 bool include_vue_header
=
1503 nir
->info
.inputs_read
& (VARYING_BIT_LAYER
| VARYING_BIT_VIEWPORT
);
1505 /* We have enough input varyings that the SF/SBE pipeline stage can't
1506 * arbitrarily rearrange them to suit our whim; we have to put them
1507 * in an order that matches the output of the previous pipeline stage
1508 * (geometry or vertex shader).
1510 struct brw_vue_map prev_stage_vue_map
;
1511 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1512 key
->input_slots_valid
,
1513 nir
->info
.separate_shader
);
1515 include_vue_header
? 0 : 2 * BRW_SF_URB_ENTRY_READ_OFFSET
;
1517 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1518 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1520 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1521 if (varying
!= BRW_VARYING_SLOT_PAD
&&
1522 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1523 BITFIELD64_BIT(varying
))) {
1524 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1527 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1530 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1531 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1532 /* Point size is packed into the header, not as a general attribute */
1533 if (i
== VARYING_SLOT_PSIZ
)
1536 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1537 /* The back color slot is skipped when the front color is
1538 * also written to. In addition, some slots can be
1539 * written in the vertex shader and not read in the
1540 * fragment shader. So the register number must always be
1541 * incremented, mapped or not.
1543 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1544 prog_data
->urb_setup
[i
] = urb_next
;
1550 * It's a FS only attribute, and we did interpolation for this attribute
1551 * in SF thread. So, count it here, too.
1553 * See compile_sf_prog() for more info.
1555 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1556 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1559 prog_data
->num_varying_inputs
= urb_next
;
1563 fs_visitor::assign_urb_setup()
1565 assert(stage
== MESA_SHADER_FRAGMENT
);
1566 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1568 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1570 /* Offset all the urb_setup[] index by the actual position of the
1571 * setup regs, now that the location of the constants has been chosen.
1573 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1574 if (inst
->opcode
== FS_OPCODE_LINTERP
) {
1575 assert(inst
->src
[1].file
== FIXED_GRF
);
1576 inst
->src
[1].nr
+= urb_start
;
1579 if (inst
->opcode
== FS_OPCODE_CINTERP
) {
1580 assert(inst
->src
[0].file
== FIXED_GRF
);
1581 inst
->src
[0].nr
+= urb_start
;
1585 /* Each attribute is 4 setup channels, each of which is half a reg. */
1586 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1590 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst
*inst
)
1592 for (int i
= 0; i
< inst
->sources
; i
++) {
1593 if (inst
->src
[i
].file
== ATTR
) {
1594 int grf
= payload
.num_regs
+
1595 prog_data
->curb_read_length
+
1597 inst
->src
[i
].reg_offset
;
1599 unsigned width
= inst
->src
[i
].stride
== 0 ? 1 : inst
->exec_size
;
1600 struct brw_reg reg
=
1601 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1602 inst
->src
[i
].subreg_offset
),
1603 inst
->exec_size
* inst
->src
[i
].stride
,
1604 width
, inst
->src
[i
].stride
);
1605 reg
.abs
= inst
->src
[i
].abs
;
1606 reg
.negate
= inst
->src
[i
].negate
;
1614 fs_visitor::assign_vs_urb_setup()
1616 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
1618 assert(stage
== MESA_SHADER_VERTEX
);
1619 int count
= _mesa_bitcount_64(vs_prog_data
->inputs_read
);
1620 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
)
1623 /* Each attribute is 4 regs. */
1624 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attributes
;
1626 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1628 /* Rewrite all ATTR file references to the hw grf that they land in. */
1629 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1630 convert_attr_sources_to_hw_regs(inst
);
1635 fs_visitor::assign_gs_urb_setup()
1637 assert(stage
== MESA_SHADER_GEOMETRY
);
1639 brw_vue_prog_data
*vue_prog_data
= (brw_vue_prog_data
*) prog_data
;
1641 first_non_payload_grf
+=
1642 8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
;
1644 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1645 /* Rewrite all ATTR file references to GRFs. */
1646 convert_attr_sources_to_hw_regs(inst
);
1652 * Split large virtual GRFs into separate components if we can.
1654 * This is mostly duplicated with what brw_fs_vector_splitting does,
1655 * but that's really conservative because it's afraid of doing
1656 * splitting that doesn't result in real progress after the rest of
1657 * the optimization phases, which would cause infinite looping in
1658 * optimization. We can do it once here, safely. This also has the
1659 * opportunity to split interpolated values, or maybe even uniforms,
1660 * which we don't have at the IR level.
1662 * We want to split, because virtual GRFs are what we register
1663 * allocate and spill (due to contiguousness requirements for some
1664 * instructions), and they're what we naturally generate in the
1665 * codegen process, but most virtual GRFs don't actually need to be
1666 * contiguous sets of GRFs. If we split, we'll end up with reduced
1667 * live intervals and better dead code elimination and coalescing.
1670 fs_visitor::split_virtual_grfs()
1672 int num_vars
= this->alloc
.count
;
1674 /* Count the total number of registers */
1676 int vgrf_to_reg
[num_vars
];
1677 for (int i
= 0; i
< num_vars
; i
++) {
1678 vgrf_to_reg
[i
] = reg_count
;
1679 reg_count
+= alloc
.sizes
[i
];
1682 /* An array of "split points". For each register slot, this indicates
1683 * if this slot can be separated from the previous slot. Every time an
1684 * instruction uses multiple elements of a register (as a source or
1685 * destination), we mark the used slots as inseparable. Then we go
1686 * through and split the registers into the smallest pieces we can.
1688 bool split_points
[reg_count
];
1689 memset(split_points
, 0, sizeof(split_points
));
1691 /* Mark all used registers as fully splittable */
1692 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1693 if (inst
->dst
.file
== VGRF
) {
1694 int reg
= vgrf_to_reg
[inst
->dst
.nr
];
1695 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.nr
]; j
++)
1696 split_points
[reg
+ j
] = true;
1699 for (int i
= 0; i
< inst
->sources
; i
++) {
1700 if (inst
->src
[i
].file
== VGRF
) {
1701 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
];
1702 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].nr
]; j
++)
1703 split_points
[reg
+ j
] = true;
1708 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1709 if (inst
->dst
.file
== VGRF
) {
1710 int reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.reg_offset
;
1711 for (int j
= 1; j
< inst
->regs_written
; j
++)
1712 split_points
[reg
+ j
] = false;
1714 for (int i
= 0; i
< inst
->sources
; i
++) {
1715 if (inst
->src
[i
].file
== VGRF
) {
1716 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].reg_offset
;
1717 for (int j
= 1; j
< inst
->regs_read(i
); j
++)
1718 split_points
[reg
+ j
] = false;
1723 int new_virtual_grf
[reg_count
];
1724 int new_reg_offset
[reg_count
];
1727 for (int i
= 0; i
< num_vars
; i
++) {
1728 /* The first one should always be 0 as a quick sanity check. */
1729 assert(split_points
[reg
] == false);
1732 new_reg_offset
[reg
] = 0;
1737 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1738 /* If this is a split point, reset the offset to 0 and allocate a
1739 * new virtual GRF for the previous offset many registers
1741 if (split_points
[reg
]) {
1742 assert(offset
<= MAX_VGRF_SIZE
);
1743 int grf
= alloc
.allocate(offset
);
1744 for (int k
= reg
- offset
; k
< reg
; k
++)
1745 new_virtual_grf
[k
] = grf
;
1748 new_reg_offset
[reg
] = offset
;
1753 /* The last one gets the original register number */
1754 assert(offset
<= MAX_VGRF_SIZE
);
1755 alloc
.sizes
[i
] = offset
;
1756 for (int k
= reg
- offset
; k
< reg
; k
++)
1757 new_virtual_grf
[k
] = i
;
1759 assert(reg
== reg_count
);
1761 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1762 if (inst
->dst
.file
== VGRF
) {
1763 reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.reg_offset
;
1764 inst
->dst
.nr
= new_virtual_grf
[reg
];
1765 inst
->dst
.reg_offset
= new_reg_offset
[reg
];
1766 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1768 for (int i
= 0; i
< inst
->sources
; i
++) {
1769 if (inst
->src
[i
].file
== VGRF
) {
1770 reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].reg_offset
;
1771 inst
->src
[i
].nr
= new_virtual_grf
[reg
];
1772 inst
->src
[i
].reg_offset
= new_reg_offset
[reg
];
1773 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1777 invalidate_live_intervals();
1781 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1783 * During code generation, we create tons of temporary variables, many of
1784 * which get immediately killed and are never used again. Yet, in later
1785 * optimization and analysis passes, such as compute_live_intervals, we need
1786 * to loop over all the virtual GRFs. Compacting them can save a lot of
1790 fs_visitor::compact_virtual_grfs()
1792 bool progress
= false;
1793 int remap_table
[this->alloc
.count
];
1794 memset(remap_table
, -1, sizeof(remap_table
));
1796 /* Mark which virtual GRFs are used. */
1797 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
1798 if (inst
->dst
.file
== VGRF
)
1799 remap_table
[inst
->dst
.nr
] = 0;
1801 for (int i
= 0; i
< inst
->sources
; i
++) {
1802 if (inst
->src
[i
].file
== VGRF
)
1803 remap_table
[inst
->src
[i
].nr
] = 0;
1807 /* Compact the GRF arrays. */
1809 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
1810 if (remap_table
[i
] == -1) {
1811 /* We just found an unused register. This means that we are
1812 * actually going to compact something.
1816 remap_table
[i
] = new_index
;
1817 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
1818 invalidate_live_intervals();
1823 this->alloc
.count
= new_index
;
1825 /* Patch all the instructions to use the newly renumbered registers */
1826 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1827 if (inst
->dst
.file
== VGRF
)
1828 inst
->dst
.nr
= remap_table
[inst
->dst
.nr
];
1830 for (int i
= 0; i
< inst
->sources
; i
++) {
1831 if (inst
->src
[i
].file
== VGRF
)
1832 inst
->src
[i
].nr
= remap_table
[inst
->src
[i
].nr
];
1836 /* Patch all the references to delta_xy, since they're used in register
1837 * allocation. If they're unused, switch them to BAD_FILE so we don't
1838 * think some random VGRF is delta_xy.
1840 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
1841 if (delta_xy
[i
].file
== VGRF
) {
1842 if (remap_table
[delta_xy
[i
].nr
] != -1) {
1843 delta_xy
[i
].nr
= remap_table
[delta_xy
[i
].nr
];
1845 delta_xy
[i
].file
= BAD_FILE
;
1854 * Assign UNIFORM file registers to either push constants or pull constants.
1856 * We allow a fragment shader to have more than the specified minimum
1857 * maximum number of fragment shader uniform components (64). If
1858 * there are too many of these, they'd fill up all of register space.
1859 * So, this will push some of them out to the pull constant buffer and
1860 * update the program to load them. We also use pull constants for all
1861 * indirect constant loads because we don't support indirect accesses in
1865 fs_visitor::assign_constant_locations()
1867 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
1868 if (dispatch_width
!= 8)
1871 unsigned int num_pull_constants
= 0;
1873 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1874 memset(pull_constant_loc
, -1, sizeof(pull_constant_loc
[0]) * uniforms
);
1876 bool is_live
[uniforms
];
1877 memset(is_live
, 0, sizeof(is_live
));
1879 /* First, we walk through the instructions and do two things:
1881 * 1) Figure out which uniforms are live.
1883 * 2) Find all indirect access of uniform arrays and flag them as needing
1884 * to go into the pull constant buffer.
1886 * Note that we don't move constant-indexed accesses to arrays. No
1887 * testing has been done of the performance impact of this choice.
1889 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
1890 for (int i
= 0 ; i
< inst
->sources
; i
++) {
1891 if (inst
->src
[i
].file
!= UNIFORM
)
1894 if (inst
->src
[i
].reladdr
) {
1895 int uniform
= inst
->src
[i
].nr
;
1897 /* If this array isn't already present in the pull constant buffer,
1900 if (pull_constant_loc
[uniform
] == -1) {
1901 assert(param_size
[uniform
]);
1902 for (int j
= 0; j
< param_size
[uniform
]; j
++)
1903 pull_constant_loc
[uniform
+ j
] = num_pull_constants
++;
1906 /* Mark the the one accessed uniform as live */
1907 int constant_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1908 if (constant_nr
>= 0 && constant_nr
< (int) uniforms
)
1909 is_live
[constant_nr
] = true;
1914 /* Only allow 16 registers (128 uniform components) as push constants.
1916 * Just demote the end of the list. We could probably do better
1917 * here, demoting things that are rarely used in the program first.
1919 * If changing this value, note the limitation about total_regs in
1922 unsigned int max_push_components
= 16 * 8;
1923 unsigned int num_push_constants
= 0;
1925 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1927 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1928 if (!is_live
[i
] || pull_constant_loc
[i
] != -1) {
1929 /* This UNIFORM register is either dead, or has already been demoted
1930 * to a pull const. Mark it as no longer living in the param[] array.
1932 push_constant_loc
[i
] = -1;
1936 if (num_push_constants
< max_push_components
) {
1937 /* Retain as a push constant. Record the location in the params[]
1940 push_constant_loc
[i
] = num_push_constants
++;
1942 /* Demote to a pull constant. */
1943 push_constant_loc
[i
] = -1;
1944 pull_constant_loc
[i
] = num_pull_constants
++;
1948 stage_prog_data
->nr_params
= num_push_constants
;
1949 stage_prog_data
->nr_pull_params
= num_pull_constants
;
1951 /* Up until now, the param[] array has been indexed by reg + reg_offset
1952 * of UNIFORM registers. Move pull constants into pull_param[] and
1953 * condense param[] to only contain the uniforms we chose to push.
1955 * NOTE: Because we are condensing the params[] array, we know that
1956 * push_constant_loc[i] <= i and we can do it in one smooth loop without
1957 * having to make a copy.
1959 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1960 const gl_constant_value
*value
= stage_prog_data
->param
[i
];
1962 if (pull_constant_loc
[i
] != -1) {
1963 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
1964 } else if (push_constant_loc
[i
] != -1) {
1965 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
1971 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
1972 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
1975 fs_visitor::demote_pull_constants()
1977 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
1978 for (int i
= 0; i
< inst
->sources
; i
++) {
1979 if (inst
->src
[i
].file
!= UNIFORM
)
1983 unsigned location
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1984 if (location
>= uniforms
) /* Out of bounds access */
1987 pull_index
= pull_constant_loc
[location
];
1989 if (pull_index
== -1)
1992 /* Set up the annotation tracking for new generated instructions. */
1993 const fs_builder
ibld(this, block
, inst
);
1994 const unsigned index
= stage_prog_data
->binding_table
.pull_constants_start
;
1995 fs_reg dst
= vgrf(glsl_type::float_type
);
1997 assert(inst
->src
[i
].stride
== 0);
1999 /* Generate a pull load into dst. */
2000 if (inst
->src
[i
].reladdr
) {
2001 VARYING_PULL_CONSTANT_LOAD(ibld
, dst
,
2003 *inst
->src
[i
].reladdr
,
2005 inst
->src
[i
].reladdr
= NULL
;
2006 inst
->src
[i
].stride
= 1;
2008 const fs_builder ubld
= ibld
.exec_all().group(8, 0);
2009 struct brw_reg offset
= brw_imm_ud((unsigned)(pull_index
* 4) & ~15);
2010 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
2011 dst
, brw_imm_ud(index
), offset
);
2012 inst
->src
[i
].set_smear(pull_index
& 3);
2014 brw_mark_surface_used(prog_data
, index
);
2016 /* Rewrite the instruction to use the temporary VGRF. */
2017 inst
->src
[i
].file
= VGRF
;
2018 inst
->src
[i
].nr
= dst
.nr
;
2019 inst
->src
[i
].reg_offset
= 0;
2022 invalidate_live_intervals();
2026 fs_visitor::opt_algebraic()
2028 bool progress
= false;
2030 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2031 switch (inst
->opcode
) {
2032 case BRW_OPCODE_MOV
:
2033 if (inst
->src
[0].file
!= IMM
)
2036 if (inst
->saturate
) {
2037 if (inst
->dst
.type
!= inst
->src
[0].type
)
2038 assert(!"unimplemented: saturate mixed types");
2040 if (brw_saturate_immediate(inst
->dst
.type
,
2041 &inst
->src
[0].as_brw_reg())) {
2042 inst
->saturate
= false;
2048 case BRW_OPCODE_MUL
:
2049 if (inst
->src
[1].file
!= IMM
)
2053 if (inst
->src
[1].is_one()) {
2054 inst
->opcode
= BRW_OPCODE_MOV
;
2055 inst
->src
[1] = reg_undef
;
2061 if (inst
->src
[1].is_negative_one()) {
2062 inst
->opcode
= BRW_OPCODE_MOV
;
2063 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2064 inst
->src
[1] = reg_undef
;
2070 if (inst
->src
[1].is_zero()) {
2071 inst
->opcode
= BRW_OPCODE_MOV
;
2072 inst
->src
[0] = inst
->src
[1];
2073 inst
->src
[1] = reg_undef
;
2078 if (inst
->src
[0].file
== IMM
) {
2079 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2080 inst
->opcode
= BRW_OPCODE_MOV
;
2081 inst
->src
[0].f
*= inst
->src
[1].f
;
2082 inst
->src
[1] = reg_undef
;
2087 case BRW_OPCODE_ADD
:
2088 if (inst
->src
[1].file
!= IMM
)
2092 if (inst
->src
[1].is_zero()) {
2093 inst
->opcode
= BRW_OPCODE_MOV
;
2094 inst
->src
[1] = reg_undef
;
2099 if (inst
->src
[0].file
== IMM
) {
2100 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2101 inst
->opcode
= BRW_OPCODE_MOV
;
2102 inst
->src
[0].f
+= inst
->src
[1].f
;
2103 inst
->src
[1] = reg_undef
;
2109 if (inst
->src
[0].equals(inst
->src
[1])) {
2110 inst
->opcode
= BRW_OPCODE_MOV
;
2111 inst
->src
[1] = reg_undef
;
2116 case BRW_OPCODE_LRP
:
2117 if (inst
->src
[1].equals(inst
->src
[2])) {
2118 inst
->opcode
= BRW_OPCODE_MOV
;
2119 inst
->src
[0] = inst
->src
[1];
2120 inst
->src
[1] = reg_undef
;
2121 inst
->src
[2] = reg_undef
;
2126 case BRW_OPCODE_CMP
:
2127 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
2129 inst
->src
[0].negate
&&
2130 inst
->src
[1].is_zero()) {
2131 inst
->src
[0].abs
= false;
2132 inst
->src
[0].negate
= false;
2133 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2138 case BRW_OPCODE_SEL
:
2139 if (inst
->src
[0].equals(inst
->src
[1])) {
2140 inst
->opcode
= BRW_OPCODE_MOV
;
2141 inst
->src
[1] = reg_undef
;
2142 inst
->predicate
= BRW_PREDICATE_NONE
;
2143 inst
->predicate_inverse
= false;
2145 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2146 switch (inst
->conditional_mod
) {
2147 case BRW_CONDITIONAL_LE
:
2148 case BRW_CONDITIONAL_L
:
2149 switch (inst
->src
[1].type
) {
2150 case BRW_REGISTER_TYPE_F
:
2151 if (inst
->src
[1].f
>= 1.0f
) {
2152 inst
->opcode
= BRW_OPCODE_MOV
;
2153 inst
->src
[1] = reg_undef
;
2154 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2162 case BRW_CONDITIONAL_GE
:
2163 case BRW_CONDITIONAL_G
:
2164 switch (inst
->src
[1].type
) {
2165 case BRW_REGISTER_TYPE_F
:
2166 if (inst
->src
[1].f
<= 0.0f
) {
2167 inst
->opcode
= BRW_OPCODE_MOV
;
2168 inst
->src
[1] = reg_undef
;
2169 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2181 case BRW_OPCODE_MAD
:
2182 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2183 inst
->opcode
= BRW_OPCODE_MOV
;
2184 inst
->src
[1] = reg_undef
;
2185 inst
->src
[2] = reg_undef
;
2187 } else if (inst
->src
[0].is_zero()) {
2188 inst
->opcode
= BRW_OPCODE_MUL
;
2189 inst
->src
[0] = inst
->src
[2];
2190 inst
->src
[2] = reg_undef
;
2192 } else if (inst
->src
[1].is_one()) {
2193 inst
->opcode
= BRW_OPCODE_ADD
;
2194 inst
->src
[1] = inst
->src
[2];
2195 inst
->src
[2] = reg_undef
;
2197 } else if (inst
->src
[2].is_one()) {
2198 inst
->opcode
= BRW_OPCODE_ADD
;
2199 inst
->src
[2] = reg_undef
;
2201 } else if (inst
->src
[1].file
== IMM
&& inst
->src
[2].file
== IMM
) {
2202 inst
->opcode
= BRW_OPCODE_ADD
;
2203 inst
->src
[1].f
*= inst
->src
[2].f
;
2204 inst
->src
[2] = reg_undef
;
2208 case SHADER_OPCODE_RCP
: {
2209 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
2210 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
2211 if (inst
->src
[0].equals(prev
->dst
)) {
2212 inst
->opcode
= SHADER_OPCODE_RSQ
;
2213 inst
->src
[0] = prev
->src
[0];
2219 case SHADER_OPCODE_BROADCAST
:
2220 if (is_uniform(inst
->src
[0])) {
2221 inst
->opcode
= BRW_OPCODE_MOV
;
2223 inst
->force_writemask_all
= true;
2225 } else if (inst
->src
[1].file
== IMM
) {
2226 inst
->opcode
= BRW_OPCODE_MOV
;
2227 inst
->src
[0] = component(inst
->src
[0],
2230 inst
->force_writemask_all
= true;
2239 /* Swap if src[0] is immediate. */
2240 if (progress
&& inst
->is_commutative()) {
2241 if (inst
->src
[0].file
== IMM
) {
2242 fs_reg tmp
= inst
->src
[1];
2243 inst
->src
[1] = inst
->src
[0];
2252 * Optimize sample messages that have constant zero values for the trailing
2253 * texture coordinates. We can just reduce the message length for these
2254 * instructions instead of reserving a register for it. Trailing parameters
2255 * that aren't sent default to zero anyway. This will cause the dead code
2256 * eliminator to remove the MOV instruction that would otherwise be emitted to
2257 * set up the zero value.
2260 fs_visitor::opt_zero_samples()
2262 /* Gen4 infers the texturing opcode based on the message length so we can't
2265 if (devinfo
->gen
< 5)
2268 bool progress
= false;
2270 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2271 if (!inst
->is_tex())
2274 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2276 if (load_payload
->is_head_sentinel() ||
2277 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2280 /* We don't want to remove the message header or the first parameter.
2281 * Removing the first parameter is not allowed, see the Haswell PRM
2282 * volume 7, page 149:
2284 * "Parameter 0 is required except for the sampleinfo message, which
2285 * has no parameter 0"
2287 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2288 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2289 (inst
->exec_size
/ 8) +
2290 inst
->header_size
- 1].is_zero()) {
2291 inst
->mlen
-= inst
->exec_size
/ 8;
2297 invalidate_live_intervals();
2303 * Optimize sample messages which are followed by the final RT write.
2305 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2306 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2307 * final texturing results copied to the framebuffer write payload and modify
2308 * them to write to the framebuffer directly.
2311 fs_visitor::opt_sampler_eot()
2313 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2315 if (stage
!= MESA_SHADER_FRAGMENT
)
2318 if (devinfo
->gen
< 9 && !devinfo
->is_cherryview
)
2321 /* FINISHME: It should be possible to implement this optimization when there
2322 * are multiple drawbuffers.
2324 if (key
->nr_color_regions
!= 1)
2327 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2328 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2329 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2330 assert(fb_write
->eot
);
2331 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE
);
2333 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2335 /* There wasn't one; nothing to do. */
2336 if (unlikely(tex_inst
->is_head_sentinel()) || !tex_inst
->is_tex())
2339 /* 3D Sampler » Messages » Message Format
2341 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2342 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2344 if (tex_inst
->opcode
== SHADER_OPCODE_TXS
||
2345 tex_inst
->opcode
== SHADER_OPCODE_SAMPLEINFO
||
2346 tex_inst
->opcode
== SHADER_OPCODE_LOD
||
2347 tex_inst
->opcode
== SHADER_OPCODE_TG4
||
2348 tex_inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
2351 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2352 * It's very likely to be the previous instruction.
2354 fs_inst
*load_payload
= (fs_inst
*) tex_inst
->prev
;
2355 if (load_payload
->is_head_sentinel() ||
2356 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2359 assert(!tex_inst
->eot
); /* We can't get here twice */
2360 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2362 const fs_builder
ibld(this, block
, tex_inst
);
2364 tex_inst
->offset
|= fb_write
->target
<< 24;
2365 tex_inst
->eot
= true;
2366 tex_inst
->dst
= ibld
.null_reg_ud();
2367 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2369 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2370 * to create a new LOAD_PAYLOAD command with the same sources and a space
2371 * saved for the header. Using a new destination register not only makes sure
2372 * we have enough space, but it will make sure the dead code eliminator kills
2373 * the instruction that this will replace.
2375 if (tex_inst
->header_size
!= 0)
2378 fs_reg send_header
= ibld
.vgrf(BRW_REGISTER_TYPE_F
,
2379 load_payload
->sources
+ 1);
2380 fs_reg
*new_sources
=
2381 ralloc_array(mem_ctx
, fs_reg
, load_payload
->sources
+ 1);
2383 new_sources
[0] = fs_reg();
2384 for (int i
= 0; i
< load_payload
->sources
; i
++)
2385 new_sources
[i
+1] = load_payload
->src
[i
];
2387 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2388 * requires a lot of information about the sources to appropriately figure
2389 * out the number of registers needed to be used. Given this stage in our
2390 * optimization, we may not have the appropriate GRFs required by
2391 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2392 * manually emit the instruction.
2394 fs_inst
*new_load_payload
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
,
2395 load_payload
->exec_size
,
2398 load_payload
->sources
+ 1);
2400 new_load_payload
->regs_written
= load_payload
->regs_written
+ 1;
2401 new_load_payload
->header_size
= 1;
2403 tex_inst
->header_size
= 1;
2404 tex_inst
->insert_before(cfg
->blocks
[cfg
->num_blocks
- 1], new_load_payload
);
2405 tex_inst
->src
[0] = send_header
;
2411 fs_visitor::opt_register_renaming()
2413 bool progress
= false;
2416 int remap
[alloc
.count
];
2417 memset(remap
, -1, sizeof(int) * alloc
.count
);
2419 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2420 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2422 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2423 inst
->opcode
== BRW_OPCODE_WHILE
) {
2427 /* Rewrite instruction sources. */
2428 for (int i
= 0; i
< inst
->sources
; i
++) {
2429 if (inst
->src
[i
].file
== VGRF
&&
2430 remap
[inst
->src
[i
].nr
] != -1 &&
2431 remap
[inst
->src
[i
].nr
] != inst
->src
[i
].nr
) {
2432 inst
->src
[i
].nr
= remap
[inst
->src
[i
].nr
];
2437 const int dst
= inst
->dst
.nr
;
2440 inst
->dst
.file
== VGRF
&&
2441 alloc
.sizes
[inst
->dst
.nr
] == inst
->exec_size
/ 8 &&
2442 !inst
->is_partial_write()) {
2443 if (remap
[dst
] == -1) {
2446 remap
[dst
] = alloc
.allocate(inst
->exec_size
/ 8);
2447 inst
->dst
.nr
= remap
[dst
];
2450 } else if (inst
->dst
.file
== VGRF
&&
2452 remap
[dst
] != dst
) {
2453 inst
->dst
.nr
= remap
[dst
];
2459 invalidate_live_intervals();
2461 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2462 if (delta_xy
[i
].file
== VGRF
&& remap
[delta_xy
[i
].nr
] != -1) {
2463 delta_xy
[i
].nr
= remap
[delta_xy
[i
].nr
];
2472 * Remove redundant or useless discard jumps.
2474 * For example, we can eliminate jumps in the following sequence:
2476 * discard-jump (redundant with the next jump)
2477 * discard-jump (useless; jumps to the next instruction)
2481 fs_visitor::opt_redundant_discard_jumps()
2483 bool progress
= false;
2485 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2487 fs_inst
*placeholder_halt
= NULL
;
2488 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2489 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2490 placeholder_halt
= inst
;
2495 if (!placeholder_halt
)
2498 /* Delete any HALTs immediately before the placeholder halt. */
2499 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2500 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2501 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2502 prev
->remove(last_bblock
);
2507 invalidate_live_intervals();
2513 fs_visitor::compute_to_mrf()
2515 bool progress
= false;
2518 /* No MRFs on Gen >= 7. */
2519 if (devinfo
->gen
>= 7)
2522 calculate_live_intervals();
2524 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2528 if (inst
->opcode
!= BRW_OPCODE_MOV
||
2529 inst
->is_partial_write() ||
2530 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= VGRF
||
2531 inst
->dst
.type
!= inst
->src
[0].type
||
2532 inst
->src
[0].abs
|| inst
->src
[0].negate
||
2533 !inst
->src
[0].is_contiguous() ||
2534 inst
->src
[0].subreg_offset
)
2537 /* Work out which hardware MRF registers are written by this
2540 int mrf_low
= inst
->dst
.nr
& ~BRW_MRF_COMPR4
;
2542 if (inst
->dst
.nr
& BRW_MRF_COMPR4
) {
2543 mrf_high
= mrf_low
+ 4;
2544 } else if (inst
->exec_size
== 16) {
2545 mrf_high
= mrf_low
+ 1;
2550 /* Can't compute-to-MRF this GRF if someone else was going to
2553 if (this->virtual_grf_end
[inst
->src
[0].nr
] > ip
)
2556 /* Found a move of a GRF to a MRF. Let's see if we can go
2557 * rewrite the thing that made this GRF to write into the MRF.
2559 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
2560 if (scan_inst
->dst
.file
== VGRF
&&
2561 scan_inst
->dst
.nr
== inst
->src
[0].nr
) {
2562 /* Found the last thing to write our reg we want to turn
2563 * into a compute-to-MRF.
2566 /* If this one instruction didn't populate all the
2567 * channels, bail. We might be able to rewrite everything
2568 * that writes that reg, but it would require smarter
2569 * tracking to delay the rewriting until complete success.
2571 if (scan_inst
->is_partial_write())
2574 /* Things returning more than one register would need us to
2575 * understand coalescing out more than one MOV at a time.
2577 if (scan_inst
->regs_written
> scan_inst
->exec_size
/ 8)
2580 /* SEND instructions can't have MRF as a destination. */
2581 if (scan_inst
->mlen
)
2584 if (devinfo
->gen
== 6) {
2585 /* gen6 math instructions must have the destination be
2586 * GRF, so no compute-to-MRF for them.
2588 if (scan_inst
->is_math()) {
2593 if (scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
2594 /* Found the creator of our MRF's source value. */
2595 scan_inst
->dst
.file
= MRF
;
2596 scan_inst
->dst
.nr
= inst
->dst
.nr
;
2597 scan_inst
->saturate
|= inst
->saturate
;
2598 inst
->remove(block
);
2604 /* We don't handle control flow here. Most computation of
2605 * values that end up in MRFs are shortly before the MRF
2608 if (block
->start() == scan_inst
)
2611 /* You can't read from an MRF, so if someone else reads our
2612 * MRF's source GRF that we wanted to rewrite, that stops us.
2614 bool interfered
= false;
2615 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
2616 if (scan_inst
->src
[i
].file
== VGRF
&&
2617 scan_inst
->src
[i
].nr
== inst
->src
[0].nr
&&
2618 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
2625 if (scan_inst
->dst
.file
== MRF
) {
2626 /* If somebody else writes our MRF here, we can't
2627 * compute-to-MRF before that.
2629 int scan_mrf_low
= scan_inst
->dst
.nr
& ~BRW_MRF_COMPR4
;
2632 if (scan_inst
->dst
.nr
& BRW_MRF_COMPR4
) {
2633 scan_mrf_high
= scan_mrf_low
+ 4;
2634 } else if (scan_inst
->exec_size
== 16) {
2635 scan_mrf_high
= scan_mrf_low
+ 1;
2637 scan_mrf_high
= scan_mrf_low
;
2640 if (mrf_low
== scan_mrf_low
||
2641 mrf_low
== scan_mrf_high
||
2642 mrf_high
== scan_mrf_low
||
2643 mrf_high
== scan_mrf_high
) {
2648 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1) {
2649 /* Found a SEND instruction, which means that there are
2650 * live values in MRFs from base_mrf to base_mrf +
2651 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2654 if (mrf_low
>= scan_inst
->base_mrf
&&
2655 mrf_low
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2658 if (mrf_high
>= scan_inst
->base_mrf
&&
2659 mrf_high
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2667 invalidate_live_intervals();
2673 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2674 * flow. We could probably do better here with some form of divergence
2678 fs_visitor::eliminate_find_live_channel()
2680 bool progress
= false;
2683 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2684 switch (inst
->opcode
) {
2690 case BRW_OPCODE_ENDIF
:
2691 case BRW_OPCODE_WHILE
:
2695 case FS_OPCODE_DISCARD_JUMP
:
2696 /* This can potentially make control flow non-uniform until the end
2701 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2703 inst
->opcode
= BRW_OPCODE_MOV
;
2704 inst
->src
[0] = brw_imm_ud(0u);
2706 inst
->force_writemask_all
= true;
2720 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2721 * instructions to FS_OPCODE_REP_FB_WRITE.
2724 fs_visitor::emit_repclear_shader()
2726 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2728 int color_mrf
= base_mrf
+ 2;
2730 fs_inst
*mov
= bld
.exec_all().group(4, 0)
2731 .MOV(brw_message_reg(color_mrf
),
2732 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
2735 if (key
->nr_color_regions
== 1) {
2736 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2737 write
->saturate
= key
->clamp_fragment_color
;
2738 write
->base_mrf
= color_mrf
;
2740 write
->header_size
= 0;
2743 assume(key
->nr_color_regions
> 0);
2744 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
2745 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2746 write
->saturate
= key
->clamp_fragment_color
;
2747 write
->base_mrf
= base_mrf
;
2749 write
->header_size
= 2;
2757 assign_constant_locations();
2758 assign_curb_setup();
2760 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2761 assert(mov
->src
[0].file
== FIXED_GRF
);
2762 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].nr
, 0);
2766 * Walks through basic blocks, looking for repeated MRF writes and
2767 * removing the later ones.
2770 fs_visitor::remove_duplicate_mrf_writes()
2772 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
2773 bool progress
= false;
2775 /* Need to update the MRF tracking for compressed instructions. */
2776 if (dispatch_width
== 16)
2779 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2781 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2782 if (inst
->is_control_flow()) {
2783 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2786 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2787 inst
->dst
.file
== MRF
) {
2788 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.nr
];
2789 if (prev_inst
&& inst
->equals(prev_inst
)) {
2790 inst
->remove(block
);
2796 /* Clear out the last-write records for MRFs that were overwritten. */
2797 if (inst
->dst
.file
== MRF
) {
2798 last_mrf_move
[inst
->dst
.nr
] = NULL
;
2801 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
2802 /* Found a SEND instruction, which will include two or fewer
2803 * implied MRF writes. We could do better here.
2805 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
2806 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
2810 /* Clear out any MRF move records whose sources got overwritten. */
2811 if (inst
->dst
.file
== VGRF
) {
2812 for (unsigned int i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
2813 if (last_mrf_move
[i
] &&
2814 last_mrf_move
[i
]->src
[0].nr
== inst
->dst
.nr
) {
2815 last_mrf_move
[i
] = NULL
;
2820 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2821 inst
->dst
.file
== MRF
&&
2822 inst
->src
[0].file
== VGRF
&&
2823 !inst
->is_partial_write()) {
2824 last_mrf_move
[inst
->dst
.nr
] = inst
;
2829 invalidate_live_intervals();
2835 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
2837 /* Clear the flag for registers that actually got read (as expected). */
2838 for (int i
= 0; i
< inst
->sources
; i
++) {
2840 if (inst
->src
[i
].file
== VGRF
|| inst
->src
[i
].file
== FIXED_GRF
) {
2841 grf
= inst
->src
[i
].nr
;
2846 if (grf
>= first_grf
&&
2847 grf
< first_grf
+ grf_len
) {
2848 deps
[grf
- first_grf
] = false;
2849 if (inst
->exec_size
== 16)
2850 deps
[grf
- first_grf
+ 1] = false;
2856 * Implements this workaround for the original 965:
2858 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2859 * check for post destination dependencies on this instruction, software
2860 * must ensure that there is no destination hazard for the case of ‘write
2861 * followed by a posted write’ shown in the following example.
2864 * 2. send r3.xy <rest of send instruction>
2867 * Due to no post-destination dependency check on the ‘send’, the above
2868 * code sequence could have two instructions (1 and 2) in flight at the
2869 * same time that both consider ‘r3’ as the target of their final writes.
2872 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
2875 int write_len
= inst
->regs_written
;
2876 int first_write_grf
= inst
->dst
.nr
;
2877 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2878 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2880 memset(needs_dep
, false, sizeof(needs_dep
));
2881 memset(needs_dep
, true, write_len
);
2883 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
2885 /* Walk backwards looking for writes to registers we're writing which
2886 * aren't read since being written. If we hit the start of the program,
2887 * we assume that there are no outstanding dependencies on entry to the
2890 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
2891 /* If we hit control flow, assume that there *are* outstanding
2892 * dependencies, and force their cleanup before our instruction.
2894 if (block
->start() == scan_inst
) {
2895 for (int i
= 0; i
< write_len
; i
++) {
2897 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
2898 first_write_grf
+ i
);
2903 /* We insert our reads as late as possible on the assumption that any
2904 * instruction but a MOV that might have left us an outstanding
2905 * dependency has more latency than a MOV.
2907 if (scan_inst
->dst
.file
== VGRF
) {
2908 for (int i
= 0; i
< scan_inst
->regs_written
; i
++) {
2909 int reg
= scan_inst
->dst
.nr
+ i
;
2911 if (reg
>= first_write_grf
&&
2912 reg
< first_write_grf
+ write_len
&&
2913 needs_dep
[reg
- first_write_grf
]) {
2914 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
2915 needs_dep
[reg
- first_write_grf
] = false;
2916 if (scan_inst
->exec_size
== 16)
2917 needs_dep
[reg
- first_write_grf
+ 1] = false;
2922 /* Clear the flag for registers that actually got read (as expected). */
2923 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2925 /* Continue the loop only if we haven't resolved all the dependencies */
2927 for (i
= 0; i
< write_len
; i
++) {
2937 * Implements this workaround for the original 965:
2939 * "[DevBW, DevCL] Errata: A destination register from a send can not be
2940 * used as a destination register until after it has been sourced by an
2941 * instruction with a different destination register.
2944 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
2946 int write_len
= inst
->regs_written
;
2947 int first_write_grf
= inst
->dst
.nr
;
2948 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2949 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2951 memset(needs_dep
, false, sizeof(needs_dep
));
2952 memset(needs_dep
, true, write_len
);
2953 /* Walk forwards looking for writes to registers we're writing which aren't
2954 * read before being written.
2956 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
) {
2957 /* If we hit control flow, force resolve all remaining dependencies. */
2958 if (block
->end() == scan_inst
) {
2959 for (int i
= 0; i
< write_len
; i
++) {
2961 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2962 first_write_grf
+ i
);
2967 /* Clear the flag for registers that actually got read (as expected). */
2968 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2970 /* We insert our reads as late as possible since they're reading the
2971 * result of a SEND, which has massive latency.
2973 if (scan_inst
->dst
.file
== VGRF
&&
2974 scan_inst
->dst
.nr
>= first_write_grf
&&
2975 scan_inst
->dst
.nr
< first_write_grf
+ write_len
&&
2976 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
]) {
2977 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2979 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
] = false;
2982 /* Continue the loop only if we haven't resolved all the dependencies */
2984 for (i
= 0; i
< write_len
; i
++) {
2994 fs_visitor::insert_gen4_send_dependency_workarounds()
2996 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
2999 bool progress
= false;
3001 /* Note that we're done with register allocation, so GRF fs_regs always
3002 * have a .reg_offset of 0.
3005 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3006 if (inst
->mlen
!= 0 && inst
->dst
.file
== VGRF
) {
3007 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3008 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3014 invalidate_live_intervals();
3018 * Turns the generic expression-style uniform pull constant load instruction
3019 * into a hardware-specific series of instructions for loading a pull
3022 * The expression style allows the CSE pass before this to optimize out
3023 * repeated loads from the same offset, and gives the pre-register-allocation
3024 * scheduling full flexibility, while the conversion to native instructions
3025 * allows the post-register-allocation scheduler the best information
3028 * Note that execution masking for setting up pull constant loads is special:
3029 * the channels that need to be written are unrelated to the current execution
3030 * mask, since a later instruction will use one of the result channels as a
3031 * source operand for all 8 or 16 of its channels.
3034 fs_visitor::lower_uniform_pull_constant_loads()
3036 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3037 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3040 if (devinfo
->gen
>= 7) {
3041 /* The offset arg before was a vec4-aligned byte offset. We need to
3042 * turn it into a dword offset.
3044 fs_reg const_offset_reg
= inst
->src
[1];
3045 assert(const_offset_reg
.file
== IMM
&&
3046 const_offset_reg
.type
== BRW_REGISTER_TYPE_UD
);
3047 const_offset_reg
.ud
/= 4;
3049 fs_reg payload
, offset
;
3050 if (devinfo
->gen
>= 9) {
3051 /* We have to use a message header on Skylake to get SIMD4x2
3052 * mode. Reserve space for the register.
3054 offset
= payload
= fs_reg(VGRF
, alloc
.allocate(2));
3055 offset
.reg_offset
++;
3058 offset
= payload
= fs_reg(VGRF
, alloc
.allocate(1));
3062 /* This is actually going to be a MOV, but since only the first dword
3063 * is accessed, we have a special opcode to do just that one. Note
3064 * that this needs to be an operation that will be considered a def
3065 * by live variable analysis, or register allocation will explode.
3067 fs_inst
*setup
= new(mem_ctx
) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET
,
3068 8, offset
, const_offset_reg
);
3069 setup
->force_writemask_all
= true;
3071 setup
->ir
= inst
->ir
;
3072 setup
->annotation
= inst
->annotation
;
3073 inst
->insert_before(block
, setup
);
3075 /* Similarly, this will only populate the first 4 channels of the
3076 * result register (since we only use smear values from 0-3), but we
3077 * don't tell the optimizer.
3079 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3080 inst
->src
[1] = payload
;
3081 inst
->base_mrf
= -1;
3083 invalidate_live_intervals();
3085 /* Before register allocation, we didn't tell the scheduler about the
3086 * MRF we use. We know it's safe to use this MRF because nothing
3087 * else does except for register spill/unspill, which generates and
3088 * uses its MRF within a single IR instruction.
3090 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
3097 fs_visitor::lower_load_payload()
3099 bool progress
= false;
3101 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3102 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3105 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== VGRF
);
3106 assert(inst
->saturate
== false);
3107 fs_reg dst
= inst
->dst
;
3109 /* Get rid of COMPR4. We'll add it back in if we need it */
3110 if (dst
.file
== MRF
)
3111 dst
.nr
= dst
.nr
& ~BRW_MRF_COMPR4
;
3113 const fs_builder
ibld(this, block
, inst
);
3114 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3116 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3117 if (inst
->src
[i
].file
!= BAD_FILE
) {
3118 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3119 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3120 hbld
.MOV(mov_dst
, mov_src
);
3122 dst
= offset(dst
, hbld
, 1);
3125 if (inst
->dst
.file
== MRF
&& (inst
->dst
.nr
& BRW_MRF_COMPR4
) &&
3126 inst
->exec_size
> 8) {
3127 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3128 * a straightforward copy. Instead, the result of the
3129 * LOAD_PAYLOAD is treated as interleaved and the first four
3130 * non-header sources are unpacked as:
3141 * This is used for gen <= 5 fb writes.
3143 assert(inst
->exec_size
== 16);
3144 assert(inst
->header_size
+ 4 <= inst
->sources
);
3145 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3146 if (inst
->src
[i
].file
!= BAD_FILE
) {
3147 if (devinfo
->has_compr4
) {
3148 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3149 compr4_dst
.nr
|= BRW_MRF_COMPR4
;
3150 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3152 /* Platform doesn't have COMPR4. We have to fake it */
3153 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3154 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3156 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3163 /* The loop above only ever incremented us through the first set
3164 * of 4 registers. However, thanks to the magic of COMPR4, we
3165 * actually wrote to the first 8 registers, so we need to take
3166 * that into account now.
3170 /* The COMPR4 code took care of the first 4 sources. We'll let
3171 * the regular path handle any remaining sources. Yes, we are
3172 * modifying the instruction but we're about to delete it so
3173 * this really doesn't hurt anything.
3175 inst
->header_size
+= 4;
3178 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3179 if (inst
->src
[i
].file
!= BAD_FILE
)
3180 ibld
.MOV(retype(dst
, inst
->src
[i
].type
), inst
->src
[i
]);
3181 dst
= offset(dst
, ibld
, 1);
3184 inst
->remove(block
);
3189 invalidate_live_intervals();
3195 fs_visitor::lower_integer_multiplication()
3197 bool progress
= false;
3199 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3200 const fs_builder
ibld(this, block
, inst
);
3202 if (inst
->opcode
== BRW_OPCODE_MUL
) {
3203 if (inst
->dst
.is_accumulator() ||
3204 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3205 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3208 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit
3209 * operation directly, but CHV/BXT cannot.
3211 if (devinfo
->gen
>= 8 &&
3212 !devinfo
->is_cherryview
&& !devinfo
->is_broxton
)
3215 if (inst
->src
[1].file
== IMM
&&
3216 inst
->src
[1].ud
< (1 << 16)) {
3217 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3218 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3221 * If multiplying by an immediate value that fits in 16-bits, do a
3222 * single MUL instruction with that value in the proper location.
3224 if (devinfo
->gen
< 7) {
3225 fs_reg
imm(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3227 ibld
.MOV(imm
, inst
->src
[1]);
3228 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3230 ibld
.MUL(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3233 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3234 * do 32-bit integer multiplication in one instruction, but instead
3235 * must do a sequence (which actually calculates a 64-bit result):
3237 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3238 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3239 * mov(8) g2<1>D acc0<8,8,1>D
3241 * But on Gen > 6, the ability to use second accumulator register
3242 * (acc1) for non-float data types was removed, preventing a simple
3243 * implementation in SIMD16. A 16-channel result can be calculated by
3244 * executing the three instructions twice in SIMD8, once with quarter
3245 * control of 1Q for the first eight channels and again with 2Q for
3246 * the second eight channels.
3248 * Which accumulator register is implicitly accessed (by AccWrEnable
3249 * for instance) is determined by the quarter control. Unfortunately
3250 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3251 * implicit accumulator access by an instruction with 2Q will access
3252 * acc1 regardless of whether the data type is usable in acc1.
3254 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3255 * integer data types.
3257 * Since we only want the low 32-bits of the result, we can do two
3258 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3259 * adjust the high result and add them (like the mach is doing):
3261 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3262 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3263 * shl(8) g9<1>D g8<8,8,1>D 16D
3264 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3266 * We avoid the shl instruction by realizing that we only want to add
3267 * the low 16-bits of the "high" result to the high 16-bits of the
3268 * "low" result and using proper regioning on the add:
3270 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3271 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3272 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3274 * Since it does not use the (single) accumulator register, we can
3275 * schedule multi-component multiplications much better.
3278 fs_reg orig_dst
= inst
->dst
;
3279 if (orig_dst
.is_null() || orig_dst
.file
== MRF
) {
3280 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3283 fs_reg low
= inst
->dst
;
3284 fs_reg
high(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3287 if (devinfo
->gen
>= 7) {
3288 fs_reg src1_0_w
= inst
->src
[1];
3289 fs_reg src1_1_w
= inst
->src
[1];
3291 if (inst
->src
[1].file
== IMM
) {
3292 src1_0_w
.ud
&= 0xffff;
3295 src1_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3296 if (src1_0_w
.stride
!= 0) {
3297 assert(src1_0_w
.stride
== 1);
3298 src1_0_w
.stride
= 2;
3301 src1_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3302 if (src1_1_w
.stride
!= 0) {
3303 assert(src1_1_w
.stride
== 1);
3304 src1_1_w
.stride
= 2;
3306 src1_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3308 ibld
.MUL(low
, inst
->src
[0], src1_0_w
);
3309 ibld
.MUL(high
, inst
->src
[0], src1_1_w
);
3311 fs_reg src0_0_w
= inst
->src
[0];
3312 fs_reg src0_1_w
= inst
->src
[0];
3314 src0_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3315 if (src0_0_w
.stride
!= 0) {
3316 assert(src0_0_w
.stride
== 1);
3317 src0_0_w
.stride
= 2;
3320 src0_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3321 if (src0_1_w
.stride
!= 0) {
3322 assert(src0_1_w
.stride
== 1);
3323 src0_1_w
.stride
= 2;
3325 src0_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3327 ibld
.MUL(low
, src0_0_w
, inst
->src
[1]);
3328 ibld
.MUL(high
, src0_1_w
, inst
->src
[1]);
3331 fs_reg dst
= inst
->dst
;
3332 dst
.type
= BRW_REGISTER_TYPE_UW
;
3333 dst
.subreg_offset
= 2;
3336 high
.type
= BRW_REGISTER_TYPE_UW
;
3339 low
.type
= BRW_REGISTER_TYPE_UW
;
3340 low
.subreg_offset
= 2;
3343 ibld
.ADD(dst
, low
, high
);
3345 if (inst
->conditional_mod
|| orig_dst
.file
== MRF
) {
3346 set_condmod(inst
->conditional_mod
,
3347 ibld
.MOV(orig_dst
, inst
->dst
));
3351 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
3352 /* Should have been lowered to 8-wide. */
3353 assert(inst
->exec_size
<= 8);
3354 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
),
3356 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
3357 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3359 if (devinfo
->gen
>= 8) {
3360 /* Until Gen8, integer multiplies read 32-bits from one source,
3361 * and 16-bits from the other, and relying on the MACH instruction
3362 * to generate the high bits of the result.
3364 * On Gen8, the multiply instruction does a full 32x32-bit
3365 * multiply, but in order to do a 64-bit multiply we can simulate
3366 * the previous behavior and then use a MACH instruction.
3368 * FINISHME: Don't use source modifiers on src1.
3370 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
3371 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3372 mul
->src
[1].type
= (type_is_signed(mul
->src
[1].type
) ?
3373 BRW_REGISTER_TYPE_W
: BRW_REGISTER_TYPE_UW
);
3374 mul
->src
[1].stride
*= 2;
3376 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
3377 inst
->force_sechalf
) {
3378 /* Among other things the quarter control bits influence which
3379 * accumulator register is used by the hardware for instructions
3380 * that access the accumulator implicitly (e.g. MACH). A
3381 * second-half instruction would normally map to acc1, which
3382 * doesn't exist on Gen7 and up (the hardware does emulate it for
3383 * floating-point instructions *only* by taking advantage of the
3384 * extra precision of acc0 not normally used for floating point
3387 * HSW and up are careful enough not to try to access an
3388 * accumulator register that doesn't exist, but on earlier Gen7
3389 * hardware we need to make sure that the quarter control bits are
3390 * zero to avoid non-deterministic behaviour and emit an extra MOV
3391 * to get the result masked correctly according to the current
3394 mach
->force_sechalf
= false;
3395 mach
->force_writemask_all
= true;
3396 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
3397 ibld
.MOV(inst
->dst
, mach
->dst
);
3403 inst
->remove(block
);
3408 invalidate_live_intervals();
3414 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
3415 fs_reg
*dst
, fs_reg color
, unsigned components
)
3417 if (key
->clamp_fragment_color
) {
3418 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
3419 assert(color
.type
== BRW_REGISTER_TYPE_F
);
3421 for (unsigned i
= 0; i
< components
; i
++)
3423 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
3428 for (unsigned i
= 0; i
< components
; i
++)
3429 dst
[i
] = offset(color
, bld
, i
);
3433 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
3434 const brw_wm_prog_data
*prog_data
,
3435 const brw_wm_prog_key
*key
,
3436 const fs_visitor::thread_payload
&payload
)
3438 assert(inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
3439 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3440 const fs_reg
&color0
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR0
];
3441 const fs_reg
&color1
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
];
3442 const fs_reg
&src0_alpha
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA
];
3443 const fs_reg
&src_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
];
3444 const fs_reg
&dst_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_DST_DEPTH
];
3445 const fs_reg
&src_stencil
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_STENCIL
];
3446 fs_reg sample_mask
= inst
->src
[FB_WRITE_LOGICAL_SRC_OMASK
];
3447 const unsigned components
=
3448 inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
3450 /* We can potentially have a message length of up to 15, so we have to set
3451 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3454 int header_size
= 2, payload_header_size
;
3455 unsigned length
= 0;
3457 /* From the Sandy Bridge PRM, volume 4, page 198:
3459 * "Dispatched Pixel Enables. One bit per pixel indicating
3460 * which pixels were originally enabled when the thread was
3461 * dispatched. This field is only required for the end-of-
3462 * thread message and on all dual-source messages."
3464 if (devinfo
->gen
>= 6 &&
3465 (devinfo
->is_haswell
|| devinfo
->gen
>= 8 || !prog_data
->uses_kill
) &&
3466 color1
.file
== BAD_FILE
&&
3467 key
->nr_color_regions
== 1) {
3471 if (header_size
!= 0) {
3472 assert(header_size
== 2);
3473 /* Allocate 2 registers for a header */
3477 if (payload
.aa_dest_stencil_reg
) {
3478 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1));
3479 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3480 .MOV(sources
[length
],
3481 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0)));
3485 if (prog_data
->uses_omask
) {
3486 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1),
3487 BRW_REGISTER_TYPE_UD
);
3489 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3490 * relevant. Since it's unsigned single words one vgrf is always
3491 * 16-wide, but only the lower or higher 8 channels will be used by the
3492 * hardware when doing a SIMD8 write depending on whether we have
3493 * selected the subspans for the first or second half respectively.
3495 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
3496 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
3497 sample_mask
.stride
*= 2;
3499 bld
.exec_all().annotate("FB write oMask")
3500 .MOV(half(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
3501 inst
->force_sechalf
),
3506 payload_header_size
= length
;
3508 if (src0_alpha
.file
!= BAD_FILE
) {
3509 /* FIXME: This is being passed at the wrong location in the payload and
3510 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3511 * It's supposed to be immediately before oMask but there seems to be no
3512 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3513 * requires header sources to form a contiguous segment at the beginning
3514 * of the message and src0_alpha has per-channel semantics.
3516 setup_color_payload(bld
, key
, &sources
[length
], src0_alpha
, 1);
3520 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
3523 if (color1
.file
!= BAD_FILE
) {
3524 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
3528 if (src_depth
.file
!= BAD_FILE
) {
3529 sources
[length
] = src_depth
;
3533 if (dst_depth
.file
!= BAD_FILE
) {
3534 sources
[length
] = dst_depth
;
3538 if (src_stencil
.file
!= BAD_FILE
) {
3539 assert(devinfo
->gen
>= 9);
3540 assert(bld
.dispatch_width() != 16);
3542 /* XXX: src_stencil is only available on gen9+. dst_depth is never
3543 * available on gen9+. As such it's impossible to have both enabled at the
3544 * same time and therefore length cannot overrun the array.
3546 assert(length
< 15);
3548 sources
[length
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3549 bld
.exec_all().annotate("FB write OS")
3550 .emit(FS_OPCODE_PACK_STENCIL_REF
, sources
[length
],
3551 retype(src_stencil
, BRW_REGISTER_TYPE_UB
));
3556 if (devinfo
->gen
>= 7) {
3557 /* Send from the GRF */
3558 fs_reg payload
= fs_reg(VGRF
, -1, BRW_REGISTER_TYPE_F
);
3559 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
3560 payload
.nr
= bld
.shader
->alloc
.allocate(load
->regs_written
);
3561 load
->dst
= payload
;
3563 inst
->src
[0] = payload
;
3564 inst
->resize_sources(1);
3565 inst
->base_mrf
= -1;
3567 /* Send from the MRF */
3568 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3569 sources
, length
, payload_header_size
);
3571 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3572 * will do this for us if we just give it a COMPR4 destination.
3574 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
3575 load
->dst
.nr
|= BRW_MRF_COMPR4
;
3577 inst
->resize_sources(0);
3581 inst
->opcode
= FS_OPCODE_FB_WRITE
;
3582 inst
->mlen
= load
->regs_written
;
3583 inst
->header_size
= header_size
;
3587 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3588 const fs_reg
&coordinate
,
3589 const fs_reg
&shadow_c
,
3590 const fs_reg
&lod
, const fs_reg
&lod2
,
3591 const fs_reg
&sampler
,
3592 unsigned coord_components
,
3593 unsigned grad_components
)
3595 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
3596 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
3597 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
3598 fs_reg msg_end
= msg_begin
;
3601 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
3603 for (unsigned i
= 0; i
< coord_components
; i
++)
3604 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
3605 offset(coordinate
, bld
, i
));
3607 msg_end
= offset(msg_end
, bld
, coord_components
);
3609 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3610 * require all three components to be present and zero if they are unused.
3612 if (coord_components
> 0 &&
3613 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
3614 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
3615 for (unsigned i
= coord_components
; i
< 3; i
++)
3616 bld
.MOV(offset(msg_end
, bld
, i
), brw_imm_f(0.0f
));
3618 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
3621 if (op
== SHADER_OPCODE_TXD
) {
3622 /* TXD unsupported in SIMD16 mode. */
3623 assert(bld
.dispatch_width() == 8);
3625 /* the slots for u and v are always present, but r is optional */
3626 if (coord_components
< 2)
3627 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
3630 * dPdx = dudx, dvdx, drdx
3631 * dPdy = dudy, dvdy, drdy
3633 * 1-arg: Does not exist.
3635 * 2-arg: dudx dvdx dudy dvdy
3636 * dPdx.x dPdx.y dPdy.x dPdy.y
3639 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3640 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3641 * m5 m6 m7 m8 m9 m10
3643 for (unsigned i
= 0; i
< grad_components
; i
++)
3644 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
3646 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3648 for (unsigned i
= 0; i
< grad_components
; i
++)
3649 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
3651 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3655 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3656 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3658 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
3659 bld
.dispatch_width() == 16);
3661 const brw_reg_type type
=
3662 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
3663 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
3664 bld
.MOV(retype(msg_end
, type
), lod
);
3665 msg_end
= offset(msg_end
, bld
, 1);
3668 if (shadow_c
.file
!= BAD_FILE
) {
3669 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
3670 /* There's no plain shadow compare message, so we use shadow
3671 * compare with a bias of 0.0.
3673 bld
.MOV(msg_end
, brw_imm_f(0.0f
));
3674 msg_end
= offset(msg_end
, bld
, 1);
3677 bld
.MOV(msg_end
, shadow_c
);
3678 msg_end
= offset(msg_end
, bld
, 1);
3682 inst
->src
[0] = reg_undef
;
3683 inst
->src
[1] = sampler
;
3684 inst
->resize_sources(2);
3685 inst
->base_mrf
= msg_begin
.nr
;
3686 inst
->mlen
= msg_end
.nr
- msg_begin
.nr
;
3687 inst
->header_size
= 1;
3691 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3693 const fs_reg
&shadow_c
,
3694 fs_reg lod
, fs_reg lod2
,
3695 const fs_reg
&sample_index
,
3696 const fs_reg
&sampler
,
3697 const fs_reg
&offset_value
,
3698 unsigned coord_components
,
3699 unsigned grad_components
)
3701 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
3702 fs_reg msg_coords
= message
;
3703 unsigned header_size
= 0;
3705 if (offset_value
.file
!= BAD_FILE
) {
3706 /* The offsets set up by the visitor are in the m1 header, so we can't
3713 for (unsigned i
= 0; i
< coord_components
; i
++) {
3714 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
), coordinate
);
3715 coordinate
= offset(coordinate
, bld
, 1);
3717 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
3718 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
3720 if (shadow_c
.file
!= BAD_FILE
) {
3721 fs_reg msg_shadow
= msg_lod
;
3722 bld
.MOV(msg_shadow
, shadow_c
);
3723 msg_lod
= offset(msg_shadow
, bld
, 1);
3728 case SHADER_OPCODE_TXL
:
3730 bld
.MOV(msg_lod
, lod
);
3731 msg_end
= offset(msg_lod
, bld
, 1);
3733 case SHADER_OPCODE_TXD
:
3736 * dPdx = dudx, dvdx, drdx
3737 * dPdy = dudy, dvdy, drdy
3739 * Load up these values:
3740 * - dudx dudy dvdx dvdy drdx drdy
3741 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3744 for (unsigned i
= 0; i
< grad_components
; i
++) {
3745 bld
.MOV(msg_end
, lod
);
3746 lod
= offset(lod
, bld
, 1);
3747 msg_end
= offset(msg_end
, bld
, 1);
3749 bld
.MOV(msg_end
, lod2
);
3750 lod2
= offset(lod2
, bld
, 1);
3751 msg_end
= offset(msg_end
, bld
, 1);
3754 case SHADER_OPCODE_TXS
:
3755 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
3756 bld
.MOV(msg_lod
, lod
);
3757 msg_end
= offset(msg_lod
, bld
, 1);
3759 case SHADER_OPCODE_TXF
:
3760 msg_lod
= offset(msg_coords
, bld
, 3);
3761 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
3762 msg_end
= offset(msg_lod
, bld
, 1);
3764 case SHADER_OPCODE_TXF_CMS
:
3765 msg_lod
= offset(msg_coords
, bld
, 3);
3767 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), brw_imm_ud(0u));
3769 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
3770 msg_end
= offset(msg_lod
, bld
, 2);
3777 inst
->src
[0] = reg_undef
;
3778 inst
->src
[1] = sampler
;
3779 inst
->resize_sources(2);
3780 inst
->base_mrf
= message
.nr
;
3781 inst
->mlen
= msg_end
.nr
- message
.nr
;
3782 inst
->header_size
= header_size
;
3784 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3785 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3789 is_high_sampler(const struct brw_device_info
*devinfo
, const fs_reg
&sampler
)
3791 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
3794 return sampler
.file
!= IMM
|| sampler
.ud
>= 16;
3798 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3800 const fs_reg
&shadow_c
,
3801 fs_reg lod
, fs_reg lod2
,
3802 const fs_reg
&sample_index
,
3803 const fs_reg
&mcs
, const fs_reg
&sampler
,
3804 fs_reg offset_value
,
3805 unsigned coord_components
,
3806 unsigned grad_components
)
3808 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3809 int reg_width
= bld
.dispatch_width() / 8;
3810 unsigned header_size
= 0, length
= 0;
3811 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
3812 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
3813 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
3815 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
3816 offset_value
.file
!= BAD_FILE
||
3817 is_high_sampler(devinfo
, sampler
)) {
3818 /* For general texture offsets (no txf workaround), we need a header to
3819 * put them in. Note that we're only reserving space for it in the
3820 * message payload as it will be initialized implicitly by the
3823 * TG4 needs to place its channel select in the header, for interaction
3824 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3825 * larger sampler numbers we need to offset the Sampler State Pointer in
3829 sources
[0] = fs_reg();
3833 if (shadow_c
.file
!= BAD_FILE
) {
3834 bld
.MOV(sources
[length
], shadow_c
);
3838 bool coordinate_done
= false;
3840 /* The sampler can only meaningfully compute LOD for fragment shader
3841 * messages. For all other stages, we change the opcode to TXL and
3842 * hardcode the LOD to 0.
3844 if (bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
&&
3845 op
== SHADER_OPCODE_TEX
) {
3846 op
= SHADER_OPCODE_TXL
;
3847 lod
= brw_imm_f(0.0f
);
3850 /* Set up the LOD info */
3853 case SHADER_OPCODE_TXL
:
3854 bld
.MOV(sources
[length
], lod
);
3857 case SHADER_OPCODE_TXD
:
3858 /* TXD should have been lowered in SIMD16 mode. */
3859 assert(bld
.dispatch_width() == 8);
3861 /* Load dPdx and the coordinate together:
3862 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
3864 for (unsigned i
= 0; i
< coord_components
; i
++) {
3865 bld
.MOV(sources
[length
], coordinate
);
3866 coordinate
= offset(coordinate
, bld
, 1);
3869 /* For cube map array, the coordinate is (u,v,r,ai) but there are
3870 * only derivatives for (u, v, r).
3872 if (i
< grad_components
) {
3873 bld
.MOV(sources
[length
], lod
);
3874 lod
= offset(lod
, bld
, 1);
3877 bld
.MOV(sources
[length
], lod2
);
3878 lod2
= offset(lod2
, bld
, 1);
3883 coordinate_done
= true;
3885 case SHADER_OPCODE_TXS
:
3886 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
3889 case SHADER_OPCODE_TXF
:
3890 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
3891 * On Gen9 they are u, v, lod, r
3893 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3894 coordinate
= offset(coordinate
, bld
, 1);
3897 if (devinfo
->gen
>= 9) {
3898 if (coord_components
>= 2) {
3899 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3900 coordinate
= offset(coordinate
, bld
, 1);
3905 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
3908 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++) {
3909 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3910 coordinate
= offset(coordinate
, bld
, 1);
3914 coordinate_done
= true;
3916 case SHADER_OPCODE_TXF_CMS
:
3917 case SHADER_OPCODE_TXF_CMS_W
:
3918 case SHADER_OPCODE_TXF_UMS
:
3919 case SHADER_OPCODE_TXF_MCS
:
3920 if (op
== SHADER_OPCODE_TXF_UMS
||
3921 op
== SHADER_OPCODE_TXF_CMS
||
3922 op
== SHADER_OPCODE_TXF_CMS_W
) {
3923 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
3927 if (op
== SHADER_OPCODE_TXF_CMS
|| op
== SHADER_OPCODE_TXF_CMS_W
) {
3928 /* Data from the multisample control surface. */
3929 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
3932 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
3935 if (op
== SHADER_OPCODE_TXF_CMS_W
) {
3936 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
),
3939 offset(mcs
, bld
, 1));
3944 /* There is no offsetting for this message; just copy in the integer
3945 * texture coordinates.
3947 for (unsigned i
= 0; i
< coord_components
; i
++) {
3948 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3949 coordinate
= offset(coordinate
, bld
, 1);
3953 coordinate_done
= true;
3955 case SHADER_OPCODE_TG4_OFFSET
:
3956 /* gather4_po_c should have been lowered in SIMD16 mode. */
3957 assert(bld
.dispatch_width() == 8 || shadow_c
.file
== BAD_FILE
);
3959 /* More crazy intermixing */
3960 for (unsigned i
= 0; i
< 2; i
++) { /* u, v */
3961 bld
.MOV(sources
[length
], coordinate
);
3962 coordinate
= offset(coordinate
, bld
, 1);
3966 for (unsigned i
= 0; i
< 2; i
++) { /* offu, offv */
3967 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
);
3968 offset_value
= offset(offset_value
, bld
, 1);
3972 if (coord_components
== 3) { /* r if present */
3973 bld
.MOV(sources
[length
], coordinate
);
3974 coordinate
= offset(coordinate
, bld
, 1);
3978 coordinate_done
= true;
3984 /* Set up the coordinate (except for cases where it was done above) */
3985 if (!coordinate_done
) {
3986 for (unsigned i
= 0; i
< coord_components
; i
++) {
3987 bld
.MOV(sources
[length
], coordinate
);
3988 coordinate
= offset(coordinate
, bld
, 1);
3995 mlen
= length
* reg_width
- header_size
;
3997 mlen
= length
* reg_width
;
3999 const fs_reg src_payload
= fs_reg(VGRF
, bld
.shader
->alloc
.allocate(mlen
),
4000 BRW_REGISTER_TYPE_F
);
4001 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
4003 /* Generate the SEND. */
4005 inst
->src
[0] = src_payload
;
4006 inst
->src
[1] = sampler
;
4007 inst
->resize_sources(2);
4008 inst
->base_mrf
= -1;
4010 inst
->header_size
= header_size
;
4012 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4013 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
4017 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
4019 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
4020 const fs_reg
&coordinate
= inst
->src
[0];
4021 const fs_reg
&shadow_c
= inst
->src
[1];
4022 const fs_reg
&lod
= inst
->src
[2];
4023 const fs_reg
&lod2
= inst
->src
[3];
4024 const fs_reg
&sample_index
= inst
->src
[4];
4025 const fs_reg
&mcs
= inst
->src
[5];
4026 const fs_reg
&sampler
= inst
->src
[6];
4027 const fs_reg
&offset_value
= inst
->src
[7];
4028 assert(inst
->src
[8].file
== IMM
&& inst
->src
[9].file
== IMM
);
4029 const unsigned coord_components
= inst
->src
[8].ud
;
4030 const unsigned grad_components
= inst
->src
[9].ud
;
4032 if (devinfo
->gen
>= 7) {
4033 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
4034 shadow_c
, lod
, lod2
, sample_index
,
4035 mcs
, sampler
, offset_value
,
4036 coord_components
, grad_components
);
4037 } else if (devinfo
->gen
>= 5) {
4038 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
4039 shadow_c
, lod
, lod2
, sample_index
,
4040 sampler
, offset_value
,
4041 coord_components
, grad_components
);
4043 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
4044 shadow_c
, lod
, lod2
, sampler
,
4045 coord_components
, grad_components
);
4050 * Initialize the header present in some typed and untyped surface
4054 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
4056 fs_builder ubld
= bld
.exec_all().group(8, 0);
4057 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4058 ubld
.MOV(dst
, brw_imm_d(0));
4059 ubld
.MOV(component(dst
, 7), sample_mask
);
4064 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4065 const fs_reg
&sample_mask
)
4067 /* Get the logical send arguments. */
4068 const fs_reg
&addr
= inst
->src
[0];
4069 const fs_reg
&src
= inst
->src
[1];
4070 const fs_reg
&surface
= inst
->src
[2];
4071 const UNUSED fs_reg
&dims
= inst
->src
[3];
4072 const fs_reg
&arg
= inst
->src
[4];
4074 /* Calculate the total number of components of the payload. */
4075 const unsigned addr_sz
= inst
->components_read(0);
4076 const unsigned src_sz
= inst
->components_read(1);
4077 const unsigned header_sz
= (sample_mask
.file
== BAD_FILE
? 0 : 1);
4078 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
4080 /* Allocate space for the payload. */
4081 fs_reg
*const components
= new fs_reg
[sz
];
4082 const fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
4085 /* Construct the payload. */
4087 components
[n
++] = emit_surface_header(bld
, sample_mask
);
4089 for (unsigned i
= 0; i
< addr_sz
; i
++)
4090 components
[n
++] = offset(addr
, bld
, i
);
4092 for (unsigned i
= 0; i
< src_sz
; i
++)
4093 components
[n
++] = offset(src
, bld
, i
);
4095 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
4097 /* Update the original instruction. */
4099 inst
->mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
4100 inst
->header_size
= header_sz
;
4102 inst
->src
[0] = payload
;
4103 inst
->src
[1] = surface
;
4105 inst
->resize_sources(3);
4107 delete[] components
;
4111 fs_visitor::lower_logical_sends()
4113 bool progress
= false;
4115 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4116 const fs_builder
ibld(this, block
, inst
);
4118 switch (inst
->opcode
) {
4119 case FS_OPCODE_FB_WRITE_LOGICAL
:
4120 assert(stage
== MESA_SHADER_FRAGMENT
);
4121 lower_fb_write_logical_send(ibld
, inst
,
4122 (const brw_wm_prog_data
*)prog_data
,
4123 (const brw_wm_prog_key
*)key
,
4127 case SHADER_OPCODE_TEX_LOGICAL
:
4128 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
4131 case SHADER_OPCODE_TXD_LOGICAL
:
4132 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
4135 case SHADER_OPCODE_TXF_LOGICAL
:
4136 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
4139 case SHADER_OPCODE_TXL_LOGICAL
:
4140 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
4143 case SHADER_OPCODE_TXS_LOGICAL
:
4144 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
4147 case FS_OPCODE_TXB_LOGICAL
:
4148 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
4151 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
4152 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
4155 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
4156 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS_W
);
4159 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
4160 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
4163 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
4164 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
4167 case SHADER_OPCODE_LOD_LOGICAL
:
4168 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
4171 case SHADER_OPCODE_TG4_LOGICAL
:
4172 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
4175 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
4176 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
4179 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
4180 lower_surface_logical_send(ibld
, inst
,
4181 SHADER_OPCODE_UNTYPED_SURFACE_READ
,
4185 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
4186 lower_surface_logical_send(ibld
, inst
,
4187 SHADER_OPCODE_UNTYPED_SURFACE_WRITE
,
4188 ibld
.sample_mask_reg());
4191 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
4192 lower_surface_logical_send(ibld
, inst
,
4193 SHADER_OPCODE_UNTYPED_ATOMIC
,
4194 ibld
.sample_mask_reg());
4197 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4198 lower_surface_logical_send(ibld
, inst
,
4199 SHADER_OPCODE_TYPED_SURFACE_READ
,
4203 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4204 lower_surface_logical_send(ibld
, inst
,
4205 SHADER_OPCODE_TYPED_SURFACE_WRITE
,
4206 ibld
.sample_mask_reg());
4209 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4210 lower_surface_logical_send(ibld
, inst
,
4211 SHADER_OPCODE_TYPED_ATOMIC
,
4212 ibld
.sample_mask_reg());
4223 invalidate_live_intervals();
4229 * Get the closest native SIMD width supported by the hardware for instruction
4230 * \p inst. The instruction will be left untouched by
4231 * fs_visitor::lower_simd_width() if the returned value is equal to the
4232 * original execution size.
4235 get_lowered_simd_width(const struct brw_device_info
*devinfo
,
4236 const fs_inst
*inst
)
4238 switch (inst
->opcode
) {
4239 case BRW_OPCODE_MOV
:
4240 case BRW_OPCODE_SEL
:
4241 case BRW_OPCODE_NOT
:
4242 case BRW_OPCODE_AND
:
4244 case BRW_OPCODE_XOR
:
4245 case BRW_OPCODE_SHR
:
4246 case BRW_OPCODE_SHL
:
4247 case BRW_OPCODE_ASR
:
4248 case BRW_OPCODE_CMP
:
4249 case BRW_OPCODE_CMPN
:
4250 case BRW_OPCODE_CSEL
:
4251 case BRW_OPCODE_F32TO16
:
4252 case BRW_OPCODE_F16TO32
:
4253 case BRW_OPCODE_BFREV
:
4254 case BRW_OPCODE_BFE
:
4255 case BRW_OPCODE_BFI1
:
4256 case BRW_OPCODE_BFI2
:
4257 case BRW_OPCODE_ADD
:
4258 case BRW_OPCODE_MUL
:
4259 case BRW_OPCODE_AVG
:
4260 case BRW_OPCODE_FRC
:
4261 case BRW_OPCODE_RNDU
:
4262 case BRW_OPCODE_RNDD
:
4263 case BRW_OPCODE_RNDE
:
4264 case BRW_OPCODE_RNDZ
:
4265 case BRW_OPCODE_LZD
:
4266 case BRW_OPCODE_FBH
:
4267 case BRW_OPCODE_FBL
:
4268 case BRW_OPCODE_CBIT
:
4269 case BRW_OPCODE_SAD2
:
4270 case BRW_OPCODE_MAD
:
4271 case BRW_OPCODE_LRP
:
4272 case SHADER_OPCODE_RCP
:
4273 case SHADER_OPCODE_RSQ
:
4274 case SHADER_OPCODE_SQRT
:
4275 case SHADER_OPCODE_EXP2
:
4276 case SHADER_OPCODE_LOG2
:
4277 case SHADER_OPCODE_POW
:
4278 case SHADER_OPCODE_INT_QUOTIENT
:
4279 case SHADER_OPCODE_INT_REMAINDER
:
4280 case SHADER_OPCODE_SIN
:
4281 case SHADER_OPCODE_COS
: {
4282 /* According to the PRMs:
4283 * "A. In Direct Addressing mode, a source cannot span more than 2
4284 * adjacent GRF registers.
4285 * B. A destination cannot span more than 2 adjacent GRF registers."
4287 * Look for the source or destination with the largest register region
4288 * which is the one that is going to limit the overal execution size of
4289 * the instruction due to this rule.
4291 unsigned reg_count
= inst
->regs_written
;
4293 for (unsigned i
= 0; i
< inst
->sources
; i
++)
4294 reg_count
= MAX2(reg_count
, (unsigned)inst
->regs_read(i
));
4296 /* Calculate the maximum execution size of the instruction based on the
4297 * factor by which it goes over the hardware limit of 2 GRFs.
4299 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
4301 case SHADER_OPCODE_MULH
:
4302 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
4303 * is 8-wide on Gen7+.
4305 return (devinfo
->gen
>= 7 ? 8 : inst
->exec_size
);
4307 case FS_OPCODE_FB_WRITE_LOGICAL
:
4308 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4311 assert(devinfo
->gen
!= 6 ||
4312 inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
].file
== BAD_FILE
||
4313 inst
->exec_size
== 8);
4314 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4315 return (inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
].file
!= BAD_FILE
?
4316 8 : inst
->exec_size
);
4318 case SHADER_OPCODE_TXD_LOGICAL
:
4319 /* TXD is unsupported in SIMD16 mode. */
4322 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
: {
4323 /* gather4_po_c is unsupported in SIMD16 mode. */
4324 const fs_reg
&shadow_c
= inst
->src
[1];
4325 return (shadow_c
.file
!= BAD_FILE
? 8 : inst
->exec_size
);
4327 case SHADER_OPCODE_TXL_LOGICAL
:
4328 case FS_OPCODE_TXB_LOGICAL
: {
4329 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4330 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4331 * mode because the message exceeds the maximum length of 11.
4333 const fs_reg
&shadow_c
= inst
->src
[1];
4334 if (devinfo
->gen
== 4 && shadow_c
.file
== BAD_FILE
)
4336 else if (devinfo
->gen
< 7 && shadow_c
.file
!= BAD_FILE
)
4339 return inst
->exec_size
;
4341 case SHADER_OPCODE_TXF_LOGICAL
:
4342 case SHADER_OPCODE_TXS_LOGICAL
:
4343 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4344 * messages. Use SIMD16 instead.
4346 if (devinfo
->gen
== 4)
4349 return inst
->exec_size
;
4351 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
: {
4352 /* This opcode can take up to 6 arguments which means that in some
4353 * circumstances it can end up with a message that is too long in SIMD16
4356 const unsigned coord_components
= inst
->src
[8].ud
;
4357 /* First three arguments are the sample index and the two arguments for
4360 if ((coord_components
+ 3) * 2 > MAX_SAMPLER_MESSAGE_SIZE
)
4363 return inst
->exec_size
;
4366 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4367 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4368 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4372 return inst
->exec_size
;
4377 * The \p rows array of registers represents a \p num_rows by \p num_columns
4378 * matrix in row-major order, write it in column-major order into the register
4379 * passed as destination. \p stride gives the separation between matrix
4380 * elements in the input in fs_builder::dispatch_width() units.
4383 emit_transpose(const fs_builder
&bld
,
4384 const fs_reg
&dst
, const fs_reg
*rows
,
4385 unsigned num_rows
, unsigned num_columns
, unsigned stride
)
4387 fs_reg
*const components
= new fs_reg
[num_rows
* num_columns
];
4389 for (unsigned i
= 0; i
< num_columns
; ++i
) {
4390 for (unsigned j
= 0; j
< num_rows
; ++j
)
4391 components
[num_rows
* i
+ j
] = offset(rows
[j
], bld
, stride
* i
);
4394 bld
.LOAD_PAYLOAD(dst
, components
, num_rows
* num_columns
, 0);
4396 delete[] components
;
4400 fs_visitor::lower_simd_width()
4402 bool progress
= false;
4404 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4405 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
4407 if (lower_width
!= inst
->exec_size
) {
4408 /* Builder matching the original instruction. We may also need to
4409 * emit an instruction of width larger than the original, set the
4410 * execution size of the builder to the highest of both for now so
4411 * we're sure that both cases can be handled.
4413 const fs_builder ibld
= bld
.at(block
, inst
)
4414 .exec_all(inst
->force_writemask_all
)
4415 .group(MAX2(inst
->exec_size
, lower_width
),
4416 inst
->force_sechalf
);
4418 /* Split the copies in chunks of the execution width of either the
4419 * original or the lowered instruction, whichever is lower.
4421 const unsigned copy_width
= MIN2(lower_width
, inst
->exec_size
);
4422 const unsigned n
= inst
->exec_size
/ copy_width
;
4423 const unsigned dst_size
= inst
->regs_written
* REG_SIZE
/
4424 inst
->dst
.component_size(inst
->exec_size
);
4427 assert(n
> 0 && n
<= ARRAY_SIZE(dsts
) &&
4428 !inst
->writes_accumulator
&& !inst
->mlen
);
4430 for (unsigned i
= 0; i
< n
; i
++) {
4431 /* Emit a copy of the original instruction with the lowered width.
4432 * If the EOT flag was set throw it away except for the last
4433 * instruction to avoid killing the thread prematurely.
4435 fs_inst split_inst
= *inst
;
4436 split_inst
.exec_size
= lower_width
;
4437 split_inst
.eot
= inst
->eot
&& i
== n
- 1;
4439 /* Select the correct channel enables for the i-th group, then
4440 * transform the sources and destination and emit the lowered
4443 const fs_builder lbld
= ibld
.group(lower_width
, i
);
4445 for (unsigned j
= 0; j
< inst
->sources
; j
++) {
4446 if (inst
->src
[j
].file
!= BAD_FILE
&&
4447 !is_uniform(inst
->src
[j
])) {
4448 /* Get the i-th copy_width-wide chunk of the source. */
4449 const fs_reg src
= horiz_offset(inst
->src
[j
], copy_width
* i
);
4450 const unsigned src_size
= inst
->components_read(j
);
4452 /* Use a trivial transposition to copy one every n
4453 * copy_width-wide components of the register into a
4454 * temporary passed as source to the lowered instruction.
4456 split_inst
.src
[j
] = lbld
.vgrf(inst
->src
[j
].type
, src_size
);
4457 emit_transpose(lbld
.group(copy_width
, 0),
4458 split_inst
.src
[j
], &src
, 1, src_size
, n
);
4462 if (inst
->regs_written
) {
4463 /* Allocate enough space to hold the result of the lowered
4464 * instruction and fix up the number of registers written.
4466 split_inst
.dst
= dsts
[i
] =
4467 lbld
.vgrf(inst
->dst
.type
, dst_size
);
4468 split_inst
.regs_written
=
4469 DIV_ROUND_UP(inst
->regs_written
* lower_width
,
4473 lbld
.emit(split_inst
);
4476 if (inst
->regs_written
) {
4477 /* Distance between useful channels in the temporaries, skipping
4478 * garbage if the lowered instruction is wider than the original.
4480 const unsigned m
= lower_width
/ copy_width
;
4482 /* Interleave the components of the result from the lowered
4483 * instructions. We need to set exec_all() when copying more than
4484 * one half per component, because LOAD_PAYLOAD (in terms of which
4485 * emit_transpose is implemented) can only use the same channel
4486 * enable signals for all of its non-header sources.
4488 emit_transpose(ibld
.exec_all(inst
->exec_size
> copy_width
)
4489 .group(copy_width
, 0),
4490 inst
->dst
, dsts
, n
, dst_size
, m
);
4493 inst
->remove(block
);
4499 invalidate_live_intervals();
4505 fs_visitor::dump_instructions()
4507 dump_instructions(NULL
);
4511 fs_visitor::dump_instructions(const char *name
)
4513 FILE *file
= stderr
;
4514 if (name
&& geteuid() != 0) {
4515 file
= fopen(name
, "w");
4521 calculate_register_pressure();
4522 int ip
= 0, max_pressure
= 0;
4523 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
4524 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
4525 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
4526 dump_instruction(inst
, file
);
4529 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
4532 foreach_in_list(backend_instruction
, inst
, &instructions
) {
4533 fprintf(file
, "%4d: ", ip
++);
4534 dump_instruction(inst
, file
);
4538 if (file
!= stderr
) {
4544 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
4546 dump_instruction(be_inst
, stderr
);
4550 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
4552 fs_inst
*inst
= (fs_inst
*)be_inst
;
4554 if (inst
->predicate
) {
4555 fprintf(file
, "(%cf0.%d) ",
4556 inst
->predicate_inverse
? '-' : '+',
4560 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
4562 fprintf(file
, ".sat");
4563 if (inst
->conditional_mod
) {
4564 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
4565 if (!inst
->predicate
&&
4566 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
4567 inst
->opcode
!= BRW_OPCODE_IF
&&
4568 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
4569 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
4572 fprintf(file
, "(%d) ", inst
->exec_size
);
4575 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
4578 switch (inst
->dst
.file
) {
4580 fprintf(file
, "vgrf%d", inst
->dst
.nr
);
4581 if (alloc
.sizes
[inst
->dst
.nr
] != inst
->regs_written
||
4582 inst
->dst
.subreg_offset
)
4583 fprintf(file
, "+%d.%d",
4584 inst
->dst
.reg_offset
, inst
->dst
.subreg_offset
);
4587 fprintf(file
, "g%d", inst
->dst
.nr
);
4590 fprintf(file
, "m%d", inst
->dst
.nr
);
4593 fprintf(file
, "(null)");
4596 fprintf(file
, "***u%d***", inst
->dst
.nr
+ inst
->dst
.reg_offset
);
4599 fprintf(file
, "***attr%d***", inst
->dst
.nr
+ inst
->dst
.reg_offset
);
4602 switch (inst
->dst
.nr
) {
4604 fprintf(file
, "null");
4606 case BRW_ARF_ADDRESS
:
4607 fprintf(file
, "a0.%d", inst
->dst
.subnr
);
4609 case BRW_ARF_ACCUMULATOR
:
4610 fprintf(file
, "acc%d", inst
->dst
.subnr
);
4613 fprintf(file
, "f%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
4616 fprintf(file
, "arf%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
4619 if (inst
->dst
.subnr
)
4620 fprintf(file
, "+%d", inst
->dst
.subnr
);
4623 unreachable("not reached");
4625 if (inst
->dst
.stride
!= 1)
4626 fprintf(file
, "<%u>", inst
->dst
.stride
);
4627 fprintf(file
, ":%s, ", brw_reg_type_letters(inst
->dst
.type
));
4629 for (int i
= 0; i
< inst
->sources
; i
++) {
4630 if (inst
->src
[i
].negate
)
4632 if (inst
->src
[i
].abs
)
4634 switch (inst
->src
[i
].file
) {
4636 fprintf(file
, "vgrf%d", inst
->src
[i
].nr
);
4637 if (alloc
.sizes
[inst
->src
[i
].nr
] != (unsigned)inst
->regs_read(i
) ||
4638 inst
->src
[i
].subreg_offset
)
4639 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4640 inst
->src
[i
].subreg_offset
);
4643 fprintf(file
, "g%d", inst
->src
[i
].nr
);
4646 fprintf(file
, "***m%d***", inst
->src
[i
].nr
);
4649 fprintf(file
, "attr%d+%d", inst
->src
[i
].nr
, inst
->src
[i
].reg_offset
);
4652 fprintf(file
, "u%d", inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
);
4653 if (inst
->src
[i
].reladdr
) {
4654 fprintf(file
, "+reladdr");
4655 } else if (inst
->src
[i
].subreg_offset
) {
4656 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4657 inst
->src
[i
].subreg_offset
);
4661 fprintf(file
, "(null)");
4664 switch (inst
->src
[i
].type
) {
4665 case BRW_REGISTER_TYPE_F
:
4666 fprintf(file
, "%ff", inst
->src
[i
].f
);
4668 case BRW_REGISTER_TYPE_W
:
4669 case BRW_REGISTER_TYPE_D
:
4670 fprintf(file
, "%dd", inst
->src
[i
].d
);
4672 case BRW_REGISTER_TYPE_UW
:
4673 case BRW_REGISTER_TYPE_UD
:
4674 fprintf(file
, "%uu", inst
->src
[i
].ud
);
4676 case BRW_REGISTER_TYPE_VF
:
4677 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
4678 brw_vf_to_float((inst
->src
[i
].ud
>> 0) & 0xff),
4679 brw_vf_to_float((inst
->src
[i
].ud
>> 8) & 0xff),
4680 brw_vf_to_float((inst
->src
[i
].ud
>> 16) & 0xff),
4681 brw_vf_to_float((inst
->src
[i
].ud
>> 24) & 0xff));
4684 fprintf(file
, "???");
4689 switch (inst
->src
[i
].nr
) {
4691 fprintf(file
, "null");
4693 case BRW_ARF_ADDRESS
:
4694 fprintf(file
, "a0.%d", inst
->src
[i
].subnr
);
4696 case BRW_ARF_ACCUMULATOR
:
4697 fprintf(file
, "acc%d", inst
->src
[i
].subnr
);
4700 fprintf(file
, "f%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
4703 fprintf(file
, "arf%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
4706 if (inst
->src
[i
].subnr
)
4707 fprintf(file
, "+%d", inst
->src
[i
].subnr
);
4710 if (inst
->src
[i
].abs
)
4713 if (inst
->src
[i
].file
!= IMM
) {
4715 if (inst
->src
[i
].file
== ARF
|| inst
->src
[i
].file
== FIXED_GRF
) {
4716 unsigned hstride
= inst
->src
[i
].hstride
;
4717 stride
= (hstride
== 0 ? 0 : (1 << (hstride
- 1)));
4719 stride
= inst
->src
[i
].stride
;
4722 fprintf(file
, "<%u>", stride
);
4724 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
4727 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
4728 fprintf(file
, ", ");
4733 if (inst
->force_writemask_all
)
4734 fprintf(file
, "NoMask ");
4736 if (dispatch_width
== 16 && inst
->exec_size
== 8) {
4737 if (inst
->force_sechalf
)
4738 fprintf(file
, "2ndhalf ");
4740 fprintf(file
, "1sthalf ");
4743 fprintf(file
, "\n");
4747 * Possibly returns an instruction that set up @param reg.
4749 * Sometimes we want to take the result of some expression/variable
4750 * dereference tree and rewrite the instruction generating the result
4751 * of the tree. When processing the tree, we know that the
4752 * instructions generated are all writing temporaries that are dead
4753 * outside of this tree. So, if we have some instructions that write
4754 * a temporary, we're free to point that temp write somewhere else.
4756 * Note that this doesn't guarantee that the instruction generated
4757 * only reg -- it might be the size=4 destination of a texture instruction.
4760 fs_visitor::get_instruction_generating_reg(fs_inst
*start
,
4765 end
->is_partial_write() ||
4767 !reg
.equals(end
->dst
)) {
4775 fs_visitor::setup_payload_gen6()
4778 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
4779 unsigned barycentric_interp_modes
=
4780 (stage
== MESA_SHADER_FRAGMENT
) ?
4781 ((brw_wm_prog_data
*) this->prog_data
)->barycentric_interp_modes
: 0;
4783 assert(devinfo
->gen
>= 6);
4785 /* R0-1: masks, pixel X/Y coordinates. */
4786 payload
.num_regs
= 2;
4787 /* R2: only for 32-pixel dispatch.*/
4789 /* R3-26: barycentric interpolation coordinates. These appear in the
4790 * same order that they appear in the brw_wm_barycentric_interp_mode
4791 * enum. Each set of coordinates occupies 2 registers if dispatch width
4792 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4793 * appear if they were enabled using the "Barycentric Interpolation
4794 * Mode" bits in WM_STATE.
4796 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
4797 if (barycentric_interp_modes
& (1 << i
)) {
4798 payload
.barycentric_coord_reg
[i
] = payload
.num_regs
;
4799 payload
.num_regs
+= 2;
4800 if (dispatch_width
== 16) {
4801 payload
.num_regs
+= 2;
4806 /* R27: interpolated depth if uses source depth */
4808 payload
.source_depth_reg
= payload
.num_regs
;
4810 if (dispatch_width
== 16) {
4811 /* R28: interpolated depth if not SIMD8. */
4815 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4817 payload
.source_w_reg
= payload
.num_regs
;
4819 if (dispatch_width
== 16) {
4820 /* R30: interpolated W if not SIMD8. */
4825 if (stage
== MESA_SHADER_FRAGMENT
) {
4826 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4827 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
4828 prog_data
->uses_pos_offset
= key
->compute_pos_offset
;
4829 /* R31: MSAA position offsets. */
4830 if (prog_data
->uses_pos_offset
) {
4831 payload
.sample_pos_reg
= payload
.num_regs
;
4836 /* R32: MSAA input coverage mask */
4837 if (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
) {
4838 assert(devinfo
->gen
>= 7);
4839 payload
.sample_mask_in_reg
= payload
.num_regs
;
4841 if (dispatch_width
== 16) {
4842 /* R33: input coverage mask if not SIMD8. */
4847 /* R34-: bary for 32-pixel. */
4848 /* R58-59: interp W for 32-pixel. */
4850 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
4851 source_depth_to_render_target
= true;
4856 fs_visitor::setup_vs_payload()
4858 /* R0: thread header, R1: urb handles */
4859 payload
.num_regs
= 2;
4863 * We are building the local ID push constant data using the simplest possible
4864 * method. We simply push the local IDs directly as they should appear in the
4865 * registers for the uvec3 gl_LocalInvocationID variable.
4867 * Therefore, for SIMD8, we use 3 full registers, and for SIMD16 we use 6
4868 * registers worth of push constant space.
4870 * Note: Any updates to brw_cs_prog_local_id_payload_dwords,
4871 * fill_local_id_payload or fs_visitor::emit_cs_local_invocation_id_setup need
4874 * FINISHME: There are a few easy optimizations to consider.
4876 * 1. If gl_WorkGroupSize x, y or z is 1, we can just use zero, and there is
4877 * no need for using push constant space for that dimension.
4879 * 2. Since GL_MAX_COMPUTE_WORK_GROUP_SIZE is currently 1024 or less, we can
4880 * easily use 16-bit words rather than 32-bit dwords in the push constant
4883 * 3. If gl_WorkGroupSize x, y or z is small, then we can use bytes for
4884 * conveying the data, and thereby reduce push constant usage.
4888 fs_visitor::setup_gs_payload()
4890 assert(stage
== MESA_SHADER_GEOMETRY
);
4892 struct brw_gs_prog_data
*gs_prog_data
=
4893 (struct brw_gs_prog_data
*) prog_data
;
4894 struct brw_vue_prog_data
*vue_prog_data
=
4895 (struct brw_vue_prog_data
*) prog_data
;
4897 /* R0: thread header, R1: output URB handles */
4898 payload
.num_regs
= 2;
4900 if (gs_prog_data
->include_primitive_id
) {
4901 /* R2: Primitive ID 0..7 */
4905 /* Use a maximum of 32 registers for push-model inputs. */
4906 const unsigned max_push_components
= 32;
4908 /* If pushing our inputs would take too many registers, reduce the URB read
4909 * length (which is in HWords, or 8 registers), and resort to pulling.
4911 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
4912 * have to multiply by VerticesIn to obtain the total storage requirement.
4914 if (8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
>
4915 max_push_components
) {
4916 gs_prog_data
->base
.include_vue_handles
= true;
4918 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
4919 payload
.num_regs
+= nir
->info
.gs
.vertices_in
;
4921 vue_prog_data
->urb_read_length
=
4922 ROUND_DOWN_TO(max_push_components
/ nir
->info
.gs
.vertices_in
, 8) / 8;
4927 fs_visitor::setup_cs_payload()
4929 assert(devinfo
->gen
>= 7);
4930 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
4932 payload
.num_regs
= 1;
4934 if (nir
->info
.system_values_read
& SYSTEM_BIT_LOCAL_INVOCATION_ID
) {
4935 prog_data
->local_invocation_id_regs
= dispatch_width
* 3 / 8;
4936 payload
.local_invocation_id_reg
= payload
.num_regs
;
4937 payload
.num_regs
+= prog_data
->local_invocation_id_regs
;
4942 fs_visitor::calculate_register_pressure()
4944 invalidate_live_intervals();
4945 calculate_live_intervals();
4947 unsigned num_instructions
= 0;
4948 foreach_block(block
, cfg
)
4949 num_instructions
+= block
->instructions
.length();
4951 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
4953 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
4954 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
4955 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
4960 fs_visitor::optimize()
4962 /* Start by validating the shader we currently have. */
4965 /* bld is the common builder object pointing at the end of the program we
4966 * used to translate it into i965 IR. For the optimization and lowering
4967 * passes coming next, any code added after the end of the program without
4968 * having explicitly called fs_builder::at() clearly points at a mistake.
4969 * Ideally optimization passes wouldn't be part of the visitor so they
4970 * wouldn't have access to bld at all, but they do, so just in case some
4971 * pass forgets to ask for a location explicitly set it to NULL here to
4972 * make it trip. The dispatch width is initialized to a bogus value to
4973 * make sure that optimizations set the execution controls explicitly to
4974 * match the code they are manipulating instead of relying on the defaults.
4976 bld
= fs_builder(this, 64);
4978 assign_constant_locations();
4979 demote_pull_constants();
4983 split_virtual_grfs();
4986 #define OPT(pass, args...) ({ \
4988 bool this_progress = pass(args); \
4990 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
4991 char filename[64]; \
4992 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
4993 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
4995 backend_shader::dump_instructions(filename); \
5000 progress = progress || this_progress; \
5004 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
5006 snprintf(filename
, 64, "%s%d-%s-00-start",
5007 stage_abbrev
, dispatch_width
, nir
->info
.name
);
5009 backend_shader::dump_instructions(filename
);
5012 bool progress
= false;
5016 OPT(lower_simd_width
);
5017 OPT(lower_logical_sends
);
5024 OPT(remove_duplicate_mrf_writes
);
5028 OPT(opt_copy_propagate
);
5029 OPT(opt_predicated_break
, this);
5030 OPT(opt_cmod_propagation
);
5031 OPT(dead_code_eliminate
);
5032 OPT(opt_peephole_sel
);
5033 OPT(dead_control_flow_eliminate
, this);
5034 OPT(opt_register_renaming
);
5035 OPT(opt_redundant_discard_jumps
);
5036 OPT(opt_saturate_propagation
);
5037 OPT(opt_zero_samples
);
5038 OPT(register_coalesce
);
5039 OPT(compute_to_mrf
);
5040 OPT(eliminate_find_live_channel
);
5042 OPT(compact_virtual_grfs
);
5047 OPT(opt_sampler_eot
);
5049 if (OPT(lower_load_payload
)) {
5050 split_virtual_grfs();
5051 OPT(register_coalesce
);
5052 OPT(compute_to_mrf
);
5053 OPT(dead_code_eliminate
);
5056 OPT(opt_combine_constants
);
5057 OPT(lower_integer_multiplication
);
5059 lower_uniform_pull_constant_loads();
5065 * Three source instruction must have a GRF/MRF destination register.
5066 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
5069 fs_visitor::fixup_3src_null_dest()
5071 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
5072 if (inst
->is_3src() && inst
->dst
.is_null()) {
5073 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
5080 fs_visitor::allocate_registers()
5082 bool allocated_without_spills
;
5084 static const enum instruction_scheduler_mode pre_modes
[] = {
5086 SCHEDULE_PRE_NON_LIFO
,
5090 /* Try each scheduling heuristic to see if it can successfully register
5091 * allocate without spilling. They should be ordered by decreasing
5092 * performance but increasing likelihood of allocating.
5094 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
5095 schedule_instructions(pre_modes
[i
]);
5098 assign_regs_trivial();
5099 allocated_without_spills
= true;
5101 allocated_without_spills
= assign_regs(false);
5103 if (allocated_without_spills
)
5107 if (!allocated_without_spills
) {
5108 /* We assume that any spilling is worse than just dropping back to
5109 * SIMD8. There's probably actually some intermediate point where
5110 * SIMD16 with a couple of spills is still better.
5112 if (dispatch_width
== 16) {
5113 fail("Failure to register allocate. Reduce number of "
5114 "live scalar values to avoid this.");
5116 compiler
->shader_perf_log(log_data
,
5117 "%s shader triggered register spilling. "
5118 "Try reducing the number of live scalar "
5119 "values to improve performance.\n",
5123 /* Since we're out of heuristics, just go spill registers until we
5124 * get an allocation.
5126 while (!assign_regs(true)) {
5132 /* This must come after all optimization and register allocation, since
5133 * it inserts dead code that happens to have side effects, and it does
5134 * so based on the actual physical registers in use.
5136 insert_gen4_send_dependency_workarounds();
5141 schedule_instructions(SCHEDULE_POST
);
5143 if (last_scratch
> 0)
5144 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
5148 fs_visitor::run_vs(gl_clip_plane
*clip_planes
)
5150 assert(stage
== MESA_SHADER_VERTEX
);
5154 if (shader_time_index
>= 0)
5155 emit_shader_time_begin();
5162 compute_clip_distance(clip_planes
);
5166 if (shader_time_index
>= 0)
5167 emit_shader_time_end();
5173 assign_curb_setup();
5174 assign_vs_urb_setup();
5176 fixup_3src_null_dest();
5177 allocate_registers();
5183 fs_visitor::run_gs()
5185 assert(stage
== MESA_SHADER_GEOMETRY
);
5189 this->final_gs_vertex_count
= vgrf(glsl_type::uint_type
);
5191 if (gs_compile
->control_data_header_size_bits
> 0) {
5192 /* Create a VGRF to store accumulated control data bits. */
5193 this->control_data_bits
= vgrf(glsl_type::uint_type
);
5195 /* If we're outputting more than 32 control data bits, then EmitVertex()
5196 * will set control_data_bits to 0 after emitting the first vertex.
5197 * Otherwise, we need to initialize it to 0 here.
5199 if (gs_compile
->control_data_header_size_bits
<= 32) {
5200 const fs_builder abld
= bld
.annotate("initialize control data bits");
5201 abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
5205 if (shader_time_index
>= 0)
5206 emit_shader_time_begin();
5210 emit_gs_thread_end();
5212 if (shader_time_index
>= 0)
5213 emit_shader_time_end();
5222 assign_curb_setup();
5223 assign_gs_urb_setup();
5225 fixup_3src_null_dest();
5226 allocate_registers();
5232 fs_visitor::run_fs(bool do_rep_send
)
5234 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
5235 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
5237 assert(stage
== MESA_SHADER_FRAGMENT
);
5239 if (devinfo
->gen
>= 6)
5240 setup_payload_gen6();
5242 setup_payload_gen4();
5246 } else if (do_rep_send
) {
5247 assert(dispatch_width
== 16);
5248 emit_repclear_shader();
5250 if (shader_time_index
>= 0)
5251 emit_shader_time_begin();
5253 calculate_urb_setup();
5254 if (nir
->info
.inputs_read
> 0) {
5255 if (devinfo
->gen
< 6)
5256 emit_interpolation_setup_gen4();
5258 emit_interpolation_setup_gen6();
5261 /* We handle discards by keeping track of the still-live pixels in f0.1.
5262 * Initialize it with the dispatched pixels.
5264 if (wm_prog_data
->uses_kill
) {
5265 fs_inst
*discard_init
= bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
5266 discard_init
->flag_subreg
= 1;
5269 /* Generate FS IR for main(). (the visitor only descends into
5270 * functions called "main").
5277 if (wm_prog_data
->uses_kill
)
5278 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
5280 if (wm_key
->alpha_test_func
)
5285 if (shader_time_index
>= 0)
5286 emit_shader_time_end();
5292 assign_curb_setup();
5295 fixup_3src_null_dest();
5296 allocate_registers();
5302 if (dispatch_width
== 8)
5303 wm_prog_data
->reg_blocks
= brw_register_blocks(grf_used
);
5305 wm_prog_data
->reg_blocks_16
= brw_register_blocks(grf_used
);
5311 fs_visitor::run_cs()
5313 assert(stage
== MESA_SHADER_COMPUTE
);
5317 if (shader_time_index
>= 0)
5318 emit_shader_time_begin();
5325 emit_cs_terminate();
5327 if (shader_time_index
>= 0)
5328 emit_shader_time_end();
5334 assign_curb_setup();
5336 fixup_3src_null_dest();
5337 allocate_registers();
5346 * Return a bitfield where bit n is set if barycentric interpolation mode n
5347 * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
5350 brw_compute_barycentric_interp_modes(const struct brw_device_info
*devinfo
,
5351 bool shade_model_flat
,
5352 bool persample_shading
,
5353 const nir_shader
*shader
)
5355 unsigned barycentric_interp_modes
= 0;
5357 nir_foreach_variable(var
, &shader
->inputs
) {
5358 enum glsl_interp_qualifier interp_qualifier
=
5359 (enum glsl_interp_qualifier
)var
->data
.interpolation
;
5360 bool is_centroid
= var
->data
.centroid
&& !persample_shading
;
5361 bool is_sample
= var
->data
.sample
|| persample_shading
;
5362 bool is_gl_Color
= (var
->data
.location
== VARYING_SLOT_COL0
) ||
5363 (var
->data
.location
== VARYING_SLOT_COL1
);
5365 /* Ignore WPOS and FACE, because they don't require interpolation. */
5366 if (var
->data
.location
== VARYING_SLOT_POS
||
5367 var
->data
.location
== VARYING_SLOT_FACE
)
5370 /* Determine the set (or sets) of barycentric coordinates needed to
5371 * interpolate this variable. Note that when
5372 * brw->needs_unlit_centroid_workaround is set, centroid interpolation
5373 * uses PIXEL interpolation for unlit pixels and CENTROID interpolation
5374 * for lit pixels, so we need both sets of barycentric coordinates.
5376 if (interp_qualifier
== INTERP_QUALIFIER_NOPERSPECTIVE
) {
5378 barycentric_interp_modes
|=
5379 1 << BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
5380 } else if (is_sample
) {
5381 barycentric_interp_modes
|=
5382 1 << BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
5384 if ((!is_centroid
&& !is_sample
) ||
5385 devinfo
->needs_unlit_centroid_workaround
) {
5386 barycentric_interp_modes
|=
5387 1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
5389 } else if (interp_qualifier
== INTERP_QUALIFIER_SMOOTH
||
5390 (!(shade_model_flat
&& is_gl_Color
) &&
5391 interp_qualifier
== INTERP_QUALIFIER_NONE
)) {
5393 barycentric_interp_modes
|=
5394 1 << BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
5395 } else if (is_sample
) {
5396 barycentric_interp_modes
|=
5397 1 << BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
5399 if ((!is_centroid
&& !is_sample
) ||
5400 devinfo
->needs_unlit_centroid_workaround
) {
5401 barycentric_interp_modes
|=
5402 1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
5407 return barycentric_interp_modes
;
5411 computed_depth_mode(const nir_shader
*shader
)
5413 if (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
5414 switch (shader
->info
.fs
.depth_layout
) {
5415 case FRAG_DEPTH_LAYOUT_NONE
:
5416 case FRAG_DEPTH_LAYOUT_ANY
:
5417 return BRW_PSCDEPTH_ON
;
5418 case FRAG_DEPTH_LAYOUT_GREATER
:
5419 return BRW_PSCDEPTH_ON_GE
;
5420 case FRAG_DEPTH_LAYOUT_LESS
:
5421 return BRW_PSCDEPTH_ON_LE
;
5422 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
5423 return BRW_PSCDEPTH_OFF
;
5426 return BRW_PSCDEPTH_OFF
;
5430 brw_compile_fs(const struct brw_compiler
*compiler
, void *log_data
,
5432 const struct brw_wm_prog_key
*key
,
5433 struct brw_wm_prog_data
*prog_data
,
5434 const nir_shader
*src_shader
,
5435 struct gl_program
*prog
,
5436 int shader_time_index8
, int shader_time_index16
,
5438 unsigned *final_assembly_size
,
5441 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
5442 shader
= brw_nir_apply_sampler_key(shader
, compiler
->devinfo
, &key
->tex
,
5444 shader
= brw_postprocess_nir(shader
, compiler
->devinfo
, true);
5446 /* key->alpha_test_func means simulating alpha testing via discards,
5447 * so the shader definitely kills pixels.
5449 prog_data
->uses_kill
= shader
->info
.fs
.uses_discard
|| key
->alpha_test_func
;
5450 prog_data
->uses_omask
=
5451 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
5452 prog_data
->computed_depth_mode
= computed_depth_mode(shader
);
5453 prog_data
->computed_stencil
=
5454 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
5456 prog_data
->early_fragment_tests
= shader
->info
.fs
.early_fragment_tests
;
5458 prog_data
->barycentric_interp_modes
=
5459 brw_compute_barycentric_interp_modes(compiler
->devinfo
,
5461 key
->persample_shading
,
5464 fs_visitor
v(compiler
, log_data
, mem_ctx
, key
,
5465 &prog_data
->base
, prog
, shader
, 8,
5466 shader_time_index8
);
5467 if (!v
.run_fs(false /* do_rep_send */)) {
5469 *error_str
= ralloc_strdup(mem_ctx
, v
.fail_msg
);
5474 cfg_t
*simd16_cfg
= NULL
;
5475 fs_visitor
v2(compiler
, log_data
, mem_ctx
, key
,
5476 &prog_data
->base
, prog
, shader
, 16,
5477 shader_time_index16
);
5478 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
5479 if (!v
.simd16_unsupported
) {
5480 /* Try a SIMD16 compile */
5481 v2
.import_uniforms(&v
);
5482 if (!v2
.run_fs(use_rep_send
)) {
5483 compiler
->shader_perf_log(log_data
,
5484 "SIMD16 shader failed to compile: %s",
5487 simd16_cfg
= v2
.cfg
;
5493 int no_simd8
= (INTEL_DEBUG
& DEBUG_NO8
) || use_rep_send
;
5494 if ((no_simd8
|| compiler
->devinfo
->gen
< 5) && simd16_cfg
) {
5496 prog_data
->no_8
= true;
5499 prog_data
->no_8
= false;
5502 fs_generator
g(compiler
, log_data
, mem_ctx
, (void *) key
, &prog_data
->base
,
5503 v
.promoted_constants
, v
.runtime_check_aads_emit
, "FS");
5505 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
5506 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
5507 shader
->info
.label
? shader
->info
.label
:
5509 shader
->info
.name
));
5513 g
.generate_code(simd8_cfg
, 8);
5515 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
5517 return g
.get_assembly(final_assembly_size
);
5521 brw_cs_fill_local_id_payload(const struct brw_cs_prog_data
*prog_data
,
5522 void *buffer
, uint32_t threads
, uint32_t stride
)
5524 if (prog_data
->local_invocation_id_regs
== 0)
5527 /* 'stride' should be an integer number of registers, that is, a multiple
5530 assert(stride
% 32 == 0);
5532 unsigned x
= 0, y
= 0, z
= 0;
5533 for (unsigned t
= 0; t
< threads
; t
++) {
5534 uint32_t *param
= (uint32_t *) buffer
+ stride
* t
/ 4;
5536 for (unsigned i
= 0; i
< prog_data
->simd_size
; i
++) {
5537 param
[0 * prog_data
->simd_size
+ i
] = x
;
5538 param
[1 * prog_data
->simd_size
+ i
] = y
;
5539 param
[2 * prog_data
->simd_size
+ i
] = z
;
5542 if (x
== prog_data
->local_size
[0]) {
5545 if (y
== prog_data
->local_size
[1]) {
5548 if (z
== prog_data
->local_size
[2])
5557 fs_visitor::emit_cs_local_invocation_id_setup()
5559 assert(stage
== MESA_SHADER_COMPUTE
);
5561 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5563 struct brw_reg src
=
5564 brw_vec8_grf(payload
.local_invocation_id_reg
, 0);
5565 src
= retype(src
, BRW_REGISTER_TYPE_UD
);
5567 src
.nr
+= dispatch_width
/ 8;
5568 bld
.MOV(offset(*reg
, bld
, 1), src
);
5569 src
.nr
+= dispatch_width
/ 8;
5570 bld
.MOV(offset(*reg
, bld
, 2), src
);
5576 fs_visitor::emit_cs_work_group_id_setup()
5578 assert(stage
== MESA_SHADER_COMPUTE
);
5580 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5582 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
5583 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
5584 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
5586 bld
.MOV(*reg
, r0_1
);
5587 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
5588 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
5594 brw_compile_cs(const struct brw_compiler
*compiler
, void *log_data
,
5596 const struct brw_cs_prog_key
*key
,
5597 struct brw_cs_prog_data
*prog_data
,
5598 const nir_shader
*src_shader
,
5599 int shader_time_index
,
5600 unsigned *final_assembly_size
,
5603 nir_shader
*shader
= nir_shader_clone(mem_ctx
, src_shader
);
5604 shader
= brw_nir_apply_sampler_key(shader
, compiler
->devinfo
, &key
->tex
,
5606 shader
= brw_postprocess_nir(shader
, compiler
->devinfo
, true);
5608 prog_data
->local_size
[0] = shader
->info
.cs
.local_size
[0];
5609 prog_data
->local_size
[1] = shader
->info
.cs
.local_size
[1];
5610 prog_data
->local_size
[2] = shader
->info
.cs
.local_size
[2];
5611 unsigned local_workgroup_size
=
5612 shader
->info
.cs
.local_size
[0] * shader
->info
.cs
.local_size
[1] *
5613 shader
->info
.cs
.local_size
[2];
5615 unsigned max_cs_threads
= compiler
->devinfo
->max_cs_threads
;
5618 const char *fail_msg
= NULL
;
5620 /* Now the main event: Visit the shader IR and generate our CS IR for it.
5622 fs_visitor
v8(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5623 NULL
, /* Never used in core profile */
5624 shader
, 8, shader_time_index
);
5626 fail_msg
= v8
.fail_msg
;
5627 } else if (local_workgroup_size
<= 8 * max_cs_threads
) {
5629 prog_data
->simd_size
= 8;
5632 fs_visitor
v16(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5633 NULL
, /* Never used in core profile */
5634 shader
, 16, shader_time_index
);
5635 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
5636 !fail_msg
&& !v8
.simd16_unsupported
&&
5637 local_workgroup_size
<= 16 * max_cs_threads
) {
5638 /* Try a SIMD16 compile */
5639 v16
.import_uniforms(&v8
);
5640 if (!v16
.run_cs()) {
5641 compiler
->shader_perf_log(log_data
,
5642 "SIMD16 shader failed to compile: %s",
5646 "Couldn't generate SIMD16 program and not "
5647 "enough threads for SIMD8";
5651 prog_data
->simd_size
= 16;
5655 if (unlikely(cfg
== NULL
)) {
5658 *error_str
= ralloc_strdup(mem_ctx
, fail_msg
);
5663 fs_generator
g(compiler
, log_data
, mem_ctx
, (void*) key
, &prog_data
->base
,
5664 v8
.promoted_constants
, v8
.runtime_check_aads_emit
, "CS");
5665 if (INTEL_DEBUG
& DEBUG_CS
) {
5666 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
5667 shader
->info
.label
? shader
->info
.label
:
5670 g
.enable_debug(name
);
5673 g
.generate_code(cfg
, prog_data
->simd_size
);
5675 return g
.get_assembly(final_assembly_size
);