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_vec4_gs_visitor.h"
48 #include "brw_dead_control_flow.h"
49 #include "main/uniforms.h"
50 #include "brw_fs_live_variables.h"
51 #include "glsl/nir/glsl_types.h"
52 #include "program/sampler.h"
57 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
58 const fs_reg
*src
, unsigned sources
)
60 memset(this, 0, sizeof(*this));
62 this->src
= new fs_reg
[MAX2(sources
, 3)];
63 for (unsigned i
= 0; i
< sources
; i
++)
64 this->src
[i
] = src
[i
];
66 this->opcode
= opcode
;
68 this->sources
= sources
;
69 this->exec_size
= exec_size
;
71 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
73 assert(this->exec_size
!= 0);
75 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
77 /* This will be the case for almost all instructions. */
84 this->regs_written
= DIV_ROUND_UP(dst
.component_size(exec_size
),
88 this->regs_written
= 0;
92 unreachable("Invalid destination register file");
95 this->writes_accumulator
= false;
100 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
103 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
105 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
108 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
)
110 init(opcode
, exec_size
, dst
, NULL
, 0);
113 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
116 const fs_reg src
[1] = { src0
};
117 init(opcode
, exec_size
, dst
, src
, 1);
120 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
121 const fs_reg
&src0
, const fs_reg
&src1
)
123 const fs_reg src
[2] = { src0
, src1
};
124 init(opcode
, exec_size
, dst
, src
, 2);
127 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
128 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
130 const fs_reg src
[3] = { src0
, src1
, src2
};
131 init(opcode
, exec_size
, dst
, src
, 3);
134 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
135 const fs_reg src
[], unsigned sources
)
137 init(opcode
, exec_width
, dst
, src
, sources
);
140 fs_inst::fs_inst(const fs_inst
&that
)
142 memcpy(this, &that
, sizeof(that
));
144 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
146 for (unsigned i
= 0; i
< that
.sources
; i
++)
147 this->src
[i
] = that
.src
[i
];
156 fs_inst::resize_sources(uint8_t num_sources
)
158 if (this->sources
!= num_sources
) {
159 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
161 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
162 src
[i
] = this->src
[i
];
166 this->sources
= num_sources
;
171 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_builder
&bld
,
173 const fs_reg
&surf_index
,
174 const fs_reg
&varying_offset
,
175 uint32_t const_offset
)
177 /* We have our constant surface use a pitch of 4 bytes, so our index can
178 * be any component of a vector, and then we load 4 contiguous
179 * components starting from that.
181 * We break down the const_offset to a portion added to the variable
182 * offset and a portion done using reg_offset, which means that if you
183 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
184 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
185 * CSE can later notice that those loads are all the same and eliminate
186 * the redundant ones.
188 fs_reg vec4_offset
= vgrf(glsl_type::int_type
);
189 bld
.ADD(vec4_offset
, varying_offset
, fs_reg(const_offset
& ~3));
192 if (devinfo
->gen
== 4 && bld
.dispatch_width() == 8) {
193 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
194 * u, v, r) as parameters, or we can just use the SIMD16 message
195 * consisting of (header, u). We choose the second, at the cost of a
196 * longer return length.
202 if (devinfo
->gen
>= 7)
203 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
;
205 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
;
207 int regs_written
= 4 * (bld
.dispatch_width() / 8) * scale
;
208 fs_reg vec4_result
= fs_reg(VGRF
, alloc
.allocate(regs_written
), dst
.type
);
209 fs_inst
*inst
= bld
.emit(op
, vec4_result
, surf_index
, vec4_offset
);
210 inst
->regs_written
= regs_written
;
212 if (devinfo
->gen
< 7) {
213 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
);
214 inst
->header_size
= 1;
215 if (devinfo
->gen
== 4)
218 inst
->mlen
= 1 + bld
.dispatch_width() / 8;
221 bld
.MOV(dst
, offset(vec4_result
, bld
, (const_offset
& 3) * scale
));
225 * A helper for MOV generation for fixing up broken hardware SEND dependency
229 fs_visitor::DEP_RESOLVE_MOV(const fs_builder
&bld
, int grf
)
231 /* The caller always wants uncompressed to emit the minimal extra
232 * dependencies, and to avoid having to deal with aligning its regs to 2.
234 const fs_builder ubld
= bld
.annotate("send dependency resolve")
237 ubld
.MOV(ubld
.null_reg_f(), fs_reg(VGRF
, grf
, BRW_REGISTER_TYPE_F
));
241 fs_inst::equals(fs_inst
*inst
) const
243 return (opcode
== inst
->opcode
&&
244 dst
.equals(inst
->dst
) &&
245 src
[0].equals(inst
->src
[0]) &&
246 src
[1].equals(inst
->src
[1]) &&
247 src
[2].equals(inst
->src
[2]) &&
248 saturate
== inst
->saturate
&&
249 predicate
== inst
->predicate
&&
250 conditional_mod
== inst
->conditional_mod
&&
251 mlen
== inst
->mlen
&&
252 base_mrf
== inst
->base_mrf
&&
253 target
== inst
->target
&&
255 header_size
== inst
->header_size
&&
256 shadow_compare
== inst
->shadow_compare
&&
257 exec_size
== inst
->exec_size
&&
258 offset
== inst
->offset
);
262 fs_inst::overwrites_reg(const fs_reg
®
) const
264 return reg
.in_range(dst
, regs_written
);
268 fs_inst::is_send_from_grf() const
271 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
272 case SHADER_OPCODE_SHADER_TIME_ADD
:
273 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
274 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
275 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
276 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
277 case SHADER_OPCODE_UNTYPED_ATOMIC
:
278 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
279 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
280 case SHADER_OPCODE_TYPED_ATOMIC
:
281 case SHADER_OPCODE_TYPED_SURFACE_READ
:
282 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
283 case SHADER_OPCODE_URB_WRITE_SIMD8
:
284 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
285 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
286 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
287 case SHADER_OPCODE_URB_READ_SIMD8
:
289 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
290 return src
[1].file
== VGRF
;
291 case FS_OPCODE_FB_WRITE
:
292 return src
[0].file
== VGRF
;
295 return src
[0].file
== VGRF
;
302 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
304 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
307 fs_reg reg
= this->src
[0];
308 if (reg
.file
!= VGRF
|| reg
.reg_offset
!= 0 || reg
.stride
== 0)
311 if (grf_alloc
.sizes
[reg
.nr
] != this->regs_written
)
314 for (int i
= 0; i
< this->sources
; i
++) {
315 reg
.type
= this->src
[i
].type
;
316 if (!this->src
[i
].equals(reg
))
319 if (i
< this->header_size
) {
322 reg
.reg_offset
+= this->exec_size
/ 8;
330 fs_inst::can_do_source_mods(const struct brw_device_info
*devinfo
)
332 if (devinfo
->gen
== 6 && is_math())
335 if (is_send_from_grf())
338 if (!backend_instruction::can_do_source_mods())
345 fs_inst::can_change_types() const
347 return dst
.type
== src
[0].type
&&
348 !src
[0].abs
&& !src
[0].negate
&& !saturate
&&
349 (opcode
== BRW_OPCODE_MOV
||
350 (opcode
== BRW_OPCODE_SEL
&&
351 dst
.type
== src
[1].type
&&
352 predicate
!= BRW_PREDICATE_NONE
&&
353 !src
[1].abs
&& !src
[1].negate
));
357 fs_inst::has_side_effects() const
359 return this->eot
|| backend_instruction::has_side_effects();
365 memset(this, 0, sizeof(*this));
369 /** Generic unset register constructor. */
373 this->file
= BAD_FILE
;
376 /** Immediate value constructor. */
377 fs_reg::fs_reg(float f
)
381 this->type
= BRW_REGISTER_TYPE_F
;
386 /** Immediate value constructor. */
387 fs_reg::fs_reg(int32_t i
)
391 this->type
= BRW_REGISTER_TYPE_D
;
396 /** Immediate value constructor. */
397 fs_reg::fs_reg(uint32_t u
)
401 this->type
= BRW_REGISTER_TYPE_UD
;
406 /** Vector float immediate value constructor. */
407 fs_reg::fs_reg(uint8_t vf
[4])
411 this->type
= BRW_REGISTER_TYPE_VF
;
412 memcpy(&this->ud
, vf
, sizeof(unsigned));
415 /** Vector float immediate value constructor. */
416 fs_reg::fs_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
)
420 this->type
= BRW_REGISTER_TYPE_VF
;
421 this->ud
= (vf0
<< 0) | (vf1
<< 8) | (vf2
<< 16) | (vf3
<< 24);
424 fs_reg::fs_reg(struct brw_reg reg
) :
427 this->file
= (enum register_file
)reg
.file
;
428 this->reg_offset
= 0;
429 this->subreg_offset
= 0;
430 this->reladdr
= NULL
;
432 if (this->file
== IMM
&&
433 (this->type
!= BRW_REGISTER_TYPE_V
&&
434 this->type
!= BRW_REGISTER_TYPE_UV
&&
435 this->type
!= BRW_REGISTER_TYPE_VF
)) {
441 fs_reg::equals(const fs_reg
&r
) const
443 return (memcmp((brw_reg
*)this, (brw_reg
*)&r
, sizeof(brw_reg
)) == 0 &&
444 reg_offset
== r
.reg_offset
&&
445 subreg_offset
== r
.subreg_offset
&&
446 !reladdr
&& !r
.reladdr
&&
451 fs_reg::set_smear(unsigned subreg
)
453 assert(file
!= ARF
&& file
!= FIXED_GRF
&& file
!= IMM
);
454 subreg_offset
= subreg
* type_sz(type
);
460 fs_reg::is_contiguous() const
466 fs_reg::component_size(unsigned width
) const
468 const unsigned stride
= ((file
!= ARF
&& file
!= FIXED_GRF
) ? this->stride
:
471 return MAX2(width
* stride
, 1) * type_sz(type
);
475 type_size_scalar(const struct glsl_type
*type
)
477 unsigned int size
, i
;
479 switch (type
->base_type
) {
482 case GLSL_TYPE_FLOAT
:
484 return type
->components();
485 case GLSL_TYPE_ARRAY
:
486 return type_size_scalar(type
->fields
.array
) * type
->length
;
487 case GLSL_TYPE_STRUCT
:
489 for (i
= 0; i
< type
->length
; i
++) {
490 size
+= type_size_scalar(type
->fields
.structure
[i
].type
);
493 case GLSL_TYPE_SAMPLER
:
494 /* Samplers take up no register space, since they're baked in at
498 case GLSL_TYPE_ATOMIC_UINT
:
500 case GLSL_TYPE_SUBROUTINE
:
502 case GLSL_TYPE_IMAGE
:
503 return BRW_IMAGE_PARAM_SIZE
;
505 case GLSL_TYPE_ERROR
:
506 case GLSL_TYPE_INTERFACE
:
507 case GLSL_TYPE_DOUBLE
:
508 unreachable("not reached");
515 * Returns the number of scalar components needed to store type, assuming
516 * that vectors are padded out to vec4.
518 * This has the packing rules of type_size_vec4(), but counts components
519 * similar to type_size_scalar().
522 type_size_vec4_times_4(const struct glsl_type
*type
)
524 return 4 * type_size_vec4(type
);
528 * Create a MOV to read the timestamp register.
530 * The caller is responsible for emitting the MOV. The return value is
531 * the destination of the MOV, with extra parameters set.
534 fs_visitor::get_timestamp(const fs_builder
&bld
)
536 assert(devinfo
->gen
>= 7);
538 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
541 BRW_REGISTER_TYPE_UD
));
543 fs_reg dst
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
545 /* We want to read the 3 fields we care about even if it's not enabled in
548 bld
.group(4, 0).exec_all().MOV(dst
, ts
);
554 fs_visitor::emit_shader_time_begin()
556 shader_start_time
= get_timestamp(bld
.annotate("shader time start"));
558 /* We want only the low 32 bits of the timestamp. Since it's running
559 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
560 * which is plenty of time for our purposes. It is identical across the
561 * EUs, but since it's tracking GPU core speed it will increment at a
562 * varying rate as render P-states change.
564 shader_start_time
.set_smear(0);
568 fs_visitor::emit_shader_time_end()
570 /* Insert our code just before the final SEND with EOT. */
571 exec_node
*end
= this->instructions
.get_tail();
572 assert(end
&& ((fs_inst
*) end
)->eot
);
573 const fs_builder ibld
= bld
.annotate("shader time end")
574 .exec_all().at(NULL
, end
);
576 fs_reg shader_end_time
= get_timestamp(ibld
);
578 /* We only use the low 32 bits of the timestamp - see
579 * emit_shader_time_begin()).
581 * We could also check if render P-states have changed (or anything
582 * else that might disrupt timing) by setting smear to 2 and checking if
583 * that field is != 0.
585 shader_end_time
.set_smear(0);
587 /* Check that there weren't any timestamp reset events (assuming these
588 * were the only two timestamp reads that happened).
590 fs_reg reset
= shader_end_time
;
592 set_condmod(BRW_CONDITIONAL_Z
,
593 ibld
.AND(ibld
.null_reg_ud(), reset
, fs_reg(1u)));
594 ibld
.IF(BRW_PREDICATE_NORMAL
);
596 fs_reg start
= shader_start_time
;
598 fs_reg diff
= fs_reg(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
);
601 const fs_builder cbld
= ibld
.group(1, 0);
602 cbld
.group(1, 0).ADD(diff
, start
, shader_end_time
);
604 /* If there were no instructions between the two timestamp gets, the diff
605 * is 2 cycles. Remove that overhead, so I can forget about that when
606 * trying to determine the time taken for single instructions.
608 cbld
.ADD(diff
, diff
, fs_reg(-2u));
609 SHADER_TIME_ADD(cbld
, 0, diff
);
610 SHADER_TIME_ADD(cbld
, 1, fs_reg(1u));
611 ibld
.emit(BRW_OPCODE_ELSE
);
612 SHADER_TIME_ADD(cbld
, 2, fs_reg(1u));
613 ibld
.emit(BRW_OPCODE_ENDIF
);
617 fs_visitor::SHADER_TIME_ADD(const fs_builder
&bld
,
618 int shader_time_subindex
,
621 int index
= shader_time_index
* 3 + shader_time_subindex
;
622 fs_reg offset
= fs_reg(index
* SHADER_TIME_STRIDE
);
625 if (dispatch_width
== 8)
626 payload
= vgrf(glsl_type::uvec2_type
);
628 payload
= vgrf(glsl_type::uint_type
);
630 bld
.emit(SHADER_OPCODE_SHADER_TIME_ADD
, fs_reg(), payload
, offset
, value
);
634 fs_visitor::vfail(const char *format
, va_list va
)
643 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
644 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
646 this->fail_msg
= msg
;
649 fprintf(stderr
, "%s", msg
);
654 fs_visitor::fail(const char *format
, ...)
658 va_start(va
, format
);
664 * Mark this program as impossible to compile in SIMD16 mode.
666 * During the SIMD8 compile (which happens first), we can detect and flag
667 * things that are unsupported in SIMD16 mode, so the compiler can skip
668 * the SIMD16 compile altogether.
670 * During a SIMD16 compile (if one happens anyway), this just calls fail().
673 fs_visitor::no16(const char *msg
)
675 if (dispatch_width
== 16) {
678 simd16_unsupported
= true;
680 compiler
->shader_perf_log(log_data
,
681 "SIMD16 shader failed to compile: %s", msg
);
686 * Returns true if the instruction has a flag that means it won't
687 * update an entire destination register.
689 * For example, dead code elimination and live variable analysis want to know
690 * when a write to a variable screens off any preceding values that were in
694 fs_inst::is_partial_write() const
696 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
697 (this->exec_size
* type_sz(this->dst
.type
)) < 32 ||
698 !this->dst
.is_contiguous());
702 fs_inst::components_read(unsigned i
) const
705 case FS_OPCODE_LINTERP
:
711 case FS_OPCODE_PIXEL_X
:
712 case FS_OPCODE_PIXEL_Y
:
716 case FS_OPCODE_FB_WRITE_LOGICAL
:
717 assert(src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
718 /* First/second FB write color. */
720 return src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
724 case SHADER_OPCODE_TEX_LOGICAL
:
725 case SHADER_OPCODE_TXD_LOGICAL
:
726 case SHADER_OPCODE_TXF_LOGICAL
:
727 case SHADER_OPCODE_TXL_LOGICAL
:
728 case SHADER_OPCODE_TXS_LOGICAL
:
729 case FS_OPCODE_TXB_LOGICAL
:
730 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
731 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
732 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
733 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
734 case SHADER_OPCODE_LOD_LOGICAL
:
735 case SHADER_OPCODE_TG4_LOGICAL
:
736 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
737 assert(src
[8].file
== IMM
&& src
[9].file
== IMM
);
738 /* Texture coordinates. */
741 /* Texture derivatives. */
742 else if ((i
== 2 || i
== 3) && opcode
== SHADER_OPCODE_TXD_LOGICAL
)
744 /* Texture offset. */
748 else if (i
== 5 && opcode
== SHADER_OPCODE_TXF_CMS_W_LOGICAL
)
753 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
754 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
755 assert(src
[3].file
== IMM
);
756 /* Surface coordinates. */
759 /* Surface operation source (ignored for reads). */
765 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
766 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
767 assert(src
[3].file
== IMM
&&
769 /* Surface coordinates. */
772 /* Surface operation source. */
778 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
779 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
: {
780 assert(src
[3].file
== IMM
&&
782 const unsigned op
= src
[4].ud
;
783 /* Surface coordinates. */
786 /* Surface operation source. */
787 else if (i
== 1 && op
== BRW_AOP_CMPWR
)
789 else if (i
== 1 && (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
||
790 op
== BRW_AOP_PREDEC
))
802 fs_inst::regs_read(int arg
) const
805 case FS_OPCODE_FB_WRITE
:
806 case SHADER_OPCODE_URB_WRITE_SIMD8
:
807 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
808 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
809 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
810 case SHADER_OPCODE_URB_READ_SIMD8
:
811 case SHADER_OPCODE_UNTYPED_ATOMIC
:
812 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
813 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
814 case SHADER_OPCODE_TYPED_ATOMIC
:
815 case SHADER_OPCODE_TYPED_SURFACE_READ
:
816 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
817 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
822 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
:
823 /* The payload is actually stored in src1 */
828 case FS_OPCODE_LINTERP
:
833 case SHADER_OPCODE_LOAD_PAYLOAD
:
834 if (arg
< this->header_size
)
838 case CS_OPCODE_CS_TERMINATE
:
839 case SHADER_OPCODE_BARRIER
:
843 if (is_tex() && arg
== 0 && src
[0].file
== VGRF
)
848 switch (src
[arg
].file
) {
858 return DIV_ROUND_UP(components_read(arg
) *
859 src
[arg
].component_size(exec_size
),
862 unreachable("MRF registers are not allowed as sources");
868 fs_inst::reads_flag() const
874 fs_inst::writes_flag() const
876 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
877 opcode
!= BRW_OPCODE_IF
&&
878 opcode
!= BRW_OPCODE_WHILE
)) ||
879 opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
;
883 * Returns how many MRFs an FS opcode will write over.
885 * Note that this is not the 0 or 1 implied writes in an actual gen
886 * instruction -- the FS opcodes often generate MOVs in addition.
889 fs_visitor::implied_mrf_writes(fs_inst
*inst
)
894 if (inst
->base_mrf
== -1)
897 switch (inst
->opcode
) {
898 case SHADER_OPCODE_RCP
:
899 case SHADER_OPCODE_RSQ
:
900 case SHADER_OPCODE_SQRT
:
901 case SHADER_OPCODE_EXP2
:
902 case SHADER_OPCODE_LOG2
:
903 case SHADER_OPCODE_SIN
:
904 case SHADER_OPCODE_COS
:
905 return 1 * dispatch_width
/ 8;
906 case SHADER_OPCODE_POW
:
907 case SHADER_OPCODE_INT_QUOTIENT
:
908 case SHADER_OPCODE_INT_REMAINDER
:
909 return 2 * dispatch_width
/ 8;
910 case SHADER_OPCODE_TEX
:
912 case SHADER_OPCODE_TXD
:
913 case SHADER_OPCODE_TXF
:
914 case SHADER_OPCODE_TXF_CMS
:
915 case SHADER_OPCODE_TXF_CMS_W
:
916 case SHADER_OPCODE_TXF_MCS
:
917 case SHADER_OPCODE_TG4
:
918 case SHADER_OPCODE_TG4_OFFSET
:
919 case SHADER_OPCODE_TXL
:
920 case SHADER_OPCODE_TXS
:
921 case SHADER_OPCODE_LOD
:
922 case SHADER_OPCODE_SAMPLEINFO
:
924 case FS_OPCODE_FB_WRITE
:
926 case FS_OPCODE_GET_BUFFER_SIZE
:
927 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
928 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
930 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
932 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
934 case SHADER_OPCODE_UNTYPED_ATOMIC
:
935 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
936 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
937 case SHADER_OPCODE_TYPED_ATOMIC
:
938 case SHADER_OPCODE_TYPED_SURFACE_READ
:
939 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
940 case SHADER_OPCODE_URB_WRITE_SIMD8
:
941 case SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
942 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
:
943 case SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
944 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
945 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
946 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
947 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
950 unreachable("not reached");
955 fs_visitor::vgrf(const glsl_type
*const type
)
957 int reg_width
= dispatch_width
/ 8;
958 return fs_reg(VGRF
, alloc
.allocate(type_size_scalar(type
) * reg_width
),
959 brw_type_for_base_type(type
));
962 fs_reg::fs_reg(enum register_file file
, int nr
)
967 this->type
= BRW_REGISTER_TYPE_F
;
968 this->stride
= (file
== UNIFORM
? 0 : 1);
971 fs_reg::fs_reg(enum register_file file
, int nr
, enum brw_reg_type type
)
977 this->stride
= (file
== UNIFORM
? 0 : 1);
980 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
981 * This brings in those uniform definitions
984 fs_visitor::import_uniforms(fs_visitor
*v
)
986 this->push_constant_loc
= v
->push_constant_loc
;
987 this->pull_constant_loc
= v
->pull_constant_loc
;
988 this->uniforms
= v
->uniforms
;
989 this->param_size
= v
->param_size
;
993 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer
,
994 bool origin_upper_left
)
996 assert(stage
== MESA_SHADER_FRAGMENT
);
997 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
998 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec4_type
));
1000 bool flip
= !origin_upper_left
^ key
->render_to_fbo
;
1002 /* gl_FragCoord.x */
1003 if (pixel_center_integer
) {
1004 bld
.MOV(wpos
, this->pixel_x
);
1006 bld
.ADD(wpos
, this->pixel_x
, fs_reg(0.5f
));
1008 wpos
= offset(wpos
, bld
, 1);
1010 /* gl_FragCoord.y */
1011 if (!flip
&& pixel_center_integer
) {
1012 bld
.MOV(wpos
, this->pixel_y
);
1014 fs_reg pixel_y
= this->pixel_y
;
1015 float offset
= (pixel_center_integer
? 0.0f
: 0.5f
);
1018 pixel_y
.negate
= true;
1019 offset
+= key
->drawable_height
- 1.0f
;
1022 bld
.ADD(wpos
, pixel_y
, fs_reg(offset
));
1024 wpos
= offset(wpos
, bld
, 1);
1026 /* gl_FragCoord.z */
1027 if (devinfo
->gen
>= 6) {
1028 bld
.MOV(wpos
, fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0)));
1030 bld
.emit(FS_OPCODE_LINTERP
, wpos
,
1031 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
1032 interp_reg(VARYING_SLOT_POS
, 2));
1034 wpos
= offset(wpos
, bld
, 1);
1036 /* gl_FragCoord.w: Already set up in emit_interpolation */
1037 bld
.MOV(wpos
, this->wpos_w
);
1043 fs_visitor::emit_linterp(const fs_reg
&attr
, const fs_reg
&interp
,
1044 glsl_interp_qualifier interpolation_mode
,
1045 bool is_centroid
, bool is_sample
)
1047 brw_wm_barycentric_interp_mode barycoord_mode
;
1048 if (devinfo
->gen
>= 6) {
1050 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1051 barycoord_mode
= BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
1053 barycoord_mode
= BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
1054 } else if (is_sample
) {
1055 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1056 barycoord_mode
= BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
1058 barycoord_mode
= BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
1060 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1061 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1063 barycoord_mode
= BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
1066 /* On Ironlake and below, there is only one interpolation mode.
1067 * Centroid interpolation doesn't mean anything on this hardware --
1068 * there is no multisampling.
1070 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1072 return bld
.emit(FS_OPCODE_LINTERP
, attr
,
1073 this->delta_xy
[barycoord_mode
], interp
);
1077 fs_visitor::emit_general_interpolation(fs_reg attr
, const char *name
,
1078 const glsl_type
*type
,
1079 glsl_interp_qualifier interpolation_mode
,
1080 int location
, bool mod_centroid
,
1083 attr
.type
= brw_type_for_base_type(type
->get_scalar_type());
1085 assert(stage
== MESA_SHADER_FRAGMENT
);
1086 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1087 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1089 unsigned int array_elements
;
1091 if (type
->is_array()) {
1092 array_elements
= type
->arrays_of_arrays_size();
1093 if (array_elements
== 0) {
1094 fail("dereferenced array '%s' has length 0\n", name
);
1096 type
= type
->without_array();
1101 if (interpolation_mode
== INTERP_QUALIFIER_NONE
) {
1103 location
== VARYING_SLOT_COL0
|| location
== VARYING_SLOT_COL1
;
1104 if (key
->flat_shade
&& is_gl_Color
) {
1105 interpolation_mode
= INTERP_QUALIFIER_FLAT
;
1107 interpolation_mode
= INTERP_QUALIFIER_SMOOTH
;
1111 for (unsigned int i
= 0; i
< array_elements
; i
++) {
1112 for (unsigned int j
= 0; j
< type
->matrix_columns
; j
++) {
1113 if (prog_data
->urb_setup
[location
] == -1) {
1114 /* If there's no incoming setup data for this slot, don't
1115 * emit interpolation for it.
1117 attr
= offset(attr
, bld
, type
->vector_elements
);
1122 if (interpolation_mode
== INTERP_QUALIFIER_FLAT
) {
1123 /* Constant interpolation (flat shading) case. The SF has
1124 * handed us defined values in only the constant offset
1125 * field of the setup reg.
1127 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1128 struct brw_reg interp
= interp_reg(location
, k
);
1129 interp
= suboffset(interp
, 3);
1130 interp
.type
= attr
.type
;
1131 bld
.emit(FS_OPCODE_CINTERP
, attr
, fs_reg(interp
));
1132 attr
= offset(attr
, bld
, 1);
1135 /* Smooth/noperspective interpolation case. */
1136 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1137 struct brw_reg interp
= interp_reg(location
, k
);
1138 if (devinfo
->needs_unlit_centroid_workaround
&& mod_centroid
) {
1139 /* Get the pixel/sample mask into f0 so that we know
1140 * which pixels are lit. Then, for each channel that is
1141 * unlit, replace the centroid data with non-centroid
1144 bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
1147 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1149 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1150 inst
->predicate_inverse
= true;
1151 if (devinfo
->has_pln
)
1152 inst
->no_dd_clear
= true;
1154 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1155 mod_centroid
&& !key
->persample_shading
,
1156 mod_sample
|| key
->persample_shading
);
1157 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1158 inst
->predicate_inverse
= false;
1159 if (devinfo
->has_pln
)
1160 inst
->no_dd_check
= true;
1163 emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1164 mod_centroid
&& !key
->persample_shading
,
1165 mod_sample
|| key
->persample_shading
);
1167 if (devinfo
->gen
< 6 && interpolation_mode
== INTERP_QUALIFIER_SMOOTH
) {
1168 bld
.MUL(attr
, attr
, this->pixel_w
);
1170 attr
= offset(attr
, bld
, 1);
1180 fs_visitor::emit_frontfacing_interpolation()
1182 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1184 if (devinfo
->gen
>= 6) {
1185 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1186 * a boolean result from this (~0/true or 0/false).
1188 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1189 * this task in only one instruction:
1190 * - a negation source modifier will flip the bit; and
1191 * - a W -> D type conversion will sign extend the bit into the high
1192 * word of the destination.
1194 * An ASR 15 fills the low word of the destination.
1196 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1199 bld
.ASR(*reg
, g0
, fs_reg(15));
1201 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1202 * a boolean result from this (1/true or 0/false).
1204 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1205 * the negation source modifier to flip it. Unfortunately the SHR
1206 * instruction only operates on UD (or D with an abs source modifier)
1207 * sources without negation.
1209 * Instead, use ASR (which will give ~0/true or 0/false).
1211 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1214 bld
.ASR(*reg
, g1_6
, fs_reg(31));
1221 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1223 assert(stage
== MESA_SHADER_FRAGMENT
);
1224 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1225 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1227 if (key
->compute_pos_offset
) {
1228 /* Convert int_sample_pos to floating point */
1229 bld
.MOV(dst
, int_sample_pos
);
1230 /* Scale to the range [0, 1] */
1231 bld
.MUL(dst
, dst
, fs_reg(1 / 16.0f
));
1234 /* From ARB_sample_shading specification:
1235 * "When rendering to a non-multisample buffer, or if multisample
1236 * rasterization is disabled, gl_SamplePosition will always be
1239 bld
.MOV(dst
, fs_reg(0.5f
));
1244 fs_visitor::emit_samplepos_setup()
1246 assert(devinfo
->gen
>= 6);
1248 const fs_builder abld
= bld
.annotate("compute sample position");
1249 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1251 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1252 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1254 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1255 * mode will be enabled.
1257 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1258 * R31.1:0 Position Offset X/Y for Slot[3:0]
1259 * R31.3:2 Position Offset X/Y for Slot[7:4]
1262 * The X, Y sample positions come in as bytes in thread payload. So, read
1263 * the positions using vstride=16, width=8, hstride=2.
1265 struct brw_reg sample_pos_reg
=
1266 stride(retype(brw_vec1_grf(payload
.sample_pos_reg
, 0),
1267 BRW_REGISTER_TYPE_B
), 16, 8, 2);
1269 if (dispatch_width
== 8) {
1270 abld
.MOV(int_sample_x
, fs_reg(sample_pos_reg
));
1272 abld
.half(0).MOV(half(int_sample_x
, 0), fs_reg(sample_pos_reg
));
1273 abld
.half(1).MOV(half(int_sample_x
, 1),
1274 fs_reg(suboffset(sample_pos_reg
, 16)));
1276 /* Compute gl_SamplePosition.x */
1277 compute_sample_position(pos
, int_sample_x
);
1278 pos
= offset(pos
, abld
, 1);
1279 if (dispatch_width
== 8) {
1280 abld
.MOV(int_sample_y
, fs_reg(suboffset(sample_pos_reg
, 1)));
1282 abld
.half(0).MOV(half(int_sample_y
, 0),
1283 fs_reg(suboffset(sample_pos_reg
, 1)));
1284 abld
.half(1).MOV(half(int_sample_y
, 1),
1285 fs_reg(suboffset(sample_pos_reg
, 17)));
1287 /* Compute gl_SamplePosition.y */
1288 compute_sample_position(pos
, int_sample_y
);
1293 fs_visitor::emit_sampleid_setup()
1295 assert(stage
== MESA_SHADER_FRAGMENT
);
1296 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1297 assert(devinfo
->gen
>= 6);
1299 const fs_builder abld
= bld
.annotate("compute sample id");
1300 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1302 if (key
->compute_sample_id
) {
1303 fs_reg
t1(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_D
);
1305 fs_reg
t2(VGRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_W
);
1307 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1308 * 8x multisampling, subspan 0 will represent sample N (where N
1309 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1310 * 7. We can find the value of N by looking at R0.0 bits 7:6
1311 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1312 * (since samples are always delivered in pairs). That is, we
1313 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1314 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1315 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1316 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1317 * populating a temporary variable with the sequence (0, 1, 2, 3),
1318 * and then reading from it using vstride=1, width=4, hstride=0.
1319 * These computations hold good for 4x multisampling as well.
1321 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1322 * the first four slots are sample 0 of subspan 0; the next four
1323 * are sample 1 of subspan 0; the third group is sample 0 of
1324 * subspan 1, and finally sample 1 of subspan 1.
1327 /* SKL+ has an extra bit for the Starting Sample Pair Index to
1328 * accomodate 16x MSAA.
1330 unsigned sspi_mask
= devinfo
->gen
>= 9 ? 0x1c0 : 0xc0;
1332 abld
.exec_all().group(1, 0)
1333 .AND(t1
, fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_D
)),
1335 abld
.exec_all().group(1, 0).SHR(t1
, t1
, fs_reg(5));
1337 /* This works for both SIMD8 and SIMD16 */
1338 abld
.exec_all().group(4, 0)
1339 .MOV(t2
, brw_imm_v(key
->persample_2x
? 0x1010 : 0x3210));
1341 /* This special instruction takes care of setting vstride=1,
1342 * width=4, hstride=0 of t2 during an ADD instruction.
1344 abld
.emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1346 /* As per GL_ARB_sample_shading specification:
1347 * "When rendering to a non-multisample buffer, or if multisample
1348 * rasterization is disabled, gl_SampleID will always be zero."
1350 abld
.MOV(*reg
, fs_reg(0));
1357 fs_visitor::resolve_source_modifiers(const fs_reg
&src
)
1359 if (!src
.abs
&& !src
.negate
)
1362 fs_reg temp
= bld
.vgrf(src
.type
);
1369 fs_visitor::emit_discard_jump()
1371 assert(((brw_wm_prog_data
*) this->prog_data
)->uses_kill
);
1373 /* For performance, after a discard, jump to the end of the
1374 * shader if all relevant channels have been discarded.
1376 fs_inst
*discard_jump
= bld
.emit(FS_OPCODE_DISCARD_JUMP
);
1377 discard_jump
->flag_subreg
= 1;
1379 discard_jump
->predicate
= (dispatch_width
== 8)
1380 ? BRW_PREDICATE_ALIGN1_ANY8H
1381 : BRW_PREDICATE_ALIGN1_ANY16H
;
1382 discard_jump
->predicate_inverse
= true;
1386 fs_visitor::emit_gs_thread_end()
1388 assert(stage
== MESA_SHADER_GEOMETRY
);
1390 struct brw_gs_prog_data
*gs_prog_data
=
1391 (struct brw_gs_prog_data
*) prog_data
;
1393 if (gs_compile
->control_data_header_size_bits
> 0) {
1394 emit_gs_control_data_bits(this->final_gs_vertex_count
);
1397 const fs_builder abld
= bld
.annotate("thread end");
1400 if (gs_prog_data
->static_vertex_count
!= -1) {
1401 foreach_in_list_reverse(fs_inst
, prev
, &this->instructions
) {
1402 if (prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
||
1403 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
||
1404 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
||
1405 prev
->opcode
== SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
) {
1408 /* Delete now dead instructions. */
1409 foreach_in_list_reverse_safe(exec_node
, dead
, &this->instructions
) {
1415 } else if (prev
->is_control_flow() || prev
->has_side_effects()) {
1419 fs_reg hdr
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1420 abld
.MOV(hdr
, fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
)));
1421 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, hdr
);
1424 fs_reg payload
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
1425 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, 2);
1426 sources
[0] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1427 sources
[1] = this->final_gs_vertex_count
;
1428 abld
.LOAD_PAYLOAD(payload
, sources
, 2, 2);
1429 inst
= abld
.emit(SHADER_OPCODE_URB_WRITE_SIMD8
, reg_undef
, payload
);
1437 fs_visitor::assign_curb_setup()
1439 if (dispatch_width
== 8) {
1440 prog_data
->dispatch_grf_start_reg
= payload
.num_regs
;
1442 if (stage
== MESA_SHADER_FRAGMENT
) {
1443 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1444 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1445 } else if (stage
== MESA_SHADER_COMPUTE
) {
1446 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
1447 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1449 unreachable("Unsupported shader type!");
1453 prog_data
->curb_read_length
= ALIGN(stage_prog_data
->nr_params
, 8) / 8;
1455 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1456 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1457 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1458 if (inst
->src
[i
].file
== UNIFORM
) {
1459 int uniform_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1461 if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1462 constant_nr
= push_constant_loc
[uniform_nr
];
1464 /* Section 5.11 of the OpenGL 4.1 spec says:
1465 * "Out-of-bounds reads return undefined values, which include
1466 * values from other variables of the active program or zero."
1467 * Just return the first push constant.
1472 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1475 brw_reg
.abs
= inst
->src
[i
].abs
;
1476 brw_reg
.negate
= inst
->src
[i
].negate
;
1478 assert(inst
->src
[i
].stride
== 0);
1479 inst
->src
[i
] = byte_offset(
1480 retype(brw_reg
, inst
->src
[i
].type
),
1481 inst
->src
[i
].subreg_offset
);
1486 /* This may be updated in assign_urb_setup or assign_vs_urb_setup. */
1487 this->first_non_payload_grf
= payload
.num_regs
+ prog_data
->curb_read_length
;
1491 fs_visitor::calculate_urb_setup()
1493 assert(stage
== MESA_SHADER_FRAGMENT
);
1494 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1495 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1497 memset(prog_data
->urb_setup
, -1,
1498 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1501 /* Figure out where each of the incoming setup attributes lands. */
1502 if (devinfo
->gen
>= 6) {
1503 if (_mesa_bitcount_64(nir
->info
.inputs_read
&
1504 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1505 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1506 * first 16 varying inputs, so we can put them wherever we want.
1507 * Just put them in order.
1509 * This is useful because it means that (a) inputs not used by the
1510 * fragment shader won't take up valuable register space, and (b) we
1511 * won't have to recompile the fragment shader if it gets paired with
1512 * a different vertex (or geometry) shader.
1514 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1515 if (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1516 BITFIELD64_BIT(i
)) {
1517 prog_data
->urb_setup
[i
] = urb_next
++;
1521 bool include_vue_header
=
1522 nir
->info
.inputs_read
& (VARYING_BIT_LAYER
| VARYING_BIT_VIEWPORT
);
1524 /* We have enough input varyings that the SF/SBE pipeline stage can't
1525 * arbitrarily rearrange them to suit our whim; we have to put them
1526 * in an order that matches the output of the previous pipeline stage
1527 * (geometry or vertex shader).
1529 struct brw_vue_map prev_stage_vue_map
;
1530 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1531 key
->input_slots_valid
,
1532 nir
->info
.separate_shader
);
1534 include_vue_header
? 0 : 2 * BRW_SF_URB_ENTRY_READ_OFFSET
;
1536 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1537 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1539 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1540 if (varying
!= BRW_VARYING_SLOT_PAD
&&
1541 (nir
->info
.inputs_read
& BRW_FS_VARYING_INPUT_MASK
&
1542 BITFIELD64_BIT(varying
))) {
1543 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1546 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1549 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1550 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1551 /* Point size is packed into the header, not as a general attribute */
1552 if (i
== VARYING_SLOT_PSIZ
)
1555 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1556 /* The back color slot is skipped when the front color is
1557 * also written to. In addition, some slots can be
1558 * written in the vertex shader and not read in the
1559 * fragment shader. So the register number must always be
1560 * incremented, mapped or not.
1562 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1563 prog_data
->urb_setup
[i
] = urb_next
;
1569 * It's a FS only attribute, and we did interpolation for this attribute
1570 * in SF thread. So, count it here, too.
1572 * See compile_sf_prog() for more info.
1574 if (nir
->info
.inputs_read
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1575 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1578 prog_data
->num_varying_inputs
= urb_next
;
1582 fs_visitor::assign_urb_setup()
1584 assert(stage
== MESA_SHADER_FRAGMENT
);
1585 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1587 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1589 /* Offset all the urb_setup[] index by the actual position of the
1590 * setup regs, now that the location of the constants has been chosen.
1592 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1593 if (inst
->opcode
== FS_OPCODE_LINTERP
) {
1594 assert(inst
->src
[1].file
== FIXED_GRF
);
1595 inst
->src
[1].nr
+= urb_start
;
1598 if (inst
->opcode
== FS_OPCODE_CINTERP
) {
1599 assert(inst
->src
[0].file
== FIXED_GRF
);
1600 inst
->src
[0].nr
+= urb_start
;
1604 /* Each attribute is 4 setup channels, each of which is half a reg. */
1605 this->first_non_payload_grf
+= prog_data
->num_varying_inputs
* 2;
1609 fs_visitor::convert_attr_sources_to_hw_regs(fs_inst
*inst
)
1611 for (int i
= 0; i
< inst
->sources
; i
++) {
1612 if (inst
->src
[i
].file
== ATTR
) {
1613 int grf
= payload
.num_regs
+
1614 prog_data
->curb_read_length
+
1616 inst
->src
[i
].reg_offset
;
1618 struct brw_reg reg
=
1619 stride(byte_offset(retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
),
1620 inst
->src
[i
].subreg_offset
),
1621 inst
->exec_size
* inst
->src
[i
].stride
,
1622 inst
->exec_size
, inst
->src
[i
].stride
);
1623 reg
.abs
= inst
->src
[i
].abs
;
1624 reg
.negate
= inst
->src
[i
].negate
;
1632 fs_visitor::assign_vs_urb_setup()
1634 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
1636 assert(stage
== MESA_SHADER_VERTEX
);
1637 int count
= _mesa_bitcount_64(vs_prog_data
->inputs_read
);
1638 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
)
1641 /* Each attribute is 4 regs. */
1642 this->first_non_payload_grf
+= 4 * vs_prog_data
->nr_attributes
;
1644 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1646 /* Rewrite all ATTR file references to the hw grf that they land in. */
1647 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1648 convert_attr_sources_to_hw_regs(inst
);
1653 fs_visitor::assign_gs_urb_setup()
1655 assert(stage
== MESA_SHADER_GEOMETRY
);
1657 brw_vue_prog_data
*vue_prog_data
= (brw_vue_prog_data
*) prog_data
;
1659 first_non_payload_grf
+=
1660 8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
;
1662 const unsigned first_icp_handle
= payload
.num_regs
-
1663 (vue_prog_data
->include_vue_handles
? nir
->info
.gs
.vertices_in
: 0);
1665 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1666 /* Lower URB_READ_SIMD8 opcodes into real messages. */
1667 if (inst
->opcode
== SHADER_OPCODE_URB_READ_SIMD8
) {
1668 assert(inst
->src
[0].file
== IMM
);
1669 inst
->src
[0] = retype(brw_vec8_grf(first_icp_handle
+
1671 0), BRW_REGISTER_TYPE_UD
);
1672 /* for now, assume constant - we can do per-slot offsets later */
1673 assert(inst
->src
[1].file
== IMM
);
1674 inst
->offset
= inst
->src
[1].ud
;
1675 inst
->src
[1] = fs_reg();
1677 inst
->base_mrf
= -1;
1680 /* Rewrite all ATTR file references to GRFs. */
1681 convert_attr_sources_to_hw_regs(inst
);
1687 * Split large virtual GRFs into separate components if we can.
1689 * This is mostly duplicated with what brw_fs_vector_splitting does,
1690 * but that's really conservative because it's afraid of doing
1691 * splitting that doesn't result in real progress after the rest of
1692 * the optimization phases, which would cause infinite looping in
1693 * optimization. We can do it once here, safely. This also has the
1694 * opportunity to split interpolated values, or maybe even uniforms,
1695 * which we don't have at the IR level.
1697 * We want to split, because virtual GRFs are what we register
1698 * allocate and spill (due to contiguousness requirements for some
1699 * instructions), and they're what we naturally generate in the
1700 * codegen process, but most virtual GRFs don't actually need to be
1701 * contiguous sets of GRFs. If we split, we'll end up with reduced
1702 * live intervals and better dead code elimination and coalescing.
1705 fs_visitor::split_virtual_grfs()
1707 int num_vars
= this->alloc
.count
;
1709 /* Count the total number of registers */
1711 int vgrf_to_reg
[num_vars
];
1712 for (int i
= 0; i
< num_vars
; i
++) {
1713 vgrf_to_reg
[i
] = reg_count
;
1714 reg_count
+= alloc
.sizes
[i
];
1717 /* An array of "split points". For each register slot, this indicates
1718 * if this slot can be separated from the previous slot. Every time an
1719 * instruction uses multiple elements of a register (as a source or
1720 * destination), we mark the used slots as inseparable. Then we go
1721 * through and split the registers into the smallest pieces we can.
1723 bool split_points
[reg_count
];
1724 memset(split_points
, 0, sizeof(split_points
));
1726 /* Mark all used registers as fully splittable */
1727 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1728 if (inst
->dst
.file
== VGRF
) {
1729 int reg
= vgrf_to_reg
[inst
->dst
.nr
];
1730 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.nr
]; j
++)
1731 split_points
[reg
+ j
] = true;
1734 for (int i
= 0; i
< inst
->sources
; i
++) {
1735 if (inst
->src
[i
].file
== VGRF
) {
1736 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
];
1737 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].nr
]; j
++)
1738 split_points
[reg
+ j
] = true;
1743 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1744 if (inst
->dst
.file
== VGRF
) {
1745 int reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.reg_offset
;
1746 for (int j
= 1; j
< inst
->regs_written
; j
++)
1747 split_points
[reg
+ j
] = false;
1749 for (int i
= 0; i
< inst
->sources
; i
++) {
1750 if (inst
->src
[i
].file
== VGRF
) {
1751 int reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].reg_offset
;
1752 for (int j
= 1; j
< inst
->regs_read(i
); j
++)
1753 split_points
[reg
+ j
] = false;
1758 int new_virtual_grf
[reg_count
];
1759 int new_reg_offset
[reg_count
];
1762 for (int i
= 0; i
< num_vars
; i
++) {
1763 /* The first one should always be 0 as a quick sanity check. */
1764 assert(split_points
[reg
] == false);
1767 new_reg_offset
[reg
] = 0;
1772 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1773 /* If this is a split point, reset the offset to 0 and allocate a
1774 * new virtual GRF for the previous offset many registers
1776 if (split_points
[reg
]) {
1777 assert(offset
<= MAX_VGRF_SIZE
);
1778 int grf
= alloc
.allocate(offset
);
1779 for (int k
= reg
- offset
; k
< reg
; k
++)
1780 new_virtual_grf
[k
] = grf
;
1783 new_reg_offset
[reg
] = offset
;
1788 /* The last one gets the original register number */
1789 assert(offset
<= MAX_VGRF_SIZE
);
1790 alloc
.sizes
[i
] = offset
;
1791 for (int k
= reg
- offset
; k
< reg
; k
++)
1792 new_virtual_grf
[k
] = i
;
1794 assert(reg
== reg_count
);
1796 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1797 if (inst
->dst
.file
== VGRF
) {
1798 reg
= vgrf_to_reg
[inst
->dst
.nr
] + inst
->dst
.reg_offset
;
1799 inst
->dst
.nr
= new_virtual_grf
[reg
];
1800 inst
->dst
.reg_offset
= new_reg_offset
[reg
];
1801 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1803 for (int i
= 0; i
< inst
->sources
; i
++) {
1804 if (inst
->src
[i
].file
== VGRF
) {
1805 reg
= vgrf_to_reg
[inst
->src
[i
].nr
] + inst
->src
[i
].reg_offset
;
1806 inst
->src
[i
].nr
= new_virtual_grf
[reg
];
1807 inst
->src
[i
].reg_offset
= new_reg_offset
[reg
];
1808 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1812 invalidate_live_intervals();
1816 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1818 * During code generation, we create tons of temporary variables, many of
1819 * which get immediately killed and are never used again. Yet, in later
1820 * optimization and analysis passes, such as compute_live_intervals, we need
1821 * to loop over all the virtual GRFs. Compacting them can save a lot of
1825 fs_visitor::compact_virtual_grfs()
1827 bool progress
= false;
1828 int remap_table
[this->alloc
.count
];
1829 memset(remap_table
, -1, sizeof(remap_table
));
1831 /* Mark which virtual GRFs are used. */
1832 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
1833 if (inst
->dst
.file
== VGRF
)
1834 remap_table
[inst
->dst
.nr
] = 0;
1836 for (int i
= 0; i
< inst
->sources
; i
++) {
1837 if (inst
->src
[i
].file
== VGRF
)
1838 remap_table
[inst
->src
[i
].nr
] = 0;
1842 /* Compact the GRF arrays. */
1844 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
1845 if (remap_table
[i
] == -1) {
1846 /* We just found an unused register. This means that we are
1847 * actually going to compact something.
1851 remap_table
[i
] = new_index
;
1852 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
1853 invalidate_live_intervals();
1858 this->alloc
.count
= new_index
;
1860 /* Patch all the instructions to use the newly renumbered registers */
1861 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1862 if (inst
->dst
.file
== VGRF
)
1863 inst
->dst
.nr
= remap_table
[inst
->dst
.nr
];
1865 for (int i
= 0; i
< inst
->sources
; i
++) {
1866 if (inst
->src
[i
].file
== VGRF
)
1867 inst
->src
[i
].nr
= remap_table
[inst
->src
[i
].nr
];
1871 /* Patch all the references to delta_xy, since they're used in register
1872 * allocation. If they're unused, switch them to BAD_FILE so we don't
1873 * think some random VGRF is delta_xy.
1875 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
1876 if (delta_xy
[i
].file
== VGRF
) {
1877 if (remap_table
[delta_xy
[i
].nr
] != -1) {
1878 delta_xy
[i
].nr
= remap_table
[delta_xy
[i
].nr
];
1880 delta_xy
[i
].file
= BAD_FILE
;
1889 * Assign UNIFORM file registers to either push constants or pull constants.
1891 * We allow a fragment shader to have more than the specified minimum
1892 * maximum number of fragment shader uniform components (64). If
1893 * there are too many of these, they'd fill up all of register space.
1894 * So, this will push some of them out to the pull constant buffer and
1895 * update the program to load them. We also use pull constants for all
1896 * indirect constant loads because we don't support indirect accesses in
1900 fs_visitor::assign_constant_locations()
1902 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
1903 if (dispatch_width
!= 8)
1906 unsigned int num_pull_constants
= 0;
1908 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1909 memset(pull_constant_loc
, -1, sizeof(pull_constant_loc
[0]) * uniforms
);
1911 bool is_live
[uniforms
];
1912 memset(is_live
, 0, sizeof(is_live
));
1914 /* First, we walk through the instructions and do two things:
1916 * 1) Figure out which uniforms are live.
1918 * 2) Find all indirect access of uniform arrays and flag them as needing
1919 * to go into the pull constant buffer.
1921 * Note that we don't move constant-indexed accesses to arrays. No
1922 * testing has been done of the performance impact of this choice.
1924 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
1925 for (int i
= 0 ; i
< inst
->sources
; i
++) {
1926 if (inst
->src
[i
].file
!= UNIFORM
)
1929 if (inst
->src
[i
].reladdr
) {
1930 int uniform
= inst
->src
[i
].nr
;
1932 /* If this array isn't already present in the pull constant buffer,
1935 if (pull_constant_loc
[uniform
] == -1) {
1936 assert(param_size
[uniform
]);
1937 for (int j
= 0; j
< param_size
[uniform
]; j
++)
1938 pull_constant_loc
[uniform
+ j
] = num_pull_constants
++;
1941 /* Mark the the one accessed uniform as live */
1942 int constant_nr
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
1943 if (constant_nr
>= 0 && constant_nr
< (int) uniforms
)
1944 is_live
[constant_nr
] = true;
1949 /* Only allow 16 registers (128 uniform components) as push constants.
1951 * Just demote the end of the list. We could probably do better
1952 * here, demoting things that are rarely used in the program first.
1954 * If changing this value, note the limitation about total_regs in
1957 unsigned int max_push_components
= 16 * 8;
1958 unsigned int num_push_constants
= 0;
1960 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
1962 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1963 if (!is_live
[i
] || pull_constant_loc
[i
] != -1) {
1964 /* This UNIFORM register is either dead, or has already been demoted
1965 * to a pull const. Mark it as no longer living in the param[] array.
1967 push_constant_loc
[i
] = -1;
1971 if (num_push_constants
< max_push_components
) {
1972 /* Retain as a push constant. Record the location in the params[]
1975 push_constant_loc
[i
] = num_push_constants
++;
1977 /* Demote to a pull constant. */
1978 push_constant_loc
[i
] = -1;
1979 pull_constant_loc
[i
] = num_pull_constants
++;
1983 stage_prog_data
->nr_params
= num_push_constants
;
1984 stage_prog_data
->nr_pull_params
= num_pull_constants
;
1986 /* Up until now, the param[] array has been indexed by reg + reg_offset
1987 * of UNIFORM registers. Move pull constants into pull_param[] and
1988 * condense param[] to only contain the uniforms we chose to push.
1990 * NOTE: Because we are condensing the params[] array, we know that
1991 * push_constant_loc[i] <= i and we can do it in one smooth loop without
1992 * having to make a copy.
1994 for (unsigned int i
= 0; i
< uniforms
; i
++) {
1995 const gl_constant_value
*value
= stage_prog_data
->param
[i
];
1997 if (pull_constant_loc
[i
] != -1) {
1998 stage_prog_data
->pull_param
[pull_constant_loc
[i
]] = value
;
1999 } else if (push_constant_loc
[i
] != -1) {
2000 stage_prog_data
->param
[push_constant_loc
[i
]] = value
;
2006 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2007 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2010 fs_visitor::demote_pull_constants()
2012 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
2013 for (int i
= 0; i
< inst
->sources
; i
++) {
2014 if (inst
->src
[i
].file
!= UNIFORM
)
2018 unsigned location
= inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
;
2019 if (location
>= uniforms
) /* Out of bounds access */
2022 pull_index
= pull_constant_loc
[location
];
2024 if (pull_index
== -1)
2027 /* Set up the annotation tracking for new generated instructions. */
2028 const fs_builder
ibld(this, block
, inst
);
2029 const unsigned index
= stage_prog_data
->binding_table
.pull_constants_start
;
2030 fs_reg dst
= vgrf(glsl_type::float_type
);
2032 assert(inst
->src
[i
].stride
== 0);
2034 /* Generate a pull load into dst. */
2035 if (inst
->src
[i
].reladdr
) {
2036 VARYING_PULL_CONSTANT_LOAD(ibld
, dst
,
2038 *inst
->src
[i
].reladdr
,
2040 inst
->src
[i
].reladdr
= NULL
;
2041 inst
->src
[i
].stride
= 1;
2043 const fs_builder ubld
= ibld
.exec_all().group(8, 0);
2044 fs_reg offset
= fs_reg((unsigned)(pull_index
* 4) & ~15);
2045 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
2046 dst
, fs_reg(index
), offset
);
2047 inst
->src
[i
].set_smear(pull_index
& 3);
2049 brw_mark_surface_used(prog_data
, index
);
2051 /* Rewrite the instruction to use the temporary VGRF. */
2052 inst
->src
[i
].file
= VGRF
;
2053 inst
->src
[i
].nr
= dst
.nr
;
2054 inst
->src
[i
].reg_offset
= 0;
2057 invalidate_live_intervals();
2061 fs_visitor::opt_algebraic()
2063 bool progress
= false;
2065 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2066 switch (inst
->opcode
) {
2067 case BRW_OPCODE_MOV
:
2068 if (inst
->src
[0].file
!= IMM
)
2071 if (inst
->saturate
) {
2072 if (inst
->dst
.type
!= inst
->src
[0].type
)
2073 assert(!"unimplemented: saturate mixed types");
2075 if (brw_saturate_immediate(inst
->dst
.type
, &inst
->src
[0])) {
2076 inst
->saturate
= false;
2082 case BRW_OPCODE_MUL
:
2083 if (inst
->src
[1].file
!= IMM
)
2087 if (inst
->src
[1].is_one()) {
2088 inst
->opcode
= BRW_OPCODE_MOV
;
2089 inst
->src
[1] = reg_undef
;
2095 if (inst
->src
[1].is_negative_one()) {
2096 inst
->opcode
= BRW_OPCODE_MOV
;
2097 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2098 inst
->src
[1] = reg_undef
;
2104 if (inst
->src
[1].is_zero()) {
2105 inst
->opcode
= BRW_OPCODE_MOV
;
2106 inst
->src
[0] = inst
->src
[1];
2107 inst
->src
[1] = reg_undef
;
2112 if (inst
->src
[0].file
== IMM
) {
2113 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2114 inst
->opcode
= BRW_OPCODE_MOV
;
2115 inst
->src
[0].f
*= inst
->src
[1].f
;
2116 inst
->src
[1] = reg_undef
;
2121 case BRW_OPCODE_ADD
:
2122 if (inst
->src
[1].file
!= IMM
)
2126 if (inst
->src
[1].is_zero()) {
2127 inst
->opcode
= BRW_OPCODE_MOV
;
2128 inst
->src
[1] = reg_undef
;
2133 if (inst
->src
[0].file
== IMM
) {
2134 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2135 inst
->opcode
= BRW_OPCODE_MOV
;
2136 inst
->src
[0].f
+= inst
->src
[1].f
;
2137 inst
->src
[1] = reg_undef
;
2143 if (inst
->src
[0].equals(inst
->src
[1])) {
2144 inst
->opcode
= BRW_OPCODE_MOV
;
2145 inst
->src
[1] = reg_undef
;
2150 case BRW_OPCODE_LRP
:
2151 if (inst
->src
[1].equals(inst
->src
[2])) {
2152 inst
->opcode
= BRW_OPCODE_MOV
;
2153 inst
->src
[0] = inst
->src
[1];
2154 inst
->src
[1] = reg_undef
;
2155 inst
->src
[2] = reg_undef
;
2160 case BRW_OPCODE_CMP
:
2161 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
2163 inst
->src
[0].negate
&&
2164 inst
->src
[1].is_zero()) {
2165 inst
->src
[0].abs
= false;
2166 inst
->src
[0].negate
= false;
2167 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2172 case BRW_OPCODE_SEL
:
2173 if (inst
->src
[0].equals(inst
->src
[1])) {
2174 inst
->opcode
= BRW_OPCODE_MOV
;
2175 inst
->src
[1] = reg_undef
;
2176 inst
->predicate
= BRW_PREDICATE_NONE
;
2177 inst
->predicate_inverse
= false;
2179 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2180 switch (inst
->conditional_mod
) {
2181 case BRW_CONDITIONAL_LE
:
2182 case BRW_CONDITIONAL_L
:
2183 switch (inst
->src
[1].type
) {
2184 case BRW_REGISTER_TYPE_F
:
2185 if (inst
->src
[1].f
>= 1.0f
) {
2186 inst
->opcode
= BRW_OPCODE_MOV
;
2187 inst
->src
[1] = reg_undef
;
2188 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2196 case BRW_CONDITIONAL_GE
:
2197 case BRW_CONDITIONAL_G
:
2198 switch (inst
->src
[1].type
) {
2199 case BRW_REGISTER_TYPE_F
:
2200 if (inst
->src
[1].f
<= 0.0f
) {
2201 inst
->opcode
= BRW_OPCODE_MOV
;
2202 inst
->src
[1] = reg_undef
;
2203 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2215 case BRW_OPCODE_MAD
:
2216 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2217 inst
->opcode
= BRW_OPCODE_MOV
;
2218 inst
->src
[1] = reg_undef
;
2219 inst
->src
[2] = reg_undef
;
2221 } else if (inst
->src
[0].is_zero()) {
2222 inst
->opcode
= BRW_OPCODE_MUL
;
2223 inst
->src
[0] = inst
->src
[2];
2224 inst
->src
[2] = reg_undef
;
2226 } else if (inst
->src
[1].is_one()) {
2227 inst
->opcode
= BRW_OPCODE_ADD
;
2228 inst
->src
[1] = inst
->src
[2];
2229 inst
->src
[2] = reg_undef
;
2231 } else if (inst
->src
[2].is_one()) {
2232 inst
->opcode
= BRW_OPCODE_ADD
;
2233 inst
->src
[2] = reg_undef
;
2235 } else if (inst
->src
[1].file
== IMM
&& inst
->src
[2].file
== IMM
) {
2236 inst
->opcode
= BRW_OPCODE_ADD
;
2237 inst
->src
[1].f
*= inst
->src
[2].f
;
2238 inst
->src
[2] = reg_undef
;
2242 case SHADER_OPCODE_RCP
: {
2243 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
2244 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
2245 if (inst
->src
[0].equals(prev
->dst
)) {
2246 inst
->opcode
= SHADER_OPCODE_RSQ
;
2247 inst
->src
[0] = prev
->src
[0];
2253 case SHADER_OPCODE_BROADCAST
:
2254 if (is_uniform(inst
->src
[0])) {
2255 inst
->opcode
= BRW_OPCODE_MOV
;
2257 inst
->force_writemask_all
= true;
2259 } else if (inst
->src
[1].file
== IMM
) {
2260 inst
->opcode
= BRW_OPCODE_MOV
;
2261 inst
->src
[0] = component(inst
->src
[0],
2264 inst
->force_writemask_all
= true;
2273 /* Swap if src[0] is immediate. */
2274 if (progress
&& inst
->is_commutative()) {
2275 if (inst
->src
[0].file
== IMM
) {
2276 fs_reg tmp
= inst
->src
[1];
2277 inst
->src
[1] = inst
->src
[0];
2286 * Optimize sample messages that have constant zero values for the trailing
2287 * texture coordinates. We can just reduce the message length for these
2288 * instructions instead of reserving a register for it. Trailing parameters
2289 * that aren't sent default to zero anyway. This will cause the dead code
2290 * eliminator to remove the MOV instruction that would otherwise be emitted to
2291 * set up the zero value.
2294 fs_visitor::opt_zero_samples()
2296 /* Gen4 infers the texturing opcode based on the message length so we can't
2299 if (devinfo
->gen
< 5)
2302 bool progress
= false;
2304 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2305 if (!inst
->is_tex())
2308 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2310 if (load_payload
->is_head_sentinel() ||
2311 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2314 /* We don't want to remove the message header or the first parameter.
2315 * Removing the first parameter is not allowed, see the Haswell PRM
2316 * volume 7, page 149:
2318 * "Parameter 0 is required except for the sampleinfo message, which
2319 * has no parameter 0"
2321 while (inst
->mlen
> inst
->header_size
+ inst
->exec_size
/ 8 &&
2322 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2323 (inst
->exec_size
/ 8) +
2324 inst
->header_size
- 1].is_zero()) {
2325 inst
->mlen
-= inst
->exec_size
/ 8;
2331 invalidate_live_intervals();
2337 * Optimize sample messages which are followed by the final RT write.
2339 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2340 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2341 * final texturing results copied to the framebuffer write payload and modify
2342 * them to write to the framebuffer directly.
2345 fs_visitor::opt_sampler_eot()
2347 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2349 if (stage
!= MESA_SHADER_FRAGMENT
)
2352 if (devinfo
->gen
< 9 && !devinfo
->is_cherryview
)
2355 /* FINISHME: It should be possible to implement this optimization when there
2356 * are multiple drawbuffers.
2358 if (key
->nr_color_regions
!= 1)
2361 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2362 bblock_t
*block
= cfg
->blocks
[cfg
->num_blocks
- 1];
2363 fs_inst
*fb_write
= (fs_inst
*)block
->end();
2364 assert(fb_write
->eot
);
2365 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE
);
2367 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2369 /* There wasn't one; nothing to do. */
2370 if (unlikely(tex_inst
->is_head_sentinel()) || !tex_inst
->is_tex())
2373 /* 3D Sampler » Messages » Message Format
2375 * “Response Length of zero is allowed on all SIMD8* and SIMD16* sampler
2376 * messages except sample+killpix, resinfo, sampleinfo, LOD, and gather4*”
2378 if (tex_inst
->opcode
== SHADER_OPCODE_TXS
||
2379 tex_inst
->opcode
== SHADER_OPCODE_SAMPLEINFO
||
2380 tex_inst
->opcode
== SHADER_OPCODE_LOD
||
2381 tex_inst
->opcode
== SHADER_OPCODE_TG4
||
2382 tex_inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
2385 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2386 * It's very likely to be the previous instruction.
2388 fs_inst
*load_payload
= (fs_inst
*) tex_inst
->prev
;
2389 if (load_payload
->is_head_sentinel() ||
2390 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2393 assert(!tex_inst
->eot
); /* We can't get here twice */
2394 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2396 const fs_builder
ibld(this, block
, tex_inst
);
2398 tex_inst
->offset
|= fb_write
->target
<< 24;
2399 tex_inst
->eot
= true;
2400 tex_inst
->dst
= ibld
.null_reg_ud();
2401 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2403 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2404 * to create a new LOAD_PAYLOAD command with the same sources and a space
2405 * saved for the header. Using a new destination register not only makes sure
2406 * we have enough space, but it will make sure the dead code eliminator kills
2407 * the instruction that this will replace.
2409 if (tex_inst
->header_size
!= 0)
2412 fs_reg send_header
= ibld
.vgrf(BRW_REGISTER_TYPE_F
,
2413 load_payload
->sources
+ 1);
2414 fs_reg
*new_sources
=
2415 ralloc_array(mem_ctx
, fs_reg
, load_payload
->sources
+ 1);
2417 new_sources
[0] = fs_reg();
2418 for (int i
= 0; i
< load_payload
->sources
; i
++)
2419 new_sources
[i
+1] = load_payload
->src
[i
];
2421 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2422 * requires a lot of information about the sources to appropriately figure
2423 * out the number of registers needed to be used. Given this stage in our
2424 * optimization, we may not have the appropriate GRFs required by
2425 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2426 * manually emit the instruction.
2428 fs_inst
*new_load_payload
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
,
2429 load_payload
->exec_size
,
2432 load_payload
->sources
+ 1);
2434 new_load_payload
->regs_written
= load_payload
->regs_written
+ 1;
2435 new_load_payload
->header_size
= 1;
2437 tex_inst
->header_size
= 1;
2438 tex_inst
->insert_before(cfg
->blocks
[cfg
->num_blocks
- 1], new_load_payload
);
2439 tex_inst
->src
[0] = send_header
;
2445 fs_visitor::opt_register_renaming()
2447 bool progress
= false;
2450 int remap
[alloc
.count
];
2451 memset(remap
, -1, sizeof(int) * alloc
.count
);
2453 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2454 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2456 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2457 inst
->opcode
== BRW_OPCODE_WHILE
) {
2461 /* Rewrite instruction sources. */
2462 for (int i
= 0; i
< inst
->sources
; i
++) {
2463 if (inst
->src
[i
].file
== VGRF
&&
2464 remap
[inst
->src
[i
].nr
] != -1 &&
2465 remap
[inst
->src
[i
].nr
] != inst
->src
[i
].nr
) {
2466 inst
->src
[i
].nr
= remap
[inst
->src
[i
].nr
];
2471 const int dst
= inst
->dst
.nr
;
2474 inst
->dst
.file
== VGRF
&&
2475 alloc
.sizes
[inst
->dst
.nr
] == inst
->exec_size
/ 8 &&
2476 !inst
->is_partial_write()) {
2477 if (remap
[dst
] == -1) {
2480 remap
[dst
] = alloc
.allocate(inst
->exec_size
/ 8);
2481 inst
->dst
.nr
= remap
[dst
];
2484 } else if (inst
->dst
.file
== VGRF
&&
2486 remap
[dst
] != dst
) {
2487 inst
->dst
.nr
= remap
[dst
];
2493 invalidate_live_intervals();
2495 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2496 if (delta_xy
[i
].file
== VGRF
&& remap
[delta_xy
[i
].nr
] != -1) {
2497 delta_xy
[i
].nr
= remap
[delta_xy
[i
].nr
];
2506 * Remove redundant or useless discard jumps.
2508 * For example, we can eliminate jumps in the following sequence:
2510 * discard-jump (redundant with the next jump)
2511 * discard-jump (useless; jumps to the next instruction)
2515 fs_visitor::opt_redundant_discard_jumps()
2517 bool progress
= false;
2519 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2521 fs_inst
*placeholder_halt
= NULL
;
2522 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2523 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2524 placeholder_halt
= inst
;
2529 if (!placeholder_halt
)
2532 /* Delete any HALTs immediately before the placeholder halt. */
2533 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2534 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2535 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2536 prev
->remove(last_bblock
);
2541 invalidate_live_intervals();
2547 fs_visitor::compute_to_mrf()
2549 bool progress
= false;
2552 /* No MRFs on Gen >= 7. */
2553 if (devinfo
->gen
>= 7)
2556 calculate_live_intervals();
2558 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2562 if (inst
->opcode
!= BRW_OPCODE_MOV
||
2563 inst
->is_partial_write() ||
2564 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= VGRF
||
2565 inst
->dst
.type
!= inst
->src
[0].type
||
2566 inst
->src
[0].abs
|| inst
->src
[0].negate
||
2567 !inst
->src
[0].is_contiguous() ||
2568 inst
->src
[0].subreg_offset
)
2571 /* Work out which hardware MRF registers are written by this
2574 int mrf_low
= inst
->dst
.nr
& ~BRW_MRF_COMPR4
;
2576 if (inst
->dst
.nr
& BRW_MRF_COMPR4
) {
2577 mrf_high
= mrf_low
+ 4;
2578 } else if (inst
->exec_size
== 16) {
2579 mrf_high
= mrf_low
+ 1;
2584 /* Can't compute-to-MRF this GRF if someone else was going to
2587 if (this->virtual_grf_end
[inst
->src
[0].nr
] > ip
)
2590 /* Found a move of a GRF to a MRF. Let's see if we can go
2591 * rewrite the thing that made this GRF to write into the MRF.
2593 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
2594 if (scan_inst
->dst
.file
== VGRF
&&
2595 scan_inst
->dst
.nr
== inst
->src
[0].nr
) {
2596 /* Found the last thing to write our reg we want to turn
2597 * into a compute-to-MRF.
2600 /* If this one instruction didn't populate all the
2601 * channels, bail. We might be able to rewrite everything
2602 * that writes that reg, but it would require smarter
2603 * tracking to delay the rewriting until complete success.
2605 if (scan_inst
->is_partial_write())
2608 /* Things returning more than one register would need us to
2609 * understand coalescing out more than one MOV at a time.
2611 if (scan_inst
->regs_written
> scan_inst
->exec_size
/ 8)
2614 /* SEND instructions can't have MRF as a destination. */
2615 if (scan_inst
->mlen
)
2618 if (devinfo
->gen
== 6) {
2619 /* gen6 math instructions must have the destination be
2620 * GRF, so no compute-to-MRF for them.
2622 if (scan_inst
->is_math()) {
2627 if (scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
2628 /* Found the creator of our MRF's source value. */
2629 scan_inst
->dst
.file
= MRF
;
2630 scan_inst
->dst
.nr
= inst
->dst
.nr
;
2631 scan_inst
->saturate
|= inst
->saturate
;
2632 inst
->remove(block
);
2638 /* We don't handle control flow here. Most computation of
2639 * values that end up in MRFs are shortly before the MRF
2642 if (block
->start() == scan_inst
)
2645 /* You can't read from an MRF, so if someone else reads our
2646 * MRF's source GRF that we wanted to rewrite, that stops us.
2648 bool interfered
= false;
2649 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
2650 if (scan_inst
->src
[i
].file
== VGRF
&&
2651 scan_inst
->src
[i
].nr
== inst
->src
[0].nr
&&
2652 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
2659 if (scan_inst
->dst
.file
== MRF
) {
2660 /* If somebody else writes our MRF here, we can't
2661 * compute-to-MRF before that.
2663 int scan_mrf_low
= scan_inst
->dst
.nr
& ~BRW_MRF_COMPR4
;
2666 if (scan_inst
->dst
.nr
& BRW_MRF_COMPR4
) {
2667 scan_mrf_high
= scan_mrf_low
+ 4;
2668 } else if (scan_inst
->exec_size
== 16) {
2669 scan_mrf_high
= scan_mrf_low
+ 1;
2671 scan_mrf_high
= scan_mrf_low
;
2674 if (mrf_low
== scan_mrf_low
||
2675 mrf_low
== scan_mrf_high
||
2676 mrf_high
== scan_mrf_low
||
2677 mrf_high
== scan_mrf_high
) {
2682 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1) {
2683 /* Found a SEND instruction, which means that there are
2684 * live values in MRFs from base_mrf to base_mrf +
2685 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2688 if (mrf_low
>= scan_inst
->base_mrf
&&
2689 mrf_low
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2692 if (mrf_high
>= scan_inst
->base_mrf
&&
2693 mrf_high
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2701 invalidate_live_intervals();
2707 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2708 * flow. We could probably do better here with some form of divergence
2712 fs_visitor::eliminate_find_live_channel()
2714 bool progress
= false;
2717 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2718 switch (inst
->opcode
) {
2724 case BRW_OPCODE_ENDIF
:
2725 case BRW_OPCODE_WHILE
:
2729 case FS_OPCODE_DISCARD_JUMP
:
2730 /* This can potentially make control flow non-uniform until the end
2735 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2737 inst
->opcode
= BRW_OPCODE_MOV
;
2738 inst
->src
[0] = fs_reg(0u);
2740 inst
->force_writemask_all
= true;
2754 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2755 * instructions to FS_OPCODE_REP_FB_WRITE.
2758 fs_visitor::emit_repclear_shader()
2760 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2762 int color_mrf
= base_mrf
+ 2;
2764 fs_inst
*mov
= bld
.exec_all().group(4, 0)
2765 .MOV(brw_message_reg(color_mrf
),
2766 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
));
2769 if (key
->nr_color_regions
== 1) {
2770 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2771 write
->saturate
= key
->clamp_fragment_color
;
2772 write
->base_mrf
= color_mrf
;
2774 write
->header_size
= 0;
2777 assume(key
->nr_color_regions
> 0);
2778 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
2779 write
= bld
.emit(FS_OPCODE_REP_FB_WRITE
);
2780 write
->saturate
= key
->clamp_fragment_color
;
2781 write
->base_mrf
= base_mrf
;
2783 write
->header_size
= 2;
2791 assign_constant_locations();
2792 assign_curb_setup();
2794 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2795 assert(mov
->src
[0].file
== FIXED_GRF
);
2796 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].nr
, 0);
2800 * Walks through basic blocks, looking for repeated MRF writes and
2801 * removing the later ones.
2804 fs_visitor::remove_duplicate_mrf_writes()
2806 fs_inst
*last_mrf_move
[BRW_MAX_MRF(devinfo
->gen
)];
2807 bool progress
= false;
2809 /* Need to update the MRF tracking for compressed instructions. */
2810 if (dispatch_width
== 16)
2813 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2815 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
2816 if (inst
->is_control_flow()) {
2817 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
2820 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2821 inst
->dst
.file
== MRF
) {
2822 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.nr
];
2823 if (prev_inst
&& inst
->equals(prev_inst
)) {
2824 inst
->remove(block
);
2830 /* Clear out the last-write records for MRFs that were overwritten. */
2831 if (inst
->dst
.file
== MRF
) {
2832 last_mrf_move
[inst
->dst
.nr
] = NULL
;
2835 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
2836 /* Found a SEND instruction, which will include two or fewer
2837 * implied MRF writes. We could do better here.
2839 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
2840 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
2844 /* Clear out any MRF move records whose sources got overwritten. */
2845 if (inst
->dst
.file
== VGRF
) {
2846 for (unsigned int i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
2847 if (last_mrf_move
[i
] &&
2848 last_mrf_move
[i
]->src
[0].nr
== inst
->dst
.nr
) {
2849 last_mrf_move
[i
] = NULL
;
2854 if (inst
->opcode
== BRW_OPCODE_MOV
&&
2855 inst
->dst
.file
== MRF
&&
2856 inst
->src
[0].file
== VGRF
&&
2857 !inst
->is_partial_write()) {
2858 last_mrf_move
[inst
->dst
.nr
] = inst
;
2863 invalidate_live_intervals();
2869 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
2871 /* Clear the flag for registers that actually got read (as expected). */
2872 for (int i
= 0; i
< inst
->sources
; i
++) {
2874 if (inst
->src
[i
].file
== VGRF
|| inst
->src
[i
].file
== FIXED_GRF
) {
2875 grf
= inst
->src
[i
].nr
;
2880 if (grf
>= first_grf
&&
2881 grf
< first_grf
+ grf_len
) {
2882 deps
[grf
- first_grf
] = false;
2883 if (inst
->exec_size
== 16)
2884 deps
[grf
- first_grf
+ 1] = false;
2890 * Implements this workaround for the original 965:
2892 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
2893 * check for post destination dependencies on this instruction, software
2894 * must ensure that there is no destination hazard for the case of ‘write
2895 * followed by a posted write’ shown in the following example.
2898 * 2. send r3.xy <rest of send instruction>
2901 * Due to no post-destination dependency check on the ‘send’, the above
2902 * code sequence could have two instructions (1 and 2) in flight at the
2903 * same time that both consider ‘r3’ as the target of their final writes.
2906 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
2909 int write_len
= inst
->regs_written
;
2910 int first_write_grf
= inst
->dst
.nr
;
2911 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2912 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2914 memset(needs_dep
, false, sizeof(needs_dep
));
2915 memset(needs_dep
, true, write_len
);
2917 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
2919 /* Walk backwards looking for writes to registers we're writing which
2920 * aren't read since being written. If we hit the start of the program,
2921 * we assume that there are no outstanding dependencies on entry to the
2924 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
2925 /* If we hit control flow, assume that there *are* outstanding
2926 * dependencies, and force their cleanup before our instruction.
2928 if (block
->start() == scan_inst
) {
2929 for (int i
= 0; i
< write_len
; i
++) {
2931 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
),
2932 first_write_grf
+ i
);
2937 /* We insert our reads as late as possible on the assumption that any
2938 * instruction but a MOV that might have left us an outstanding
2939 * dependency has more latency than a MOV.
2941 if (scan_inst
->dst
.file
== VGRF
) {
2942 for (int i
= 0; i
< scan_inst
->regs_written
; i
++) {
2943 int reg
= scan_inst
->dst
.nr
+ i
;
2945 if (reg
>= first_write_grf
&&
2946 reg
< first_write_grf
+ write_len
&&
2947 needs_dep
[reg
- first_write_grf
]) {
2948 DEP_RESOLVE_MOV(fs_builder(this, block
, inst
), reg
);
2949 needs_dep
[reg
- first_write_grf
] = false;
2950 if (scan_inst
->exec_size
== 16)
2951 needs_dep
[reg
- first_write_grf
+ 1] = false;
2956 /* Clear the flag for registers that actually got read (as expected). */
2957 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
2959 /* Continue the loop only if we haven't resolved all the dependencies */
2961 for (i
= 0; i
< write_len
; i
++) {
2971 * Implements this workaround for the original 965:
2973 * "[DevBW, DevCL] Errata: A destination register from a send can not be
2974 * used as a destination register until after it has been sourced by an
2975 * instruction with a different destination register.
2978 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
2980 int write_len
= inst
->regs_written
;
2981 int first_write_grf
= inst
->dst
.nr
;
2982 bool needs_dep
[BRW_MAX_MRF(devinfo
->gen
)];
2983 assert(write_len
< (int)sizeof(needs_dep
) - 1);
2985 memset(needs_dep
, false, sizeof(needs_dep
));
2986 memset(needs_dep
, true, write_len
);
2987 /* Walk forwards looking for writes to registers we're writing which aren't
2988 * read before being written.
2990 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
) {
2991 /* If we hit control flow, force resolve all remaining dependencies. */
2992 if (block
->end() == scan_inst
) {
2993 for (int i
= 0; i
< write_len
; i
++) {
2995 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
2996 first_write_grf
+ i
);
3001 /* Clear the flag for registers that actually got read (as expected). */
3002 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3004 /* We insert our reads as late as possible since they're reading the
3005 * result of a SEND, which has massive latency.
3007 if (scan_inst
->dst
.file
== VGRF
&&
3008 scan_inst
->dst
.nr
>= first_write_grf
&&
3009 scan_inst
->dst
.nr
< first_write_grf
+ write_len
&&
3010 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
]) {
3011 DEP_RESOLVE_MOV(fs_builder(this, block
, scan_inst
),
3013 needs_dep
[scan_inst
->dst
.nr
- first_write_grf
] = false;
3016 /* Continue the loop only if we haven't resolved all the dependencies */
3018 for (i
= 0; i
< write_len
; i
++) {
3028 fs_visitor::insert_gen4_send_dependency_workarounds()
3030 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
3033 bool progress
= false;
3035 /* Note that we're done with register allocation, so GRF fs_regs always
3036 * have a .reg_offset of 0.
3039 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3040 if (inst
->mlen
!= 0 && inst
->dst
.file
== VGRF
) {
3041 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3042 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3048 invalidate_live_intervals();
3052 * Turns the generic expression-style uniform pull constant load instruction
3053 * into a hardware-specific series of instructions for loading a pull
3056 * The expression style allows the CSE pass before this to optimize out
3057 * repeated loads from the same offset, and gives the pre-register-allocation
3058 * scheduling full flexibility, while the conversion to native instructions
3059 * allows the post-register-allocation scheduler the best information
3062 * Note that execution masking for setting up pull constant loads is special:
3063 * the channels that need to be written are unrelated to the current execution
3064 * mask, since a later instruction will use one of the result channels as a
3065 * source operand for all 8 or 16 of its channels.
3068 fs_visitor::lower_uniform_pull_constant_loads()
3070 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3071 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3074 if (devinfo
->gen
>= 7) {
3075 /* The offset arg before was a vec4-aligned byte offset. We need to
3076 * turn it into a dword offset.
3078 fs_reg const_offset_reg
= inst
->src
[1];
3079 assert(const_offset_reg
.file
== IMM
&&
3080 const_offset_reg
.type
== BRW_REGISTER_TYPE_UD
);
3081 const_offset_reg
.ud
/= 4;
3083 fs_reg payload
, offset
;
3084 if (devinfo
->gen
>= 9) {
3085 /* We have to use a message header on Skylake to get SIMD4x2
3086 * mode. Reserve space for the register.
3088 offset
= payload
= fs_reg(VGRF
, alloc
.allocate(2));
3089 offset
.reg_offset
++;
3092 offset
= payload
= fs_reg(VGRF
, alloc
.allocate(1));
3096 /* This is actually going to be a MOV, but since only the first dword
3097 * is accessed, we have a special opcode to do just that one. Note
3098 * that this needs to be an operation that will be considered a def
3099 * by live variable analysis, or register allocation will explode.
3101 fs_inst
*setup
= new(mem_ctx
) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET
,
3102 8, offset
, const_offset_reg
);
3103 setup
->force_writemask_all
= true;
3105 setup
->ir
= inst
->ir
;
3106 setup
->annotation
= inst
->annotation
;
3107 inst
->insert_before(block
, setup
);
3109 /* Similarly, this will only populate the first 4 channels of the
3110 * result register (since we only use smear values from 0-3), but we
3111 * don't tell the optimizer.
3113 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3114 inst
->src
[1] = payload
;
3115 inst
->base_mrf
= -1;
3117 invalidate_live_intervals();
3119 /* Before register allocation, we didn't tell the scheduler about the
3120 * MRF we use. We know it's safe to use this MRF because nothing
3121 * else does except for register spill/unspill, which generates and
3122 * uses its MRF within a single IR instruction.
3124 inst
->base_mrf
= FIRST_PULL_LOAD_MRF(devinfo
->gen
) + 1;
3131 fs_visitor::lower_load_payload()
3133 bool progress
= false;
3135 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3136 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3139 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== VGRF
);
3140 assert(inst
->saturate
== false);
3141 fs_reg dst
= inst
->dst
;
3143 /* Get rid of COMPR4. We'll add it back in if we need it */
3144 if (dst
.file
== MRF
)
3145 dst
.nr
= dst
.nr
& ~BRW_MRF_COMPR4
;
3147 const fs_builder
ibld(this, block
, inst
);
3148 const fs_builder hbld
= ibld
.exec_all().group(8, 0);
3150 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3151 if (inst
->src
[i
].file
!= BAD_FILE
) {
3152 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3153 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3154 hbld
.MOV(mov_dst
, mov_src
);
3156 dst
= offset(dst
, hbld
, 1);
3159 if (inst
->dst
.file
== MRF
&& (inst
->dst
.nr
& BRW_MRF_COMPR4
) &&
3160 inst
->exec_size
> 8) {
3161 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3162 * a straightforward copy. Instead, the result of the
3163 * LOAD_PAYLOAD is treated as interleaved and the first four
3164 * non-header sources are unpacked as:
3175 * This is used for gen <= 5 fb writes.
3177 assert(inst
->exec_size
== 16);
3178 assert(inst
->header_size
+ 4 <= inst
->sources
);
3179 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3180 if (inst
->src
[i
].file
!= BAD_FILE
) {
3181 if (devinfo
->has_compr4
) {
3182 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3183 compr4_dst
.nr
|= BRW_MRF_COMPR4
;
3184 ibld
.MOV(compr4_dst
, inst
->src
[i
]);
3186 /* Platform doesn't have COMPR4. We have to fake it */
3187 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3188 ibld
.half(0).MOV(mov_dst
, half(inst
->src
[i
], 0));
3190 ibld
.half(1).MOV(mov_dst
, half(inst
->src
[i
], 1));
3197 /* The loop above only ever incremented us through the first set
3198 * of 4 registers. However, thanks to the magic of COMPR4, we
3199 * actually wrote to the first 8 registers, so we need to take
3200 * that into account now.
3204 /* The COMPR4 code took care of the first 4 sources. We'll let
3205 * the regular path handle any remaining sources. Yes, we are
3206 * modifying the instruction but we're about to delete it so
3207 * this really doesn't hurt anything.
3209 inst
->header_size
+= 4;
3212 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3213 if (inst
->src
[i
].file
!= BAD_FILE
)
3214 ibld
.MOV(retype(dst
, inst
->src
[i
].type
), inst
->src
[i
]);
3215 dst
= offset(dst
, ibld
, 1);
3218 inst
->remove(block
);
3223 invalidate_live_intervals();
3229 fs_visitor::lower_integer_multiplication()
3231 bool progress
= false;
3233 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3234 const fs_builder
ibld(this, block
, inst
);
3236 if (inst
->opcode
== BRW_OPCODE_MUL
) {
3237 if (inst
->dst
.is_accumulator() ||
3238 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3239 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3242 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit
3243 * operation directly, but CHV/BXT cannot.
3245 if (devinfo
->gen
>= 8 &&
3246 !devinfo
->is_cherryview
&& !devinfo
->is_broxton
)
3249 if (inst
->src
[1].file
== IMM
&&
3250 inst
->src
[1].ud
< (1 << 16)) {
3251 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3252 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3255 * If multiplying by an immediate value that fits in 16-bits, do a
3256 * single MUL instruction with that value in the proper location.
3258 if (devinfo
->gen
< 7) {
3259 fs_reg
imm(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3261 ibld
.MOV(imm
, inst
->src
[1]);
3262 ibld
.MUL(inst
->dst
, imm
, inst
->src
[0]);
3264 ibld
.MUL(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3267 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3268 * do 32-bit integer multiplication in one instruction, but instead
3269 * must do a sequence (which actually calculates a 64-bit result):
3271 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3272 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3273 * mov(8) g2<1>D acc0<8,8,1>D
3275 * But on Gen > 6, the ability to use second accumulator register
3276 * (acc1) for non-float data types was removed, preventing a simple
3277 * implementation in SIMD16. A 16-channel result can be calculated by
3278 * executing the three instructions twice in SIMD8, once with quarter
3279 * control of 1Q for the first eight channels and again with 2Q for
3280 * the second eight channels.
3282 * Which accumulator register is implicitly accessed (by AccWrEnable
3283 * for instance) is determined by the quarter control. Unfortunately
3284 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3285 * implicit accumulator access by an instruction with 2Q will access
3286 * acc1 regardless of whether the data type is usable in acc1.
3288 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3289 * integer data types.
3291 * Since we only want the low 32-bits of the result, we can do two
3292 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3293 * adjust the high result and add them (like the mach is doing):
3295 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3296 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3297 * shl(8) g9<1>D g8<8,8,1>D 16D
3298 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3300 * We avoid the shl instruction by realizing that we only want to add
3301 * the low 16-bits of the "high" result to the high 16-bits of the
3302 * "low" result and using proper regioning on the add:
3304 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3305 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3306 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3308 * Since it does not use the (single) accumulator register, we can
3309 * schedule multi-component multiplications much better.
3312 fs_reg orig_dst
= inst
->dst
;
3313 if (orig_dst
.is_null() || orig_dst
.file
== MRF
) {
3314 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3317 fs_reg low
= inst
->dst
;
3318 fs_reg
high(VGRF
, alloc
.allocate(dispatch_width
/ 8),
3321 if (devinfo
->gen
>= 7) {
3322 fs_reg src1_0_w
= inst
->src
[1];
3323 fs_reg src1_1_w
= inst
->src
[1];
3325 if (inst
->src
[1].file
== IMM
) {
3326 src1_0_w
.ud
&= 0xffff;
3329 src1_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3330 if (src1_0_w
.stride
!= 0) {
3331 assert(src1_0_w
.stride
== 1);
3332 src1_0_w
.stride
= 2;
3335 src1_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3336 if (src1_1_w
.stride
!= 0) {
3337 assert(src1_1_w
.stride
== 1);
3338 src1_1_w
.stride
= 2;
3340 src1_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3342 ibld
.MUL(low
, inst
->src
[0], src1_0_w
);
3343 ibld
.MUL(high
, inst
->src
[0], src1_1_w
);
3345 fs_reg src0_0_w
= inst
->src
[0];
3346 fs_reg src0_1_w
= inst
->src
[0];
3348 src0_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3349 if (src0_0_w
.stride
!= 0) {
3350 assert(src0_0_w
.stride
== 1);
3351 src0_0_w
.stride
= 2;
3354 src0_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3355 if (src0_1_w
.stride
!= 0) {
3356 assert(src0_1_w
.stride
== 1);
3357 src0_1_w
.stride
= 2;
3359 src0_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3361 ibld
.MUL(low
, src0_0_w
, inst
->src
[1]);
3362 ibld
.MUL(high
, src0_1_w
, inst
->src
[1]);
3365 fs_reg dst
= inst
->dst
;
3366 dst
.type
= BRW_REGISTER_TYPE_UW
;
3367 dst
.subreg_offset
= 2;
3370 high
.type
= BRW_REGISTER_TYPE_UW
;
3373 low
.type
= BRW_REGISTER_TYPE_UW
;
3374 low
.subreg_offset
= 2;
3377 ibld
.ADD(dst
, low
, high
);
3379 if (inst
->conditional_mod
|| orig_dst
.file
== MRF
) {
3380 set_condmod(inst
->conditional_mod
,
3381 ibld
.MOV(orig_dst
, inst
->dst
));
3385 } else if (inst
->opcode
== SHADER_OPCODE_MULH
) {
3386 /* Should have been lowered to 8-wide. */
3387 assert(inst
->exec_size
<= 8);
3388 const fs_reg acc
= retype(brw_acc_reg(inst
->exec_size
),
3390 fs_inst
*mul
= ibld
.MUL(acc
, inst
->src
[0], inst
->src
[1]);
3391 fs_inst
*mach
= ibld
.MACH(inst
->dst
, inst
->src
[0], inst
->src
[1]);
3393 if (devinfo
->gen
>= 8) {
3394 /* Until Gen8, integer multiplies read 32-bits from one source,
3395 * and 16-bits from the other, and relying on the MACH instruction
3396 * to generate the high bits of the result.
3398 * On Gen8, the multiply instruction does a full 32x32-bit
3399 * multiply, but in order to do a 64-bit multiply we can simulate
3400 * the previous behavior and then use a MACH instruction.
3402 * FINISHME: Don't use source modifiers on src1.
3404 assert(mul
->src
[1].type
== BRW_REGISTER_TYPE_D
||
3405 mul
->src
[1].type
== BRW_REGISTER_TYPE_UD
);
3406 mul
->src
[1].type
= (type_is_signed(mul
->src
[1].type
) ?
3407 BRW_REGISTER_TYPE_W
: BRW_REGISTER_TYPE_UW
);
3408 mul
->src
[1].stride
*= 2;
3410 } else if (devinfo
->gen
== 7 && !devinfo
->is_haswell
&&
3411 inst
->force_sechalf
) {
3412 /* Among other things the quarter control bits influence which
3413 * accumulator register is used by the hardware for instructions
3414 * that access the accumulator implicitly (e.g. MACH). A
3415 * second-half instruction would normally map to acc1, which
3416 * doesn't exist on Gen7 and up (the hardware does emulate it for
3417 * floating-point instructions *only* by taking advantage of the
3418 * extra precision of acc0 not normally used for floating point
3421 * HSW and up are careful enough not to try to access an
3422 * accumulator register that doesn't exist, but on earlier Gen7
3423 * hardware we need to make sure that the quarter control bits are
3424 * zero to avoid non-deterministic behaviour and emit an extra MOV
3425 * to get the result masked correctly according to the current
3428 mach
->force_sechalf
= false;
3429 mach
->force_writemask_all
= true;
3430 mach
->dst
= ibld
.vgrf(inst
->dst
.type
);
3431 ibld
.MOV(inst
->dst
, mach
->dst
);
3437 inst
->remove(block
);
3442 invalidate_live_intervals();
3448 setup_color_payload(const fs_builder
&bld
, const brw_wm_prog_key
*key
,
3449 fs_reg
*dst
, fs_reg color
, unsigned components
)
3451 if (key
->clamp_fragment_color
) {
3452 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4);
3453 assert(color
.type
== BRW_REGISTER_TYPE_F
);
3455 for (unsigned i
= 0; i
< components
; i
++)
3457 bld
.MOV(offset(tmp
, bld
, i
), offset(color
, bld
, i
)));
3462 for (unsigned i
= 0; i
< components
; i
++)
3463 dst
[i
] = offset(color
, bld
, i
);
3467 lower_fb_write_logical_send(const fs_builder
&bld
, fs_inst
*inst
,
3468 const brw_wm_prog_data
*prog_data
,
3469 const brw_wm_prog_key
*key
,
3470 const fs_visitor::thread_payload
&payload
)
3472 assert(inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].file
== IMM
);
3473 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3474 const fs_reg
&color0
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR0
];
3475 const fs_reg
&color1
= inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
];
3476 const fs_reg
&src0_alpha
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC0_ALPHA
];
3477 const fs_reg
&src_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
];
3478 const fs_reg
&dst_depth
= inst
->src
[FB_WRITE_LOGICAL_SRC_DST_DEPTH
];
3479 const fs_reg
&src_stencil
= inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_STENCIL
];
3480 fs_reg sample_mask
= inst
->src
[FB_WRITE_LOGICAL_SRC_OMASK
];
3481 const unsigned components
=
3482 inst
->src
[FB_WRITE_LOGICAL_SRC_COMPONENTS
].ud
;
3484 /* We can potentially have a message length of up to 15, so we have to set
3485 * base_mrf to either 0 or 1 in order to fit in m0..m15.
3488 int header_size
= 2, payload_header_size
;
3489 unsigned length
= 0;
3491 /* From the Sandy Bridge PRM, volume 4, page 198:
3493 * "Dispatched Pixel Enables. One bit per pixel indicating
3494 * which pixels were originally enabled when the thread was
3495 * dispatched. This field is only required for the end-of-
3496 * thread message and on all dual-source messages."
3498 if (devinfo
->gen
>= 6 &&
3499 (devinfo
->is_haswell
|| devinfo
->gen
>= 8 || !prog_data
->uses_kill
) &&
3500 color1
.file
== BAD_FILE
&&
3501 key
->nr_color_regions
== 1) {
3505 if (header_size
!= 0) {
3506 assert(header_size
== 2);
3507 /* Allocate 2 registers for a header */
3511 if (payload
.aa_dest_stencil_reg
) {
3512 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1));
3513 bld
.group(8, 0).exec_all().annotate("FB write stencil/AA alpha")
3514 .MOV(sources
[length
],
3515 fs_reg(brw_vec8_grf(payload
.aa_dest_stencil_reg
, 0)));
3519 if (prog_data
->uses_omask
) {
3520 sources
[length
] = fs_reg(VGRF
, bld
.shader
->alloc
.allocate(1),
3521 BRW_REGISTER_TYPE_UD
);
3523 /* Hand over gl_SampleMask. Only the lower 16 bits of each channel are
3524 * relevant. Since it's unsigned single words one vgrf is always
3525 * 16-wide, but only the lower or higher 8 channels will be used by the
3526 * hardware when doing a SIMD8 write depending on whether we have
3527 * selected the subspans for the first or second half respectively.
3529 assert(sample_mask
.file
!= BAD_FILE
&& type_sz(sample_mask
.type
) == 4);
3530 sample_mask
.type
= BRW_REGISTER_TYPE_UW
;
3531 sample_mask
.stride
*= 2;
3533 bld
.exec_all().annotate("FB write oMask")
3534 .MOV(half(retype(sources
[length
], BRW_REGISTER_TYPE_UW
),
3535 inst
->force_sechalf
),
3540 payload_header_size
= length
;
3542 if (src0_alpha
.file
!= BAD_FILE
) {
3543 /* FIXME: This is being passed at the wrong location in the payload and
3544 * doesn't work when gl_SampleMask and MRTs are used simultaneously.
3545 * It's supposed to be immediately before oMask but there seems to be no
3546 * reasonable way to pass them in the correct order because LOAD_PAYLOAD
3547 * requires header sources to form a contiguous segment at the beginning
3548 * of the message and src0_alpha has per-channel semantics.
3550 setup_color_payload(bld
, key
, &sources
[length
], src0_alpha
, 1);
3554 setup_color_payload(bld
, key
, &sources
[length
], color0
, components
);
3557 if (color1
.file
!= BAD_FILE
) {
3558 setup_color_payload(bld
, key
, &sources
[length
], color1
, components
);
3562 if (src_depth
.file
!= BAD_FILE
) {
3563 sources
[length
] = src_depth
;
3567 if (dst_depth
.file
!= BAD_FILE
) {
3568 sources
[length
] = dst_depth
;
3572 if (src_stencil
.file
!= BAD_FILE
) {
3573 assert(devinfo
->gen
>= 9);
3574 assert(bld
.dispatch_width() != 16);
3576 sources
[length
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3577 bld
.exec_all().annotate("FB write OS")
3578 .emit(FS_OPCODE_PACK_STENCIL_REF
, sources
[length
],
3579 retype(src_stencil
, BRW_REGISTER_TYPE_UB
));
3584 if (devinfo
->gen
>= 7) {
3585 /* Send from the GRF */
3586 fs_reg payload
= fs_reg(VGRF
, -1, BRW_REGISTER_TYPE_F
);
3587 load
= bld
.LOAD_PAYLOAD(payload
, sources
, length
, payload_header_size
);
3588 payload
.nr
= bld
.shader
->alloc
.allocate(load
->regs_written
);
3589 load
->dst
= payload
;
3591 inst
->src
[0] = payload
;
3592 inst
->resize_sources(1);
3593 inst
->base_mrf
= -1;
3595 /* Send from the MRF */
3596 load
= bld
.LOAD_PAYLOAD(fs_reg(MRF
, 1, BRW_REGISTER_TYPE_F
),
3597 sources
, length
, payload_header_size
);
3599 /* On pre-SNB, we have to interlace the color values. LOAD_PAYLOAD
3600 * will do this for us if we just give it a COMPR4 destination.
3602 if (devinfo
->gen
< 6 && bld
.dispatch_width() == 16)
3603 load
->dst
.nr
|= BRW_MRF_COMPR4
;
3605 inst
->resize_sources(0);
3609 inst
->opcode
= FS_OPCODE_FB_WRITE
;
3610 inst
->mlen
= load
->regs_written
;
3611 inst
->header_size
= header_size
;
3615 lower_sampler_logical_send_gen4(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3616 const fs_reg
&coordinate
,
3617 const fs_reg
&shadow_c
,
3618 const fs_reg
&lod
, const fs_reg
&lod2
,
3619 const fs_reg
&sampler
,
3620 unsigned coord_components
,
3621 unsigned grad_components
)
3623 const bool has_lod
= (op
== SHADER_OPCODE_TXL
|| op
== FS_OPCODE_TXB
||
3624 op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
);
3625 fs_reg
msg_begin(MRF
, 1, BRW_REGISTER_TYPE_F
);
3626 fs_reg msg_end
= msg_begin
;
3629 msg_end
= offset(msg_end
, bld
.group(8, 0), 1);
3631 for (unsigned i
= 0; i
< coord_components
; i
++)
3632 bld
.MOV(retype(offset(msg_end
, bld
, i
), coordinate
.type
),
3633 offset(coordinate
, bld
, i
));
3635 msg_end
= offset(msg_end
, bld
, coord_components
);
3637 /* Messages other than SAMPLE and RESINFO in SIMD16 and TXD in SIMD8
3638 * require all three components to be present and zero if they are unused.
3640 if (coord_components
> 0 &&
3641 (has_lod
|| shadow_c
.file
!= BAD_FILE
||
3642 (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8))) {
3643 for (unsigned i
= coord_components
; i
< 3; i
++)
3644 bld
.MOV(offset(msg_end
, bld
, i
), fs_reg(0.0f
));
3646 msg_end
= offset(msg_end
, bld
, 3 - coord_components
);
3649 if (op
== SHADER_OPCODE_TXD
) {
3650 /* TXD unsupported in SIMD16 mode. */
3651 assert(bld
.dispatch_width() == 8);
3653 /* the slots for u and v are always present, but r is optional */
3654 if (coord_components
< 2)
3655 msg_end
= offset(msg_end
, bld
, 2 - coord_components
);
3658 * dPdx = dudx, dvdx, drdx
3659 * dPdy = dudy, dvdy, drdy
3661 * 1-arg: Does not exist.
3663 * 2-arg: dudx dvdx dudy dvdy
3664 * dPdx.x dPdx.y dPdy.x dPdy.y
3667 * 3-arg: dudx dvdx drdx dudy dvdy drdy
3668 * dPdx.x dPdx.y dPdx.z dPdy.x dPdy.y dPdy.z
3669 * m5 m6 m7 m8 m9 m10
3671 for (unsigned i
= 0; i
< grad_components
; i
++)
3672 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod
, bld
, i
));
3674 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3676 for (unsigned i
= 0; i
< grad_components
; i
++)
3677 bld
.MOV(offset(msg_end
, bld
, i
), offset(lod2
, bld
, i
));
3679 msg_end
= offset(msg_end
, bld
, MAX2(grad_components
, 2));
3683 /* Bias/LOD with shadow comparitor is unsupported in SIMD16 -- *Without*
3684 * shadow comparitor (including RESINFO) it's unsupported in SIMD8 mode.
3686 assert(shadow_c
.file
!= BAD_FILE
? bld
.dispatch_width() == 8 :
3687 bld
.dispatch_width() == 16);
3689 const brw_reg_type type
=
3690 (op
== SHADER_OPCODE_TXF
|| op
== SHADER_OPCODE_TXS
?
3691 BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_F
);
3692 bld
.MOV(retype(msg_end
, type
), lod
);
3693 msg_end
= offset(msg_end
, bld
, 1);
3696 if (shadow_c
.file
!= BAD_FILE
) {
3697 if (op
== SHADER_OPCODE_TEX
&& bld
.dispatch_width() == 8) {
3698 /* There's no plain shadow compare message, so we use shadow
3699 * compare with a bias of 0.0.
3701 bld
.MOV(msg_end
, fs_reg(0.0f
));
3702 msg_end
= offset(msg_end
, bld
, 1);
3705 bld
.MOV(msg_end
, shadow_c
);
3706 msg_end
= offset(msg_end
, bld
, 1);
3710 inst
->src
[0] = reg_undef
;
3711 inst
->src
[1] = sampler
;
3712 inst
->resize_sources(2);
3713 inst
->base_mrf
= msg_begin
.nr
;
3714 inst
->mlen
= msg_end
.nr
- msg_begin
.nr
;
3715 inst
->header_size
= 1;
3719 lower_sampler_logical_send_gen5(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3721 const fs_reg
&shadow_c
,
3722 fs_reg lod
, fs_reg lod2
,
3723 const fs_reg
&sample_index
,
3724 const fs_reg
&sampler
,
3725 const fs_reg
&offset_value
,
3726 unsigned coord_components
,
3727 unsigned grad_components
)
3729 fs_reg
message(MRF
, 2, BRW_REGISTER_TYPE_F
);
3730 fs_reg msg_coords
= message
;
3731 unsigned header_size
= 0;
3733 if (offset_value
.file
!= BAD_FILE
) {
3734 /* The offsets set up by the visitor are in the m1 header, so we can't
3741 for (unsigned i
= 0; i
< coord_components
; i
++) {
3742 bld
.MOV(retype(offset(msg_coords
, bld
, i
), coordinate
.type
), coordinate
);
3743 coordinate
= offset(coordinate
, bld
, 1);
3745 fs_reg msg_end
= offset(msg_coords
, bld
, coord_components
);
3746 fs_reg msg_lod
= offset(msg_coords
, bld
, 4);
3748 if (shadow_c
.file
!= BAD_FILE
) {
3749 fs_reg msg_shadow
= msg_lod
;
3750 bld
.MOV(msg_shadow
, shadow_c
);
3751 msg_lod
= offset(msg_shadow
, bld
, 1);
3756 case SHADER_OPCODE_TXL
:
3758 bld
.MOV(msg_lod
, lod
);
3759 msg_end
= offset(msg_lod
, bld
, 1);
3761 case SHADER_OPCODE_TXD
:
3764 * dPdx = dudx, dvdx, drdx
3765 * dPdy = dudy, dvdy, drdy
3767 * Load up these values:
3768 * - dudx dudy dvdx dvdy drdx drdy
3769 * - dPdx.x dPdy.x dPdx.y dPdy.y dPdx.z dPdy.z
3772 for (unsigned i
= 0; i
< grad_components
; i
++) {
3773 bld
.MOV(msg_end
, lod
);
3774 lod
= offset(lod
, bld
, 1);
3775 msg_end
= offset(msg_end
, bld
, 1);
3777 bld
.MOV(msg_end
, lod2
);
3778 lod2
= offset(lod2
, bld
, 1);
3779 msg_end
= offset(msg_end
, bld
, 1);
3782 case SHADER_OPCODE_TXS
:
3783 msg_lod
= retype(msg_end
, BRW_REGISTER_TYPE_UD
);
3784 bld
.MOV(msg_lod
, lod
);
3785 msg_end
= offset(msg_lod
, bld
, 1);
3787 case SHADER_OPCODE_TXF
:
3788 msg_lod
= offset(msg_coords
, bld
, 3);
3789 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), lod
);
3790 msg_end
= offset(msg_lod
, bld
, 1);
3792 case SHADER_OPCODE_TXF_CMS
:
3793 msg_lod
= offset(msg_coords
, bld
, 3);
3795 bld
.MOV(retype(msg_lod
, BRW_REGISTER_TYPE_UD
), fs_reg(0u));
3797 bld
.MOV(retype(offset(msg_lod
, bld
, 1), BRW_REGISTER_TYPE_UD
), sample_index
);
3798 msg_end
= offset(msg_lod
, bld
, 2);
3805 inst
->src
[0] = reg_undef
;
3806 inst
->src
[1] = sampler
;
3807 inst
->resize_sources(2);
3808 inst
->base_mrf
= message
.nr
;
3809 inst
->mlen
= msg_end
.nr
- message
.nr
;
3810 inst
->header_size
= header_size
;
3812 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
3813 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
3817 is_high_sampler(const struct brw_device_info
*devinfo
, const fs_reg
&sampler
)
3819 if (devinfo
->gen
< 8 && !devinfo
->is_haswell
)
3822 return sampler
.file
!= IMM
|| sampler
.ud
>= 16;
3826 lower_sampler_logical_send_gen7(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
3828 const fs_reg
&shadow_c
,
3829 fs_reg lod
, fs_reg lod2
,
3830 const fs_reg
&sample_index
,
3831 const fs_reg
&mcs
, const fs_reg
&sampler
,
3832 fs_reg offset_value
,
3833 unsigned coord_components
,
3834 unsigned grad_components
)
3836 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
3837 int reg_width
= bld
.dispatch_width() / 8;
3838 unsigned header_size
= 0, length
= 0;
3839 fs_reg sources
[MAX_SAMPLER_MESSAGE_SIZE
];
3840 for (unsigned i
= 0; i
< ARRAY_SIZE(sources
); i
++)
3841 sources
[i
] = bld
.vgrf(BRW_REGISTER_TYPE_F
);
3843 if (op
== SHADER_OPCODE_TG4
|| op
== SHADER_OPCODE_TG4_OFFSET
||
3844 offset_value
.file
!= BAD_FILE
||
3845 is_high_sampler(devinfo
, sampler
)) {
3846 /* For general texture offsets (no txf workaround), we need a header to
3847 * put them in. Note that we're only reserving space for it in the
3848 * message payload as it will be initialized implicitly by the
3851 * TG4 needs to place its channel select in the header, for interaction
3852 * with ARB_texture_swizzle. The sampler index is only 4-bits, so for
3853 * larger sampler numbers we need to offset the Sampler State Pointer in
3857 sources
[0] = fs_reg();
3861 if (shadow_c
.file
!= BAD_FILE
) {
3862 bld
.MOV(sources
[length
], shadow_c
);
3866 bool coordinate_done
= false;
3868 /* The sampler can only meaningfully compute LOD for fragment shader
3869 * messages. For all other stages, we change the opcode to TXL and
3870 * hardcode the LOD to 0.
3872 if (bld
.shader
->stage
!= MESA_SHADER_FRAGMENT
&&
3873 op
== SHADER_OPCODE_TEX
) {
3874 op
= SHADER_OPCODE_TXL
;
3878 /* Set up the LOD info */
3881 case SHADER_OPCODE_TXL
:
3882 bld
.MOV(sources
[length
], lod
);
3885 case SHADER_OPCODE_TXD
:
3886 /* TXD should have been lowered in SIMD16 mode. */
3887 assert(bld
.dispatch_width() == 8);
3889 /* Load dPdx and the coordinate together:
3890 * [hdr], [ref], x, dPdx.x, dPdy.x, y, dPdx.y, dPdy.y, z, dPdx.z, dPdy.z
3892 for (unsigned i
= 0; i
< coord_components
; i
++) {
3893 bld
.MOV(sources
[length
], coordinate
);
3894 coordinate
= offset(coordinate
, bld
, 1);
3897 /* For cube map array, the coordinate is (u,v,r,ai) but there are
3898 * only derivatives for (u, v, r).
3900 if (i
< grad_components
) {
3901 bld
.MOV(sources
[length
], lod
);
3902 lod
= offset(lod
, bld
, 1);
3905 bld
.MOV(sources
[length
], lod2
);
3906 lod2
= offset(lod2
, bld
, 1);
3911 coordinate_done
= true;
3913 case SHADER_OPCODE_TXS
:
3914 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), lod
);
3917 case SHADER_OPCODE_TXF
:
3918 /* Unfortunately, the parameters for LD are intermixed: u, lod, v, r.
3919 * On Gen9 they are u, v, lod, r
3921 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3922 coordinate
= offset(coordinate
, bld
, 1);
3925 if (devinfo
->gen
>= 9) {
3926 if (coord_components
>= 2) {
3927 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3928 coordinate
= offset(coordinate
, bld
, 1);
3933 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), lod
);
3936 for (unsigned i
= devinfo
->gen
>= 9 ? 2 : 1; i
< coord_components
; i
++) {
3937 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3938 coordinate
= offset(coordinate
, bld
, 1);
3942 coordinate_done
= true;
3944 case SHADER_OPCODE_TXF_CMS
:
3945 case SHADER_OPCODE_TXF_CMS_W
:
3946 case SHADER_OPCODE_TXF_UMS
:
3947 case SHADER_OPCODE_TXF_MCS
:
3948 if (op
== SHADER_OPCODE_TXF_UMS
||
3949 op
== SHADER_OPCODE_TXF_CMS
||
3950 op
== SHADER_OPCODE_TXF_CMS_W
) {
3951 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), sample_index
);
3955 if (op
== SHADER_OPCODE_TXF_CMS
|| op
== SHADER_OPCODE_TXF_CMS_W
) {
3956 /* Data from the multisample control surface. */
3957 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
), mcs
);
3960 /* On Gen9+ we'll use ld2dms_w instead which has two registers for
3963 if (op
== SHADER_OPCODE_TXF_CMS_W
) {
3964 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_UD
),
3967 offset(mcs
, bld
, 1));
3972 /* There is no offsetting for this message; just copy in the integer
3973 * texture coordinates.
3975 for (unsigned i
= 0; i
< coord_components
; i
++) {
3976 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), coordinate
);
3977 coordinate
= offset(coordinate
, bld
, 1);
3981 coordinate_done
= true;
3983 case SHADER_OPCODE_TG4_OFFSET
:
3984 /* gather4_po_c should have been lowered in SIMD16 mode. */
3985 assert(bld
.dispatch_width() == 8 || shadow_c
.file
== BAD_FILE
);
3987 /* More crazy intermixing */
3988 for (unsigned i
= 0; i
< 2; i
++) { /* u, v */
3989 bld
.MOV(sources
[length
], coordinate
);
3990 coordinate
= offset(coordinate
, bld
, 1);
3994 for (unsigned i
= 0; i
< 2; i
++) { /* offu, offv */
3995 bld
.MOV(retype(sources
[length
], BRW_REGISTER_TYPE_D
), offset_value
);
3996 offset_value
= offset(offset_value
, bld
, 1);
4000 if (coord_components
== 3) { /* r if present */
4001 bld
.MOV(sources
[length
], coordinate
);
4002 coordinate
= offset(coordinate
, bld
, 1);
4006 coordinate_done
= true;
4012 /* Set up the coordinate (except for cases where it was done above) */
4013 if (!coordinate_done
) {
4014 for (unsigned i
= 0; i
< coord_components
; i
++) {
4015 bld
.MOV(sources
[length
], coordinate
);
4016 coordinate
= offset(coordinate
, bld
, 1);
4023 mlen
= length
* reg_width
- header_size
;
4025 mlen
= length
* reg_width
;
4027 const fs_reg src_payload
= fs_reg(VGRF
, bld
.shader
->alloc
.allocate(mlen
),
4028 BRW_REGISTER_TYPE_F
);
4029 bld
.LOAD_PAYLOAD(src_payload
, sources
, length
, header_size
);
4031 /* Generate the SEND. */
4033 inst
->src
[0] = src_payload
;
4034 inst
->src
[1] = sampler
;
4035 inst
->resize_sources(2);
4036 inst
->base_mrf
= -1;
4038 inst
->header_size
= header_size
;
4040 /* Message length > MAX_SAMPLER_MESSAGE_SIZE disallowed by hardware. */
4041 assert(inst
->mlen
<= MAX_SAMPLER_MESSAGE_SIZE
);
4045 lower_sampler_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
)
4047 const brw_device_info
*devinfo
= bld
.shader
->devinfo
;
4048 const fs_reg
&coordinate
= inst
->src
[0];
4049 const fs_reg
&shadow_c
= inst
->src
[1];
4050 const fs_reg
&lod
= inst
->src
[2];
4051 const fs_reg
&lod2
= inst
->src
[3];
4052 const fs_reg
&sample_index
= inst
->src
[4];
4053 const fs_reg
&mcs
= inst
->src
[5];
4054 const fs_reg
&sampler
= inst
->src
[6];
4055 const fs_reg
&offset_value
= inst
->src
[7];
4056 assert(inst
->src
[8].file
== IMM
&& inst
->src
[9].file
== IMM
);
4057 const unsigned coord_components
= inst
->src
[8].ud
;
4058 const unsigned grad_components
= inst
->src
[9].ud
;
4060 if (devinfo
->gen
>= 7) {
4061 lower_sampler_logical_send_gen7(bld
, inst
, op
, coordinate
,
4062 shadow_c
, lod
, lod2
, sample_index
,
4063 mcs
, sampler
, offset_value
,
4064 coord_components
, grad_components
);
4065 } else if (devinfo
->gen
>= 5) {
4066 lower_sampler_logical_send_gen5(bld
, inst
, op
, coordinate
,
4067 shadow_c
, lod
, lod2
, sample_index
,
4068 sampler
, offset_value
,
4069 coord_components
, grad_components
);
4071 lower_sampler_logical_send_gen4(bld
, inst
, op
, coordinate
,
4072 shadow_c
, lod
, lod2
, sampler
,
4073 coord_components
, grad_components
);
4078 * Initialize the header present in some typed and untyped surface
4082 emit_surface_header(const fs_builder
&bld
, const fs_reg
&sample_mask
)
4084 fs_builder ubld
= bld
.exec_all().group(8, 0);
4085 const fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4086 ubld
.MOV(dst
, fs_reg(0));
4087 ubld
.MOV(component(dst
, 7), sample_mask
);
4092 lower_surface_logical_send(const fs_builder
&bld
, fs_inst
*inst
, opcode op
,
4093 const fs_reg
&sample_mask
)
4095 /* Get the logical send arguments. */
4096 const fs_reg
&addr
= inst
->src
[0];
4097 const fs_reg
&src
= inst
->src
[1];
4098 const fs_reg
&surface
= inst
->src
[2];
4099 const UNUSED fs_reg
&dims
= inst
->src
[3];
4100 const fs_reg
&arg
= inst
->src
[4];
4102 /* Calculate the total number of components of the payload. */
4103 const unsigned addr_sz
= inst
->components_read(0);
4104 const unsigned src_sz
= inst
->components_read(1);
4105 const unsigned header_sz
= (sample_mask
.file
== BAD_FILE
? 0 : 1);
4106 const unsigned sz
= header_sz
+ addr_sz
+ src_sz
;
4108 /* Allocate space for the payload. */
4109 fs_reg
*const components
= new fs_reg
[sz
];
4110 const fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, sz
);
4113 /* Construct the payload. */
4115 components
[n
++] = emit_surface_header(bld
, sample_mask
);
4117 for (unsigned i
= 0; i
< addr_sz
; i
++)
4118 components
[n
++] = offset(addr
, bld
, i
);
4120 for (unsigned i
= 0; i
< src_sz
; i
++)
4121 components
[n
++] = offset(src
, bld
, i
);
4123 bld
.LOAD_PAYLOAD(payload
, components
, sz
, header_sz
);
4125 /* Update the original instruction. */
4127 inst
->mlen
= header_sz
+ (addr_sz
+ src_sz
) * inst
->exec_size
/ 8;
4128 inst
->header_size
= header_sz
;
4130 inst
->src
[0] = payload
;
4131 inst
->src
[1] = surface
;
4133 inst
->resize_sources(3);
4135 delete[] components
;
4139 fs_visitor::lower_logical_sends()
4141 bool progress
= false;
4143 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4144 const fs_builder
ibld(this, block
, inst
);
4146 switch (inst
->opcode
) {
4147 case FS_OPCODE_FB_WRITE_LOGICAL
:
4148 assert(stage
== MESA_SHADER_FRAGMENT
);
4149 lower_fb_write_logical_send(ibld
, inst
,
4150 (const brw_wm_prog_data
*)prog_data
,
4151 (const brw_wm_prog_key
*)key
,
4155 case SHADER_OPCODE_TEX_LOGICAL
:
4156 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TEX
);
4159 case SHADER_OPCODE_TXD_LOGICAL
:
4160 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXD
);
4163 case SHADER_OPCODE_TXF_LOGICAL
:
4164 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF
);
4167 case SHADER_OPCODE_TXL_LOGICAL
:
4168 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXL
);
4171 case SHADER_OPCODE_TXS_LOGICAL
:
4172 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXS
);
4175 case FS_OPCODE_TXB_LOGICAL
:
4176 lower_sampler_logical_send(ibld
, inst
, FS_OPCODE_TXB
);
4179 case SHADER_OPCODE_TXF_CMS_LOGICAL
:
4180 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS
);
4183 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
4184 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_CMS_W
);
4187 case SHADER_OPCODE_TXF_UMS_LOGICAL
:
4188 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_UMS
);
4191 case SHADER_OPCODE_TXF_MCS_LOGICAL
:
4192 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TXF_MCS
);
4195 case SHADER_OPCODE_LOD_LOGICAL
:
4196 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_LOD
);
4199 case SHADER_OPCODE_TG4_LOGICAL
:
4200 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4
);
4203 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
:
4204 lower_sampler_logical_send(ibld
, inst
, SHADER_OPCODE_TG4_OFFSET
);
4207 case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
:
4208 lower_surface_logical_send(ibld
, inst
,
4209 SHADER_OPCODE_UNTYPED_SURFACE_READ
,
4213 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
:
4214 lower_surface_logical_send(ibld
, inst
,
4215 SHADER_OPCODE_UNTYPED_SURFACE_WRITE
,
4216 ibld
.sample_mask_reg());
4219 case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
:
4220 lower_surface_logical_send(ibld
, inst
,
4221 SHADER_OPCODE_UNTYPED_ATOMIC
,
4222 ibld
.sample_mask_reg());
4225 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4226 lower_surface_logical_send(ibld
, inst
,
4227 SHADER_OPCODE_TYPED_SURFACE_READ
,
4231 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4232 lower_surface_logical_send(ibld
, inst
,
4233 SHADER_OPCODE_TYPED_SURFACE_WRITE
,
4234 ibld
.sample_mask_reg());
4237 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4238 lower_surface_logical_send(ibld
, inst
,
4239 SHADER_OPCODE_TYPED_ATOMIC
,
4240 ibld
.sample_mask_reg());
4251 invalidate_live_intervals();
4257 * Get the closest native SIMD width supported by the hardware for instruction
4258 * \p inst. The instruction will be left untouched by
4259 * fs_visitor::lower_simd_width() if the returned value is equal to the
4260 * original execution size.
4263 get_lowered_simd_width(const struct brw_device_info
*devinfo
,
4264 const fs_inst
*inst
)
4266 switch (inst
->opcode
) {
4267 case BRW_OPCODE_MOV
:
4268 case BRW_OPCODE_SEL
:
4269 case BRW_OPCODE_NOT
:
4270 case BRW_OPCODE_AND
:
4272 case BRW_OPCODE_XOR
:
4273 case BRW_OPCODE_SHR
:
4274 case BRW_OPCODE_SHL
:
4275 case BRW_OPCODE_ASR
:
4276 case BRW_OPCODE_CMP
:
4277 case BRW_OPCODE_CMPN
:
4278 case BRW_OPCODE_CSEL
:
4279 case BRW_OPCODE_F32TO16
:
4280 case BRW_OPCODE_F16TO32
:
4281 case BRW_OPCODE_BFREV
:
4282 case BRW_OPCODE_BFE
:
4283 case BRW_OPCODE_BFI1
:
4284 case BRW_OPCODE_BFI2
:
4285 case BRW_OPCODE_ADD
:
4286 case BRW_OPCODE_MUL
:
4287 case BRW_OPCODE_AVG
:
4288 case BRW_OPCODE_FRC
:
4289 case BRW_OPCODE_RNDU
:
4290 case BRW_OPCODE_RNDD
:
4291 case BRW_OPCODE_RNDE
:
4292 case BRW_OPCODE_RNDZ
:
4293 case BRW_OPCODE_LZD
:
4294 case BRW_OPCODE_FBH
:
4295 case BRW_OPCODE_FBL
:
4296 case BRW_OPCODE_CBIT
:
4297 case BRW_OPCODE_SAD2
:
4298 case BRW_OPCODE_MAD
:
4299 case BRW_OPCODE_LRP
:
4300 case SHADER_OPCODE_RCP
:
4301 case SHADER_OPCODE_RSQ
:
4302 case SHADER_OPCODE_SQRT
:
4303 case SHADER_OPCODE_EXP2
:
4304 case SHADER_OPCODE_LOG2
:
4305 case SHADER_OPCODE_POW
:
4306 case SHADER_OPCODE_INT_QUOTIENT
:
4307 case SHADER_OPCODE_INT_REMAINDER
:
4308 case SHADER_OPCODE_SIN
:
4309 case SHADER_OPCODE_COS
: {
4310 /* According to the PRMs:
4311 * "A. In Direct Addressing mode, a source cannot span more than 2
4312 * adjacent GRF registers.
4313 * B. A destination cannot span more than 2 adjacent GRF registers."
4315 * Look for the source or destination with the largest register region
4316 * which is the one that is going to limit the overal execution size of
4317 * the instruction due to this rule.
4319 unsigned reg_count
= inst
->regs_written
;
4321 for (unsigned i
= 0; i
< inst
->sources
; i
++)
4322 reg_count
= MAX2(reg_count
, (unsigned)inst
->regs_read(i
));
4324 /* Calculate the maximum execution size of the instruction based on the
4325 * factor by which it goes over the hardware limit of 2 GRFs.
4327 return inst
->exec_size
/ DIV_ROUND_UP(reg_count
, 2);
4329 case SHADER_OPCODE_MULH
:
4330 /* MULH is lowered to the MUL/MACH sequence using the accumulator, which
4331 * is 8-wide on Gen7+.
4333 return (devinfo
->gen
>= 7 ? 8 : inst
->exec_size
);
4335 case FS_OPCODE_FB_WRITE_LOGICAL
:
4336 /* Gen6 doesn't support SIMD16 depth writes but we cannot handle them
4339 assert(devinfo
->gen
!= 6 ||
4340 inst
->src
[FB_WRITE_LOGICAL_SRC_SRC_DEPTH
].file
== BAD_FILE
||
4341 inst
->exec_size
== 8);
4342 /* Dual-source FB writes are unsupported in SIMD16 mode. */
4343 return (inst
->src
[FB_WRITE_LOGICAL_SRC_COLOR1
].file
!= BAD_FILE
?
4344 8 : inst
->exec_size
);
4346 case SHADER_OPCODE_TXD_LOGICAL
:
4347 /* TXD is unsupported in SIMD16 mode. */
4350 case SHADER_OPCODE_TG4_OFFSET_LOGICAL
: {
4351 /* gather4_po_c is unsupported in SIMD16 mode. */
4352 const fs_reg
&shadow_c
= inst
->src
[1];
4353 return (shadow_c
.file
!= BAD_FILE
? 8 : inst
->exec_size
);
4355 case SHADER_OPCODE_TXL_LOGICAL
:
4356 case FS_OPCODE_TXB_LOGICAL
: {
4357 /* Gen4 doesn't have SIMD8 non-shadow-compare bias/LOD instructions, and
4358 * Gen4-6 can't support TXL and TXB with shadow comparison in SIMD16
4359 * mode because the message exceeds the maximum length of 11.
4361 const fs_reg
&shadow_c
= inst
->src
[1];
4362 if (devinfo
->gen
== 4 && shadow_c
.file
== BAD_FILE
)
4364 else if (devinfo
->gen
< 7 && shadow_c
.file
!= BAD_FILE
)
4367 return inst
->exec_size
;
4369 case SHADER_OPCODE_TXF_LOGICAL
:
4370 case SHADER_OPCODE_TXS_LOGICAL
:
4371 /* Gen4 doesn't have SIMD8 variants for the RESINFO and LD-with-LOD
4372 * messages. Use SIMD16 instead.
4374 if (devinfo
->gen
== 4)
4377 return inst
->exec_size
;
4379 case SHADER_OPCODE_TXF_CMS_W_LOGICAL
: {
4380 /* This opcode can take up to 6 arguments which means that in some
4381 * circumstances it can end up with a message that is too long in SIMD16
4384 const unsigned coord_components
= inst
->src
[8].ud
;
4385 /* First three arguments are the sample index and the two arguments for
4388 if ((coord_components
+ 3) * 2 > MAX_SAMPLER_MESSAGE_SIZE
)
4391 return inst
->exec_size
;
4394 case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
:
4395 case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
:
4396 case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
:
4400 return inst
->exec_size
;
4405 * The \p rows array of registers represents a \p num_rows by \p num_columns
4406 * matrix in row-major order, write it in column-major order into the register
4407 * passed as destination. \p stride gives the separation between matrix
4408 * elements in the input in fs_builder::dispatch_width() units.
4411 emit_transpose(const fs_builder
&bld
,
4412 const fs_reg
&dst
, const fs_reg
*rows
,
4413 unsigned num_rows
, unsigned num_columns
, unsigned stride
)
4415 fs_reg
*const components
= new fs_reg
[num_rows
* num_columns
];
4417 for (unsigned i
= 0; i
< num_columns
; ++i
) {
4418 for (unsigned j
= 0; j
< num_rows
; ++j
)
4419 components
[num_rows
* i
+ j
] = offset(rows
[j
], bld
, stride
* i
);
4422 bld
.LOAD_PAYLOAD(dst
, components
, num_rows
* num_columns
, 0);
4424 delete[] components
;
4428 fs_visitor::lower_simd_width()
4430 bool progress
= false;
4432 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
4433 const unsigned lower_width
= get_lowered_simd_width(devinfo
, inst
);
4435 if (lower_width
!= inst
->exec_size
) {
4436 /* Builder matching the original instruction. We may also need to
4437 * emit an instruction of width larger than the original, set the
4438 * execution size of the builder to the highest of both for now so
4439 * we're sure that both cases can be handled.
4441 const fs_builder ibld
= bld
.at(block
, inst
)
4442 .exec_all(inst
->force_writemask_all
)
4443 .group(MAX2(inst
->exec_size
, lower_width
),
4444 inst
->force_sechalf
);
4446 /* Split the copies in chunks of the execution width of either the
4447 * original or the lowered instruction, whichever is lower.
4449 const unsigned copy_width
= MIN2(lower_width
, inst
->exec_size
);
4450 const unsigned n
= inst
->exec_size
/ copy_width
;
4451 const unsigned dst_size
= inst
->regs_written
* REG_SIZE
/
4452 inst
->dst
.component_size(inst
->exec_size
);
4455 assert(n
> 0 && n
<= ARRAY_SIZE(dsts
) &&
4456 !inst
->writes_accumulator
&& !inst
->mlen
);
4458 for (unsigned i
= 0; i
< n
; i
++) {
4459 /* Emit a copy of the original instruction with the lowered width.
4460 * If the EOT flag was set throw it away except for the last
4461 * instruction to avoid killing the thread prematurely.
4463 fs_inst split_inst
= *inst
;
4464 split_inst
.exec_size
= lower_width
;
4465 split_inst
.eot
= inst
->eot
&& i
== n
- 1;
4467 /* Select the correct channel enables for the i-th group, then
4468 * transform the sources and destination and emit the lowered
4471 const fs_builder lbld
= ibld
.group(lower_width
, i
);
4473 for (unsigned j
= 0; j
< inst
->sources
; j
++) {
4474 if (inst
->src
[j
].file
!= BAD_FILE
&&
4475 !is_uniform(inst
->src
[j
])) {
4476 /* Get the i-th copy_width-wide chunk of the source. */
4477 const fs_reg src
= horiz_offset(inst
->src
[j
], copy_width
* i
);
4478 const unsigned src_size
= inst
->components_read(j
);
4480 /* Use a trivial transposition to copy one every n
4481 * copy_width-wide components of the register into a
4482 * temporary passed as source to the lowered instruction.
4484 split_inst
.src
[j
] = lbld
.vgrf(inst
->src
[j
].type
, src_size
);
4485 emit_transpose(lbld
.group(copy_width
, 0),
4486 split_inst
.src
[j
], &src
, 1, src_size
, n
);
4490 if (inst
->regs_written
) {
4491 /* Allocate enough space to hold the result of the lowered
4492 * instruction and fix up the number of registers written.
4494 split_inst
.dst
= dsts
[i
] =
4495 lbld
.vgrf(inst
->dst
.type
, dst_size
);
4496 split_inst
.regs_written
=
4497 DIV_ROUND_UP(inst
->regs_written
* lower_width
,
4501 lbld
.emit(split_inst
);
4504 if (inst
->regs_written
) {
4505 /* Distance between useful channels in the temporaries, skipping
4506 * garbage if the lowered instruction is wider than the original.
4508 const unsigned m
= lower_width
/ copy_width
;
4510 /* Interleave the components of the result from the lowered
4511 * instructions. We need to set exec_all() when copying more than
4512 * one half per component, because LOAD_PAYLOAD (in terms of which
4513 * emit_transpose is implemented) can only use the same channel
4514 * enable signals for all of its non-header sources.
4516 emit_transpose(ibld
.exec_all(inst
->exec_size
> copy_width
)
4517 .group(copy_width
, 0),
4518 inst
->dst
, dsts
, n
, dst_size
, m
);
4521 inst
->remove(block
);
4527 invalidate_live_intervals();
4533 fs_visitor::dump_instructions()
4535 dump_instructions(NULL
);
4539 fs_visitor::dump_instructions(const char *name
)
4541 FILE *file
= stderr
;
4542 if (name
&& geteuid() != 0) {
4543 file
= fopen(name
, "w");
4549 calculate_register_pressure();
4550 int ip
= 0, max_pressure
= 0;
4551 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
4552 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
4553 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
4554 dump_instruction(inst
, file
);
4557 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
4560 foreach_in_list(backend_instruction
, inst
, &instructions
) {
4561 fprintf(file
, "%4d: ", ip
++);
4562 dump_instruction(inst
, file
);
4566 if (file
!= stderr
) {
4572 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
4574 dump_instruction(be_inst
, stderr
);
4578 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
4580 fs_inst
*inst
= (fs_inst
*)be_inst
;
4582 if (inst
->predicate
) {
4583 fprintf(file
, "(%cf0.%d) ",
4584 inst
->predicate_inverse
? '-' : '+',
4588 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
4590 fprintf(file
, ".sat");
4591 if (inst
->conditional_mod
) {
4592 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
4593 if (!inst
->predicate
&&
4594 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
4595 inst
->opcode
!= BRW_OPCODE_IF
&&
4596 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
4597 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
4600 fprintf(file
, "(%d) ", inst
->exec_size
);
4603 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
4606 switch (inst
->dst
.file
) {
4608 fprintf(file
, "vgrf%d", inst
->dst
.nr
);
4609 if (alloc
.sizes
[inst
->dst
.nr
] != inst
->regs_written
||
4610 inst
->dst
.subreg_offset
)
4611 fprintf(file
, "+%d.%d",
4612 inst
->dst
.reg_offset
, inst
->dst
.subreg_offset
);
4615 fprintf(file
, "g%d", inst
->dst
.nr
);
4618 fprintf(file
, "m%d", inst
->dst
.nr
);
4621 fprintf(file
, "(null)");
4624 fprintf(file
, "***u%d***", inst
->dst
.nr
+ inst
->dst
.reg_offset
);
4627 fprintf(file
, "***attr%d***", inst
->dst
.nr
+ inst
->dst
.reg_offset
);
4630 switch (inst
->dst
.nr
) {
4632 fprintf(file
, "null");
4634 case BRW_ARF_ADDRESS
:
4635 fprintf(file
, "a0.%d", inst
->dst
.subnr
);
4637 case BRW_ARF_ACCUMULATOR
:
4638 fprintf(file
, "acc%d", inst
->dst
.subnr
);
4641 fprintf(file
, "f%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
4644 fprintf(file
, "arf%d.%d", inst
->dst
.nr
& 0xf, inst
->dst
.subnr
);
4647 if (inst
->dst
.subnr
)
4648 fprintf(file
, "+%d", inst
->dst
.subnr
);
4651 unreachable("not reached");
4653 fprintf(file
, ":%s, ", brw_reg_type_letters(inst
->dst
.type
));
4655 for (int i
= 0; i
< inst
->sources
; i
++) {
4656 if (inst
->src
[i
].negate
)
4658 if (inst
->src
[i
].abs
)
4660 switch (inst
->src
[i
].file
) {
4662 fprintf(file
, "vgrf%d", inst
->src
[i
].nr
);
4663 if (alloc
.sizes
[inst
->src
[i
].nr
] != (unsigned)inst
->regs_read(i
) ||
4664 inst
->src
[i
].subreg_offset
)
4665 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4666 inst
->src
[i
].subreg_offset
);
4669 fprintf(file
, "g%d", inst
->src
[i
].nr
);
4672 fprintf(file
, "***m%d***", inst
->src
[i
].nr
);
4675 fprintf(file
, "attr%d+%d", inst
->src
[i
].nr
, inst
->src
[i
].reg_offset
);
4678 fprintf(file
, "u%d", inst
->src
[i
].nr
+ inst
->src
[i
].reg_offset
);
4679 if (inst
->src
[i
].reladdr
) {
4680 fprintf(file
, "+reladdr");
4681 } else if (inst
->src
[i
].subreg_offset
) {
4682 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
4683 inst
->src
[i
].subreg_offset
);
4687 fprintf(file
, "(null)");
4690 switch (inst
->src
[i
].type
) {
4691 case BRW_REGISTER_TYPE_F
:
4692 fprintf(file
, "%ff", inst
->src
[i
].f
);
4694 case BRW_REGISTER_TYPE_W
:
4695 case BRW_REGISTER_TYPE_D
:
4696 fprintf(file
, "%dd", inst
->src
[i
].d
);
4698 case BRW_REGISTER_TYPE_UW
:
4699 case BRW_REGISTER_TYPE_UD
:
4700 fprintf(file
, "%uu", inst
->src
[i
].ud
);
4702 case BRW_REGISTER_TYPE_VF
:
4703 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
4704 brw_vf_to_float((inst
->src
[i
].ud
>> 0) & 0xff),
4705 brw_vf_to_float((inst
->src
[i
].ud
>> 8) & 0xff),
4706 brw_vf_to_float((inst
->src
[i
].ud
>> 16) & 0xff),
4707 brw_vf_to_float((inst
->src
[i
].ud
>> 24) & 0xff));
4710 fprintf(file
, "???");
4715 switch (inst
->src
[i
].nr
) {
4717 fprintf(file
, "null");
4719 case BRW_ARF_ADDRESS
:
4720 fprintf(file
, "a0.%d", inst
->src
[i
].subnr
);
4722 case BRW_ARF_ACCUMULATOR
:
4723 fprintf(file
, "acc%d", inst
->src
[i
].subnr
);
4726 fprintf(file
, "f%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
4729 fprintf(file
, "arf%d.%d", inst
->src
[i
].nr
& 0xf, inst
->src
[i
].subnr
);
4732 if (inst
->src
[i
].subnr
)
4733 fprintf(file
, "+%d", inst
->src
[i
].subnr
);
4736 if (inst
->src
[i
].abs
)
4739 if (inst
->src
[i
].file
!= IMM
) {
4740 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
4743 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
4744 fprintf(file
, ", ");
4749 if (inst
->force_writemask_all
)
4750 fprintf(file
, "NoMask ");
4752 if (dispatch_width
== 16 && inst
->exec_size
== 8) {
4753 if (inst
->force_sechalf
)
4754 fprintf(file
, "2ndhalf ");
4756 fprintf(file
, "1sthalf ");
4759 fprintf(file
, "\n");
4763 * Possibly returns an instruction that set up @param reg.
4765 * Sometimes we want to take the result of some expression/variable
4766 * dereference tree and rewrite the instruction generating the result
4767 * of the tree. When processing the tree, we know that the
4768 * instructions generated are all writing temporaries that are dead
4769 * outside of this tree. So, if we have some instructions that write
4770 * a temporary, we're free to point that temp write somewhere else.
4772 * Note that this doesn't guarantee that the instruction generated
4773 * only reg -- it might be the size=4 destination of a texture instruction.
4776 fs_visitor::get_instruction_generating_reg(fs_inst
*start
,
4781 end
->is_partial_write() ||
4783 !reg
.equals(end
->dst
)) {
4791 fs_visitor::setup_payload_gen6()
4794 (nir
->info
.inputs_read
& (1 << VARYING_SLOT_POS
)) != 0;
4795 unsigned barycentric_interp_modes
=
4796 (stage
== MESA_SHADER_FRAGMENT
) ?
4797 ((brw_wm_prog_data
*) this->prog_data
)->barycentric_interp_modes
: 0;
4799 assert(devinfo
->gen
>= 6);
4801 /* R0-1: masks, pixel X/Y coordinates. */
4802 payload
.num_regs
= 2;
4803 /* R2: only for 32-pixel dispatch.*/
4805 /* R3-26: barycentric interpolation coordinates. These appear in the
4806 * same order that they appear in the brw_wm_barycentric_interp_mode
4807 * enum. Each set of coordinates occupies 2 registers if dispatch width
4808 * == 8 and 4 registers if dispatch width == 16. Coordinates only
4809 * appear if they were enabled using the "Barycentric Interpolation
4810 * Mode" bits in WM_STATE.
4812 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
4813 if (barycentric_interp_modes
& (1 << i
)) {
4814 payload
.barycentric_coord_reg
[i
] = payload
.num_regs
;
4815 payload
.num_regs
+= 2;
4816 if (dispatch_width
== 16) {
4817 payload
.num_regs
+= 2;
4822 /* R27: interpolated depth if uses source depth */
4824 payload
.source_depth_reg
= payload
.num_regs
;
4826 if (dispatch_width
== 16) {
4827 /* R28: interpolated depth if not SIMD8. */
4831 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
4833 payload
.source_w_reg
= payload
.num_regs
;
4835 if (dispatch_width
== 16) {
4836 /* R30: interpolated W if not SIMD8. */
4841 if (stage
== MESA_SHADER_FRAGMENT
) {
4842 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4843 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
4844 prog_data
->uses_pos_offset
= key
->compute_pos_offset
;
4845 /* R31: MSAA position offsets. */
4846 if (prog_data
->uses_pos_offset
) {
4847 payload
.sample_pos_reg
= payload
.num_regs
;
4852 /* R32: MSAA input coverage mask */
4853 if (nir
->info
.system_values_read
& SYSTEM_BIT_SAMPLE_MASK_IN
) {
4854 assert(devinfo
->gen
>= 7);
4855 payload
.sample_mask_in_reg
= payload
.num_regs
;
4857 if (dispatch_width
== 16) {
4858 /* R33: input coverage mask if not SIMD8. */
4863 /* R34-: bary for 32-pixel. */
4864 /* R58-59: interp W for 32-pixel. */
4866 if (nir
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
4867 source_depth_to_render_target
= true;
4872 fs_visitor::setup_vs_payload()
4874 /* R0: thread header, R1: urb handles */
4875 payload
.num_regs
= 2;
4879 * We are building the local ID push constant data using the simplest possible
4880 * method. We simply push the local IDs directly as they should appear in the
4881 * registers for the uvec3 gl_LocalInvocationID variable.
4883 * Therefore, for SIMD8, we use 3 full registers, and for SIMD16 we use 6
4884 * registers worth of push constant space.
4886 * Note: Any updates to brw_cs_prog_local_id_payload_dwords,
4887 * fill_local_id_payload or fs_visitor::emit_cs_local_invocation_id_setup need
4890 * FINISHME: There are a few easy optimizations to consider.
4892 * 1. If gl_WorkGroupSize x, y or z is 1, we can just use zero, and there is
4893 * no need for using push constant space for that dimension.
4895 * 2. Since GL_MAX_COMPUTE_WORK_GROUP_SIZE is currently 1024 or less, we can
4896 * easily use 16-bit words rather than 32-bit dwords in the push constant
4899 * 3. If gl_WorkGroupSize x, y or z is small, then we can use bytes for
4900 * conveying the data, and thereby reduce push constant usage.
4904 fs_visitor::setup_gs_payload()
4906 assert(stage
== MESA_SHADER_GEOMETRY
);
4908 struct brw_gs_prog_data
*gs_prog_data
=
4909 (struct brw_gs_prog_data
*) prog_data
;
4910 struct brw_vue_prog_data
*vue_prog_data
=
4911 (struct brw_vue_prog_data
*) prog_data
;
4913 /* R0: thread header, R1: output URB handles */
4914 payload
.num_regs
= 2;
4916 if (gs_prog_data
->include_primitive_id
) {
4917 /* R2: Primitive ID 0..7 */
4921 /* Use a maximum of 32 registers for push-model inputs. */
4922 const unsigned max_push_components
= 32;
4924 /* If pushing our inputs would take too many registers, reduce the URB read
4925 * length (which is in HWords, or 8 registers), and resort to pulling.
4927 * Note that the GS reads <URB Read Length> HWords for every vertex - so we
4928 * have to multiply by VerticesIn to obtain the total storage requirement.
4930 if (8 * vue_prog_data
->urb_read_length
* nir
->info
.gs
.vertices_in
>
4931 max_push_components
) {
4932 gs_prog_data
->base
.include_vue_handles
= true;
4934 /* R3..RN: ICP Handles for each incoming vertex (when using pull model) */
4935 payload
.num_regs
+= nir
->info
.gs
.vertices_in
;
4937 vue_prog_data
->urb_read_length
=
4938 ROUND_DOWN_TO(max_push_components
/ nir
->info
.gs
.vertices_in
, 8) / 8;
4943 fs_visitor::setup_cs_payload()
4945 assert(devinfo
->gen
>= 7);
4946 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
4948 payload
.num_regs
= 1;
4950 if (nir
->info
.system_values_read
& SYSTEM_BIT_LOCAL_INVOCATION_ID
) {
4951 prog_data
->local_invocation_id_regs
= dispatch_width
* 3 / 8;
4952 payload
.local_invocation_id_reg
= payload
.num_regs
;
4953 payload
.num_regs
+= prog_data
->local_invocation_id_regs
;
4958 fs_visitor::calculate_register_pressure()
4960 invalidate_live_intervals();
4961 calculate_live_intervals();
4963 unsigned num_instructions
= 0;
4964 foreach_block(block
, cfg
)
4965 num_instructions
+= block
->instructions
.length();
4967 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
4969 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
4970 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
4971 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
4976 fs_visitor::optimize()
4978 /* Start by validating the shader we currently have. */
4981 /* bld is the common builder object pointing at the end of the program we
4982 * used to translate it into i965 IR. For the optimization and lowering
4983 * passes coming next, any code added after the end of the program without
4984 * having explicitly called fs_builder::at() clearly points at a mistake.
4985 * Ideally optimization passes wouldn't be part of the visitor so they
4986 * wouldn't have access to bld at all, but they do, so just in case some
4987 * pass forgets to ask for a location explicitly set it to NULL here to
4988 * make it trip. The dispatch width is initialized to a bogus value to
4989 * make sure that optimizations set the execution controls explicitly to
4990 * match the code they are manipulating instead of relying on the defaults.
4992 bld
= fs_builder(this, 64);
4994 assign_constant_locations();
4995 demote_pull_constants();
4999 split_virtual_grfs();
5002 #define OPT(pass, args...) ({ \
5004 bool this_progress = pass(args); \
5006 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
5007 char filename[64]; \
5008 snprintf(filename, 64, "%s%d-%s-%02d-%02d-" #pass, \
5009 stage_abbrev, dispatch_width, nir->info.name, iteration, pass_num); \
5011 backend_shader::dump_instructions(filename); \
5016 progress = progress || this_progress; \
5020 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
5022 snprintf(filename
, 64, "%s%d-%s-00-start",
5023 stage_abbrev
, dispatch_width
, nir
->info
.name
);
5025 backend_shader::dump_instructions(filename
);
5028 bool progress
= false;
5032 OPT(lower_simd_width
);
5033 OPT(lower_logical_sends
);
5040 OPT(remove_duplicate_mrf_writes
);
5044 OPT(opt_copy_propagate
);
5045 OPT(opt_predicated_break
, this);
5046 OPT(opt_cmod_propagation
);
5047 OPT(dead_code_eliminate
);
5048 OPT(opt_peephole_sel
);
5049 OPT(dead_control_flow_eliminate
, this);
5050 OPT(opt_register_renaming
);
5051 OPT(opt_redundant_discard_jumps
);
5052 OPT(opt_saturate_propagation
);
5053 OPT(opt_zero_samples
);
5054 OPT(register_coalesce
);
5055 OPT(compute_to_mrf
);
5056 OPT(eliminate_find_live_channel
);
5058 OPT(compact_virtual_grfs
);
5063 OPT(opt_sampler_eot
);
5065 if (OPT(lower_load_payload
)) {
5066 split_virtual_grfs();
5067 OPT(register_coalesce
);
5068 OPT(compute_to_mrf
);
5069 OPT(dead_code_eliminate
);
5072 OPT(opt_combine_constants
);
5073 OPT(lower_integer_multiplication
);
5075 lower_uniform_pull_constant_loads();
5081 * Three source instruction must have a GRF/MRF destination register.
5082 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
5085 fs_visitor::fixup_3src_null_dest()
5087 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
5088 if (inst
->is_3src() && inst
->dst
.is_null()) {
5089 inst
->dst
= fs_reg(VGRF
, alloc
.allocate(dispatch_width
/ 8),
5096 fs_visitor::allocate_registers()
5098 bool allocated_without_spills
;
5100 static const enum instruction_scheduler_mode pre_modes
[] = {
5102 SCHEDULE_PRE_NON_LIFO
,
5106 /* Try each scheduling heuristic to see if it can successfully register
5107 * allocate without spilling. They should be ordered by decreasing
5108 * performance but increasing likelihood of allocating.
5110 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
5111 schedule_instructions(pre_modes
[i
]);
5114 assign_regs_trivial();
5115 allocated_without_spills
= true;
5117 allocated_without_spills
= assign_regs(false);
5119 if (allocated_without_spills
)
5123 if (!allocated_without_spills
) {
5124 /* We assume that any spilling is worse than just dropping back to
5125 * SIMD8. There's probably actually some intermediate point where
5126 * SIMD16 with a couple of spills is still better.
5128 if (dispatch_width
== 16) {
5129 fail("Failure to register allocate. Reduce number of "
5130 "live scalar values to avoid this.");
5132 compiler
->shader_perf_log(log_data
,
5133 "%s shader triggered register spilling. "
5134 "Try reducing the number of live scalar "
5135 "values to improve performance.\n",
5139 /* Since we're out of heuristics, just go spill registers until we
5140 * get an allocation.
5142 while (!assign_regs(true)) {
5148 /* This must come after all optimization and register allocation, since
5149 * it inserts dead code that happens to have side effects, and it does
5150 * so based on the actual physical registers in use.
5152 insert_gen4_send_dependency_workarounds();
5157 schedule_instructions(SCHEDULE_POST
);
5159 if (last_scratch
> 0)
5160 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
5164 fs_visitor::run_vs(gl_clip_plane
*clip_planes
)
5166 assert(stage
== MESA_SHADER_VERTEX
);
5170 if (shader_time_index
>= 0)
5171 emit_shader_time_begin();
5178 compute_clip_distance(clip_planes
);
5182 if (shader_time_index
>= 0)
5183 emit_shader_time_end();
5189 assign_curb_setup();
5190 assign_vs_urb_setup();
5192 fixup_3src_null_dest();
5193 allocate_registers();
5199 fs_visitor::run_gs()
5201 assert(stage
== MESA_SHADER_GEOMETRY
);
5205 this->final_gs_vertex_count
= vgrf(glsl_type::uint_type
);
5207 if (gs_compile
->control_data_header_size_bits
> 0) {
5208 /* Create a VGRF to store accumulated control data bits. */
5209 this->control_data_bits
= vgrf(glsl_type::uint_type
);
5211 /* If we're outputting more than 32 control data bits, then EmitVertex()
5212 * will set control_data_bits to 0 after emitting the first vertex.
5213 * Otherwise, we need to initialize it to 0 here.
5215 if (gs_compile
->control_data_header_size_bits
<= 32) {
5216 const fs_builder abld
= bld
.annotate("initialize control data bits");
5217 abld
.MOV(this->control_data_bits
, fs_reg(0u));
5221 if (shader_time_index
>= 0)
5222 emit_shader_time_begin();
5226 emit_gs_thread_end();
5228 if (shader_time_index
>= 0)
5229 emit_shader_time_end();
5238 assign_curb_setup();
5239 assign_gs_urb_setup();
5241 fixup_3src_null_dest();
5242 allocate_registers();
5248 fs_visitor::run_fs(bool do_rep_send
)
5250 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
5251 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
5253 assert(stage
== MESA_SHADER_FRAGMENT
);
5255 if (devinfo
->gen
>= 6)
5256 setup_payload_gen6();
5258 setup_payload_gen4();
5262 } else if (do_rep_send
) {
5263 assert(dispatch_width
== 16);
5264 emit_repclear_shader();
5266 if (shader_time_index
>= 0)
5267 emit_shader_time_begin();
5269 calculate_urb_setup();
5270 if (nir
->info
.inputs_read
> 0) {
5271 if (devinfo
->gen
< 6)
5272 emit_interpolation_setup_gen4();
5274 emit_interpolation_setup_gen6();
5277 /* We handle discards by keeping track of the still-live pixels in f0.1.
5278 * Initialize it with the dispatched pixels.
5280 if (wm_prog_data
->uses_kill
) {
5281 fs_inst
*discard_init
= bld
.emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
5282 discard_init
->flag_subreg
= 1;
5285 /* Generate FS IR for main(). (the visitor only descends into
5286 * functions called "main").
5293 if (wm_prog_data
->uses_kill
)
5294 bld
.emit(FS_OPCODE_PLACEHOLDER_HALT
);
5296 if (wm_key
->alpha_test_func
)
5301 if (shader_time_index
>= 0)
5302 emit_shader_time_end();
5308 assign_curb_setup();
5311 fixup_3src_null_dest();
5312 allocate_registers();
5318 if (dispatch_width
== 8)
5319 wm_prog_data
->reg_blocks
= brw_register_blocks(grf_used
);
5321 wm_prog_data
->reg_blocks_16
= brw_register_blocks(grf_used
);
5327 fs_visitor::run_cs()
5329 assert(stage
== MESA_SHADER_COMPUTE
);
5333 if (shader_time_index
>= 0)
5334 emit_shader_time_begin();
5341 emit_cs_terminate();
5343 if (shader_time_index
>= 0)
5344 emit_shader_time_end();
5350 assign_curb_setup();
5352 fixup_3src_null_dest();
5353 allocate_registers();
5362 * Return a bitfield where bit n is set if barycentric interpolation mode n
5363 * (see enum brw_wm_barycentric_interp_mode) is needed by the fragment shader.
5366 brw_compute_barycentric_interp_modes(const struct brw_device_info
*devinfo
,
5367 bool shade_model_flat
,
5368 bool persample_shading
,
5369 const nir_shader
*shader
)
5371 unsigned barycentric_interp_modes
= 0;
5373 nir_foreach_variable(var
, &shader
->inputs
) {
5374 enum glsl_interp_qualifier interp_qualifier
=
5375 (enum glsl_interp_qualifier
)var
->data
.interpolation
;
5376 bool is_centroid
= var
->data
.centroid
&& !persample_shading
;
5377 bool is_sample
= var
->data
.sample
|| persample_shading
;
5378 bool is_gl_Color
= (var
->data
.location
== VARYING_SLOT_COL0
) ||
5379 (var
->data
.location
== VARYING_SLOT_COL1
);
5381 /* Ignore WPOS and FACE, because they don't require interpolation. */
5382 if (var
->data
.location
== VARYING_SLOT_POS
||
5383 var
->data
.location
== VARYING_SLOT_FACE
)
5386 /* Determine the set (or sets) of barycentric coordinates needed to
5387 * interpolate this variable. Note that when
5388 * brw->needs_unlit_centroid_workaround is set, centroid interpolation
5389 * uses PIXEL interpolation for unlit pixels and CENTROID interpolation
5390 * for lit pixels, so we need both sets of barycentric coordinates.
5392 if (interp_qualifier
== INTERP_QUALIFIER_NOPERSPECTIVE
) {
5394 barycentric_interp_modes
|=
5395 1 << BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
5396 } else if (is_sample
) {
5397 barycentric_interp_modes
|=
5398 1 << BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
5400 if ((!is_centroid
&& !is_sample
) ||
5401 devinfo
->needs_unlit_centroid_workaround
) {
5402 barycentric_interp_modes
|=
5403 1 << BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
5405 } else if (interp_qualifier
== INTERP_QUALIFIER_SMOOTH
||
5406 (!(shade_model_flat
&& is_gl_Color
) &&
5407 interp_qualifier
== INTERP_QUALIFIER_NONE
)) {
5409 barycentric_interp_modes
|=
5410 1 << BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
5411 } else if (is_sample
) {
5412 barycentric_interp_modes
|=
5413 1 << BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
5415 if ((!is_centroid
&& !is_sample
) ||
5416 devinfo
->needs_unlit_centroid_workaround
) {
5417 barycentric_interp_modes
|=
5418 1 << BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
5423 return barycentric_interp_modes
;
5427 computed_depth_mode(const nir_shader
*shader
)
5429 if (shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
5430 switch (shader
->info
.fs
.depth_layout
) {
5431 case FRAG_DEPTH_LAYOUT_NONE
:
5432 case FRAG_DEPTH_LAYOUT_ANY
:
5433 return BRW_PSCDEPTH_ON
;
5434 case FRAG_DEPTH_LAYOUT_GREATER
:
5435 return BRW_PSCDEPTH_ON_GE
;
5436 case FRAG_DEPTH_LAYOUT_LESS
:
5437 return BRW_PSCDEPTH_ON_LE
;
5438 case FRAG_DEPTH_LAYOUT_UNCHANGED
:
5439 return BRW_PSCDEPTH_OFF
;
5442 return BRW_PSCDEPTH_OFF
;
5446 brw_compile_fs(const struct brw_compiler
*compiler
, void *log_data
,
5448 const struct brw_wm_prog_key
*key
,
5449 struct brw_wm_prog_data
*prog_data
,
5450 const nir_shader
*shader
,
5451 struct gl_program
*prog
,
5452 int shader_time_index8
, int shader_time_index16
,
5454 unsigned *final_assembly_size
,
5457 /* key->alpha_test_func means simulating alpha testing via discards,
5458 * so the shader definitely kills pixels.
5460 prog_data
->uses_kill
= shader
->info
.fs
.uses_discard
|| key
->alpha_test_func
;
5461 prog_data
->uses_omask
=
5462 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
);
5463 prog_data
->computed_depth_mode
= computed_depth_mode(shader
);
5464 prog_data
->computed_stencil
=
5465 shader
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_STENCIL
);
5467 prog_data
->early_fragment_tests
= shader
->info
.fs
.early_fragment_tests
;
5469 prog_data
->barycentric_interp_modes
=
5470 brw_compute_barycentric_interp_modes(compiler
->devinfo
,
5472 key
->persample_shading
,
5475 fs_visitor
v(compiler
, log_data
, mem_ctx
, key
,
5476 &prog_data
->base
, prog
, shader
, 8,
5477 shader_time_index8
);
5478 if (!v
.run_fs(false /* do_rep_send */)) {
5480 *error_str
= ralloc_strdup(mem_ctx
, v
.fail_msg
);
5485 cfg_t
*simd16_cfg
= NULL
;
5486 fs_visitor
v2(compiler
, log_data
, mem_ctx
, key
,
5487 &prog_data
->base
, prog
, shader
, 16,
5488 shader_time_index16
);
5489 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
) || use_rep_send
)) {
5490 if (!v
.simd16_unsupported
) {
5491 /* Try a SIMD16 compile */
5492 v2
.import_uniforms(&v
);
5493 if (!v2
.run_fs(use_rep_send
)) {
5494 compiler
->shader_perf_log(log_data
,
5495 "SIMD16 shader failed to compile: %s",
5498 simd16_cfg
= v2
.cfg
;
5504 int no_simd8
= (INTEL_DEBUG
& DEBUG_NO8
) || use_rep_send
;
5505 if ((no_simd8
|| compiler
->devinfo
->gen
< 5) && simd16_cfg
) {
5507 prog_data
->no_8
= true;
5510 prog_data
->no_8
= false;
5513 fs_generator
g(compiler
, log_data
, mem_ctx
, (void *) key
, &prog_data
->base
,
5514 v
.promoted_constants
, v
.runtime_check_aads_emit
, "FS");
5516 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
5517 g
.enable_debug(ralloc_asprintf(mem_ctx
, "%s fragment shader %s",
5518 shader
->info
.label
? shader
->info
.label
:
5520 shader
->info
.name
));
5524 g
.generate_code(simd8_cfg
, 8);
5526 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
5528 return g
.get_assembly(final_assembly_size
);
5532 brw_cs_fill_local_id_payload(const struct brw_cs_prog_data
*prog_data
,
5533 void *buffer
, uint32_t threads
, uint32_t stride
)
5535 if (prog_data
->local_invocation_id_regs
== 0)
5538 /* 'stride' should be an integer number of registers, that is, a multiple
5541 assert(stride
% 32 == 0);
5543 unsigned x
= 0, y
= 0, z
= 0;
5544 for (unsigned t
= 0; t
< threads
; t
++) {
5545 uint32_t *param
= (uint32_t *) buffer
+ stride
* t
/ 4;
5547 for (unsigned i
= 0; i
< prog_data
->simd_size
; i
++) {
5548 param
[0 * prog_data
->simd_size
+ i
] = x
;
5549 param
[1 * prog_data
->simd_size
+ i
] = y
;
5550 param
[2 * prog_data
->simd_size
+ i
] = z
;
5553 if (x
== prog_data
->local_size
[0]) {
5556 if (y
== prog_data
->local_size
[1]) {
5559 if (z
== prog_data
->local_size
[2])
5568 fs_visitor::emit_cs_local_invocation_id_setup()
5570 assert(stage
== MESA_SHADER_COMPUTE
);
5572 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5574 struct brw_reg src
=
5575 brw_vec8_grf(payload
.local_invocation_id_reg
, 0);
5576 src
= retype(src
, BRW_REGISTER_TYPE_UD
);
5578 src
.nr
+= dispatch_width
/ 8;
5579 bld
.MOV(offset(*reg
, bld
, 1), src
);
5580 src
.nr
+= dispatch_width
/ 8;
5581 bld
.MOV(offset(*reg
, bld
, 2), src
);
5587 fs_visitor::emit_cs_work_group_id_setup()
5589 assert(stage
== MESA_SHADER_COMPUTE
);
5591 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::uvec3_type
));
5593 struct brw_reg
r0_1(retype(brw_vec1_grf(0, 1), BRW_REGISTER_TYPE_UD
));
5594 struct brw_reg
r0_6(retype(brw_vec1_grf(0, 6), BRW_REGISTER_TYPE_UD
));
5595 struct brw_reg
r0_7(retype(brw_vec1_grf(0, 7), BRW_REGISTER_TYPE_UD
));
5597 bld
.MOV(*reg
, r0_1
);
5598 bld
.MOV(offset(*reg
, bld
, 1), r0_6
);
5599 bld
.MOV(offset(*reg
, bld
, 2), r0_7
);
5605 brw_compile_cs(const struct brw_compiler
*compiler
, void *log_data
,
5607 const struct brw_cs_prog_key
*key
,
5608 struct brw_cs_prog_data
*prog_data
,
5609 const nir_shader
*shader
,
5610 int shader_time_index
,
5611 unsigned *final_assembly_size
,
5614 prog_data
->local_size
[0] = shader
->info
.cs
.local_size
[0];
5615 prog_data
->local_size
[1] = shader
->info
.cs
.local_size
[1];
5616 prog_data
->local_size
[2] = shader
->info
.cs
.local_size
[2];
5617 unsigned local_workgroup_size
=
5618 shader
->info
.cs
.local_size
[0] * shader
->info
.cs
.local_size
[1] *
5619 shader
->info
.cs
.local_size
[2];
5621 unsigned max_cs_threads
= compiler
->devinfo
->max_cs_threads
;
5624 const char *fail_msg
= NULL
;
5626 /* Now the main event: Visit the shader IR and generate our CS IR for it.
5628 fs_visitor
v8(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5629 NULL
, /* Never used in core profile */
5630 shader
, 8, shader_time_index
);
5632 fail_msg
= v8
.fail_msg
;
5633 } else if (local_workgroup_size
<= 8 * max_cs_threads
) {
5635 prog_data
->simd_size
= 8;
5638 fs_visitor
v16(compiler
, log_data
, mem_ctx
, key
, &prog_data
->base
,
5639 NULL
, /* Never used in core profile */
5640 shader
, 16, shader_time_index
);
5641 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
)) &&
5642 !fail_msg
&& !v8
.simd16_unsupported
&&
5643 local_workgroup_size
<= 16 * max_cs_threads
) {
5644 /* Try a SIMD16 compile */
5645 v16
.import_uniforms(&v8
);
5646 if (!v16
.run_cs()) {
5647 compiler
->shader_perf_log(log_data
,
5648 "SIMD16 shader failed to compile: %s",
5652 "Couldn't generate SIMD16 program and not "
5653 "enough threads for SIMD8";
5657 prog_data
->simd_size
= 16;
5661 if (unlikely(cfg
== NULL
)) {
5664 *error_str
= ralloc_strdup(mem_ctx
, fail_msg
);
5669 fs_generator
g(compiler
, log_data
, mem_ctx
, (void*) key
, &prog_data
->base
,
5670 v8
.promoted_constants
, v8
.runtime_check_aads_emit
, "CS");
5671 if (INTEL_DEBUG
& DEBUG_CS
) {
5672 char *name
= ralloc_asprintf(mem_ctx
, "%s compute shader %s",
5673 shader
->info
.label
? shader
->info
.label
:
5676 g
.enable_debug(name
);
5679 g
.generate_code(cfg
, prog_data
->simd_size
);
5681 return g
.get_assembly(final_assembly_size
);