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"
46 #include "brw_dead_control_flow.h"
47 #include "main/uniforms.h"
48 #include "brw_fs_live_variables.h"
49 #include "glsl/glsl_types.h"
50 #include "program/sampler.h"
55 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
56 const fs_reg
*src
, unsigned sources
)
58 memset(this, 0, sizeof(*this));
60 this->src
= new fs_reg
[MAX2(sources
, 3)];
61 for (unsigned i
= 0; i
< sources
; i
++)
62 this->src
[i
] = src
[i
];
64 this->opcode
= opcode
;
66 this->sources
= sources
;
67 this->exec_size
= exec_size
;
69 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
71 assert(this->exec_size
!= 0);
73 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
75 /* This will be the case for almost all instructions. */
81 this->regs_written
= DIV_ROUND_UP(dst
.component_size(exec_size
),
85 this->regs_written
= 0;
89 unreachable("Invalid destination register file");
91 unreachable("Invalid register file");
94 this->writes_accumulator
= false;
99 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
102 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
104 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
107 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
109 init(opcode
, exec_size
, dst
, NULL
, 0);
112 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
115 const fs_reg src
[1] = { src0
};
116 init(opcode
, exec_size
, dst
, src
, 1);
119 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
120 const fs_reg
&src0
, const fs_reg
&src1
)
122 const fs_reg src
[2] = { src0
, src1
};
123 init(opcode
, exec_size
, dst
, src
, 2);
126 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
127 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
129 const fs_reg src
[3] = { src0
, src1
, src2
};
130 init(opcode
, exec_size
, dst
, src
, 3);
133 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
134 const fs_reg src
[], unsigned sources
)
136 init(opcode
, exec_width
, dst
, src
, sources
);
139 fs_inst::fs_inst(const fs_inst
&that
)
141 memcpy(this, &that
, sizeof(that
));
143 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
145 for (unsigned i
= 0; i
< that
.sources
; i
++)
146 this->src
[i
] = that
.src
[i
];
155 fs_inst::resize_sources(uint8_t num_sources
)
157 if (this->sources
!= num_sources
) {
158 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
160 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
161 src
[i
] = this->src
[i
];
165 this->sources
= num_sources
;
170 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
172 const fs_reg
&surf_index
,
173 const fs_reg
&varying_offset
,
174 uint32_t const_offset
)
176 /* We have our constant surface use a pitch of 4 bytes, so our index can
177 * be any component of a vector, and then we load 4 contiguous
178 * components starting from that.
180 * We break down the const_offset to a portion added to the variable
181 * offset and a portion done using reg_offset, which means that if you
182 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
183 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
184 * CSE can later notice that those loads are all the same and eliminate
185 * the redundant ones.
187 fs_reg vec4_offset
= vgrf(glsl_type::int_type
);
188 bld
.ADD(vec4_offset
, varying_offset
, fs_reg(const_offset
& ~3));
191 if (devinfo
->gen
== 4 && bld
.dispatch_width() == 8) {
192 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
193 * u, v, r) as parameters, or we can just use the SIMD16 message
194 * consisting of (header, u). We choose the second, at the cost of a
195 * longer return length.
201 if (devinfo
->gen
>= 7)
202 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
;
204 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
;
206 int regs_written
= 4 * (bld
.dispatch_width() / 8) * scale
;
207 fs_reg vec4_result
= fs_reg(GRF
, alloc
.allocate(regs_written
), dst
.type
);
208 fs_inst
*inst
= bld
.emit(op
, vec4_result
, surf_index
, vec4_offset
);
209 inst
->regs_written
= regs_written
;
211 if (devinfo
->gen
< 7) {
213 inst
->header_size
= 1;
214 if (devinfo
->gen
== 4)
217 inst
->mlen
= 1 + bld
.dispatch_width() / 8;
220 bld
.MOV(dst
, offset(vec4_result
, bld
, (const_offset
& 3) * scale
));
224 * A helper for MOV generation for fixing up broken hardware SEND dependency
228 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
230 /* The caller always wants uncompressed to emit the minimal extra
231 * dependencies, and to avoid having to deal with aligning its regs to 2.
233 const fs_builder ubld
= bld
.annotate("send dependency resolve")
236 ubld
.MOV(ubld
.null_reg_f(), fs_reg(GRF
, grf
, BRW_REGISTER_TYPE_F
));
240 fs_inst::equals(fs_inst
*inst
) const
242 return (opcode
== inst
->opcode
&&
243 dst
.equals(inst
->dst
) &&
244 src
[0].equals(inst
->src
[0]) &&
245 src
[1].equals(inst
->src
[1]) &&
246 src
[2].equals(inst
->src
[2]) &&
247 saturate
== inst
->saturate
&&
248 predicate
== inst
->predicate
&&
249 conditional_mod
== inst
->conditional_mod
&&
250 mlen
== inst
->mlen
&&
251 base_mrf
== inst
->base_mrf
&&
252 target
== inst
->target
&&
254 header_size
== inst
->header_size
&&
255 shadow_compare
== inst
->shadow_compare
&&
256 exec_size
== inst
->exec_size
&&
257 offset
== inst
->offset
);
261 fs_inst::overwrites_reg(const fs_reg
®
) const
263 return reg
.in_range(dst
, regs_written
);
267 fs_inst::is_send_from_grf() const
270 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
271 case SHADER_OPCODE_SHADER_TIME_ADD
:
272 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
273 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
274 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
275 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
276 case SHADER_OPCODE_UNTYPED_ATOMIC
:
277 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
278 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
279 case SHADER_OPCODE_TYPED_ATOMIC
:
280 case SHADER_OPCODE_TYPED_SURFACE_READ
:
281 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
282 case SHADER_OPCODE_URB_WRITE_SIMD8
:
284 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
285 return src
[1].file
== GRF
;
286 case FS_OPCODE_FB_WRITE
:
287 return src
[0].file
== GRF
;
290 return src
[0].file
== GRF
;
297 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
299 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
302 fs_reg reg
= this->src
[0];
303 if (reg
.file
!= GRF
|| reg
.reg_offset
!= 0 || reg
.stride
== 0)
306 if (grf_alloc
.sizes
[reg
.reg
] != this->regs_written
)
309 for (int i
= 0; i
< this->sources
; i
++) {
310 reg
.type
= this->src
[i
].type
;
311 if (!this->src
[i
].equals(reg
))
314 if (i
< this->header_size
) {
317 reg
.reg_offset
+= this->exec_size
/ 8;
325 fs_inst::can_do_source_mods(const struct brw_device_info
*devinfo
)
327 if (devinfo
->gen
== 6 && is_math())
330 if (is_send_from_grf())
333 if (!backend_instruction::can_do_source_mods())
340 fs_inst::has_side_effects() const
342 return this->eot
|| backend_instruction::has_side_effects();
348 memset(this, 0, sizeof(*this));
352 /** Generic unset register constructor. */
356 this->file
= BAD_FILE
;
359 /** Immediate value constructor. */
360 fs_reg::fs_reg(float f
)
364 this->type
= BRW_REGISTER_TYPE_F
;
366 this->fixed_hw_reg
.dw1
.f
= f
;
369 /** Immediate value constructor. */
370 fs_reg::fs_reg(int32_t i
)
374 this->type
= BRW_REGISTER_TYPE_D
;
376 this->fixed_hw_reg
.dw1
.d
= i
;
379 /** Immediate value constructor. */
380 fs_reg::fs_reg(uint32_t u
)
384 this->type
= BRW_REGISTER_TYPE_UD
;
386 this->fixed_hw_reg
.dw1
.ud
= u
;
389 /** Vector float immediate value constructor. */
390 fs_reg::fs_reg(uint8_t vf
[4])
394 this->type
= BRW_REGISTER_TYPE_VF
;
395 memcpy(&this->fixed_hw_reg
.dw1
.ud
, vf
, sizeof(unsigned));
398 /** Vector float immediate value constructor. */
399 fs_reg::fs_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
)
403 this->type
= BRW_REGISTER_TYPE_VF
;
404 this->fixed_hw_reg
.dw1
.ud
= (vf0
<< 0) |
410 /** Fixed brw_reg. */
411 fs_reg::fs_reg(struct brw_reg fixed_hw_reg
)
415 this->fixed_hw_reg
= fixed_hw_reg
;
416 this->type
= fixed_hw_reg
.type
;
420 fs_reg::equals(const fs_reg
&r
) const
422 return (file
== r
.file
&&
424 reg_offset
== r
.reg_offset
&&
425 subreg_offset
== r
.subreg_offset
&&
427 negate
== r
.negate
&&
429 !reladdr
&& !r
.reladdr
&&
430 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
, sizeof(fixed_hw_reg
)) == 0 &&
435 fs_reg::set_smear(unsigned subreg
)
437 assert(file
!= HW_REG
&& file
!= IMM
);
438 subreg_offset
= subreg
* type_sz(type
);
444 fs_reg::is_contiguous() const
450 fs_reg::component_size(unsigned width
) const
452 const unsigned stride
= (file
!= HW_REG
? this->stride
:
453 fixed_hw_reg
.hstride
== 0 ? 0 :
454 1 << (fixed_hw_reg
.hstride
- 1));
455 return MAX2(width
* stride
, 1) * type_sz(type
);
459 fs_visitor::type_size(const struct glsl_type
*type
)
461 unsigned int size
, i
;
463 switch (type
->base_type
) {
466 case GLSL_TYPE_FLOAT
:
468 return type
->components();
469 case GLSL_TYPE_ARRAY
:
470 return type_size(type
->fields
.array
) * type
->length
;
471 case GLSL_TYPE_STRUCT
:
473 for (i
= 0; i
< type
->length
; i
++) {
474 size
+= type_size(type
->fields
.structure
[i
].type
);
477 case GLSL_TYPE_SAMPLER
:
478 /* Samplers take up no register space, since they're baked in at
482 case GLSL_TYPE_ATOMIC_UINT
:
484 case GLSL_TYPE_SUBROUTINE
:
486 case GLSL_TYPE_IMAGE
:
488 case GLSL_TYPE_ERROR
:
489 case GLSL_TYPE_INTERFACE
:
490 case GLSL_TYPE_DOUBLE
:
491 unreachable("not reached");
498 * Create a MOV to read the timestamp register.
500 * The caller is responsible for emitting the MOV. The return value is
501 * the destination of the MOV, with extra parameters set.
504 fs_visitor::get_timestamp(const fs_builder
&bld
)
506 assert(devinfo
->gen
>= 7);
508 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
511 BRW_REGISTER_TYPE_UD
));
513 fs_reg dst
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
515 /* We want to read the 3 fields we care about even if it's not enabled in
518 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
520 /* The caller wants the low 32 bits of the timestamp. Since it's running
521 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
522 * which is plenty of time for our purposes. It is identical across the
523 * EUs, but since it's tracking GPU core speed it will increment at a
524 * varying rate as render P-states change.
526 * The caller could also check if render P-states have changed (or anything
527 * else that might disrupt timing) by setting smear to 2 and checking if
528 * that field is != 0.
536 fs_visitor::emit_shader_time_begin()
538 shader_start_time
= get_timestamp(bld
.annotate("shader time start"));
542 fs_visitor::emit_shader_time_end()
544 /* Insert our code just before the final SEND with EOT. */
545 exec_node
*end
= this->instructions
.get_tail();
546 assert(end
&& ((fs_inst
*) end
)->eot
);
547 const fs_builder ibld
= bld
.annotate("shader time end")
548 .exec_all().at(NULL
, end
);
550 fs_reg shader_end_time
= get_timestamp(ibld
);
552 /* Check that there weren't any timestamp reset events (assuming these
553 * were the only two timestamp reads that happened).
555 fs_reg reset
= shader_end_time
;
557 set_condmod(BRW_CONDITIONAL_Z
,
558 ibld
.AND(ibld
.null_reg_ud(), reset
, fs_reg(1u)));
559 ibld
.IF(BRW_PREDICATE_NORMAL
);
561 fs_reg start
= shader_start_time
;
563 fs_reg diff
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
566 const fs_builder cbld
= ibld
.group(1, 0);
567 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
569 /* If there were no instructions between the two timestamp gets, the diff
570 * is 2 cycles. Remove that overhead, so I can forget about that when
571 * trying to determine the time taken for single instructions.
573 cbld
.ADD(diff
, diff
, fs_reg(-2u));
574 SHADER_TIME_ADD(cbld
, 0, diff
);
575 SHADER_TIME_ADD(cbld
, 1, fs_reg(1u));
576 ibld
.emit(BRW_OPCODE_ELSE
);
577 SHADER_TIME_ADD(cbld
, 2, fs_reg(1u));
578 ibld
.emit(BRW_OPCODE_ENDIF
);
582 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
583 int shader_time_subindex
,
586 int index
= shader_time_index
* 3 + shader_time_subindex
;
587 fs_reg offset
= fs_reg(index
* SHADER_TIME_STRIDE
);
590 if (dispatch_width
== 8)
591 payload
= vgrf(glsl_type::uvec2_type
);
593 payload
= vgrf(glsl_type::uint_type
);
595 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
599 fs_visitor::vfail(const char *format
, va_list va
)
608 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
609 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
611 this->fail_msg
= msg
;
614 fprintf(stderr
, "%s", msg
);
619 fs_visitor::fail(const char *format
, ...)
623 va_start(va
, format
);
629 * Mark this program as impossible to compile in SIMD16 mode.
631 * During the SIMD8 compile (which happens first), we can detect and flag
632 * things that are unsupported in SIMD16 mode, so the compiler can skip
633 * the SIMD16 compile altogether.
635 * During a SIMD16 compile (if one happens anyway), this just calls fail().
638 fs_visitor::no16(const char *msg
)
640 if (dispatch_width
== 16) {
643 simd16_unsupported
= true;
645 compiler
->shader_perf_log(log_data
,
646 "SIMD16 shader failed to compile: %s", msg
);
651 * Returns true if the instruction has a flag that means it won't
652 * update an entire destination register.
654 * For example, dead code elimination and live variable analysis want to know
655 * when a write to a variable screens off any preceding values that were in
659 fs_inst::is_partial_write() const
661 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
662 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
663 !this->dst
.is_contiguous());
667 fs_inst::components_read(unsigned i
) const
670 case FS_OPCODE_LINTERP
:
676 case FS_OPCODE_PIXEL_X
:
677 case FS_OPCODE_PIXEL_Y
:
681 case FS_OPCODE_FB_WRITE_LOGICAL
:
682 assert(src
[6].file
== IMM
);
683 /* First/second FB write color. */
685 return src
[6].fixed_hw_reg
.dw1
.ud
;
689 case SHADER_OPCODE_TEX_LOGICAL
:
690 case SHADER_OPCODE_TXD_LOGICAL
:
691 case SHADER_OPCODE_TXF_LOGICAL
:
692 case SHADER_OPCODE_TXL_LOGICAL
:
693 case SHADER_OPCODE_TXS_LOGICAL
:
694 case FS_OPCODE_TXB_LOGICAL
:
695 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
696 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
697 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
698 case SHADER_OPCODE_LOD_LOGICAL
:
699 case SHADER_OPCODE_TG4_LOGICAL
:
700 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
701 assert(src
[8].file
== IMM
&& src
[9].file
== IMM
);
702 /* Texture coordinates. */
704 return src
[8].fixed_hw_reg
.dw1
.ud
;
705 /* Texture derivatives. */
706 else if ((i
== 2 || i
== 3) && opcode
== SHADER_OPCODE_TXD_LOGICAL
)
707 return src
[9].fixed_hw_reg
.dw1
.ud
;
708 /* Texture offset. */
714 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
715 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
716 assert(src
[3].file
== IMM
);
717 /* Surface coordinates. */
719 return src
[3].fixed_hw_reg
.dw1
.ud
;
720 /* Surface operation source (ignored for reads). */
726 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
727 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
728 assert(src
[3].file
== IMM
&&
730 /* Surface coordinates. */
732 return src
[3].fixed_hw_reg
.dw1
.ud
;
733 /* Surface operation source. */
735 return src
[4].fixed_hw_reg
.dw1
.ud
;
739 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
740 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
741 assert(src
[3].file
== IMM
&&
743 const unsigned op
= src
[4].fixed_hw_reg
.dw1
.ud
;
744 /* Surface coordinates. */
746 return src
[3].fixed_hw_reg
.dw1
.ud
;
747 /* Surface operation source. */
748 else if (i
== 1 && op
== BRW_AOP_CMPWR
)
750 else if (i
== 1 && (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
||
751 op
== BRW_AOP_PREDEC
))
763 fs_inst::regs_read(int arg
) const
766 case FS_OPCODE_FB_WRITE
:
767 case SHADER_OPCODE_URB_WRITE_SIMD8
:
768 case SHADER_OPCODE_UNTYPED_ATOMIC
:
769 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
770 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
771 case SHADER_OPCODE_TYPED_ATOMIC
:
772 case SHADER_OPCODE_TYPED_SURFACE_READ
:
773 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
774 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
779 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
780 /* The payload is actually stored in src1 */
785 case FS_OPCODE_LINTERP
:
790 case SHADER_OPCODE_LOAD_PAYLOAD
:
791 if (arg
< this->header_size
)
795 case CS_OPCODE_CS_TERMINATE
:
799 if (is_tex() && arg
== 0 && src
[0].file
== GRF
)
804 switch (src
[arg
].file
) {
812 return DIV_ROUND_UP(components_read(arg
) *
813 src
[arg
].component_size(exec_size
),
816 unreachable("MRF registers are not allowed as sources");
818 unreachable("Invalid register file");
823 fs_inst::reads_flag() const
829 fs_inst::writes_flag() const
831 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
832 opcode
!= BRW_OPCODE_IF
&&
833 opcode
!= BRW_OPCODE_WHILE
)) ||
834 opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
;
838 * Returns how many MRFs an FS opcode will write over.
840 * Note that this is not the 0 or 1 implied writes in an actual gen
841 * instruction -- the FS opcodes often generate MOVs in addition.
844 fs_visitor::implied_mrf_writes(fs_inst
*inst
)
849 if (inst
->base_mrf
== -1)
852 switch (inst
->opcode
) {
853 case SHADER_OPCODE_RCP
:
854 case SHADER_OPCODE_RSQ
:
855 case SHADER_OPCODE_SQRT
:
856 case SHADER_OPCODE_EXP2
:
857 case SHADER_OPCODE_LOG2
:
858 case SHADER_OPCODE_SIN
:
859 case SHADER_OPCODE_COS
:
860 return 1 * dispatch_width
/ 8;
861 case SHADER_OPCODE_POW
:
862 case SHADER_OPCODE_INT_QUOTIENT
:
863 case SHADER_OPCODE_INT_REMAINDER
:
864 return 2 * dispatch_width
/ 8;
865 case SHADER_OPCODE_TEX
:
867 case SHADER_OPCODE_TXD
:
868 case SHADER_OPCODE_TXF
:
869 case SHADER_OPCODE_TXF_CMS
:
870 case SHADER_OPCODE_TXF_MCS
:
871 case SHADER_OPCODE_TG4
:
872 case SHADER_OPCODE_TG4_OFFSET
:
873 case SHADER_OPCODE_TXL
:
874 case SHADER_OPCODE_TXS
:
875 case SHADER_OPCODE_LOD
:
877 case FS_OPCODE_FB_WRITE
:
879 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
880 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
882 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
884 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
886 case SHADER_OPCODE_UNTYPED_ATOMIC
:
887 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
888 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
889 case SHADER_OPCODE_TYPED_ATOMIC
:
890 case SHADER_OPCODE_TYPED_SURFACE_READ
:
891 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
892 case SHADER_OPCODE_URB_WRITE_SIMD8
:
893 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
894 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
895 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
896 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
899 unreachable("not reached");
904 fs_visitor::vgrf(const glsl_type
*const type
)
906 int reg_width
= dispatch_width
/ 8;
907 return fs_reg(GRF
, alloc
.allocate(type_size(type
) * reg_width
),
908 brw_type_for_base_type(type
));
911 /** Fixed HW reg constructor. */
912 fs_reg::fs_reg(enum register_file file
, int reg
)
917 this->type
= BRW_REGISTER_TYPE_F
;
918 this->stride
= (file
== UNIFORM
? 0 : 1);
921 /** Fixed HW reg constructor. */
922 fs_reg::fs_reg(enum register_file file
, int reg
, enum brw_reg_type type
)
928 this->stride
= (file
== UNIFORM
? 0 : 1);
931 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
932 * This brings in those uniform definitions
935 fs_visitor::import_uniforms(fs_visitor
*v
)
937 this->push_constant_loc
= v
->push_constant_loc
;
938 this->pull_constant_loc
= v
->pull_constant_loc
;
939 this->uniforms
= v
->uniforms
;
940 this->param_size
= v
->param_size
;
944 fs_visitor::setup_vector_uniform_values(const gl_constant_value
*values
, unsigned n
)
946 static const gl_constant_value zero
= { 0 };
948 for (unsigned i
= 0; i
< n
; ++i
)
949 stage_prog_data
->param
[uniforms
++] = &values
[i
];
951 for (unsigned i
= n
; i
< 4; ++i
)
952 stage_prog_data
->param
[uniforms
++] = &zero
;
956 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer
,
957 bool origin_upper_left
)
959 assert(stage
== MESA_SHADER_FRAGMENT
);
960 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
961 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec4_type
));
963 bool flip
= !origin_upper_left
^ key
->render_to_fbo
;
966 if (pixel_center_integer
) {
967 bld
.MOV(wpos
, this->pixel_x
);
969 bld
.ADD(wpos
, this->pixel_x
, fs_reg(0.5f
));
971 wpos
= offset(wpos
, bld
, 1);
974 if (!flip
&& pixel_center_integer
) {
975 bld
.MOV(wpos
, this->pixel_y
);
977 fs_reg pixel_y
= this->pixel_y
;
978 float offset
= (pixel_center_integer
? 0.0f
: 0.5f
);
981 pixel_y
.negate
= true;
982 offset
+= key
->drawable_height
- 1.0f
;
985 bld
.ADD(wpos
, pixel_y
, fs_reg(offset
));
987 wpos
= offset(wpos
, bld
, 1);
990 if (devinfo
->gen
>= 6) {
991 bld
.MOV(wpos
, fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0)));
993 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
994 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
995 interp_reg(VARYING_SLOT_POS
, 2));
997 wpos
= offset(wpos
, bld
, 1);
999 /* gl_FragCoord.w: Already set up in emit_interpolation */
1000 bld
.MOV(wpos
, this->wpos_w
);
1006 fs_visitor::emit_linterp(const fs_reg
&attr
, const fs_reg
&interp
,
1007 glsl_interp_qualifier interpolation_mode
,
1008 bool is_centroid
, bool is_sample
)
1010 brw_wm_barycentric_interp_mode barycoord_mode
;
1011 if (devinfo
->gen
>= 6) {
1013 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1014 barycoord_mode
= BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
1016 barycoord_mode
= BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
1017 } else if (is_sample
) {
1018 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1019 barycoord_mode
= BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
1021 barycoord_mode
= BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
1023 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1024 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1026 barycoord_mode
= BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
1029 /* On Ironlake and below, there is only one interpolation mode.
1030 * Centroid interpolation doesn't mean anything on this hardware --
1031 * there is no multisampling.
1033 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1035 return bld
.emit(FS_OPCODE_LINTERP
, attr
,
1036 this->delta_xy
[barycoord_mode
], interp
);
1040 fs_visitor::emit_general_interpolation(fs_reg attr
, const char *name
,
1041 const glsl_type
*type
,
1042 glsl_interp_qualifier interpolation_mode
,
1043 int location
, bool mod_centroid
,
1046 attr
.type
= brw_type_for_base_type(type
->get_scalar_type());
1048 assert(stage
== MESA_SHADER_FRAGMENT
);
1049 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1050 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1052 unsigned int array_elements
;
1054 if (type
->is_array()) {
1055 array_elements
= type
->length
;
1056 if (array_elements
== 0) {
1057 fail("dereferenced array '%s' has length 0\n", name
);
1059 type
= type
->fields
.array
;
1064 if (interpolation_mode
== INTERP_QUALIFIER_NONE
) {
1066 location
== VARYING_SLOT_COL0
|| location
== VARYING_SLOT_COL1
;
1067 if (key
->flat_shade
&& is_gl_Color
) {
1068 interpolation_mode
= INTERP_QUALIFIER_FLAT
;
1070 interpolation_mode
= INTERP_QUALIFIER_SMOOTH
;
1074 for (unsigned int i
= 0; i
< array_elements
; i
++) {
1075 for (unsigned int j
= 0; j
< type
->matrix_columns
; j
++) {
1076 if (prog_data
->urb_setup
[location
] == -1) {
1077 /* If there's no incoming setup data for this slot, don't
1078 * emit interpolation for it.
1080 attr
= offset(attr
, bld
, type
->vector_elements
);
1085 if (interpolation_mode
== INTERP_QUALIFIER_FLAT
) {
1086 /* Constant interpolation (flat shading) case. The SF has
1087 * handed us defined values in only the constant offset
1088 * field of the setup reg.
1090 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1091 struct brw_reg interp
= interp_reg(location
, k
);
1092 interp
= suboffset(interp
, 3);
1093 interp
.type
= attr
.type
;
1094 bld
.emit(FS_OPCODE_CINTERP
, attr
, fs_reg(interp
));
1095 attr
= offset(attr
, bld
, 1);
1098 /* Smooth/noperspective interpolation case. */
1099 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1100 struct brw_reg interp
= interp_reg(location
, k
);
1101 if (devinfo
->needs_unlit_centroid_workaround
&& mod_centroid
) {
1102 /* Get the pixel/sample mask into f0 so that we know
1103 * which pixels are lit. Then, for each channel that is
1104 * unlit, replace the centroid data with non-centroid
1107 bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
1110 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1112 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1113 inst
->predicate_inverse
= true;
1114 if (devinfo
->has_pln
)
1115 inst
->no_dd_clear
= true;
1117 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1118 mod_centroid
&& !key
->persample_shading
,
1119 mod_sample
|| key
->persample_shading
);
1120 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1121 inst
->predicate_inverse
= false;
1122 if (devinfo
->has_pln
)
1123 inst
->no_dd_check
= true;
1126 emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1127 mod_centroid
&& !key
->persample_shading
,
1128 mod_sample
|| key
->persample_shading
);
1130 if (devinfo
->gen
< 6 && interpolation_mode
== INTERP_QUALIFIER_SMOOTH
) {
1131 bld
.MUL(attr
, attr
, this->pixel_w
);
1133 attr
= offset(attr
, bld
, 1);
1143 fs_visitor::emit_frontfacing_interpolation()
1145 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1147 if (devinfo
->gen
>= 6) {
1148 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1149 * a boolean result from this (~0/true or 0/false).
1151 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1152 * this task in only one instruction:
1153 * - a negation source modifier will flip the bit; and
1154 * - a W -> D type conversion will sign extend the bit into the high
1155 * word of the destination.
1157 * An ASR 15 fills the low word of the destination.
1159 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1162 bld
.ASR(*reg
, g0
, fs_reg(15));
1164 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1165 * a boolean result from this (1/true or 0/false).
1167 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1168 * the negation source modifier to flip it. Unfortunately the SHR
1169 * instruction only operates on UD (or D with an abs source modifier)
1170 * sources without negation.
1172 * Instead, use ASR (which will give ~0/true or 0/false).
1174 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1177 bld
.ASR(*reg
, g1_6
, fs_reg(31));
1184 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1186 assert(stage
== MESA_SHADER_FRAGMENT
);
1187 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1188 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1190 if (key
->compute_pos_offset
) {
1191 /* Convert int_sample_pos to floating point */
1192 bld
.MOV(dst
, int_sample_pos
);
1193 /* Scale to the range [0, 1] */
1194 bld
.MUL(dst
, dst
, fs_reg(1 / 16.0f
));
1197 /* From ARB_sample_shading specification:
1198 * "When rendering to a non-multisample buffer, or if multisample
1199 * rasterization is disabled, gl_SamplePosition will always be
1202 bld
.MOV(dst
, fs_reg(0.5f
));
1207 fs_visitor::emit_samplepos_setup()
1209 assert(devinfo
->gen
>= 6);
1211 const fs_builder abld
= bld
.annotate("compute sample position");
1212 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1214 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1215 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1217 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1218 * mode will be enabled.
1220 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1221 * R31.1:0 Position Offset X/Y for Slot[3:0]
1222 * R31.3:2 Position Offset X/Y for Slot[7:4]
1225 * The X, Y sample positions come in as bytes in thread payload. So, read
1226 * the positions using vstride=16, width=8, hstride=2.
1228 struct brw_reg sample_pos_reg
=
1229 stride(retype(brw_vec1_grf(payload
.sample_pos_reg
, 0),
1230 BRW_REGISTER_TYPE_B
), 16, 8, 2);
1232 if (dispatch_width
== 8) {
1233 abld
.MOV(int_sample_x
, fs_reg(sample_pos_reg
));
1235 abld
.half(0).MOV(half(int_sample_x
, 0), fs_reg(sample_pos_reg
));
1236 abld
.half(1).MOV(half(int_sample_x
, 1),
1237 fs_reg(suboffset(sample_pos_reg
, 16)));
1239 /* Compute gl_SamplePosition.x */
1240 compute_sample_position(pos
, int_sample_x
);
1241 pos
= offset(pos
, abld
, 1);
1242 if (dispatch_width
== 8) {
1243 abld
.MOV(int_sample_y
, fs_reg(suboffset(sample_pos_reg
, 1)));
1245 abld
.half(0).MOV(half(int_sample_y
, 0),
1246 fs_reg(suboffset(sample_pos_reg
, 1)));
1247 abld
.half(1).MOV(half(int_sample_y
, 1),
1248 fs_reg(suboffset(sample_pos_reg
, 17)));
1250 /* Compute gl_SamplePosition.y */
1251 compute_sample_position(pos
, int_sample_y
);
1256 fs_visitor::emit_sampleid_setup()
1258 assert(stage
== MESA_SHADER_FRAGMENT
);
1259 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1260 assert(devinfo
->gen
>= 6);
1262 const fs_builder abld
= bld
.annotate("compute sample id");
1263 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1265 if (key
->compute_sample_id
) {
1266 fs_reg t1
= vgrf(glsl_type::int_type
);
1267 fs_reg t2
= vgrf(glsl_type::int_type
);
1268 t2
.type
= BRW_REGISTER_TYPE_UW
;
1270 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1271 * 8x multisampling, subspan 0 will represent sample N (where N
1272 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1273 * 7. We can find the value of N by looking at R0.0 bits 7:6
1274 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1275 * (since samples are always delivered in pairs). That is, we
1276 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1277 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1278 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1279 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1280 * populating a temporary variable with the sequence (0, 1, 2, 3),
1281 * and then reading from it using vstride=1, width=4, hstride=0.
1282 * These computations hold good for 4x multisampling as well.
1284 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1285 * the first four slots are sample 0 of subspan 0; the next four
1286 * are sample 1 of subspan 0; the third group is sample 0 of
1287 * subspan 1, and finally sample 1 of subspan 1.
1290 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
1292 abld
.exec_all().SHR(t1
, t1
, fs_reg(5));
1294 /* This works for both SIMD8 and SIMD16 */
1296 .MOV(t2
, brw_imm_v(key
->persample_2x
? 0x1010 : 0x3210));
1298 /* This special instruction takes care of setting vstride=1,
1299 * width=4, hstride=0 of t2 during an ADD instruction.
1301 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1303 /* As per GL_ARB_sample_shading specification:
1304 * "When rendering to a non-multisample buffer, or if multisample
1305 * rasterization is disabled, gl_SampleID will always be zero."
1307 abld
.MOV(*reg
, fs_reg(0));
1314 fs_visitor::resolve_source_modifiers(fs_reg
*src
)
1316 if (!src
->abs
&& !src
->negate
)
1319 fs_reg temp
= bld
.vgrf(src
->type
);
1320 bld
.MOV(temp
, *src
);
1325 fs_visitor::emit_discard_jump()
1327 assert(((brw_wm_prog_data
*) this->prog_data
)->uses_kill
);
1329 /* For performance, after a discard, jump to the end of the
1330 * shader if all relevant channels have been discarded.
1332 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1333 discard_jump
->flag_subreg
= 1;
1335 discard_jump
->predicate
= (dispatch_width
== 8)
1336 ? BRW_PREDICATE_ALIGN1_ANY8H
1337 : BRW_PREDICATE_ALIGN1_ANY16H
;
1338 discard_jump
->predicate_inverse
= true;
1342 fs_visitor::assign_curb_setup()
1344 if (dispatch_width
== 8) {
1345 prog_data
->dispatch_grf_start_reg
= payload
.num_regs
;
1347 if (stage
== MESA_SHADER_FRAGMENT
) {
1348 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1349 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1350 } else if (stage
== MESA_SHADER_COMPUTE
) {
1351 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
1352 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1354 unreachable("Unsupported shader type!");
1358 prog_data
->curb_read_length
= ALIGN(stage_prog_data
->nr_params
, 8) / 8;
1360 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1361 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1362 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1363 if (inst
->src
[i
].file
== UNIFORM
) {
1364 int uniform_nr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1366 if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1367 constant_nr
= push_constant_loc
[uniform_nr
];
1369 /* Section 5.11 of the OpenGL 4.1 spec says:
1370 * "Out-of-bounds reads return undefined values, which include
1371 * values from other variables of the active program or zero."
1372 * Just return the first push constant.
1377 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1381 assert(inst
->src
[i
].stride
== 0);
1382 inst
->src
[i
].file
= HW_REG
;
1383 inst
->src
[i
].fixed_hw_reg
= byte_offset(
1384 retype(brw_reg
, inst
->src
[i
].type
),
1385 inst
->src
[i
].subreg_offset
);
1392 fs_visitor::calculate_urb_setup()
1394 assert(stage
== MESA_SHADER_FRAGMENT
);
1395 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1396 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1398 memset(prog_data
->urb_setup
, -1,
1399 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1402 /* Figure out where each of the incoming setup attributes lands. */
1403 if (devinfo
->gen
>= 6) {
1404 if (_mesa_bitcount_64(prog
->InputsRead
&
1405 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1406 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1407 * first 16 varying inputs, so we can put them wherever we want.
1408 * Just put them in order.
1410 * This is useful because it means that (a) inputs not used by the
1411 * fragment shader won't take up valuable register space, and (b) we
1412 * won't have to recompile the fragment shader if it gets paired with
1413 * a different vertex (or geometry) shader.
1415 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1416 if (prog
->InputsRead
& BRW_FS_VARYING_INPUT_MASK
&
1417 BITFIELD64_BIT(i
)) {
1418 prog_data
->urb_setup
[i
] = urb_next
++;
1422 /* We have enough input varyings that the SF/SBE pipeline stage can't
1423 * arbitrarily rearrange them to suit our whim; we have to put them
1424 * in an order that matches the output of the previous pipeline stage
1425 * (geometry or vertex shader).
1427 struct brw_vue_map prev_stage_vue_map
;
1428 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1429 key
->input_slots_valid
);
1430 int first_slot
= 2 * BRW_SF_URB_ENTRY_READ_OFFSET
;
1431 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1432 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1434 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1435 /* Note that varying == BRW_VARYING_SLOT_COUNT when a slot is
1438 if (varying
!= BRW_VARYING_SLOT_COUNT
&&
1439 (prog
->InputsRead
& BRW_FS_VARYING_INPUT_MASK
&
1440 BITFIELD64_BIT(varying
))) {
1441 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1444 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1447 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1448 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1449 /* Point size is packed into the header, not as a general attribute */
1450 if (i
== VARYING_SLOT_PSIZ
)
1453 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1454 /* The back color slot is skipped when the front color is
1455 * also written to. In addition, some slots can be
1456 * written in the vertex shader and not read in the
1457 * fragment shader. So the register number must always be
1458 * incremented, mapped or not.
1460 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1461 prog_data
->urb_setup
[i
] = urb_next
;
1467 * It's a FS only attribute, and we did interpolation for this attribute
1468 * in SF thread. So, count it here, too.
1470 * See compile_sf_prog() for more info.
1472 if (prog
->InputsRead
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1473 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1476 prog_data
->num_varying_inputs
= urb_next
;
1480 fs_visitor::assign_urb_setup()
1482 assert(stage
== MESA_SHADER_FRAGMENT
);
1483 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1485 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1487 /* Offset all the urb_setup[] index by the actual position of the
1488 * setup regs, now that the location of the constants has been chosen.
1490 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1491 if (inst
->opcode
== FS_OPCODE_LINTERP
) {
1492 assert(inst
->src
[1].file
== HW_REG
);
1493 inst
->src
[1].fixed_hw_reg
.nr
+= urb_start
;
1496 if (inst
->opcode
== FS_OPCODE_CINTERP
) {
1497 assert(inst
->src
[0].file
== HW_REG
);
1498 inst
->src
[0].fixed_hw_reg
.nr
+= urb_start
;
1502 /* Each attribute is 4 setup channels, each of which is half a reg. */
1503 this->first_non_payload_grf
=
1504 urb_start
+ prog_data
->num_varying_inputs
* 2;
1508 fs_visitor::assign_vs_urb_setup()
1510 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
1511 int grf
, count
, slot
, channel
, attr
;
1513 assert(stage
== MESA_SHADER_VERTEX
);
1514 count
= _mesa_bitcount_64(vs_prog_data
->inputs_read
);
1515 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
)
1518 /* Each attribute is 4 regs. */
1519 this->first_non_payload_grf
=
1520 payload
.num_regs
+ prog_data
->curb_read_length
+ count
* 4;
1522 unsigned vue_entries
=
1523 MAX2(count
, vs_prog_data
->base
.vue_map
.num_slots
);
1525 vs_prog_data
->base
.urb_entry_size
= ALIGN(vue_entries
, 4) / 4;
1526 vs_prog_data
->base
.urb_read_length
= (count
+ 1) / 2;
1528 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1530 /* Rewrite all ATTR file references to the hw grf that they land in. */
1531 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1532 for (int i
= 0; i
< inst
->sources
; i
++) {
1533 if (inst
->src
[i
].file
== ATTR
) {
1535 if (inst
->src
[i
].reg
== VERT_ATTRIB_MAX
) {
1538 /* Attributes come in in a contiguous block, ordered by their
1539 * gl_vert_attrib value. That means we can compute the slot
1540 * number for an attribute by masking out the enabled
1541 * attributes before it and counting the bits.
1543 attr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
/ 4;
1544 slot
= _mesa_bitcount_64(vs_prog_data
->inputs_read
&
1545 BITFIELD64_MASK(attr
));
1548 channel
= inst
->src
[i
].reg_offset
& 3;
1550 grf
= payload
.num_regs
+
1551 prog_data
->curb_read_length
+
1554 inst
->src
[i
].file
= HW_REG
;
1555 inst
->src
[i
].fixed_hw_reg
=
1556 retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
);
1563 * Split large virtual GRFs into separate components if we can.
1565 * This is mostly duplicated with what brw_fs_vector_splitting does,
1566 * but that's really conservative because it's afraid of doing
1567 * splitting that doesn't result in real progress after the rest of
1568 * the optimization phases, which would cause infinite looping in
1569 * optimization. We can do it once here, safely. This also has the
1570 * opportunity to split interpolated values, or maybe even uniforms,
1571 * which we don't have at the IR level.
1573 * We want to split, because virtual GRFs are what we register
1574 * allocate and spill (due to contiguousness requirements for some
1575 * instructions), and they're what we naturally generate in the
1576 * codegen process, but most virtual GRFs don't actually need to be
1577 * contiguous sets of GRFs. If we split, we'll end up with reduced
1578 * live intervals and better dead code elimination and coalescing.
1581 fs_visitor::split_virtual_grfs()
1583 int num_vars
= this->alloc
.count
;
1585 /* Count the total number of registers */
1587 int vgrf_to_reg
[num_vars
];
1588 for (int i
= 0; i
< num_vars
; i
++) {
1589 vgrf_to_reg
[i
] = reg_count
;
1590 reg_count
+= alloc
.sizes
[i
];
1593 /* An array of "split points". For each register slot, this indicates
1594 * if this slot can be separated from the previous slot. Every time an
1595 * instruction uses multiple elements of a register (as a source or
1596 * destination), we mark the used slots as inseparable. Then we go
1597 * through and split the registers into the smallest pieces we can.
1599 bool split_points
[reg_count
];
1600 memset(split_points
, 0, sizeof(split_points
));
1602 /* Mark all used registers as fully splittable */
1603 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1604 if (inst
->dst
.file
== GRF
) {
1605 int reg
= vgrf_to_reg
[inst
->dst
.reg
];
1606 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.reg
]; j
++)
1607 split_points
[reg
+ j
] = true;
1610 for (int i
= 0; i
< inst
->sources
; i
++) {
1611 if (inst
->src
[i
].file
== GRF
) {
1612 int reg
= vgrf_to_reg
[inst
->src
[i
].reg
];
1613 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].reg
]; j
++)
1614 split_points
[reg
+ j
] = true;
1619 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1620 if (inst
->dst
.file
== GRF
) {
1621 int reg
= vgrf_to_reg
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
1622 for (int j
= 1; j
< inst
->regs_written
; j
++)
1623 split_points
[reg
+ j
] = false;
1625 for (int i
= 0; i
< inst
->sources
; i
++) {
1626 if (inst
->src
[i
].file
== GRF
) {
1627 int reg
= vgrf_to_reg
[inst
->src
[i
].reg
] + inst
->src
[i
].reg_offset
;
1628 for (int j
= 1; j
< inst
->regs_read(i
); j
++)
1629 split_points
[reg
+ j
] = false;
1634 int new_virtual_grf
[reg_count
];
1635 int new_reg_offset
[reg_count
];
1638 for (int i
= 0; i
< num_vars
; i
++) {
1639 /* The first one should always be 0 as a quick sanity check. */
1640 assert(split_points
[reg
] == false);
1643 new_reg_offset
[reg
] = 0;
1648 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1649 /* If this is a split point, reset the offset to 0 and allocate a
1650 * new virtual GRF for the previous offset many registers
1652 if (split_points
[reg
]) {
1653 assert(offset
<= MAX_VGRF_SIZE
);
1654 int grf
= alloc
.allocate(offset
);
1655 for (int k
= reg
- offset
; k
< reg
; k
++)
1656 new_virtual_grf
[k
] = grf
;
1659 new_reg_offset
[reg
] = offset
;
1664 /* The last one gets the original register number */
1665 assert(offset
<= MAX_VGRF_SIZE
);
1666 alloc
.sizes
[i
] = offset
;
1667 for (int k
= reg
- offset
; k
< reg
; k
++)
1668 new_virtual_grf
[k
] = i
;
1670 assert(reg
== reg_count
);
1672 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1673 if (inst
->dst
.file
== GRF
) {
1674 reg
= vgrf_to_reg
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
1675 inst
->dst
.reg
= new_virtual_grf
[reg
];
1676 inst
->dst
.reg_offset
= new_reg_offset
[reg
];
1677 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1679 for (int i
= 0; i
< inst
->sources
; i
++) {
1680 if (inst
->src
[i
].file
== GRF
) {
1681 reg
= vgrf_to_reg
[inst
->src
[i
].reg
] + inst
->src
[i
].reg_offset
;
1682 inst
->src
[i
].reg
= new_virtual_grf
[reg
];
1683 inst
->src
[i
].reg_offset
= new_reg_offset
[reg
];
1684 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1688 invalidate_live_intervals();
1692 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1694 * During code generation, we create tons of temporary variables, many of
1695 * which get immediately killed and are never used again. Yet, in later
1696 * optimization and analysis passes, such as compute_live_intervals, we need
1697 * to loop over all the virtual GRFs. Compacting them can save a lot of
1701 fs_visitor::compact_virtual_grfs()
1703 bool progress
= false;
1704 int remap_table
[this->alloc
.count
];
1705 memset(remap_table
, -1, sizeof(remap_table
));
1707 /* Mark which virtual GRFs are used. */
1708 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
1709 if (inst
->dst
.file
== GRF
)
1710 remap_table
[inst
->dst
.reg
] = 0;
1712 for (int i
= 0; i
< inst
->sources
; i
++) {
1713 if (inst
->src
[i
].file
== GRF
)
1714 remap_table
[inst
->src
[i
].reg
] = 0;
1718 /* Compact the GRF arrays. */
1720 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
1721 if (remap_table
[i
] == -1) {
1722 /* We just found an unused register. This means that we are
1723 * actually going to compact something.
1727 remap_table
[i
] = new_index
;
1728 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
1729 invalidate_live_intervals();
1734 this->alloc
.count
= new_index
;
1736 /* Patch all the instructions to use the newly renumbered registers */
1737 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1738 if (inst
->dst
.file
== GRF
)
1739 inst
->dst
.reg
= remap_table
[inst
->dst
.reg
];
1741 for (int i
= 0; i
< inst
->sources
; i
++) {
1742 if (inst
->src
[i
].file
== GRF
)
1743 inst
->src
[i
].reg
= remap_table
[inst
->src
[i
].reg
];
1747 /* Patch all the references to delta_xy, since they're used in register
1748 * allocation. If they're unused, switch them to BAD_FILE so we don't
1749 * think some random VGRF is delta_xy.
1751 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
1752 if (delta_xy
[i
].file
== GRF
) {
1753 if (remap_table
[delta_xy
[i
].reg
] != -1) {
1754 delta_xy
[i
].reg
= remap_table
[delta_xy
[i
].reg
];
1756 delta_xy
[i
].file
= BAD_FILE
;
1765 * Implements array access of uniforms by inserting a
1766 * PULL_CONSTANT_LOAD instruction.
1768 * Unlike temporary GRF array access (where we don't support it due to
1769 * the difficulty of doing relative addressing on instruction
1770 * destinations), we could potentially do array access of uniforms
1771 * that were loaded in GRF space as push constants. In real-world
1772 * usage we've seen, though, the arrays being used are always larger
1773 * than we could load as push constants, so just always move all
1774 * uniform array access out to a pull constant buffer.
1777 fs_visitor::move_uniform_array_access_to_pull_constants()
1779 if (dispatch_width
!= 8)
1782 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1783 memset(pull_constant_loc
, -1, sizeof(pull_constant_loc
[0]) * uniforms
);
1785 /* Walk through and find array access of uniforms. Put a copy of that
1786 * uniform in the pull constant buffer.
1788 * Note that we don't move constant-indexed accesses to arrays. No
1789 * testing has been done of the performance impact of this choice.
1791 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
1792 for (int i
= 0 ; i
< inst
->sources
; i
++) {
1793 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
1796 int uniform
= inst
->src
[i
].reg
;
1798 /* If this array isn't already present in the pull constant buffer,
1801 if (pull_constant_loc
[uniform
] == -1) {
1802 const gl_constant_value
**values
= &stage_prog_data
->param
[uniform
];
1804 assert(param_size
[uniform
]);
1806 for (int j
= 0; j
< param_size
[uniform
]; j
++) {
1807 pull_constant_loc
[uniform
+ j
] = stage_prog_data
->nr_pull_params
;
1809 stage_prog_data
->pull_param
[stage_prog_data
->nr_pull_params
++] =
1818 * Assign UNIFORM file registers to either push constants or pull constants.
1820 * We allow a fragment shader to have more than the specified minimum
1821 * maximum number of fragment shader uniform components (64). If
1822 * there are too many of these, they'd fill up all of register space.
1823 * So, this will push some of them out to the pull constant buffer and
1824 * update the program to load them.
1827 fs_visitor::assign_constant_locations()
1829 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
1830 if (dispatch_width
!= 8)
1833 /* Find which UNIFORM registers are still in use. */
1834 bool is_live
[uniforms
];
1835 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1839 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1840 for (int i
= 0; i
< inst
->sources
; i
++) {
1841 if (inst
->src
[i
].file
!= UNIFORM
)
1844 int constant_nr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1845 if (constant_nr
>= 0 && constant_nr
< (int) uniforms
)
1846 is_live
[constant_nr
] = true;
1850 /* Only allow 16 registers (128 uniform components) as push constants.
1852 * Just demote the end of the list. We could probably do better
1853 * here, demoting things that are rarely used in the program first.
1855 * If changing this value, note the limitation about total_regs in
1858 unsigned int max_push_components
= 16 * 8;
1859 unsigned int num_push_constants
= 0;
1861 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1863 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1864 if (!is_live
[i
] || pull_constant_loc
[i
] != -1) {
1865 /* This UNIFORM register is either dead, or has already been demoted
1866 * to a pull const. Mark it as no longer living in the param[] array.
1868 push_constant_loc
[i
] = -1;
1872 if (num_push_constants
< max_push_components
) {
1873 /* Retain as a push constant. Record the location in the params[]
1876 push_constant_loc
[i
] = num_push_constants
++;
1878 /* Demote to a pull constant. */
1879 push_constant_loc
[i
] = -1;
1881 int pull_index
= stage_prog_data
->nr_pull_params
++;
1882 stage_prog_data
->pull_param
[pull_index
] = stage_prog_data
->param
[i
];
1883 pull_constant_loc
[i
] = pull_index
;
1887 stage_prog_data
->nr_params
= num_push_constants
;
1889 /* Up until now, the param[] array has been indexed by reg + reg_offset
1890 * of UNIFORM registers. Condense it to only contain the uniforms we
1891 * chose to upload as push constants.
1893 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1894 int remapped
= push_constant_loc
[i
];
1899 assert(remapped
<= (int)i
);
1900 stage_prog_data
->param
[remapped
] = stage_prog_data
->param
[i
];
1905 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
1906 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
1909 fs_visitor::demote_pull_constants()
1911 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
1912 for (int i
= 0; i
< inst
->sources
; i
++) {
1913 if (inst
->src
[i
].file
!= UNIFORM
)
1917 unsigned location
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1918 if (location
>= uniforms
) /* Out of bounds access */
1921 pull_index
= pull_constant_loc
[location
];
1923 if (pull_index
== -1)
1926 /* Set up the annotation tracking for new generated instructions. */
1927 const fs_builder
ibld(this, block
, inst
);
1928 fs_reg
surf_index(stage_prog_data
->binding_table
.pull_constants_start
);
1929 fs_reg dst
= vgrf(glsl_type::float_type
);
1931 assert(inst
->src
[i
].stride
== 0);
1933 /* Generate a pull load into dst. */
1934 if (inst
->src
[i
].reladdr
) {
1935 VARYING_PULL_CONSTANT_LOAD(ibld
, dst
,
1937 *inst
->src
[i
].reladdr
,
1939 inst
->src
[i
].reladdr
= NULL
;
1940 inst
->src
[i
].stride
= 1;
1942 const fs_builder ubld
= ibld
.exec_all().group(8, 0);
1943 fs_reg offset
= fs_reg((unsigned)(pull_index
* 4) & ~15);
1944 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
1945 dst
, surf_index
, offset
);
1946 inst
->src
[i
].set_smear(pull_index
& 3);
1949 /* Rewrite the instruction to use the temporary VGRF. */
1950 inst
->src
[i
].file
= GRF
;
1951 inst
->src
[i
].reg
= dst
.reg
;
1952 inst
->src
[i
].reg_offset
= 0;
1955 invalidate_live_intervals();
1959 fs_visitor::opt_algebraic()
1961 bool progress
= false;
1963 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1964 switch (inst
->opcode
) {
1965 case BRW_OPCODE_MOV
:
1966 if (inst
->src
[0].file
!= IMM
)
1969 if (inst
->saturate
) {
1970 if (inst
->dst
.type
!= inst
->src
[0].type
)
1971 assert(!"unimplemented: saturate mixed types");
1973 if (brw_saturate_immediate(inst
->dst
.type
,
1974 &inst
->src
[0].fixed_hw_reg
)) {
1975 inst
->saturate
= false;
1981 case BRW_OPCODE_MUL
:
1982 if (inst
->src
[1].file
!= IMM
)
1986 if (inst
->src
[1].is_one()) {
1987 inst
->opcode
= BRW_OPCODE_MOV
;
1988 inst
->src
[1] = reg_undef
;
1994 if (inst
->src
[1].is_negative_one()) {
1995 inst
->opcode
= BRW_OPCODE_MOV
;
1996 inst
->src
[0].negate
= !inst
->src
[0].negate
;
1997 inst
->src
[1] = reg_undef
;
2003 if (inst
->src
[1].is_zero()) {
2004 inst
->opcode
= BRW_OPCODE_MOV
;
2005 inst
->src
[0] = inst
->src
[1];
2006 inst
->src
[1] = reg_undef
;
2011 if (inst
->src
[0].file
== IMM
) {
2012 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2013 inst
->opcode
= BRW_OPCODE_MOV
;
2014 inst
->src
[0].fixed_hw_reg
.dw1
.f
*= inst
->src
[1].fixed_hw_reg
.dw1
.f
;
2015 inst
->src
[1] = reg_undef
;
2020 case BRW_OPCODE_ADD
:
2021 if (inst
->src
[1].file
!= IMM
)
2025 if (inst
->src
[1].is_zero()) {
2026 inst
->opcode
= BRW_OPCODE_MOV
;
2027 inst
->src
[1] = reg_undef
;
2032 if (inst
->src
[0].file
== IMM
) {
2033 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2034 inst
->opcode
= BRW_OPCODE_MOV
;
2035 inst
->src
[0].fixed_hw_reg
.dw1
.f
+= inst
->src
[1].fixed_hw_reg
.dw1
.f
;
2036 inst
->src
[1] = reg_undef
;
2042 if (inst
->src
[0].equals(inst
->src
[1])) {
2043 inst
->opcode
= BRW_OPCODE_MOV
;
2044 inst
->src
[1] = reg_undef
;
2049 case BRW_OPCODE_LRP
:
2050 if (inst
->src
[1].equals(inst
->src
[2])) {
2051 inst
->opcode
= BRW_OPCODE_MOV
;
2052 inst
->src
[0] = inst
->src
[1];
2053 inst
->src
[1] = reg_undef
;
2054 inst
->src
[2] = reg_undef
;
2059 case BRW_OPCODE_CMP
:
2060 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
2062 inst
->src
[0].negate
&&
2063 inst
->src
[1].is_zero()) {
2064 inst
->src
[0].abs
= false;
2065 inst
->src
[0].negate
= false;
2066 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2071 case BRW_OPCODE_SEL
:
2072 if (inst
->src
[0].equals(inst
->src
[1])) {
2073 inst
->opcode
= BRW_OPCODE_MOV
;
2074 inst
->src
[1] = reg_undef
;
2075 inst
->predicate
= BRW_PREDICATE_NONE
;
2076 inst
->predicate_inverse
= false;
2078 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2079 switch (inst
->conditional_mod
) {
2080 case BRW_CONDITIONAL_LE
:
2081 case BRW_CONDITIONAL_L
:
2082 switch (inst
->src
[1].type
) {
2083 case BRW_REGISTER_TYPE_F
:
2084 if (inst
->src
[1].fixed_hw_reg
.dw1
.f
>= 1.0f
) {
2085 inst
->opcode
= BRW_OPCODE_MOV
;
2086 inst
->src
[1] = reg_undef
;
2087 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2095 case BRW_CONDITIONAL_GE
:
2096 case BRW_CONDITIONAL_G
:
2097 switch (inst
->src
[1].type
) {
2098 case BRW_REGISTER_TYPE_F
:
2099 if (inst
->src
[1].fixed_hw_reg
.dw1
.f
<= 0.0f
) {
2100 inst
->opcode
= BRW_OPCODE_MOV
;
2101 inst
->src
[1] = reg_undef
;
2102 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2114 case BRW_OPCODE_MAD
:
2115 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2116 inst
->opcode
= BRW_OPCODE_MOV
;
2117 inst
->src
[1] = reg_undef
;
2118 inst
->src
[2] = reg_undef
;
2120 } else if (inst
->src
[0].is_zero()) {
2121 inst
->opcode
= BRW_OPCODE_MUL
;
2122 inst
->src
[0] = inst
->src
[2];
2123 inst
->src
[2] = reg_undef
;
2125 } else if (inst
->src
[1].is_one()) {
2126 inst
->opcode
= BRW_OPCODE_ADD
;
2127 inst
->src
[1] = inst
->src
[2];
2128 inst
->src
[2] = reg_undef
;
2130 } else if (inst
->src
[2].is_one()) {
2131 inst
->opcode
= BRW_OPCODE_ADD
;
2132 inst
->src
[2] = reg_undef
;
2134 } else if (inst
->src
[1].file
== IMM
&& inst
->src
[2].file
== IMM
) {
2135 inst
->opcode
= BRW_OPCODE_ADD
;
2136 inst
->src
[1].fixed_hw_reg
.dw1
.f
*= inst
->src
[2].fixed_hw_reg
.dw1
.f
;
2137 inst
->src
[2] = reg_undef
;
2141 case SHADER_OPCODE_RCP
: {
2142 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
2143 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
2144 if (inst
->src
[0].equals(prev
->dst
)) {
2145 inst
->opcode
= SHADER_OPCODE_RSQ
;
2146 inst
->src
[0] = prev
->src
[0];
2152 case SHADER_OPCODE_BROADCAST
:
2153 if (is_uniform(inst
->src
[0])) {
2154 inst
->opcode
= BRW_OPCODE_MOV
;
2156 inst
->force_writemask_all
= true;
2158 } else if (inst
->src
[1].file
== IMM
) {
2159 inst
->opcode
= BRW_OPCODE_MOV
;
2160 inst
->src
[0] = component(inst
->src
[0],
2161 inst
->src
[1].fixed_hw_reg
.dw1
.ud
);
2163 inst
->force_writemask_all
= true;
2172 /* Swap if src[0] is immediate. */
2173 if (progress
&& inst
->is_commutative()) {
2174 if (inst
->src
[0].file
== IMM
) {
2175 fs_reg tmp
= inst
->src
[1];
2176 inst
->src
[1] = inst
->src
[0];
2185 * Optimize sample messages that have constant zero values for the trailing
2186 * texture coordinates. We can just reduce the message length for these
2187 * instructions instead of reserving a register for it. Trailing parameters
2188 * that aren't sent default to zero anyway. This will cause the dead code
2189 * eliminator to remove the MOV instruction that would otherwise be emitted to
2190 * set up the zero value.
2193 fs_visitor::opt_zero_samples()
2195 /* Gen4 infers the texturing opcode based on the message length so we can't
2198 if (devinfo
->gen
< 5)
2201 bool progress
= false;
2203 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2204 if (!inst
->is_tex())
2207 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2209 if (load_payload
->is_head_sentinel() ||
2210 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2213 /* We don't want to remove the message header or the first parameter.
2214 * Removing the first parameter is not allowed, see the Haswell PRM
2215 * volume 7, page 149:
2217 * "Parameter 0 is required except for the sampleinfo message, which
2218 * has no parameter 0"
2220 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2221 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2222 (inst
->exec_size
/ 8) +
2223 inst
->header_size
- 1].is_zero()) {
2224 inst
->mlen
-= inst
->exec_size
/ 8;
2230 invalidate_live_intervals();
2236 * Optimize sample messages which are followed by the final RT write.
2238 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2239 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2240 * final texturing results copied to the framebuffer write payload and modify
2241 * them to write to the framebuffer directly.
2244 fs_visitor::opt_sampler_eot()
2246 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2248 if (stage
!= MESA_SHADER_FRAGMENT
)
2251 if (devinfo
->gen
< 9 && !devinfo
->is_cherryview
)
2254 /* FINISHME: It should be possible to implement this optimization when there
2255 * are multiple drawbuffers.
2257 if (key
->nr_color_regions
!= 1)
2260 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2261 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2262 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2263 assert(fb_write
->eot
);
2264 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE
);
2266 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2268 /* There wasn't one; nothing to do. */
2269 if (unlikely(tex_inst
->is_head_sentinel()) || !tex_inst
->is_tex())
2272 /* This optimisation doesn't seem to work for textureGather for some
2273 * reason. I can't find any documentation or known workarounds to indicate
2274 * that this is expected, but considering that it is probably pretty
2275 * unlikely that a shader would directly write out the results from
2276 * textureGather we might as well just disable it.
2278 if (tex_inst
->opcode
== SHADER_OPCODE_TG4
||
2279 tex_inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
2282 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2283 * It's very likely to be the previous instruction.
2285 fs_inst
*load_payload
= (fs_inst
*) tex_inst
->prev
;
2286 if (load_payload
->is_head_sentinel() ||
2287 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2290 assert(!tex_inst
->eot
); /* We can't get here twice */
2291 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2293 const fs_builder
ibld(this, block
, tex_inst
);
2295 tex_inst
->offset
|= fb_write
->target
<< 24;
2296 tex_inst
->eot
= true;
2297 tex_inst
->dst
= ibld
.null_reg_ud();
2298 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2300 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2301 * to create a new LOAD_PAYLOAD command with the same sources and a space
2302 * saved for the header. Using a new destination register not only makes sure
2303 * we have enough space, but it will make sure the dead code eliminator kills
2304 * the instruction that this will replace.
2306 if (tex_inst
->header_size
!= 0)
2309 fs_reg send_header
= ibld
.vgrf(BRW_REGISTER_TYPE_F
,
2310 load_payload
->sources
+ 1);
2311 fs_reg
*new_sources
=
2312 ralloc_array(mem_ctx
, fs_reg
, load_payload
->sources
+ 1);
2314 new_sources
[0] = fs_reg();
2315 for (int i
= 0; i
< load_payload
->sources
; i
++)
2316 new_sources
[i
+1] = load_payload
->src
[i
];
2318 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2319 * requires a lot of information about the sources to appropriately figure
2320 * out the number of registers needed to be used. Given this stage in our
2321 * optimization, we may not have the appropriate GRFs required by
2322 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2323 * manually emit the instruction.
2325 fs_inst
*new_load_payload
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
,
2326 load_payload
->exec_size
,
2329 load_payload
->sources
+ 1);
2331 new_load_payload
->regs_written
= load_payload
->regs_written
+ 1;
2332 new_load_payload
->header_size
= 1;
2334 tex_inst
->header_size
= 1;
2335 tex_inst
->insert_before(cfg
->blocks
[cfg
->num_blocks
- 1], new_load_payload
);
2336 tex_inst
->src
[0] = send_header
;
2342 fs_visitor::opt_register_renaming()
2344 bool progress
= false;
2347 int remap
[alloc
.count
];
2348 memset(remap
, -1, sizeof(int) * alloc
.count
);
2350 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2351 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2353 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2354 inst
->opcode
== BRW_OPCODE_WHILE
) {
2358 /* Rewrite instruction sources. */
2359 for (int i
= 0; i
< inst
->sources
; i
++) {
2360 if (inst
->src
[i
].file
== GRF
&&
2361 remap
[inst
->src
[i
].reg
] != -1 &&
2362 remap
[inst
->src
[i
].reg
] != inst
->src
[i
].reg
) {
2363 inst
->src
[i
].reg
= remap
[inst
->src
[i
].reg
];
2368 const int dst
= inst
->dst
.reg
;
2371 inst
->dst
.file
== GRF
&&
2372 alloc
.sizes
[inst
->dst
.reg
] == inst
->exec_size
/ 8 &&
2373 !inst
->is_partial_write()) {
2374 if (remap
[dst
] == -1) {
2377 remap
[dst
] = alloc
.allocate(inst
->exec_size
/ 8);
2378 inst
->dst
.reg
= remap
[dst
];
2381 } else if (inst
->dst
.file
== GRF
&&
2383 remap
[dst
] != dst
) {
2384 inst
->dst
.reg
= remap
[dst
];
2390 invalidate_live_intervals();
2392 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2393 if (delta_xy
[i
].file
== GRF
&& remap
[delta_xy
[i
].reg
] != -1) {
2394 delta_xy
[i
].reg
= remap
[delta_xy
[i
].reg
];
2403 * Remove redundant or useless discard jumps.
2405 * For example, we can eliminate jumps in the following sequence:
2407 * discard-jump (redundant with the next jump)
2408 * discard-jump (useless; jumps to the next instruction)
2412 fs_visitor::opt_redundant_discard_jumps()
2414 bool progress
= false;
2416 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2418 fs_inst
*placeholder_halt
= NULL
;
2419 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2420 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2421 placeholder_halt
= inst
;
2426 if (!placeholder_halt
)
2429 /* Delete any HALTs immediately before the placeholder halt. */
2430 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2431 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2432 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2433 prev
->remove(last_bblock
);
2438 invalidate_live_intervals();
2444 fs_visitor::compute_to_mrf()
2446 bool progress
= false;
2449 /* No MRFs on Gen >= 7. */
2450 if (devinfo
->gen
>= 7)
2453 calculate_live_intervals();
2455 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2459 if (inst
->opcode
!= BRW_OPCODE_MOV
||
2460 inst
->is_partial_write() ||
2461 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= GRF
||
2462 inst
->dst
.type
!= inst
->src
[0].type
||
2463 inst
->src
[0].abs
|| inst
->src
[0].negate
||
2464 !inst
->src
[0].is_contiguous() ||
2465 inst
->src
[0].subreg_offset
)
2468 /* Work out which hardware MRF registers are written by this
2471 int mrf_low
= inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
2473 if (inst
->dst
.reg
& BRW_MRF_COMPR4
) {
2474 mrf_high
= mrf_low
+ 4;
2475 } else if (inst
->exec_size
== 16) {
2476 mrf_high
= mrf_low
+ 1;
2481 /* Can't compute-to-MRF this GRF if someone else was going to
2484 if (this->virtual_grf_end
[inst
->src
[0].reg
] > ip
)
2487 /* Found a move of a GRF to a MRF. Let's see if we can go
2488 * rewrite the thing that made this GRF to write into the MRF.
2490 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2491 if (scan_inst
->dst
.file
== GRF
&&
2492 scan_inst
->dst
.reg
== inst
->src
[0].reg
) {
2493 /* Found the last thing to write our reg we want to turn
2494 * into a compute-to-MRF.
2497 /* If this one instruction didn't populate all the
2498 * channels, bail. We might be able to rewrite everything
2499 * that writes that reg, but it would require smarter
2500 * tracking to delay the rewriting until complete success.
2502 if (scan_inst
->is_partial_write())
2505 /* Things returning more than one register would need us to
2506 * understand coalescing out more than one MOV at a time.
2508 if (scan_inst
->regs_written
> scan_inst
->exec_size
/ 8)
2511 /* SEND instructions can't have MRF as a destination. */
2512 if (scan_inst
->mlen
)
2515 if (devinfo
->gen
== 6) {
2516 /* gen6 math instructions must have the destination be
2517 * GRF, so no compute-to-MRF for them.
2519 if (scan_inst
->is_math()) {
2524 if (scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
2525 /* Found the creator of our MRF's source value. */
2526 scan_inst
->dst
.file
= MRF
;
2527 scan_inst
->dst
.reg
= inst
->dst
.reg
;
2528 scan_inst
->saturate
|= inst
->saturate
;
2529 inst
->remove(block
);
2535 /* We don't handle control flow here. Most computation of
2536 * values that end up in MRFs are shortly before the MRF
2539 if (block
->start() == scan_inst
)
2542 /* You can't read from an MRF, so if someone else reads our
2543 * MRF's source GRF that we wanted to rewrite, that stops us.
2545 bool interfered
= false;
2546 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
2547 if (scan_inst
->src
[i
].file
== GRF
&&
2548 scan_inst
->src
[i
].reg
== inst
->src
[0].reg
&&
2549 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
2556 if (scan_inst
->dst
.file
== MRF
) {
2557 /* If somebody else writes our MRF here, we can't
2558 * compute-to-MRF before that.
2560 int scan_mrf_low
= scan_inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
2563 if (scan_inst
->dst
.reg
& BRW_MRF_COMPR4
) {
2564 scan_mrf_high
= scan_mrf_low
+ 4;
2565 } else if (scan_inst
->exec_size
== 16) {
2566 scan_mrf_high
= scan_mrf_low
+ 1;
2568 scan_mrf_high
= scan_mrf_low
;
2571 if (mrf_low
== scan_mrf_low
||
2572 mrf_low
== scan_mrf_high
||
2573 mrf_high
== scan_mrf_low
||
2574 mrf_high
== scan_mrf_high
) {
2579 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1) {
2580 /* Found a SEND instruction, which means that there are
2581 * live values in MRFs from base_mrf to base_mrf +
2582 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2585 if (mrf_low
>= scan_inst
->base_mrf
&&
2586 mrf_low
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2589 if (mrf_high
>= scan_inst
->base_mrf
&&
2590 mrf_high
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2598 invalidate_live_intervals();
2604 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2605 * flow. We could probably do better here with some form of divergence
2609 fs_visitor::eliminate_find_live_channel()
2611 bool progress
= false;
2614 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2615 switch (inst
->opcode
) {
2621 case BRW_OPCODE_ENDIF
:
2622 case BRW_OPCODE_WHILE
:
2626 case FS_OPCODE_DISCARD_JUMP
:
2627 /* This can potentially make control flow non-uniform until the end
2632 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2634 inst
->opcode
= BRW_OPCODE_MOV
;
2635 inst
->src
[0] = fs_reg(0);
2637 inst
->force_writemask_all
= true;
2651 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2652 * instructions to FS_OPCODE_REP_FB_WRITE.
2655 fs_visitor::emit_repclear_shader()
2657 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2659 int color_mrf
= base_mrf
+ 2;
2661 fs_inst
*mov
= bld
.exec_all().MOV(vec4(brw_message_reg(color_mrf
)),
2662 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
2665 if (key
->nr_color_regions
== 1) {
2666 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2667 write
->saturate
= key
->clamp_fragment_color
;
2668 write
->base_mrf
= color_mrf
;
2670 write
->header_size
= 0;
2673 assume(key
->nr_color_regions
> 0);
2674 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
2675 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2676 write
->saturate
= key
->clamp_fragment_color
;
2677 write
->base_mrf
= base_mrf
;
2679 write
->header_size
= 2;
2687 assign_constant_locations();
2688 assign_curb_setup();
2690 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2691 assert(mov
->src
[0].file
== HW_REG
);
2692 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].fixed_hw_reg
.nr
, 0);
2696 * Walks through basic blocks, looking for repeated MRF writes and
2697 * removing the later ones.
2700 fs_visitor::remove_duplicate_mrf_writes()
2702 fs_inst
*last_mrf_move
[16];
2703 bool progress
= false;
2705 /* Need to update the MRF tracking for compressed instructions. */
2706 if (dispatch_width
== 16)
2709 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2711 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2712 if (inst
->is_control_flow()) {
2713 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2716 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2717 inst
->dst
.file
== MRF
) {
2718 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.reg
];
2719 if (prev_inst
&& inst
->equals(prev_inst
)) {
2720 inst
->remove(block
);
2726 /* Clear out the last-write records for MRFs that were overwritten. */
2727 if (inst
->dst
.file
== MRF
) {
2728 last_mrf_move
[inst
->dst
.reg
] = NULL
;
2731 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
2732 /* Found a SEND instruction, which will include two or fewer
2733 * implied MRF writes. We could do better here.
2735 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
2736 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
2740 /* Clear out any MRF move records whose sources got overwritten. */
2741 if (inst
->dst
.file
== GRF
) {
2742 for (unsigned int i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
2743 if (last_mrf_move
[i
] &&
2744 last_mrf_move
[i
]->src
[0].reg
== inst
->dst
.reg
) {
2745 last_mrf_move
[i
] = NULL
;
2750 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2751 inst
->dst
.file
== MRF
&&
2752 inst
->src
[0].file
== GRF
&&
2753 !inst
->is_partial_write()) {
2754 last_mrf_move
[inst
->dst
.reg
] = inst
;
2759 invalidate_live_intervals();
2765 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
2767 /* Clear the flag for registers that actually got read (as expected). */
2768 for (int i
= 0; i
< inst
->sources
; i
++) {
2770 if (inst
->src
[i
].file
== GRF
) {
2771 grf
= inst
->src
[i
].reg
;
2772 } else if (inst
->src
[i
].file
== HW_REG
&&
2773 inst
->src
[i
].fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
) {
2774 grf
= inst
->src
[i
].fixed_hw_reg
.nr
;
2779 if (grf
>= first_grf
&&
2780 grf
< first_grf
+ grf_len
) {
2781 deps
[grf
- first_grf
] = false;
2782 if (inst
->exec_size
== 16)
2783 deps
[grf
- first_grf
+ 1] = false;
2789 * Implements this workaround for the original 965:
2791 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2792 * check for post destination dependencies on this instruction, software
2793 * must ensure that there is no destination hazard for the case of ‘write
2794 * followed by a posted write’ shown in the following example.
2797 * 2. send r3.xy <rest of send instruction>
2800 * Due to no post-destination dependency check on the ‘send’, the above
2801 * code sequence could have two instructions (1 and 2) in flight at the
2802 * same time that both consider ‘r3’ as the target of their final writes.
2805 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
2808 int write_len
= inst
->regs_written
;
2809 int first_write_grf
= inst
->dst
.reg
;
2810 bool needs_dep
[BRW_MAX_MRF
];
2811 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2813 memset(needs_dep
, false, sizeof(needs_dep
));
2814 memset(needs_dep
, true, write_len
);
2816 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
2818 /* Walk backwards looking for writes to registers we're writing which
2819 * aren't read since being written. If we hit the start of the program,
2820 * we assume that there are no outstanding dependencies on entry to the
2823 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2824 /* If we hit control flow, assume that there *are* outstanding
2825 * dependencies, and force their cleanup before our instruction.
2827 if (block
->start() == scan_inst
) {
2828 for (int i
= 0; i
< write_len
; i
++) {
2830 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
2831 first_write_grf
+ i
);
2836 /* We insert our reads as late as possible on the assumption that any
2837 * instruction but a MOV that might have left us an outstanding
2838 * dependency has more latency than a MOV.
2840 if (scan_inst
->dst
.file
== GRF
) {
2841 for (int i
= 0; i
< scan_inst
->regs_written
; i
++) {
2842 int reg
= scan_inst
->dst
.reg
+ i
;
2844 if (reg
>= first_write_grf
&&
2845 reg
< first_write_grf
+ write_len
&&
2846 needs_dep
[reg
- first_write_grf
]) {
2847 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
2848 needs_dep
[reg
- first_write_grf
] = false;
2849 if (scan_inst
->exec_size
== 16)
2850 needs_dep
[reg
- first_write_grf
+ 1] = false;
2855 /* Clear the flag for registers that actually got read (as expected). */
2856 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2858 /* Continue the loop only if we haven't resolved all the dependencies */
2860 for (i
= 0; i
< write_len
; i
++) {
2870 * Implements this workaround for the original 965:
2872 * "[DevBW, DevCL] Errata: A destination register from a send can not be
2873 * used as a destination register until after it has been sourced by an
2874 * instruction with a different destination register.
2877 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
2879 int write_len
= inst
->regs_written
;
2880 int first_write_grf
= inst
->dst
.reg
;
2881 bool needs_dep
[BRW_MAX_MRF
];
2882 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2884 memset(needs_dep
, false, sizeof(needs_dep
));
2885 memset(needs_dep
, true, write_len
);
2886 /* Walk forwards looking for writes to registers we're writing which aren't
2887 * read before being written.
2889 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2890 /* If we hit control flow, force resolve all remaining dependencies. */
2891 if (block
->end() == scan_inst
) {
2892 for (int i
= 0; i
< write_len
; i
++) {
2894 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2895 first_write_grf
+ i
);
2900 /* Clear the flag for registers that actually got read (as expected). */
2901 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2903 /* We insert our reads as late as possible since they're reading the
2904 * result of a SEND, which has massive latency.
2906 if (scan_inst
->dst
.file
== GRF
&&
2907 scan_inst
->dst
.reg
>= first_write_grf
&&
2908 scan_inst
->dst
.reg
< first_write_grf
+ write_len
&&
2909 needs_dep
[scan_inst
->dst
.reg
- first_write_grf
]) {
2910 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2911 scan_inst
->dst
.reg
);
2912 needs_dep
[scan_inst
->dst
.reg
- first_write_grf
] = false;
2915 /* Continue the loop only if we haven't resolved all the dependencies */
2917 for (i
= 0; i
< write_len
; i
++) {
2927 fs_visitor::insert_gen4_send_dependency_workarounds()
2929 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
2932 bool progress
= false;
2934 /* Note that we're done with register allocation, so GRF fs_regs always
2935 * have a .reg_offset of 0.
2938 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2939 if (inst
->mlen
!= 0 && inst
->dst
.file
== GRF
) {
2940 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
2941 insert_gen4_post_send_dependency_workarounds(block
, inst
);
2947 invalidate_live_intervals();
2951 * Turns the generic expression-style uniform pull constant load instruction
2952 * into a hardware-specific series of instructions for loading a pull
2955 * The expression style allows the CSE pass before this to optimize out
2956 * repeated loads from the same offset, and gives the pre-register-allocation
2957 * scheduling full flexibility, while the conversion to native instructions
2958 * allows the post-register-allocation scheduler the best information
2961 * Note that execution masking for setting up pull constant loads is special:
2962 * the channels that need to be written are unrelated to the current execution
2963 * mask, since a later instruction will use one of the result channels as a
2964 * source operand for all 8 or 16 of its channels.
2967 fs_visitor::lower_uniform_pull_constant_loads()
2969 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
2970 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
2973 if (devinfo
->gen
>= 7) {
2974 /* The offset arg before was a vec4-aligned byte offset. We need to
2975 * turn it into a dword offset.
2977 fs_reg const_offset_reg
= inst
->src
[1];
2978 assert(const_offset_reg
.file
== IMM
&&
2979 const_offset_reg
.type
== BRW_REGISTER_TYPE_UD
);
2980 const_offset_reg
.fixed_hw_reg
.dw1
.ud
/= 4;
2982 fs_reg payload
, offset
;
2983 if (devinfo
->gen
>= 9) {
2984 /* We have to use a message header on Skylake to get SIMD4x2
2985 * mode. Reserve space for the register.
2987 offset
= payload
= fs_reg(GRF
, alloc
.allocate(2));
2988 offset
.reg_offset
++;
2991 offset
= payload
= fs_reg(GRF
, alloc
.allocate(1));
2995 /* This is actually going to be a MOV, but since only the first dword
2996 * is accessed, we have a special opcode to do just that one. Note
2997 * that this needs to be an operation that will be considered a def
2998 * by live variable analysis, or register allocation will explode.
3000 fs_inst
*setup
= new(mem_ctx
) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET
,
3001 8, offset
, const_offset_reg
);
3002 setup
->force_writemask_all
= true;
3004 setup
->ir
= inst
->ir
;
3005 setup
->annotation
= inst
->annotation
;
3006 inst
->insert_before(block
, setup
);
3008 /* Similarly, this will only populate the first 4 channels of the
3009 * result register (since we only use smear values from 0-3), but we
3010 * don't tell the optimizer.
3012 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3013 inst
->src
[1] = payload
;
3014 inst
->base_mrf
= -1;
3016 invalidate_live_intervals();
3018 /* Before register allocation, we didn't tell the scheduler about the
3019 * MRF we use. We know it's safe to use this MRF because nothing
3020 * else does except for register spill/unspill, which generates and
3021 * uses its MRF within a single IR instruction.
3023 inst
->base_mrf
= 14;
3030 fs_visitor::lower_load_payload()
3032 bool progress
= false;
3034 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3035 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3038 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== GRF
);
3039 assert(inst
->saturate
== false);
3040 fs_reg dst
= inst
->dst
;
3042 /* Get rid of COMPR4. We'll add it back in if we need it */
3043 if (dst
.file
== MRF
)
3044 dst
.reg
= dst
.reg
& ~BRW_MRF_COMPR4
;
3046 const fs_builder
ibld(this, block
, inst
);
3047 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3049 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3050 if (inst
->src
[i
].file
!= BAD_FILE
) {
3051 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3052 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3053 hbld
.MOV(mov_dst
, mov_src
);
3055 dst
= offset(dst
, hbld
, 1);
3058 if (inst
->dst
.file
== MRF
&& (inst
->dst
.reg
& BRW_MRF_COMPR4
) &&
3059 inst
->exec_size
> 8) {
3060 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3061 * a straightforward copy. Instead, the result of the
3062 * LOAD_PAYLOAD is treated as interleaved and the first four
3063 * non-header sources are unpacked as:
3074 * This is used for gen <= 5 fb writes.
3076 assert(inst
->exec_size
== 16);
3077 assert(inst
->header_size
+ 4 <= inst
->sources
);
3078 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3079 if (inst
->src
[i
].file
!= BAD_FILE
) {
3080 if (devinfo
->has_compr4
) {
3081 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3082 compr4_dst
.reg
|= BRW_MRF_COMPR4
;
3083 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3085 /* Platform doesn't have COMPR4. We have to fake it */
3086 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3087 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3089 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3096 /* The loop above only ever incremented us through the first set
3097 * of 4 registers. However, thanks to the magic of COMPR4, we
3098 * actually wrote to the first 8 registers, so we need to take
3099 * that into account now.
3103 /* The COMPR4 code took care of the first 4 sources. We'll let
3104 * the regular path handle any remaining sources. Yes, we are
3105 * modifying the instruction but we're about to delete it so
3106 * this really doesn't hurt anything.
3108 inst
->header_size
+= 4;
3111 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3112 if (inst
->src
[i
].file
!= BAD_FILE
)
3113 ibld
.MOV(retype(dst
, inst
->src
[i
].type
), inst
->src
[i
]);
3114 dst
= offset(dst
, ibld
, 1);
3117 inst
->remove(block
);
3122 invalidate_live_intervals();
3128 fs_visitor::lower_integer_multiplication()
3130 bool progress
= false;
3132 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit operation
3133 * directly, but Cherryview cannot.
3135 if (devinfo
->gen
>= 8 && !devinfo
->is_cherryview
)
3138 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3139 if (inst
->opcode
!= BRW_OPCODE_MUL
||
3140 inst
->dst
.is_accumulator() ||
3141 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3142 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3145 const fs_builder
ibld(this, block
, inst
);
3147 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3148 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3151 * If multiplying by an immediate value that fits in 16-bits, do a
3152 * single MUL instruction with that value in the proper location.
3154 if (inst
->src
[1].file
== IMM
&&
3155 inst
->src
[1].fixed_hw_reg
.dw1
.ud
< (1 << 16)) {
3156 if (devinfo
->gen
< 7) {
3157 fs_reg
imm(GRF
, alloc
.allocate(dispatch_width
/ 8),
3159 ibld
.MOV(imm
, inst
->src
[1]);
3160 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3162 ibld
.MUL(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3165 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3166 * do 32-bit integer multiplication in one instruction, but instead
3167 * must do a sequence (which actually calculates a 64-bit result):
3169 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3170 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3171 * mov(8) g2<1>D acc0<8,8,1>D
3173 * But on Gen > 6, the ability to use second accumulator register
3174 * (acc1) for non-float data types was removed, preventing a simple
3175 * implementation in SIMD16. A 16-channel result can be calculated by
3176 * executing the three instructions twice in SIMD8, once with quarter
3177 * control of 1Q for the first eight channels and again with 2Q for
3178 * the second eight channels.
3180 * Which accumulator register is implicitly accessed (by AccWrEnable
3181 * for instance) is determined by the quarter control. Unfortunately
3182 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3183 * implicit accumulator access by an instruction with 2Q will access
3184 * acc1 regardless of whether the data type is usable in acc1.
3186 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3187 * integer data types.
3189 * Since we only want the low 32-bits of the result, we can do two
3190 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3191 * adjust the high result and add them (like the mach is doing):
3193 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3194 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3195 * shl(8) g9<1>D g8<8,8,1>D 16D
3196 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3198 * We avoid the shl instruction by realizing that we only want to add
3199 * the low 16-bits of the "high" result to the high 16-bits of the
3200 * "low" result and using proper regioning on the add:
3202 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3203 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3204 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3206 * Since it does not use the (single) accumulator register, we can
3207 * schedule multi-component multiplications much better.
3210 if (inst
->conditional_mod
&& inst
->dst
.is_null()) {
3211 inst
->dst
= fs_reg(GRF
, alloc
.allocate(dispatch_width
/ 8),
3214 fs_reg low
= inst
->dst
;
3215 fs_reg
high(GRF
, alloc
.allocate(dispatch_width
/ 8),
3218 if (devinfo
->gen
>= 7) {
3219 fs_reg src1_0_w
= inst
->src
[1];
3220 fs_reg src1_1_w
= inst
->src
[1];
3222 if (inst
->src
[1].file
== IMM
) {
3223 src1_0_w
.fixed_hw_reg
.dw1
.ud
&= 0xffff;
3224 src1_1_w
.fixed_hw_reg
.dw1
.ud
>>= 16;
3226 src1_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3227 if (src1_0_w
.stride
!= 0) {
3228 assert(src1_0_w
.stride
== 1);
3229 src1_0_w
.stride
= 2;
3232 src1_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3233 if (src1_1_w
.stride
!= 0) {
3234 assert(src1_1_w
.stride
== 1);
3235 src1_1_w
.stride
= 2;
3237 src1_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3239 ibld
.MUL(low
, inst
->src
[0], src1_0_w
);
3240 ibld
.MUL(high
, inst
->src
[0], src1_1_w
);
3242 fs_reg src0_0_w
= inst
->src
[0];
3243 fs_reg src0_1_w
= inst
->src
[0];
3245 src0_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3246 if (src0_0_w
.stride
!= 0) {
3247 assert(src0_0_w
.stride
== 1);
3248 src0_0_w
.stride
= 2;
3251 src0_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3252 if (src0_1_w
.stride
!= 0) {
3253 assert(src0_1_w
.stride
== 1);
3254 src0_1_w
.stride
= 2;
3256 src0_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3258 ibld
.MUL(low
, src0_0_w
, inst
->src
[1]);
3259 ibld
.MUL(high
, src0_1_w
, inst
->src
[1]);
3262 fs_reg dst
= inst
->dst
;
3263 dst
.type
= BRW_REGISTER_TYPE_UW
;
3264 dst
.subreg_offset
= 2;
3267 high
.type
= BRW_REGISTER_TYPE_UW
;
3270 low
.type
= BRW_REGISTER_TYPE_UW
;
3271 low
.subreg_offset
= 2;
3274 ibld
.ADD(dst
, low
, high
);
3276 if (inst
->conditional_mod
) {
3277 fs_reg
null(retype(ibld
.null_reg_f(), inst
->dst
.type
));
3278 set_condmod(inst
->conditional_mod
,
3279 ibld
.MOV(null
, inst
->dst
));
3283 inst
->remove(block
);
3288 invalidate_live_intervals();
3294 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
3295 fs_reg
*dst
, fs_reg color
, unsigned components
)
3297 if (key
->clamp_fragment_color
) {
3298 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
3299 assert(color
.type
== BRW_REGISTER_TYPE_F
);
3301 for (unsigned i
= 0; i
< components
; i
++)
3303 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
3308 for (unsigned i
= 0; i
< components
; i
++)
3309 dst
[i
] = offset(color
, bld
, i
);
3313 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
3314 const brw_wm_prog_data
*prog_data
,
3315 const brw_wm_prog_key
*key
,
3316 const fs_visitor::thread_payload
&payload
)
3318 assert(inst
->src
[6].file
== IMM
);
3319 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3320 const fs_reg
&color0
= inst
->src
[0];
3321 const fs_reg
&color1
= inst
->src
[1];
3322 const fs_reg
&src0_alpha
= inst
->src
[2];
3323 const fs_reg
&src_depth
= inst
->src
[3];
3324 const fs_reg
&dst_depth
= inst
->src
[4];
3325 fs_reg sample_mask
= inst
->src
[5];
3326 const unsigned components
= inst
->src
[6].fixed_hw_reg
.dw1
.ud
;
3328 /* We can potentially have a message length of up to 15, so we have to set
3329 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3332 int header_size
= 2, payload_header_size
;
3333 unsigned length
= 0;
3335 /* From the Sandy Bridge PRM, volume 4, page 198:
3337 * "Dispatched Pixel Enables. One bit per pixel indicating
3338 * which pixels were originally enabled when the thread was
3339 * dispatched. This field is only required for the end-of-
3340 * thread message and on all dual-source messages."
3342 if (devinfo
->gen
>= 6 &&
3343 (devinfo
->is_haswell
|| devinfo
->gen
>= 8 || !prog_data
->uses_kill
) &&
3344 color1
.file
== BAD_FILE
&&
3345 key
->nr_color_regions
== 1) {
3349 if (header_size
!= 0) {
3350 assert(header_size
== 2);
3351 /* Allocate 2 registers for a header */
3355 if (payload
.aa_dest_stencil_reg
) {
3356 sources
[length
] = fs_reg(GRF
, bld
.shader
->alloc
.allocate(1));
3357 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3358 .MOV(sources
[length
],
3359 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0)));
3363 if (prog_data
->uses_omask
) {
3364 sources
[length
] = fs_reg(GRF
, bld
.shader
->alloc
.allocate(1),
3365 BRW_REGISTER_TYPE_UD
);
3367 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3368 * relevant. Since it's unsigned single words one vgrf is always
3369 * 16-wide, but only the lower or higher 8 channels will be used by the
3370 * hardware when doing a SIMD8 write depending on whether we have
3371 * selected the subspans for the first or second half respectively.
3373 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
3374 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
3375 sample_mask
.stride
*= 2;
3377 bld
.exec_all().annotate("FB write oMask")
3378 .MOV(half(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
3379 inst
->force_sechalf
),
3384 payload_header_size
= length
;
3386 if (src0_alpha
.file
!= BAD_FILE
) {
3387 /* FIXME: This is being passed at the wrong location in the payload and
3388 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3389 * It's supposed to be immediately before oMask but there seems to be no
3390 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3391 * requires header sources to form a contiguous segment at the beginning
3392 * of the message and src0_alpha has per-channel semantics.
3394 setup_color_payload(bld
, key
, &sources
[length
], src0_alpha
, 1);
3398 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
3401 if (color1
.file
!= BAD_FILE
) {
3402 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
3406 if (src_depth
.file
!= BAD_FILE
) {
3407 sources
[length
] = src_depth
;
3411 if (dst_depth
.file
!= BAD_FILE
) {
3412 sources
[length
] = dst_depth
;
3417 if (devinfo
->gen
>= 7) {
3418 /* Send from the GRF */
3419 fs_reg payload
= fs_reg(GRF
, -1, BRW_REGISTER_TYPE_F
);
3420 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
3421 payload
.reg
= bld
.shader
->alloc
.allocate(load
->regs_written
);
3422 load
->dst
= payload
;
3424 inst
->src
[0] = payload
;
3425 inst
->resize_sources(1);
3426 inst
->base_mrf
= -1;
3428 /* Send from the MRF */
3429 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3430 sources
, length
, payload_header_size
);
3432 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3433 * will do this for us if we just give it a COMPR4 destination.
3435 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
3436 load
->dst
.reg
|= BRW_MRF_COMPR4
;
3438 inst
->resize_sources(0);
3442 inst
->opcode
= FS_OPCODE_FB_WRITE
;
3443 inst
->mlen
= load
->regs_written
;
3444 inst
->header_size
= header_size
;
3448 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3449 const fs_reg
&coordinate
,
3450 const fs_reg
&shadow_c
,
3451 const fs_reg
&lod
, const fs_reg
&lod2
,
3452 const fs_reg
&sampler
,
3453 unsigned coord_components
,
3454 unsigned grad_components
)
3456 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
3457 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
3458 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
3459 fs_reg msg_end
= msg_begin
;
3462 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
3464 for (unsigned i
= 0; i
< coord_components
; i
++)
3465 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
3466 offset(coordinate
, bld
, i
));
3468 msg_end
= offset(msg_end
, bld
, coord_components
);
3470 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3471 * require all three components to be present and zero if they are unused.
3473 if (coord_components
> 0 &&
3474 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
3475 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
3476 for (unsigned i
= coord_components
; i
< 3; i
++)
3477 bld
.MOV(offset(msg_end
, bld
, i
), fs_reg(0.0f
));
3479 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
3482 if (op
== SHADER_OPCODE_TXD
) {
3483 /* TXD unsupported in SIMD16 mode. */
3484 assert(bld
.dispatch_width() == 8);
3486 /* the slots for u and v are always present, but r is optional */
3487 if (coord_components
< 2)
3488 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
3491 * dPdx = dudx, dvdx, drdx
3492 * dPdy = dudy, dvdy, drdy
3494 * 1-arg: Does not exist.
3496 * 2-arg: dudx dvdx dudy dvdy
3497 * dPdx.x dPdx.y dPdy.x dPdy.y
3500 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3501 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3502 * m5 m6 m7 m8 m9 m10
3504 for (unsigned i
= 0; i
< grad_components
; i
++)
3505 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
3507 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3509 for (unsigned i
= 0; i
< grad_components
; i
++)
3510 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
3512 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3516 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3517 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3519 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
3520 bld
.dispatch_width() == 16);
3522 const brw_reg_type type
=
3523 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
3524 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
3525 bld
.MOV(retype(msg_end
, type
), lod
);
3526 msg_end
= offset(msg_end
, bld
, 1);
3529 if (shadow_c
.file
!= BAD_FILE
) {
3530 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
3531 /* There's no plain shadow compare message, so we use shadow
3532 * compare with a bias of 0.0.
3534 bld
.MOV(msg_end
, fs_reg(0.0f
));
3535 msg_end
= offset(msg_end
, bld
, 1);
3538 bld
.MOV(msg_end
, shadow_c
);
3539 msg_end
= offset(msg_end
, bld
, 1);
3543 inst
->src
[0] = reg_undef
;
3544 inst
->src
[1] = sampler
;
3545 inst
->resize_sources(2);
3546 inst
->base_mrf
= msg_begin
.reg
;
3547 inst
->mlen
= msg_end
.reg
- msg_begin
.reg
;
3548 inst
->header_size
= 1;
3552 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3554 const fs_reg
&shadow_c
,
3555 fs_reg lod
, fs_reg lod2
,
3556 const fs_reg
&sample_index
,
3557 const fs_reg
&sampler
,
3558 const fs_reg
&offset_value
,
3559 unsigned coord_components
,
3560 unsigned grad_components
)
3562 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
3563 fs_reg msg_coords
= message
;
3564 unsigned header_size
= 0;
3566 if (offset_value
.file
!= BAD_FILE
) {
3567 /* The offsets set up by the visitor are in the m1 header, so we can't
3574 for (unsigned i
= 0; i
< coord_components
; i
++) {
3575 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
), coordinate
);
3576 coordinate
= offset(coordinate
, bld
, 1);
3578 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
3579 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
3581 if (shadow_c
.file
!= BAD_FILE
) {
3582 fs_reg msg_shadow
= msg_lod
;
3583 bld
.MOV(msg_shadow
, shadow_c
);
3584 msg_lod
= offset(msg_shadow
, bld
, 1);
3589 case SHADER_OPCODE_TXL
:
3591 bld
.MOV(msg_lod
, lod
);
3592 msg_end
= offset(msg_lod
, bld
, 1);
3594 case SHADER_OPCODE_TXD
:
3597 * dPdx = dudx, dvdx, drdx
3598 * dPdy = dudy, dvdy, drdy
3600 * Load up these values:
3601 * - dudx dudy dvdx dvdy drdx drdy
3602 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3605 for (unsigned i
= 0; i
< grad_components
; i
++) {
3606 bld
.MOV(msg_end
, lod
);
3607 lod
= offset(lod
, bld
, 1);
3608 msg_end
= offset(msg_end
, bld
, 1);
3610 bld
.MOV(msg_end
, lod2
);
3611 lod2
= offset(lod2
, bld
, 1);
3612 msg_end
= offset(msg_end
, bld
, 1);
3615 case SHADER_OPCODE_TXS
:
3616 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
3617 bld
.MOV(msg_lod
, lod
);
3618 msg_end
= offset(msg_lod
, bld
, 1);
3620 case SHADER_OPCODE_TXF
:
3621 msg_lod
= offset(msg_coords
, bld
, 3);
3622 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
3623 msg_end
= offset(msg_lod
, bld
, 1);
3625 case SHADER_OPCODE_TXF_CMS
:
3626 msg_lod
= offset(msg_coords
, bld
, 3);
3628 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u));
3630 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
3631 msg_end
= offset(msg_lod
, bld
, 2);
3638 inst
->src
[0] = reg_undef
;
3639 inst
->src
[1] = sampler
;
3640 inst
->resize_sources(2);
3641 inst
->base_mrf
= message
.reg
;
3642 inst
->mlen
= msg_end
.reg
- message
.reg
;
3643 inst
->header_size
= header_size
;
3645 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3646 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3650 is_high_sampler(const struct brw_device_info
*devinfo
, const fs_reg
&sampler
)
3652 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
3655 return sampler
.file
!= IMM
|| sampler
.fixed_hw_reg
.dw1
.ud
>= 16;
3659 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3661 const fs_reg
&shadow_c
,
3662 fs_reg lod
, fs_reg lod2
,
3663 const fs_reg
&sample_index
,
3664 const fs_reg
&mcs
, const fs_reg
&sampler
,
3665 fs_reg offset_value
,
3666 unsigned coord_components
,
3667 unsigned grad_components
)
3669 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3670 int reg_width
= bld
.dispatch_width() / 8;
3671 unsigned header_size
= 0, length
= 0;
3672 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
3673 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
3674 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
3676 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
3677 offset_value
.file
!= BAD_FILE
||
3678 is_high_sampler(devinfo
, sampler
)) {
3679 /* For general texture offsets (no txf workaround), we need a header to
3680 * put them in. Note that we're only reserving space for it in the
3681 * message payload as it will be initialized implicitly by the
3684 * TG4 needs to place its channel select in the header, for interaction
3685 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3686 * larger sampler numbers we need to offset the Sampler State Pointer in
3690 sources
[0] = fs_reg();
3694 if (shadow_c
.file
!= BAD_FILE
) {
3695 bld
.MOV(sources
[length
], shadow_c
);
3699 bool coordinate_done
= false;
3701 /* The sampler can only meaningfully compute LOD for fragment shader
3702 * messages. For all other stages, we change the opcode to TXL and
3703 * hardcode the LOD to 0.
3705 if (bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
&&
3706 op
== SHADER_OPCODE_TEX
) {
3707 op
= SHADER_OPCODE_TXL
;
3711 /* Set up the LOD info */
3714 case SHADER_OPCODE_TXL
:
3715 bld
.MOV(sources
[length
], lod
);
3718 case SHADER_OPCODE_TXD
:
3719 /* TXD should have been lowered in SIMD16 mode. */
3720 assert(bld
.dispatch_width() == 8);
3722 /* Load dPdx and the coordinate together:
3723 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
3725 for (unsigned i
= 0; i
< coord_components
; i
++) {
3726 bld
.MOV(sources
[length
], coordinate
);
3727 coordinate
= offset(coordinate
, bld
, 1);
3730 /* For cube map array, the coordinate is (u,v,r,ai) but there are
3731 * only derivatives for (u, v, r).
3733 if (i
< grad_components
) {
3734 bld
.MOV(sources
[length
], lod
);
3735 lod
= offset(lod
, bld
, 1);
3738 bld
.MOV(sources
[length
], lod2
);
3739 lod2
= offset(lod2
, bld
, 1);
3744 coordinate_done
= true;
3746 case SHADER_OPCODE_TXS
:
3747 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
3750 case SHADER_OPCODE_TXF
:
3751 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
3752 * On Gen9 they are u, v, lod, r
3754 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3755 coordinate
= offset(coordinate
, bld
, 1);
3758 if (devinfo
->gen
>= 9) {
3759 if (coord_components
>= 2) {
3760 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3761 coordinate
= offset(coordinate
, bld
, 1);
3766 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
3769 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++) {
3770 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3771 coordinate
= offset(coordinate
, bld
, 1);
3775 coordinate_done
= true;
3777 case SHADER_OPCODE_TXF_CMS
:
3778 case SHADER_OPCODE_TXF_UMS
:
3779 case SHADER_OPCODE_TXF_MCS
:
3780 if (op
== SHADER_OPCODE_TXF_UMS
|| op
== SHADER_OPCODE_TXF_CMS
) {
3781 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
3785 if (op
== SHADER_OPCODE_TXF_CMS
) {
3786 /* Data from the multisample control surface. */
3787 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
3791 /* There is no offsetting for this message; just copy in the integer
3792 * texture coordinates.
3794 for (unsigned i
= 0; i
< coord_components
; i
++) {
3795 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3796 coordinate
= offset(coordinate
, bld
, 1);
3800 coordinate_done
= true;
3802 case SHADER_OPCODE_TG4_OFFSET
:
3803 /* gather4_po_c should have been lowered in SIMD16 mode. */
3804 assert(bld
.dispatch_width() == 8 || shadow_c
.file
== BAD_FILE
);
3806 /* More crazy intermixing */
3807 for (unsigned i
= 0; i
< 2; i
++) { /* u, v */
3808 bld
.MOV(sources
[length
], coordinate
);
3809 coordinate
= offset(coordinate
, bld
, 1);
3813 for (unsigned i
= 0; i
< 2; i
++) { /* offu, offv */
3814 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
);
3815 offset_value
= offset(offset_value
, bld
, 1);
3819 if (coord_components
== 3) { /* r if present */
3820 bld
.MOV(sources
[length
], coordinate
);
3821 coordinate
= offset(coordinate
, bld
, 1);
3825 coordinate_done
= true;
3831 /* Set up the coordinate (except for cases where it was done above) */
3832 if (!coordinate_done
) {
3833 for (unsigned i
= 0; i
< coord_components
; i
++) {
3834 bld
.MOV(sources
[length
], coordinate
);
3835 coordinate
= offset(coordinate
, bld
, 1);
3842 mlen
= length
* reg_width
- header_size
;
3844 mlen
= length
* reg_width
;
3846 const fs_reg src_payload
= fs_reg(GRF
, bld
.shader
->alloc
.allocate(mlen
),
3847 BRW_REGISTER_TYPE_F
);
3848 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
3850 /* Generate the SEND. */
3852 inst
->src
[0] = src_payload
;
3853 inst
->src
[1] = sampler
;
3854 inst
->resize_sources(2);
3855 inst
->base_mrf
= -1;
3857 inst
->header_size
= header_size
;
3859 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3860 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3864 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
3866 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3867 const fs_reg
&coordinate
= inst
->src
[0];
3868 const fs_reg
&shadow_c
= inst
->src
[1];
3869 const fs_reg
&lod
= inst
->src
[2];
3870 const fs_reg
&lod2
= inst
->src
[3];
3871 const fs_reg
&sample_index
= inst
->src
[4];
3872 const fs_reg
&mcs
= inst
->src
[5];
3873 const fs_reg
&sampler
= inst
->src
[6];
3874 const fs_reg
&offset_value
= inst
->src
[7];
3875 assert(inst
->src
[8].file
== IMM
&& inst
->src
[9].file
== IMM
);
3876 const unsigned coord_components
= inst
->src
[8].fixed_hw_reg
.dw1
.ud
;
3877 const unsigned grad_components
= inst
->src
[9].fixed_hw_reg
.dw1
.ud
;
3879 if (devinfo
->gen
>= 7) {
3880 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
3881 shadow_c
, lod
, lod2
, sample_index
,
3882 mcs
, sampler
, offset_value
,
3883 coord_components
, grad_components
);
3884 } else if (devinfo
->gen
>= 5) {
3885 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
3886 shadow_c
, lod
, lod2
, sample_index
,
3887 sampler
, offset_value
,
3888 coord_components
, grad_components
);
3890 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
3891 shadow_c
, lod
, lod2
, sampler
,
3892 coord_components
, grad_components
);
3897 * Initialize the header present in some typed and untyped surface
3901 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
3903 fs_builder ubld
= bld
.exec_all().group(8, 0);
3904 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
3905 ubld
.MOV(dst
, fs_reg(0));
3906 ubld
.MOV(component(dst
, 7), sample_mask
);
3911 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3912 const fs_reg
&sample_mask
)
3914 /* Get the logical send arguments. */
3915 const fs_reg
&addr
= inst
->src
[0];
3916 const fs_reg
&src
= inst
->src
[1];
3917 const fs_reg
&surface
= inst
->src
[2];
3918 const UNUSED fs_reg
&dims
= inst
->src
[3];
3919 const fs_reg
&arg
= inst
->src
[4];
3921 /* Calculate the total number of components of the payload. */
3922 const unsigned addr_sz
= inst
->components_read(0);
3923 const unsigned src_sz
= inst
->components_read(1);
3924 const unsigned header_sz
= (sample_mask
.file
== BAD_FILE
? 0 : 1);
3925 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
3927 /* Allocate space for the payload. */
3928 fs_reg
*const components
= new fs_reg
[sz
];
3929 const fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
3932 /* Construct the payload. */
3934 components
[n
++] = emit_surface_header(bld
, sample_mask
);
3936 for (unsigned i
= 0; i
< addr_sz
; i
++)
3937 components
[n
++] = offset(addr
, bld
, i
);
3939 for (unsigned i
= 0; i
< src_sz
; i
++)
3940 components
[n
++] = offset(src
, bld
, i
);
3942 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
3944 /* Update the original instruction. */
3946 inst
->mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
3947 inst
->header_size
= header_sz
;
3949 inst
->src
[0] = payload
;
3950 inst
->src
[1] = surface
;
3952 inst
->resize_sources(3);
3954 delete[] components
;
3958 fs_visitor::lower_logical_sends()
3960 bool progress
= false;
3962 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3963 const fs_builder
ibld(this, block
, inst
);
3965 switch (inst
->opcode
) {
3966 case FS_OPCODE_FB_WRITE_LOGICAL
:
3967 assert(stage
== MESA_SHADER_FRAGMENT
);
3968 lower_fb_write_logical_send(ibld
, inst
,
3969 (const brw_wm_prog_data
*)prog_data
,
3970 (const brw_wm_prog_key
*)key
,
3974 case SHADER_OPCODE_TEX_LOGICAL
:
3975 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
3978 case SHADER_OPCODE_TXD_LOGICAL
:
3979 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
3982 case SHADER_OPCODE_TXF_LOGICAL
:
3983 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
3986 case SHADER_OPCODE_TXL_LOGICAL
:
3987 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
3990 case SHADER_OPCODE_TXS_LOGICAL
:
3991 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
3994 case FS_OPCODE_TXB_LOGICAL
:
3995 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
3998 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
3999 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
4002 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
4003 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
4006 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
4007 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
4010 case SHADER_OPCODE_LOD_LOGICAL
:
4011 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
4014 case SHADER_OPCODE_TG4_LOGICAL
:
4015 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
4018 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
4019 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
4022 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
4023 lower_surface_logical_send(ibld
, inst
,
4024 SHADER_OPCODE_UNTYPED_SURFACE_READ
,
4028 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
4029 lower_surface_logical_send(ibld
, inst
,
4030 SHADER_OPCODE_UNTYPED_SURFACE_WRITE
,
4031 ibld
.sample_mask_reg());
4034 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
4035 lower_surface_logical_send(ibld
, inst
,
4036 SHADER_OPCODE_UNTYPED_ATOMIC
,
4037 ibld
.sample_mask_reg());
4040 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4041 lower_surface_logical_send(ibld
, inst
,
4042 SHADER_OPCODE_TYPED_SURFACE_READ
,
4046 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4047 lower_surface_logical_send(ibld
, inst
,
4048 SHADER_OPCODE_TYPED_SURFACE_WRITE
,
4049 ibld
.sample_mask_reg());
4052 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4053 lower_surface_logical_send(ibld
, inst
,
4054 SHADER_OPCODE_TYPED_ATOMIC
,
4055 ibld
.sample_mask_reg());
4066 invalidate_live_intervals();
4072 * Get the closest native SIMD width supported by the hardware for instruction
4073 * \p inst. The instruction will be left untouched by
4074 * fs_visitor::lower_simd_width() if the returned value is equal to the
4075 * original execution size.
4078 get_lowered_simd_width(const struct brw_device_info
*devinfo
,
4079 const fs_inst
*inst
)
4081 switch (inst
->opcode
) {
4082 case FS_OPCODE_FB_WRITE_LOGICAL
:
4083 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4086 assert(devinfo
->gen
!= 6 || inst
->src
[3].file
== BAD_FILE
||
4087 inst
->exec_size
== 8);
4088 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4089 return (inst
->src
[1].file
!= BAD_FILE
? 8 : inst
->exec_size
);
4091 case SHADER_OPCODE_TXD_LOGICAL
:
4092 /* TXD is unsupported in SIMD16 mode. */
4095 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
: {
4096 /* gather4_po_c is unsupported in SIMD16 mode. */
4097 const fs_reg
&shadow_c
= inst
->src
[1];
4098 return (shadow_c
.file
!= BAD_FILE
? 8 : inst
->exec_size
);
4100 case SHADER_OPCODE_TXL_LOGICAL
:
4101 case FS_OPCODE_TXB_LOGICAL
: {
4102 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4103 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4104 * mode because the message exceeds the maximum length of 11.
4106 const fs_reg
&shadow_c
= inst
->src
[1];
4107 if (devinfo
->gen
== 4 && shadow_c
.file
== BAD_FILE
)
4109 else if (devinfo
->gen
< 7 && shadow_c
.file
!= BAD_FILE
)
4112 return inst
->exec_size
;
4114 case SHADER_OPCODE_TXF_LOGICAL
:
4115 case SHADER_OPCODE_TXS_LOGICAL
:
4116 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4117 * messages. Use SIMD16 instead.
4119 if (devinfo
->gen
== 4)
4122 return inst
->exec_size
;
4124 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4125 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4126 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4130 return inst
->exec_size
;
4135 * The \p rows array of registers represents a \p num_rows by \p num_columns
4136 * matrix in row-major order, write it in column-major order into the register
4137 * passed as destination. \p stride gives the separation between matrix
4138 * elements in the input in fs_builder::dispatch_width() units.
4141 emit_transpose(const fs_builder
&bld
,
4142 const fs_reg
&dst
, const fs_reg
*rows
,
4143 unsigned num_rows
, unsigned num_columns
, unsigned stride
)
4145 fs_reg
*const components
= new fs_reg
[num_rows
* num_columns
];
4147 for (unsigned i
= 0; i
< num_columns
; ++i
) {
4148 for (unsigned j
= 0; j
< num_rows
; ++j
)
4149 components
[num_rows
* i
+ j
] = offset(rows
[j
], bld
, stride
* i
);
4152 bld
.LOAD_PAYLOAD(dst
, components
, num_rows
* num_columns
, 0);
4154 delete[] components
;
4158 fs_visitor::lower_simd_width()
4160 bool progress
= false;
4162 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4163 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
4165 if (lower_width
!= inst
->exec_size
) {
4166 /* Builder matching the original instruction. We may also need to
4167 * emit an instruction of width larger than the original, set the
4168 * execution size of the builder to the highest of both for now so
4169 * we're sure that both cases can be handled.
4171 const fs_builder ibld
= bld
.at(block
, inst
)
4172 .exec_all(inst
->force_writemask_all
)
4173 .group(MAX2(inst
->exec_size
, lower_width
),
4174 inst
->force_sechalf
);
4176 /* Split the copies in chunks of the execution width of either the
4177 * original or the lowered instruction, whichever is lower.
4179 const unsigned copy_width
= MIN2(lower_width
, inst
->exec_size
);
4180 const unsigned n
= inst
->exec_size
/ copy_width
;
4181 const unsigned dst_size
= inst
->regs_written
* REG_SIZE
/
4182 inst
->dst
.component_size(inst
->exec_size
);
4185 assert(n
> 0 && n
<= ARRAY_SIZE(dsts
) &&
4186 !inst
->writes_accumulator
&& !inst
->mlen
);
4188 for (unsigned i
= 0; i
< n
; i
++) {
4189 /* Emit a copy of the original instruction with the lowered width.
4190 * If the EOT flag was set throw it away except for the last
4191 * instruction to avoid killing the thread prematurely.
4193 fs_inst split_inst
= *inst
;
4194 split_inst
.exec_size
= lower_width
;
4195 split_inst
.eot
= inst
->eot
&& i
== n
- 1;
4197 /* Select the correct channel enables for the i-th group, then
4198 * transform the sources and destination and emit the lowered
4201 const fs_builder lbld
= ibld
.group(lower_width
, i
);
4203 for (unsigned j
= 0; j
< inst
->sources
; j
++) {
4204 if (inst
->src
[j
].file
!= BAD_FILE
&&
4205 !is_uniform(inst
->src
[j
])) {
4206 /* Get the i-th copy_width-wide chunk of the source. */
4207 const fs_reg src
= horiz_offset(inst
->src
[j
], copy_width
* i
);
4208 const unsigned src_size
= inst
->components_read(j
);
4210 /* Use a trivial transposition to copy one every n
4211 * copy_width-wide components of the register into a
4212 * temporary passed as source to the lowered instruction.
4214 split_inst
.src
[j
] = lbld
.vgrf(inst
->src
[j
].type
, src_size
);
4215 emit_transpose(lbld
.group(copy_width
, 0),
4216 split_inst
.src
[j
], &src
, 1, src_size
, n
);
4220 if (inst
->regs_written
) {
4221 /* Allocate enough space to hold the result of the lowered
4222 * instruction and fix up the number of registers written.
4224 split_inst
.dst
= dsts
[i
] =
4225 lbld
.vgrf(inst
->dst
.type
, dst_size
);
4226 split_inst
.regs_written
=
4227 DIV_ROUND_UP(inst
->regs_written
* lower_width
,
4231 lbld
.emit(split_inst
);
4234 if (inst
->regs_written
) {
4235 /* Distance between useful channels in the temporaries, skipping
4236 * garbage if the lowered instruction is wider than the original.
4238 const unsigned m
= lower_width
/ copy_width
;
4240 /* Interleave the components of the result from the lowered
4241 * instructions. We need to set exec_all() when copying more than
4242 * one half per component, because LOAD_PAYLOAD (in terms of which
4243 * emit_transpose is implemented) can only use the same channel
4244 * enable signals for all of its non-header sources.
4246 emit_transpose(ibld
.exec_all(inst
->exec_size
> copy_width
)
4247 .group(copy_width
, 0),
4248 inst
->dst
, dsts
, n
, dst_size
, m
);
4251 inst
->remove(block
);
4257 invalidate_live_intervals();
4263 fs_visitor::dump_instructions()
4265 dump_instructions(NULL
);
4269 fs_visitor::dump_instructions(const char *name
)
4271 FILE *file
= stderr
;
4272 if (name
&& geteuid() != 0) {
4273 file
= fopen(name
, "w");
4279 calculate_register_pressure();
4280 int ip
= 0, max_pressure
= 0;
4281 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
4282 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
4283 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
4284 dump_instruction(inst
, file
);
4287 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
4290 foreach_in_list(backend_instruction
, inst
, &instructions
) {
4291 fprintf(file
, "%4d: ", ip
++);
4292 dump_instruction(inst
, file
);
4296 if (file
!= stderr
) {
4302 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
4304 dump_instruction(be_inst
, stderr
);
4308 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
4310 fs_inst
*inst
= (fs_inst
*)be_inst
;
4312 if (inst
->predicate
) {
4313 fprintf(file
, "(%cf0.%d) ",
4314 inst
->predicate_inverse
? '-' : '+',
4318 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
4320 fprintf(file
, ".sat");
4321 if (inst
->conditional_mod
) {
4322 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
4323 if (!inst
->predicate
&&
4324 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
4325 inst
->opcode
!= BRW_OPCODE_IF
&&
4326 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
4327 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
4330 fprintf(file
, "(%d) ", inst
->exec_size
);
4333 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
4336 switch (inst
->dst
.file
) {
4338 fprintf(file
, "vgrf%d", inst
->dst
.reg
);
4339 if (alloc
.sizes
[inst
->dst
.reg
] != inst
->regs_written
||
4340 inst
->dst
.subreg_offset
)
4341 fprintf(file
, "+%d.%d",
4342 inst
->dst
.reg_offset
, inst
->dst
.subreg_offset
);
4345 fprintf(file
, "m%d", inst
->dst
.reg
);
4348 fprintf(file
, "(null)");
4351 fprintf(file
, "***u%d***", inst
->dst
.reg
+ inst
->dst
.reg_offset
);
4354 fprintf(file
, "***attr%d***", inst
->dst
.reg
+ inst
->dst
.reg_offset
);
4357 if (inst
->dst
.fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
4358 switch (inst
->dst
.fixed_hw_reg
.nr
) {
4360 fprintf(file
, "null");
4362 case BRW_ARF_ADDRESS
:
4363 fprintf(file
, "a0.%d", inst
->dst
.fixed_hw_reg
.subnr
);
4365 case BRW_ARF_ACCUMULATOR
:
4366 fprintf(file
, "acc%d", inst
->dst
.fixed_hw_reg
.subnr
);
4369 fprintf(file
, "f%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
4370 inst
->dst
.fixed_hw_reg
.subnr
);
4373 fprintf(file
, "arf%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
4374 inst
->dst
.fixed_hw_reg
.subnr
);
4378 fprintf(file
, "hw_reg%d", inst
->dst
.fixed_hw_reg
.nr
);
4380 if (inst
->dst
.fixed_hw_reg
.subnr
)
4381 fprintf(file
, "+%d", inst
->dst
.fixed_hw_reg
.subnr
);
4384 fprintf(file
, "???");
4387 fprintf(file
, ":%s, ", brw_reg_type_letters(inst
->dst
.type
));
4389 for (int i
= 0; i
< inst
->sources
; i
++) {
4390 if (inst
->src
[i
].negate
)
4392 if (inst
->src
[i
].abs
)
4394 switch (inst
->src
[i
].file
) {
4396 fprintf(file
, "vgrf%d", inst
->src
[i
].reg
);
4397 if (alloc
.sizes
[inst
->src
[i
].reg
] != (unsigned)inst
->regs_read(i
) ||
4398 inst
->src
[i
].subreg_offset
)
4399 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4400 inst
->src
[i
].subreg_offset
);
4403 fprintf(file
, "***m%d***", inst
->src
[i
].reg
);
4406 fprintf(file
, "attr%d", inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
);
4409 fprintf(file
, "u%d", inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
);
4410 if (inst
->src
[i
].reladdr
) {
4411 fprintf(file
, "+reladdr");
4412 } else if (inst
->src
[i
].subreg_offset
) {
4413 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4414 inst
->src
[i
].subreg_offset
);
4418 fprintf(file
, "(null)");
4421 switch (inst
->src
[i
].type
) {
4422 case BRW_REGISTER_TYPE_F
:
4423 fprintf(file
, "%ff", inst
->src
[i
].fixed_hw_reg
.dw1
.f
);
4425 case BRW_REGISTER_TYPE_W
:
4426 case BRW_REGISTER_TYPE_D
:
4427 fprintf(file
, "%dd", inst
->src
[i
].fixed_hw_reg
.dw1
.d
);
4429 case BRW_REGISTER_TYPE_UW
:
4430 case BRW_REGISTER_TYPE_UD
:
4431 fprintf(file
, "%uu", inst
->src
[i
].fixed_hw_reg
.dw1
.ud
);
4433 case BRW_REGISTER_TYPE_VF
:
4434 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
4435 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 0) & 0xff),
4436 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 8) & 0xff),
4437 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 16) & 0xff),
4438 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 24) & 0xff));
4441 fprintf(file
, "???");
4446 if (inst
->src
[i
].fixed_hw_reg
.negate
)
4448 if (inst
->src
[i
].fixed_hw_reg
.abs
)
4450 if (inst
->src
[i
].fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
4451 switch (inst
->src
[i
].fixed_hw_reg
.nr
) {
4453 fprintf(file
, "null");
4455 case BRW_ARF_ADDRESS
:
4456 fprintf(file
, "a0.%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
4458 case BRW_ARF_ACCUMULATOR
:
4459 fprintf(file
, "acc%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
4462 fprintf(file
, "f%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
4463 inst
->src
[i
].fixed_hw_reg
.subnr
);
4466 fprintf(file
, "arf%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
4467 inst
->src
[i
].fixed_hw_reg
.subnr
);
4471 fprintf(file
, "hw_reg%d", inst
->src
[i
].fixed_hw_reg
.nr
);
4473 if (inst
->src
[i
].fixed_hw_reg
.subnr
)
4474 fprintf(file
, "+%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
4475 if (inst
->src
[i
].fixed_hw_reg
.abs
)
4479 fprintf(file
, "???");
4482 if (inst
->src
[i
].abs
)
4485 if (inst
->src
[i
].file
!= IMM
) {
4486 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
4489 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
4490 fprintf(file
, ", ");
4495 if (dispatch_width
== 16 && inst
->exec_size
== 8) {
4496 if (inst
->force_sechalf
)
4497 fprintf(file
, "2ndhalf ");
4499 fprintf(file
, "1sthalf ");
4502 fprintf(file
, "\n");
4506 * Possibly returns an instruction that set up @param reg.
4508 * Sometimes we want to take the result of some expression/variable
4509 * dereference tree and rewrite the instruction generating the result
4510 * of the tree. When processing the tree, we know that the
4511 * instructions generated are all writing temporaries that are dead
4512 * outside of this tree. So, if we have some instructions that write
4513 * a temporary, we're free to point that temp write somewhere else.
4515 * Note that this doesn't guarantee that the instruction generated
4516 * only reg -- it might be the size=4 destination of a texture instruction.
4519 fs_visitor::get_instruction_generating_reg(fs_inst
*start
,
4524 end
->is_partial_write() ||
4526 !reg
.equals(end
->dst
)) {
4534 fs_visitor::setup_payload_gen6()
4537 (prog
->InputsRead
& (1 << VARYING_SLOT_POS
)) != 0;
4538 unsigned barycentric_interp_modes
=
4539 (stage
== MESA_SHADER_FRAGMENT
) ?
4540 ((brw_wm_prog_data
*) this->prog_data
)->barycentric_interp_modes
: 0;
4542 assert(devinfo
->gen
>= 6);
4544 /* R0-1: masks, pixel X/Y coordinates. */
4545 payload
.num_regs
= 2;
4546 /* R2: only for 32-pixel dispatch.*/
4548 /* R3-26: barycentric interpolation coordinates. These appear in the
4549 * same order that they appear in the brw_wm_barycentric_interp_mode
4550 * enum. Each set of coordinates occupies 2 registers if dispatch width
4551 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4552 * appear if they were enabled using the "Barycentric Interpolation
4553 * Mode" bits in WM_STATE.
4555 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
4556 if (barycentric_interp_modes
& (1 << i
)) {
4557 payload
.barycentric_coord_reg
[i
] = payload
.num_regs
;
4558 payload
.num_regs
+= 2;
4559 if (dispatch_width
== 16) {
4560 payload
.num_regs
+= 2;
4565 /* R27: interpolated depth if uses source depth */
4567 payload
.source_depth_reg
= payload
.num_regs
;
4569 if (dispatch_width
== 16) {
4570 /* R28: interpolated depth if not SIMD8. */
4574 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4576 payload
.source_w_reg
= payload
.num_regs
;
4578 if (dispatch_width
== 16) {
4579 /* R30: interpolated W if not SIMD8. */
4584 if (stage
== MESA_SHADER_FRAGMENT
) {
4585 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4586 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
4587 prog_data
->uses_pos_offset
= key
->compute_pos_offset
;
4588 /* R31: MSAA position offsets. */
4589 if (prog_data
->uses_pos_offset
) {
4590 payload
.sample_pos_reg
= payload
.num_regs
;
4595 /* R32: MSAA input coverage mask */
4596 if (prog
->SystemValuesRead
& SYSTEM_BIT_SAMPLE_MASK_IN
) {
4597 assert(devinfo
->gen
>= 7);
4598 payload
.sample_mask_in_reg
= payload
.num_regs
;
4600 if (dispatch_width
== 16) {
4601 /* R33: input coverage mask if not SIMD8. */
4606 /* R34-: bary for 32-pixel. */
4607 /* R58-59: interp W for 32-pixel. */
4609 if (prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
4610 source_depth_to_render_target
= true;
4615 fs_visitor::setup_vs_payload()
4617 /* R0: thread header, R1: urb handles */
4618 payload
.num_regs
= 2;
4622 fs_visitor::setup_cs_payload()
4624 assert(devinfo
->gen
>= 7);
4626 payload
.num_regs
= 1;
4630 fs_visitor::assign_binding_table_offsets()
4632 assert(stage
== MESA_SHADER_FRAGMENT
);
4633 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4634 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
4635 uint32_t next_binding_table_offset
= 0;
4637 /* If there are no color regions, we still perform an FB write to a null
4638 * renderbuffer, which we place at surface index 0.
4640 prog_data
->binding_table
.render_target_start
= next_binding_table_offset
;
4641 next_binding_table_offset
+= MAX2(key
->nr_color_regions
, 1);
4643 assign_common_binding_table_offsets(next_binding_table_offset
);
4647 fs_visitor::calculate_register_pressure()
4649 invalidate_live_intervals();
4650 calculate_live_intervals();
4652 unsigned num_instructions
= 0;
4653 foreach_block(block
, cfg
)
4654 num_instructions
+= block
->instructions
.length();
4656 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
4658 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
4659 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
4660 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
4665 fs_visitor::optimize()
4667 /* bld is the common builder object pointing at the end of the program we
4668 * used to translate it into i965 IR. For the optimization and lowering
4669 * passes coming next, any code added after the end of the program without
4670 * having explicitly called fs_builder::at() clearly points at a mistake.
4671 * Ideally optimization passes wouldn't be part of the visitor so they
4672 * wouldn't have access to bld at all, but they do, so just in case some
4673 * pass forgets to ask for a location explicitly set it to NULL here to
4674 * make it trip. The dispatch width is initialized to a bogus value to
4675 * make sure that optimizations set the execution controls explicitly to
4676 * match the code they are manipulating instead of relying on the defaults.
4678 bld
= fs_builder(this, 64);
4680 split_virtual_grfs();
4682 move_uniform_array_access_to_pull_constants();
4683 assign_constant_locations();
4684 demote_pull_constants();
4686 #define OPT(pass, args...) ({ \
4688 bool this_progress = pass(args); \
4690 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
4691 char filename[64]; \
4692 snprintf(filename, 64, "%s%d-%04d-%02d-%02d-" #pass, \
4693 stage_abbrev, dispatch_width, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
4695 backend_shader::dump_instructions(filename); \
4698 progress = progress || this_progress; \
4702 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
4704 snprintf(filename
, 64, "%s%d-%04d-00-start",
4705 stage_abbrev
, dispatch_width
,
4706 shader_prog
? shader_prog
->Name
: 0);
4708 backend_shader::dump_instructions(filename
);
4711 bool progress
= false;
4715 OPT(lower_simd_width
);
4716 OPT(lower_logical_sends
);
4723 OPT(remove_duplicate_mrf_writes
);
4727 OPT(opt_copy_propagate
);
4728 OPT(opt_peephole_predicated_break
);
4729 OPT(opt_cmod_propagation
);
4730 OPT(dead_code_eliminate
);
4731 OPT(opt_peephole_sel
);
4732 OPT(dead_control_flow_eliminate
, this);
4733 OPT(opt_register_renaming
);
4734 OPT(opt_redundant_discard_jumps
);
4735 OPT(opt_saturate_propagation
);
4736 OPT(opt_zero_samples
);
4737 OPT(register_coalesce
);
4738 OPT(compute_to_mrf
);
4739 OPT(eliminate_find_live_channel
);
4741 OPT(compact_virtual_grfs
);
4746 OPT(opt_sampler_eot
);
4748 if (OPT(lower_load_payload
)) {
4749 split_virtual_grfs();
4750 OPT(register_coalesce
);
4751 OPT(compute_to_mrf
);
4752 OPT(dead_code_eliminate
);
4755 OPT(opt_combine_constants
);
4756 OPT(lower_integer_multiplication
);
4758 lower_uniform_pull_constant_loads();
4762 * Three source instruction must have a GRF/MRF destination register.
4763 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
4766 fs_visitor::fixup_3src_null_dest()
4768 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
4769 if (inst
->is_3src() && inst
->dst
.is_null()) {
4770 inst
->dst
= fs_reg(GRF
, alloc
.allocate(dispatch_width
/ 8),
4777 fs_visitor::allocate_registers()
4779 bool allocated_without_spills
;
4781 static const enum instruction_scheduler_mode pre_modes
[] = {
4783 SCHEDULE_PRE_NON_LIFO
,
4787 /* Try each scheduling heuristic to see if it can successfully register
4788 * allocate without spilling. They should be ordered by decreasing
4789 * performance but increasing likelihood of allocating.
4791 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
4792 schedule_instructions(pre_modes
[i
]);
4795 assign_regs_trivial();
4796 allocated_without_spills
= true;
4798 allocated_without_spills
= assign_regs(false);
4800 if (allocated_without_spills
)
4804 if (!allocated_without_spills
) {
4805 /* We assume that any spilling is worse than just dropping back to
4806 * SIMD8. There's probably actually some intermediate point where
4807 * SIMD16 with a couple of spills is still better.
4809 if (dispatch_width
== 16) {
4810 fail("Failure to register allocate. Reduce number of "
4811 "live scalar values to avoid this.");
4813 compiler
->shader_perf_log(log_data
,
4814 "%s shader triggered register spilling. "
4815 "Try reducing the number of live scalar "
4816 "values to improve performance.\n",
4820 /* Since we're out of heuristics, just go spill registers until we
4821 * get an allocation.
4823 while (!assign_regs(true)) {
4829 /* This must come after all optimization and register allocation, since
4830 * it inserts dead code that happens to have side effects, and it does
4831 * so based on the actual physical registers in use.
4833 insert_gen4_send_dependency_workarounds();
4838 if (!allocated_without_spills
)
4839 schedule_instructions(SCHEDULE_POST
);
4841 if (last_scratch
> 0)
4842 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
4846 fs_visitor::run_vs(gl_clip_plane
*clip_planes
)
4848 assert(stage
== MESA_SHADER_VERTEX
);
4850 assign_common_binding_table_offsets(0);
4853 if (shader_time_index
>= 0)
4854 emit_shader_time_begin();
4861 compute_clip_distance(clip_planes
);
4865 if (shader_time_index
>= 0)
4866 emit_shader_time_end();
4872 assign_curb_setup();
4873 assign_vs_urb_setup();
4875 fixup_3src_null_dest();
4876 allocate_registers();
4882 fs_visitor::run_fs(bool do_rep_send
)
4884 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4885 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
4887 assert(stage
== MESA_SHADER_FRAGMENT
);
4889 sanity_param_count
= prog
->Parameters
->NumParameters
;
4891 assign_binding_table_offsets();
4893 if (devinfo
->gen
>= 6)
4894 setup_payload_gen6();
4896 setup_payload_gen4();
4900 } else if (do_rep_send
) {
4901 assert(dispatch_width
== 16);
4902 emit_repclear_shader();
4904 if (shader_time_index
>= 0)
4905 emit_shader_time_begin();
4907 calculate_urb_setup();
4908 if (prog
->InputsRead
> 0) {
4909 if (devinfo
->gen
< 6)
4910 emit_interpolation_setup_gen4();
4912 emit_interpolation_setup_gen6();
4915 /* We handle discards by keeping track of the still-live pixels in f0.1.
4916 * Initialize it with the dispatched pixels.
4918 if (wm_prog_data
->uses_kill
) {
4919 fs_inst
*discard_init
= bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
4920 discard_init
->flag_subreg
= 1;
4923 /* Generate FS IR for main(). (the visitor only descends into
4924 * functions called "main").
4931 if (wm_prog_data
->uses_kill
)
4932 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
4934 if (wm_key
->alpha_test_func
)
4939 if (shader_time_index
>= 0)
4940 emit_shader_time_end();
4946 assign_curb_setup();
4949 fixup_3src_null_dest();
4950 allocate_registers();
4956 if (dispatch_width
== 8)
4957 wm_prog_data
->reg_blocks
= brw_register_blocks(grf_used
);
4959 wm_prog_data
->reg_blocks_16
= brw_register_blocks(grf_used
);
4961 /* If any state parameters were appended, then ParameterValues could have
4962 * been realloced, in which case the driver uniform storage set up by
4963 * _mesa_associate_uniform_storage() would point to freed memory. Make
4964 * sure that didn't happen.
4966 assert(sanity_param_count
== prog
->Parameters
->NumParameters
);
4972 fs_visitor::run_cs()
4974 assert(stage
== MESA_SHADER_COMPUTE
);
4977 sanity_param_count
= prog
->Parameters
->NumParameters
;
4979 assign_common_binding_table_offsets(0);
4983 if (shader_time_index
>= 0)
4984 emit_shader_time_begin();
4991 emit_cs_terminate();
4993 if (shader_time_index
>= 0)
4994 emit_shader_time_end();
5000 assign_curb_setup();
5002 fixup_3src_null_dest();
5003 allocate_registers();
5008 /* If any state parameters were appended, then ParameterValues could have
5009 * been realloced, in which case the driver uniform storage set up by
5010 * _mesa_associate_uniform_storage() would point to freed memory. Make
5011 * sure that didn't happen.
5013 assert(sanity_param_count
== prog
->Parameters
->NumParameters
);
5019 brw_wm_fs_emit(struct brw_context
*brw
,
5021 const struct brw_wm_prog_key
*key
,
5022 struct brw_wm_prog_data
*prog_data
,
5023 struct gl_fragment_program
*fp
,
5024 struct gl_shader_program
*prog
,
5025 unsigned *final_assembly_size
)
5027 bool start_busy
= false;
5028 double start_time
= 0;
5030 if (unlikely(brw
->perf_debug
)) {
5031 start_busy
= (brw
->batch
.last_bo
&&
5032 drm_intel_bo_busy(brw
->batch
.last_bo
));
5033 start_time
= get_time();
5036 struct brw_shader
*shader
= NULL
;
5038 shader
= (brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
5040 if (unlikely(INTEL_DEBUG
& DEBUG_WM
))
5041 brw_dump_ir("fragment", prog
, &shader
->base
, &fp
->Base
);
5043 int st_index8
= -1, st_index16
= -1;
5044 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
) {
5045 st_index8
= brw_get_shader_time_index(brw
, prog
, &fp
->Base
, ST_FS8
);
5046 st_index16
= brw_get_shader_time_index(brw
, prog
, &fp
->Base
, ST_FS16
);
5049 /* Now the main event: Visit the shader IR and generate our FS IR for it.
5051 fs_visitor
v(brw
->intelScreen
->compiler
, brw
,
5052 mem_ctx
, MESA_SHADER_FRAGMENT
, key
, &prog_data
->base
,
5053 prog
, &fp
->Base
, 8, st_index8
);
5054 if (!v
.run_fs(false /* do_rep_send */)) {
5056 prog
->LinkStatus
= false;
5057 ralloc_strcat(&prog
->InfoLog
, v
.fail_msg
);
5060 _mesa_problem(NULL
, "Failed to compile fragment shader: %s\n",
5066 cfg_t
*simd16_cfg
= NULL
;
5067 fs_visitor
v2(brw
->intelScreen
->compiler
, brw
,
5068 mem_ctx
, MESA_SHADER_FRAGMENT
, key
, &prog_data
->base
,
5069 prog
, &fp
->Base
, 16, st_index16
);
5070 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
) || brw
->use_rep_send
)) {
5071 if (!v
.simd16_unsupported
) {
5072 /* Try a SIMD16 compile */
5073 v2
.import_uniforms(&v
);
5074 if (!v2
.run_fs(brw
->use_rep_send
)) {
5075 perf_debug("SIMD16 shader failed to compile: %s", v2
.fail_msg
);
5077 simd16_cfg
= v2
.cfg
;
5083 int no_simd8
= (INTEL_DEBUG
& DEBUG_NO8
) || brw
->no_simd8
;
5084 if ((no_simd8
|| brw
->gen
< 5) && simd16_cfg
) {
5086 prog_data
->no_8
= true;
5089 prog_data
->no_8
= false;
5092 fs_generator
g(brw
->intelScreen
->compiler
, brw
,
5093 mem_ctx
, (void *) key
, &prog_data
->base
,
5094 &fp
->Base
, v
.promoted_constants
, v
.runtime_check_aads_emit
, "FS");
5096 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
5099 name
= ralloc_asprintf(mem_ctx
, "%s fragment shader %d",
5100 prog
->Label
? prog
->Label
: "unnamed",
5103 name
= ralloc_asprintf(mem_ctx
, "fragment program %d", fp
->Base
.Id
);
5105 g
.enable_debug(name
);
5109 g
.generate_code(simd8_cfg
, 8);
5111 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
5113 if (unlikely(brw
->perf_debug
) && shader
) {
5114 if (shader
->compiled_once
)
5115 brw_wm_debug_recompile(brw
, prog
, key
);
5116 shader
->compiled_once
= true;
5118 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
5119 perf_debug("FS compile took %.03f ms and stalled the GPU\n",
5120 (get_time() - start_time
) * 1000);
5124 return g
.get_assembly(final_assembly_size
);
5128 brw_fs_precompile(struct gl_context
*ctx
,
5129 struct gl_shader_program
*shader_prog
,
5130 struct gl_program
*prog
)
5132 struct brw_context
*brw
= brw_context(ctx
);
5133 struct brw_wm_prog_key key
;
5135 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*) prog
;
5136 struct brw_fragment_program
*bfp
= brw_fragment_program(fp
);
5137 bool program_uses_dfdy
= fp
->UsesDFdy
;
5139 memset(&key
, 0, sizeof(key
));
5143 key
.iz_lookup
|= IZ_PS_KILL_ALPHATEST_BIT
;
5145 if (fp
->Base
.OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
5146 key
.iz_lookup
|= IZ_PS_COMPUTES_DEPTH_BIT
;
5148 /* Just assume depth testing. */
5149 key
.iz_lookup
|= IZ_DEPTH_TEST_ENABLE_BIT
;
5150 key
.iz_lookup
|= IZ_DEPTH_WRITE_ENABLE_BIT
;
5153 if (brw
->gen
< 6 || _mesa_bitcount_64(fp
->Base
.InputsRead
&
5154 BRW_FS_VARYING_INPUT_MASK
) > 16)
5155 key
.input_slots_valid
= fp
->Base
.InputsRead
| VARYING_BIT_POS
;
5157 brw_setup_tex_for_precompile(brw
, &key
.tex
, &fp
->Base
);
5159 if (fp
->Base
.InputsRead
& VARYING_BIT_POS
) {
5160 key
.drawable_height
= ctx
->DrawBuffer
->Height
;
5163 key
.nr_color_regions
= _mesa_bitcount_64(fp
->Base
.OutputsWritten
&
5164 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH
) |
5165 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)));
5167 if ((fp
->Base
.InputsRead
& VARYING_BIT_POS
) || program_uses_dfdy
) {
5168 key
.render_to_fbo
= _mesa_is_user_fbo(ctx
->DrawBuffer
) ||
5169 key
.nr_color_regions
> 1;
5172 key
.program_string_id
= bfp
->id
;
5174 uint32_t old_prog_offset
= brw
->wm
.base
.prog_offset
;
5175 struct brw_wm_prog_data
*old_prog_data
= brw
->wm
.prog_data
;
5177 bool success
= brw_codegen_wm_prog(brw
, shader_prog
, bfp
, &key
);
5179 brw
->wm
.base
.prog_offset
= old_prog_offset
;
5180 brw
->wm
.prog_data
= old_prog_data
;
5186 brw_setup_tex_for_precompile(struct brw_context
*brw
,
5187 struct brw_sampler_prog_key_data
*tex
,
5188 struct gl_program
*prog
)
5190 const bool has_shader_channel_select
= brw
->is_haswell
|| brw
->gen
>= 8;
5191 unsigned sampler_count
= _mesa_fls(prog
->SamplersUsed
);
5192 for (unsigned i
= 0; i
< sampler_count
; i
++) {
5193 if (!has_shader_channel_select
&& (prog
->ShadowSamplers
& (1 << i
))) {
5194 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
5196 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
5198 /* Color sampler: assume no swizzling. */
5199 tex
->swizzles
[i
] = SWIZZLE_XYZW
;