2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * This file drives the GLSL IR -> LIR translation, contains the
27 * optimizations on the LIR, and drives the generation of native code
31 #include <sys/types.h>
33 #include "util/hash_table.h"
34 #include "main/macros.h"
35 #include "main/shaderobj.h"
36 #include "main/fbobject.h"
37 #include "program/prog_parameter.h"
38 #include "program/prog_print.h"
39 #include "util/register_allocate.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
46 #include "brw_dead_control_flow.h"
47 #include "main/uniforms.h"
48 #include "brw_fs_live_variables.h"
49 #include "glsl/glsl_types.h"
50 #include "program/sampler.h"
53 fs_inst::init(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
54 const fs_reg
*src
, unsigned sources
)
56 memset(this, 0, sizeof(*this));
58 this->src
= new fs_reg
[MAX2(sources
, 3)];
59 for (unsigned i
= 0; i
< sources
; i
++)
60 this->src
[i
] = src
[i
];
62 this->opcode
= opcode
;
64 this->sources
= sources
;
65 this->exec_size
= exec_size
;
67 assert(dst
.file
!= IMM
&& dst
.file
!= UNIFORM
);
69 /* If exec_size == 0, try to guess it from the registers. Since all
70 * manner of things may use hardware registers, we first try to guess
71 * based on GRF registers. If this fails, we will go ahead and take the
72 * width from the destination register.
74 if (this->exec_size
== 0) {
75 if (dst
.file
== GRF
) {
76 this->exec_size
= dst
.width
;
78 for (unsigned i
= 0; i
< sources
; ++i
) {
79 if (src
[i
].file
!= GRF
&& src
[i
].file
!= ATTR
)
82 if (this->exec_size
<= 1)
83 this->exec_size
= src
[i
].width
;
84 assert(src
[i
].width
== 1 || src
[i
].width
== this->exec_size
);
88 if (this->exec_size
== 0 && dst
.file
!= BAD_FILE
)
89 this->exec_size
= dst
.width
;
91 assert(this->exec_size
!= 0);
93 this->conditional_mod
= BRW_CONDITIONAL_NONE
;
95 /* This will be the case for almost all instructions. */
102 DIV_ROUND_UP(MAX2(dst
.width
* dst
.stride
, 1) * type_sz(dst
.type
), 32);
105 this->regs_written
= 0;
109 unreachable("Invalid destination register file");
111 unreachable("Invalid register file");
114 this->writes_accumulator
= false;
119 init(BRW_OPCODE_NOP
, 8, dst
, NULL
, 0);
122 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
)
124 init(opcode
, exec_size
, reg_undef
, NULL
, 0);
127 fs_inst::fs_inst(enum opcode opcode
, const fs_reg
&dst
)
129 init(opcode
, 0, dst
, NULL
, 0);
132 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
135 const fs_reg src
[1] = { src0
};
136 init(opcode
, exec_size
, dst
, src
, 1);
139 fs_inst::fs_inst(enum opcode opcode
, const fs_reg
&dst
, const fs_reg
&src0
)
141 const fs_reg src
[1] = { src0
};
142 init(opcode
, 0, dst
, src
, 1);
145 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
146 const fs_reg
&src0
, const fs_reg
&src1
)
148 const fs_reg src
[2] = { src0
, src1
};
149 init(opcode
, exec_size
, dst
, src
, 2);
152 fs_inst::fs_inst(enum opcode opcode
, const fs_reg
&dst
, const fs_reg
&src0
,
155 const fs_reg src
[2] = { src0
, src1
};
156 init(opcode
, 0, dst
, src
, 2);
159 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
160 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
)
162 const fs_reg src
[3] = { src0
, src1
, src2
};
163 init(opcode
, exec_size
, dst
, src
, 3);
166 fs_inst::fs_inst(enum opcode opcode
, const fs_reg
&dst
, const fs_reg
&src0
,
167 const fs_reg
&src1
, const fs_reg
&src2
)
169 const fs_reg src
[3] = { src0
, src1
, src2
};
170 init(opcode
, 0, dst
, src
, 3);
173 fs_inst::fs_inst(enum opcode opcode
, const fs_reg
&dst
,
174 const fs_reg src
[], unsigned sources
)
176 init(opcode
, 0, dst
, src
, sources
);
179 fs_inst::fs_inst(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
180 const fs_reg src
[], unsigned sources
)
182 init(opcode
, exec_width
, dst
, src
, sources
);
185 fs_inst::fs_inst(const fs_inst
&that
)
187 memcpy(this, &that
, sizeof(that
));
189 this->src
= new fs_reg
[MAX2(that
.sources
, 3)];
191 for (unsigned i
= 0; i
< that
.sources
; i
++)
192 this->src
[i
] = that
.src
[i
];
201 fs_inst::resize_sources(uint8_t num_sources
)
203 if (this->sources
!= num_sources
) {
204 fs_reg
*src
= new fs_reg
[MAX2(num_sources
, 3)];
206 for (unsigned i
= 0; i
< MIN2(this->sources
, num_sources
); ++i
)
207 src
[i
] = this->src
[i
];
211 this->sources
= num_sources
;
217 fs_visitor::op(const fs_reg &dst, const fs_reg &src0) \
219 return new(mem_ctx) fs_inst(BRW_OPCODE_##op, dst, src0); \
224 fs_visitor::op(const fs_reg &dst, const fs_reg &src0, \
225 const fs_reg &src1) \
227 return new(mem_ctx) fs_inst(BRW_OPCODE_##op, dst, src0, src1); \
230 #define ALU2_ACC(op) \
232 fs_visitor::op(const fs_reg &dst, const fs_reg &src0, \
233 const fs_reg &src1) \
235 fs_inst *inst = new(mem_ctx) fs_inst(BRW_OPCODE_##op, dst, src0, src1);\
236 inst->writes_accumulator = true; \
242 fs_visitor::op(const fs_reg &dst, const fs_reg &src0, \
243 const fs_reg &src1, const fs_reg &src2) \
245 return new(mem_ctx) fs_inst(BRW_OPCODE_##op, dst, src0, src1, src2);\
277 /** Gen4 predicated IF. */
279 fs_visitor::IF(enum brw_predicate predicate
)
281 fs_inst
*inst
= new(mem_ctx
) fs_inst(BRW_OPCODE_IF
, dispatch_width
);
282 inst
->predicate
= predicate
;
286 /** Gen6 IF with embedded comparison. */
288 fs_visitor::IF(const fs_reg
&src0
, const fs_reg
&src1
,
289 enum brw_conditional_mod condition
)
291 assert(devinfo
->gen
== 6);
292 fs_inst
*inst
= new(mem_ctx
) fs_inst(BRW_OPCODE_IF
, dispatch_width
,
293 reg_null_d
, src0
, src1
);
294 inst
->conditional_mod
= condition
;
299 * CMP: Sets the low bit of the destination channels with the result
300 * of the comparison, while the upper bits are undefined, and updates
301 * the flag register with the packed 16 bits of the result.
304 fs_visitor::CMP(fs_reg dst
, fs_reg src0
, fs_reg src1
,
305 enum brw_conditional_mod condition
)
309 /* Take the instruction:
311 * CMP null<d> src0<f> src1<f>
313 * Original gen4 does type conversion to the destination type before
314 * comparison, producing garbage results for floating point comparisons.
316 * The destination type doesn't matter on newer generations, so we set the
317 * type to match src0 so we can compact the instruction.
319 dst
.type
= src0
.type
;
320 if (dst
.file
== HW_REG
)
321 dst
.fixed_hw_reg
.type
= dst
.type
;
323 resolve_ud_negate(&src0
);
324 resolve_ud_negate(&src1
);
326 inst
= new(mem_ctx
) fs_inst(BRW_OPCODE_CMP
, dst
, src0
, src1
);
327 inst
->conditional_mod
= condition
;
333 fs_visitor::LOAD_PAYLOAD(const fs_reg
&dst
, fs_reg
*src
, int sources
,
336 assert(dst
.width
% 8 == 0);
337 fs_inst
*inst
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
, dst
.width
,
339 inst
->header_size
= header_size
;
341 for (int i
= 0; i
< header_size
; i
++)
342 assert(src
[i
].file
!= GRF
|| src
[i
].width
* type_sz(src
[i
].type
) == 32);
343 inst
->regs_written
= header_size
;
345 for (int i
= header_size
; i
< sources
; ++i
)
346 assert(src
[i
].file
!= GRF
|| src
[i
].width
== dst
.width
);
347 inst
->regs_written
+= (sources
- header_size
) * (dst
.width
/ 8);
353 fs_visitor::VARYING_PULL_CONSTANT_LOAD(const fs_reg
&dst
,
354 const fs_reg
&surf_index
,
355 const fs_reg
&varying_offset
,
356 uint32_t const_offset
)
358 exec_list instructions
;
361 /* We have our constant surface use a pitch of 4 bytes, so our index can
362 * be any component of a vector, and then we load 4 contiguous
363 * components starting from that.
365 * We break down the const_offset to a portion added to the variable
366 * offset and a portion done using reg_offset, which means that if you
367 * have GLSL using something like "uniform vec4 a[20]; gl_FragColor =
368 * a[i]", we'll temporarily generate 4 vec4 loads from offset i * 4, and
369 * CSE can later notice that those loads are all the same and eliminate
370 * the redundant ones.
372 fs_reg vec4_offset
= vgrf(glsl_type::int_type
);
373 instructions
.push_tail(ADD(vec4_offset
,
374 varying_offset
, fs_reg(const_offset
& ~3)));
377 if (devinfo
->gen
== 4 && dst
.width
== 8) {
378 /* Pre-gen5, we can either use a SIMD8 message that requires (header,
379 * u, v, r) as parameters, or we can just use the SIMD16 message
380 * consisting of (header, u). We choose the second, at the cost of a
381 * longer return length.
387 if (devinfo
->gen
>= 7)
388 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
;
390 op
= FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
;
392 assert(dst
.width
% 8 == 0);
393 int regs_written
= 4 * (dst
.width
/ 8) * scale
;
394 fs_reg vec4_result
= fs_reg(GRF
, alloc
.allocate(regs_written
),
395 dst
.type
, dst
.width
);
396 inst
= new(mem_ctx
) fs_inst(op
, vec4_result
, surf_index
, vec4_offset
);
397 inst
->regs_written
= regs_written
;
398 instructions
.push_tail(inst
);
400 if (devinfo
->gen
< 7) {
402 inst
->header_size
= 1;
403 if (devinfo
->gen
== 4)
406 inst
->mlen
= 1 + dispatch_width
/ 8;
409 fs_reg result
= offset(vec4_result
, (const_offset
& 3) * scale
);
410 instructions
.push_tail(MOV(dst
, result
));
416 * A helper for MOV generation for fixing up broken hardware SEND dependency
420 fs_visitor::DEP_RESOLVE_MOV(int grf
)
422 fs_inst
*inst
= MOV(brw_null_reg(), fs_reg(GRF
, grf
, BRW_REGISTER_TYPE_F
));
425 inst
->annotation
= "send dependency resolve";
427 /* The caller always wants uncompressed to emit the minimal extra
428 * dependencies, and to avoid having to deal with aligning its regs to 2.
436 fs_inst::equals(fs_inst
*inst
) const
438 return (opcode
== inst
->opcode
&&
439 dst
.equals(inst
->dst
) &&
440 src
[0].equals(inst
->src
[0]) &&
441 src
[1].equals(inst
->src
[1]) &&
442 src
[2].equals(inst
->src
[2]) &&
443 saturate
== inst
->saturate
&&
444 predicate
== inst
->predicate
&&
445 conditional_mod
== inst
->conditional_mod
&&
446 mlen
== inst
->mlen
&&
447 base_mrf
== inst
->base_mrf
&&
448 target
== inst
->target
&&
450 header_size
== inst
->header_size
&&
451 shadow_compare
== inst
->shadow_compare
&&
452 exec_size
== inst
->exec_size
&&
453 offset
== inst
->offset
);
457 fs_inst::overwrites_reg(const fs_reg
®
) const
459 return reg
.in_range(dst
, regs_written
);
463 fs_inst::is_send_from_grf() const
466 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
467 case SHADER_OPCODE_SHADER_TIME_ADD
:
468 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
469 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
470 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
471 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
472 case SHADER_OPCODE_UNTYPED_ATOMIC
:
473 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
474 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
475 case SHADER_OPCODE_TYPED_ATOMIC
:
476 case SHADER_OPCODE_TYPED_SURFACE_READ
:
477 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
478 case SHADER_OPCODE_URB_WRITE_SIMD8
:
480 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
481 return src
[1].file
== GRF
;
482 case FS_OPCODE_FB_WRITE
:
483 return src
[0].file
== GRF
;
486 return src
[0].file
== GRF
;
493 fs_inst::is_copy_payload(const brw::simple_allocator
&grf_alloc
) const
495 if (this->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
498 fs_reg reg
= this->src
[0];
499 if (reg
.file
!= GRF
|| reg
.reg_offset
!= 0 || reg
.stride
== 0)
502 if (grf_alloc
.sizes
[reg
.reg
] != this->regs_written
)
505 for (int i
= 0; i
< this->sources
; i
++) {
506 reg
.type
= this->src
[i
].type
;
507 reg
.width
= this->src
[i
].width
;
508 if (!this->src
[i
].equals(reg
))
510 reg
= ::offset(reg
, 1);
517 fs_inst::can_do_source_mods(const struct brw_device_info
*devinfo
)
519 if (devinfo
->gen
== 6 && is_math())
522 if (is_send_from_grf())
525 if (!backend_instruction::can_do_source_mods())
532 fs_inst::has_side_effects() const
534 return this->eot
|| backend_instruction::has_side_effects();
540 memset(this, 0, sizeof(*this));
544 /** Generic unset register constructor. */
548 this->file
= BAD_FILE
;
551 /** Immediate value constructor. */
552 fs_reg::fs_reg(float f
)
556 this->type
= BRW_REGISTER_TYPE_F
;
557 this->fixed_hw_reg
.dw1
.f
= f
;
561 /** Immediate value constructor. */
562 fs_reg::fs_reg(int32_t i
)
566 this->type
= BRW_REGISTER_TYPE_D
;
567 this->fixed_hw_reg
.dw1
.d
= i
;
571 /** Immediate value constructor. */
572 fs_reg::fs_reg(uint32_t u
)
576 this->type
= BRW_REGISTER_TYPE_UD
;
577 this->fixed_hw_reg
.dw1
.ud
= u
;
581 /** Vector float immediate value constructor. */
582 fs_reg::fs_reg(uint8_t vf
[4])
586 this->type
= BRW_REGISTER_TYPE_VF
;
587 memcpy(&this->fixed_hw_reg
.dw1
.ud
, vf
, sizeof(unsigned));
590 /** Vector float immediate value constructor. */
591 fs_reg::fs_reg(uint8_t vf0
, uint8_t vf1
, uint8_t vf2
, uint8_t vf3
)
595 this->type
= BRW_REGISTER_TYPE_VF
;
596 this->fixed_hw_reg
.dw1
.ud
= (vf0
<< 0) |
602 /** Fixed brw_reg. */
603 fs_reg::fs_reg(struct brw_reg fixed_hw_reg
)
607 this->fixed_hw_reg
= fixed_hw_reg
;
608 this->type
= fixed_hw_reg
.type
;
609 this->width
= 1 << fixed_hw_reg
.width
;
613 fs_reg::equals(const fs_reg
&r
) const
615 return (file
== r
.file
&&
617 reg_offset
== r
.reg_offset
&&
618 subreg_offset
== r
.subreg_offset
&&
620 negate
== r
.negate
&&
622 !reladdr
&& !r
.reladdr
&&
623 memcmp(&fixed_hw_reg
, &r
.fixed_hw_reg
, sizeof(fixed_hw_reg
)) == 0 &&
629 fs_reg::set_smear(unsigned subreg
)
631 assert(file
!= HW_REG
&& file
!= IMM
);
632 subreg_offset
= subreg
* type_sz(type
);
638 fs_reg::is_contiguous() const
644 fs_visitor::type_size(const struct glsl_type
*type
)
646 unsigned int size
, i
;
648 switch (type
->base_type
) {
651 case GLSL_TYPE_FLOAT
:
653 return type
->components();
654 case GLSL_TYPE_ARRAY
:
655 return type_size(type
->fields
.array
) * type
->length
;
656 case GLSL_TYPE_STRUCT
:
658 for (i
= 0; i
< type
->length
; i
++) {
659 size
+= type_size(type
->fields
.structure
[i
].type
);
662 case GLSL_TYPE_SAMPLER
:
663 /* Samplers take up no register space, since they're baked in at
667 case GLSL_TYPE_ATOMIC_UINT
:
669 case GLSL_TYPE_IMAGE
:
671 case GLSL_TYPE_ERROR
:
672 case GLSL_TYPE_INTERFACE
:
673 case GLSL_TYPE_DOUBLE
:
674 unreachable("not reached");
681 * Create a MOV to read the timestamp register.
683 * The caller is responsible for emitting the MOV. The return value is
684 * the destination of the MOV, with extra parameters set.
687 fs_visitor::get_timestamp(fs_inst
**out_mov
)
689 assert(devinfo
->gen
>= 7);
691 fs_reg ts
= fs_reg(retype(brw_vec4_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
694 BRW_REGISTER_TYPE_UD
));
696 fs_reg dst
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
, 4);
698 fs_inst
*mov
= MOV(dst
, ts
);
699 /* We want to read the 3 fields we care about even if it's not enabled in
702 mov
->force_writemask_all
= true;
704 /* The caller wants the low 32 bits of the timestamp. Since it's running
705 * at the GPU clock rate of ~1.2ghz, it will roll over every ~3 seconds,
706 * which is plenty of time for our purposes. It is identical across the
707 * EUs, but since it's tracking GPU core speed it will increment at a
708 * varying rate as render P-states change.
710 * The caller could also check if render P-states have changed (or anything
711 * else that might disrupt timing) by setting smear to 2 and checking if
712 * that field is != 0.
721 fs_visitor::emit_shader_time_begin()
723 current_annotation
= "shader time start";
725 shader_start_time
= get_timestamp(&mov
);
730 fs_visitor::emit_shader_time_end()
732 current_annotation
= "shader time end";
734 enum shader_time_shader_type type
, written_type
, reset_type
;
736 case MESA_SHADER_VERTEX
:
738 written_type
= ST_VS_WRITTEN
;
739 reset_type
= ST_VS_RESET
;
741 case MESA_SHADER_GEOMETRY
:
743 written_type
= ST_GS_WRITTEN
;
744 reset_type
= ST_GS_RESET
;
746 case MESA_SHADER_FRAGMENT
:
747 if (dispatch_width
== 8) {
749 written_type
= ST_FS8_WRITTEN
;
750 reset_type
= ST_FS8_RESET
;
752 assert(dispatch_width
== 16);
754 written_type
= ST_FS16_WRITTEN
;
755 reset_type
= ST_FS16_RESET
;
758 case MESA_SHADER_COMPUTE
:
760 written_type
= ST_CS_WRITTEN
;
761 reset_type
= ST_CS_RESET
;
764 unreachable("fs_visitor::emit_shader_time_end missing code");
767 /* Insert our code just before the final SEND with EOT. */
768 exec_node
*end
= this->instructions
.get_tail();
769 assert(end
&& ((fs_inst
*) end
)->eot
);
772 fs_reg shader_end_time
= get_timestamp(&tm_read
);
773 end
->insert_before(tm_read
);
775 /* Check that there weren't any timestamp reset events (assuming these
776 * were the only two timestamp reads that happened).
778 fs_reg reset
= shader_end_time
;
780 fs_inst
*test
= AND(reg_null_d
, reset
, fs_reg(1u));
781 test
->conditional_mod
= BRW_CONDITIONAL_Z
;
782 test
->force_writemask_all
= true;
783 end
->insert_before(test
);
784 end
->insert_before(IF(BRW_PREDICATE_NORMAL
));
786 fs_reg start
= shader_start_time
;
788 fs_reg diff
= fs_reg(GRF
, alloc
.allocate(1), BRW_REGISTER_TYPE_UD
, 1);
790 fs_inst
*add
= ADD(diff
, start
, shader_end_time
);
791 add
->force_writemask_all
= true;
792 end
->insert_before(add
);
794 /* If there were no instructions between the two timestamp gets, the diff
795 * is 2 cycles. Remove that overhead, so I can forget about that when
796 * trying to determine the time taken for single instructions.
798 add
= ADD(diff
, diff
, fs_reg(-2u));
799 add
->force_writemask_all
= true;
800 end
->insert_before(add
);
802 end
->insert_before(SHADER_TIME_ADD(type
, diff
));
803 end
->insert_before(SHADER_TIME_ADD(written_type
, fs_reg(1u)));
804 end
->insert_before(new(mem_ctx
) fs_inst(BRW_OPCODE_ELSE
, dispatch_width
));
805 end
->insert_before(SHADER_TIME_ADD(reset_type
, fs_reg(1u)));
806 end
->insert_before(new(mem_ctx
) fs_inst(BRW_OPCODE_ENDIF
, dispatch_width
));
810 fs_visitor::SHADER_TIME_ADD(enum shader_time_shader_type type
, fs_reg value
)
812 int shader_time_index
=
813 brw_get_shader_time_index(brw
, shader_prog
, prog
, type
);
814 fs_reg offset
= fs_reg(shader_time_index
* SHADER_TIME_STRIDE
);
817 if (dispatch_width
== 8)
818 payload
= vgrf(glsl_type::uvec2_type
);
820 payload
= vgrf(glsl_type::uint_type
);
822 return new(mem_ctx
) fs_inst(SHADER_OPCODE_SHADER_TIME_ADD
,
823 fs_reg(), payload
, offset
, value
);
827 fs_visitor::vfail(const char *format
, va_list va
)
836 msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
837 msg
= ralloc_asprintf(mem_ctx
, "%s compile failed: %s\n", stage_abbrev
, msg
);
839 this->fail_msg
= msg
;
842 fprintf(stderr
, "%s", msg
);
847 fs_visitor::fail(const char *format
, ...)
851 va_start(va
, format
);
857 * Mark this program as impossible to compile in SIMD16 mode.
859 * During the SIMD8 compile (which happens first), we can detect and flag
860 * things that are unsupported in SIMD16 mode, so the compiler can skip
861 * the SIMD16 compile altogether.
863 * During a SIMD16 compile (if one happens anyway), this just calls fail().
866 fs_visitor::no16(const char *format
, ...)
870 va_start(va
, format
);
872 if (dispatch_width
== 16) {
875 simd16_unsupported
= true;
877 if (brw
->perf_debug
) {
879 ralloc_vasprintf_append(&no16_msg
, format
, va
);
881 no16_msg
= ralloc_vasprintf(mem_ctx
, format
, va
);
889 fs_visitor::emit(enum opcode opcode
)
891 return emit(new(mem_ctx
) fs_inst(opcode
, dispatch_width
));
895 fs_visitor::emit(enum opcode opcode
, const fs_reg
&dst
)
897 return emit(new(mem_ctx
) fs_inst(opcode
, dst
));
901 fs_visitor::emit(enum opcode opcode
, const fs_reg
&dst
, const fs_reg
&src0
)
903 return emit(new(mem_ctx
) fs_inst(opcode
, dst
, src0
));
907 fs_visitor::emit(enum opcode opcode
, const fs_reg
&dst
, const fs_reg
&src0
,
910 return emit(new(mem_ctx
) fs_inst(opcode
, dst
, src0
, src1
));
914 fs_visitor::emit(enum opcode opcode
, const fs_reg
&dst
, const fs_reg
&src0
,
915 const fs_reg
&src1
, const fs_reg
&src2
)
917 return emit(new(mem_ctx
) fs_inst(opcode
, dst
, src0
, src1
, src2
));
921 fs_visitor::emit(enum opcode opcode
, const fs_reg
&dst
,
922 fs_reg src
[], int sources
)
924 return emit(new(mem_ctx
) fs_inst(opcode
, dst
, src
, sources
));
928 * Returns true if the instruction has a flag that means it won't
929 * update an entire destination register.
931 * For example, dead code elimination and live variable analysis want to know
932 * when a write to a variable screens off any preceding values that were in
936 fs_inst::is_partial_write() const
938 return ((this->predicate
&& this->opcode
!= BRW_OPCODE_SEL
) ||
939 (this->dst
.width
* type_sz(this->dst
.type
)) < 32 ||
940 !this->dst
.is_contiguous());
944 fs_inst::regs_read(int arg
) const
946 if (is_tex() && arg
== 0 && src
[0].file
== GRF
) {
948 } else if (opcode
== FS_OPCODE_FB_WRITE
&& arg
== 0) {
950 } else if (opcode
== SHADER_OPCODE_URB_WRITE_SIMD8
&& arg
== 0) {
952 } else if (opcode
== SHADER_OPCODE_UNTYPED_ATOMIC
&& arg
== 0) {
954 } else if (opcode
== SHADER_OPCODE_UNTYPED_SURFACE_READ
&& arg
== 0) {
956 } else if (opcode
== SHADER_OPCODE_UNTYPED_SURFACE_WRITE
&& arg
== 0) {
958 } else if (opcode
== SHADER_OPCODE_TYPED_ATOMIC
&& arg
== 0) {
960 } else if (opcode
== SHADER_OPCODE_TYPED_SURFACE_READ
&& arg
== 0) {
962 } else if (opcode
== SHADER_OPCODE_TYPED_SURFACE_WRITE
&& arg
== 0) {
964 } else if (opcode
== FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
&& arg
== 0) {
966 } else if (opcode
== FS_OPCODE_LINTERP
&& arg
== 0) {
967 return exec_size
/ 4;
970 switch (src
[arg
].file
) {
977 if (src
[arg
].stride
== 0) {
980 int size
= src
[arg
].width
* src
[arg
].stride
* type_sz(src
[arg
].type
);
981 return (size
+ 31) / 32;
984 unreachable("MRF registers are not allowed as sources");
986 unreachable("Invalid register file");
991 fs_inst::reads_flag() const
997 fs_inst::writes_flag() const
999 return (conditional_mod
&& (opcode
!= BRW_OPCODE_SEL
&&
1000 opcode
!= BRW_OPCODE_IF
&&
1001 opcode
!= BRW_OPCODE_WHILE
)) ||
1002 opcode
== FS_OPCODE_MOV_DISPATCH_TO_FLAGS
;
1006 * Returns how many MRFs an FS opcode will write over.
1008 * Note that this is not the 0 or 1 implied writes in an actual gen
1009 * instruction -- the FS opcodes often generate MOVs in addition.
1012 fs_visitor::implied_mrf_writes(fs_inst
*inst
)
1014 if (inst
->mlen
== 0)
1017 if (inst
->base_mrf
== -1)
1020 switch (inst
->opcode
) {
1021 case SHADER_OPCODE_RCP
:
1022 case SHADER_OPCODE_RSQ
:
1023 case SHADER_OPCODE_SQRT
:
1024 case SHADER_OPCODE_EXP2
:
1025 case SHADER_OPCODE_LOG2
:
1026 case SHADER_OPCODE_SIN
:
1027 case SHADER_OPCODE_COS
:
1028 return 1 * dispatch_width
/ 8;
1029 case SHADER_OPCODE_POW
:
1030 case SHADER_OPCODE_INT_QUOTIENT
:
1031 case SHADER_OPCODE_INT_REMAINDER
:
1032 return 2 * dispatch_width
/ 8;
1033 case SHADER_OPCODE_TEX
:
1035 case SHADER_OPCODE_TXD
:
1036 case SHADER_OPCODE_TXF
:
1037 case SHADER_OPCODE_TXF_CMS
:
1038 case SHADER_OPCODE_TXF_MCS
:
1039 case SHADER_OPCODE_TG4
:
1040 case SHADER_OPCODE_TG4_OFFSET
:
1041 case SHADER_OPCODE_TXL
:
1042 case SHADER_OPCODE_TXS
:
1043 case SHADER_OPCODE_LOD
:
1045 case FS_OPCODE_FB_WRITE
:
1047 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
1048 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
1050 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
1052 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
1054 case SHADER_OPCODE_UNTYPED_ATOMIC
:
1055 case SHADER_OPCODE_UNTYPED_SURFACE_READ
:
1056 case SHADER_OPCODE_UNTYPED_SURFACE_WRITE
:
1057 case SHADER_OPCODE_TYPED_ATOMIC
:
1058 case SHADER_OPCODE_TYPED_SURFACE_READ
:
1059 case SHADER_OPCODE_TYPED_SURFACE_WRITE
:
1060 case SHADER_OPCODE_URB_WRITE_SIMD8
:
1061 case FS_OPCODE_INTERPOLATE_AT_CENTROID
:
1062 case FS_OPCODE_INTERPOLATE_AT_SAMPLE
:
1063 case FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
:
1064 case FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
:
1067 unreachable("not reached");
1072 fs_visitor::vgrf(const glsl_type
*const type
)
1074 int reg_width
= dispatch_width
/ 8;
1075 return fs_reg(GRF
, alloc
.allocate(type_size(type
) * reg_width
),
1076 brw_type_for_base_type(type
), dispatch_width
);
1080 fs_visitor::vgrf(int num_components
)
1082 int reg_width
= dispatch_width
/ 8;
1083 return fs_reg(GRF
, alloc
.allocate(num_components
* reg_width
),
1084 BRW_REGISTER_TYPE_F
, dispatch_width
);
1087 /** Fixed HW reg constructor. */
1088 fs_reg::fs_reg(enum register_file file
, int reg
)
1093 this->type
= BRW_REGISTER_TYPE_F
;
1104 /** Fixed HW reg constructor. */
1105 fs_reg::fs_reg(enum register_file file
, int reg
, enum brw_reg_type type
)
1121 /** Fixed HW reg constructor. */
1122 fs_reg::fs_reg(enum register_file file
, int reg
, enum brw_reg_type type
,
1129 this->width
= width
;
1132 /* For SIMD16, we need to follow from the uniform setup of SIMD8 dispatch.
1133 * This brings in those uniform definitions
1136 fs_visitor::import_uniforms(fs_visitor
*v
)
1138 this->push_constant_loc
= v
->push_constant_loc
;
1139 this->pull_constant_loc
= v
->pull_constant_loc
;
1140 this->uniforms
= v
->uniforms
;
1141 this->param_size
= v
->param_size
;
1145 fs_visitor::emit_fragcoord_interpolation(bool pixel_center_integer
,
1146 bool origin_upper_left
)
1148 assert(stage
== MESA_SHADER_FRAGMENT
);
1149 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1150 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec4_type
));
1152 bool flip
= !origin_upper_left
^ key
->render_to_fbo
;
1154 /* gl_FragCoord.x */
1155 if (pixel_center_integer
) {
1156 emit(MOV(wpos
, this->pixel_x
));
1158 emit(ADD(wpos
, this->pixel_x
, fs_reg(0.5f
)));
1160 wpos
= offset(wpos
, 1);
1162 /* gl_FragCoord.y */
1163 if (!flip
&& pixel_center_integer
) {
1164 emit(MOV(wpos
, this->pixel_y
));
1166 fs_reg pixel_y
= this->pixel_y
;
1167 float offset
= (pixel_center_integer
? 0.0 : 0.5);
1170 pixel_y
.negate
= true;
1171 offset
+= key
->drawable_height
- 1.0;
1174 emit(ADD(wpos
, pixel_y
, fs_reg(offset
)));
1176 wpos
= offset(wpos
, 1);
1178 /* gl_FragCoord.z */
1179 if (devinfo
->gen
>= 6) {
1180 emit(MOV(wpos
, fs_reg(brw_vec8_grf(payload
.source_depth_reg
, 0))));
1182 emit(FS_OPCODE_LINTERP
, wpos
,
1183 this->delta_xy
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
],
1184 interp_reg(VARYING_SLOT_POS
, 2));
1186 wpos
= offset(wpos
, 1);
1188 /* gl_FragCoord.w: Already set up in emit_interpolation */
1189 emit(BRW_OPCODE_MOV
, wpos
, this->wpos_w
);
1195 fs_visitor::emit_linterp(const fs_reg
&attr
, const fs_reg
&interp
,
1196 glsl_interp_qualifier interpolation_mode
,
1197 bool is_centroid
, bool is_sample
)
1199 brw_wm_barycentric_interp_mode barycoord_mode
;
1200 if (devinfo
->gen
>= 6) {
1202 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1203 barycoord_mode
= BRW_WM_PERSPECTIVE_CENTROID_BARYCENTRIC
;
1205 barycoord_mode
= BRW_WM_NONPERSPECTIVE_CENTROID_BARYCENTRIC
;
1206 } else if (is_sample
) {
1207 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1208 barycoord_mode
= BRW_WM_PERSPECTIVE_SAMPLE_BARYCENTRIC
;
1210 barycoord_mode
= BRW_WM_NONPERSPECTIVE_SAMPLE_BARYCENTRIC
;
1212 if (interpolation_mode
== INTERP_QUALIFIER_SMOOTH
)
1213 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1215 barycoord_mode
= BRW_WM_NONPERSPECTIVE_PIXEL_BARYCENTRIC
;
1218 /* On Ironlake and below, there is only one interpolation mode.
1219 * Centroid interpolation doesn't mean anything on this hardware --
1220 * there is no multisampling.
1222 barycoord_mode
= BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
;
1224 return emit(FS_OPCODE_LINTERP
, attr
,
1225 this->delta_xy
[barycoord_mode
], interp
);
1229 fs_visitor::emit_general_interpolation(fs_reg attr
, const char *name
,
1230 const glsl_type
*type
,
1231 glsl_interp_qualifier interpolation_mode
,
1232 int location
, bool mod_centroid
,
1235 attr
.type
= brw_type_for_base_type(type
->get_scalar_type());
1237 assert(stage
== MESA_SHADER_FRAGMENT
);
1238 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1239 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1241 unsigned int array_elements
;
1243 if (type
->is_array()) {
1244 array_elements
= type
->length
;
1245 if (array_elements
== 0) {
1246 fail("dereferenced array '%s' has length 0\n", name
);
1248 type
= type
->fields
.array
;
1253 if (interpolation_mode
== INTERP_QUALIFIER_NONE
) {
1255 location
== VARYING_SLOT_COL0
|| location
== VARYING_SLOT_COL1
;
1256 if (key
->flat_shade
&& is_gl_Color
) {
1257 interpolation_mode
= INTERP_QUALIFIER_FLAT
;
1259 interpolation_mode
= INTERP_QUALIFIER_SMOOTH
;
1263 for (unsigned int i
= 0; i
< array_elements
; i
++) {
1264 for (unsigned int j
= 0; j
< type
->matrix_columns
; j
++) {
1265 if (prog_data
->urb_setup
[location
] == -1) {
1266 /* If there's no incoming setup data for this slot, don't
1267 * emit interpolation for it.
1269 attr
= offset(attr
, type
->vector_elements
);
1274 if (interpolation_mode
== INTERP_QUALIFIER_FLAT
) {
1275 /* Constant interpolation (flat shading) case. The SF has
1276 * handed us defined values in only the constant offset
1277 * field of the setup reg.
1279 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1280 struct brw_reg interp
= interp_reg(location
, k
);
1281 interp
= suboffset(interp
, 3);
1282 interp
.type
= attr
.type
;
1283 emit(FS_OPCODE_CINTERP
, attr
, fs_reg(interp
));
1284 attr
= offset(attr
, 1);
1287 /* Smooth/noperspective interpolation case. */
1288 for (unsigned int k
= 0; k
< type
->vector_elements
; k
++) {
1289 struct brw_reg interp
= interp_reg(location
, k
);
1290 if (devinfo
->needs_unlit_centroid_workaround
&& mod_centroid
) {
1291 /* Get the pixel/sample mask into f0 so that we know
1292 * which pixels are lit. Then, for each channel that is
1293 * unlit, replace the centroid data with non-centroid
1296 emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
1299 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1301 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1302 inst
->predicate_inverse
= true;
1303 if (devinfo
->has_pln
)
1304 inst
->no_dd_clear
= true;
1306 inst
= emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1307 mod_centroid
&& !key
->persample_shading
,
1308 mod_sample
|| key
->persample_shading
);
1309 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1310 inst
->predicate_inverse
= false;
1311 if (devinfo
->has_pln
)
1312 inst
->no_dd_check
= true;
1315 emit_linterp(attr
, fs_reg(interp
), interpolation_mode
,
1316 mod_centroid
&& !key
->persample_shading
,
1317 mod_sample
|| key
->persample_shading
);
1319 if (devinfo
->gen
< 6 && interpolation_mode
== INTERP_QUALIFIER_SMOOTH
) {
1320 emit(BRW_OPCODE_MUL
, attr
, attr
, this->pixel_w
);
1322 attr
= offset(attr
, 1);
1332 fs_visitor::emit_frontfacing_interpolation()
1334 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::bool_type
));
1336 if (devinfo
->gen
>= 6) {
1337 /* Bit 15 of g0.0 is 0 if the polygon is front facing. We want to create
1338 * a boolean result from this (~0/true or 0/false).
1340 * We can use the fact that bit 15 is the MSB of g0.0:W to accomplish
1341 * this task in only one instruction:
1342 * - a negation source modifier will flip the bit; and
1343 * - a W -> D type conversion will sign extend the bit into the high
1344 * word of the destination.
1346 * An ASR 15 fills the low word of the destination.
1348 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
1351 emit(ASR(*reg
, g0
, fs_reg(15)));
1353 /* Bit 31 of g1.6 is 0 if the polygon is front facing. We want to create
1354 * a boolean result from this (1/true or 0/false).
1356 * Like in the above case, since the bit is the MSB of g1.6:UD we can use
1357 * the negation source modifier to flip it. Unfortunately the SHR
1358 * instruction only operates on UD (or D with an abs source modifier)
1359 * sources without negation.
1361 * Instead, use ASR (which will give ~0/true or 0/false).
1363 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
1366 emit(ASR(*reg
, g1_6
, fs_reg(31)));
1373 fs_visitor::compute_sample_position(fs_reg dst
, fs_reg int_sample_pos
)
1375 assert(stage
== MESA_SHADER_FRAGMENT
);
1376 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1377 assert(dst
.type
== BRW_REGISTER_TYPE_F
);
1379 if (key
->compute_pos_offset
) {
1380 /* Convert int_sample_pos to floating point */
1381 emit(MOV(dst
, int_sample_pos
));
1382 /* Scale to the range [0, 1] */
1383 emit(MUL(dst
, dst
, fs_reg(1 / 16.0f
)));
1386 /* From ARB_sample_shading specification:
1387 * "When rendering to a non-multisample buffer, or if multisample
1388 * rasterization is disabled, gl_SamplePosition will always be
1391 emit(MOV(dst
, fs_reg(0.5f
)));
1396 fs_visitor::emit_samplepos_setup()
1398 assert(devinfo
->gen
>= 6);
1400 this->current_annotation
= "compute sample position";
1401 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::vec2_type
));
1403 fs_reg int_sample_x
= vgrf(glsl_type::int_type
);
1404 fs_reg int_sample_y
= vgrf(glsl_type::int_type
);
1406 /* WM will be run in MSDISPMODE_PERSAMPLE. So, only one of SIMD8 or SIMD16
1407 * mode will be enabled.
1409 * From the Ivy Bridge PRM, volume 2 part 1, page 344:
1410 * R31.1:0 Position Offset X/Y for Slot[3:0]
1411 * R31.3:2 Position Offset X/Y for Slot[7:4]
1414 * The X, Y sample positions come in as bytes in thread payload. So, read
1415 * the positions using vstride=16, width=8, hstride=2.
1417 struct brw_reg sample_pos_reg
=
1418 stride(retype(brw_vec1_grf(payload
.sample_pos_reg
, 0),
1419 BRW_REGISTER_TYPE_B
), 16, 8, 2);
1421 if (dispatch_width
== 8) {
1422 emit(MOV(int_sample_x
, fs_reg(sample_pos_reg
)));
1424 emit(MOV(half(int_sample_x
, 0), fs_reg(sample_pos_reg
)));
1425 emit(MOV(half(int_sample_x
, 1), fs_reg(suboffset(sample_pos_reg
, 16))))
1426 ->force_sechalf
= true;
1428 /* Compute gl_SamplePosition.x */
1429 compute_sample_position(pos
, int_sample_x
);
1430 pos
= offset(pos
, 1);
1431 if (dispatch_width
== 8) {
1432 emit(MOV(int_sample_y
, fs_reg(suboffset(sample_pos_reg
, 1))));
1434 emit(MOV(half(int_sample_y
, 0),
1435 fs_reg(suboffset(sample_pos_reg
, 1))));
1436 emit(MOV(half(int_sample_y
, 1), fs_reg(suboffset(sample_pos_reg
, 17))))
1437 ->force_sechalf
= true;
1439 /* Compute gl_SamplePosition.y */
1440 compute_sample_position(pos
, int_sample_y
);
1445 fs_visitor::emit_sampleid_setup()
1447 assert(stage
== MESA_SHADER_FRAGMENT
);
1448 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1449 assert(devinfo
->gen
>= 6);
1451 this->current_annotation
= "compute sample id";
1452 fs_reg
*reg
= new(this->mem_ctx
) fs_reg(vgrf(glsl_type::int_type
));
1454 if (key
->compute_sample_id
) {
1455 fs_reg t1
= vgrf(glsl_type::int_type
);
1456 fs_reg t2
= vgrf(glsl_type::int_type
);
1457 t2
.type
= BRW_REGISTER_TYPE_UW
;
1459 /* The PS will be run in MSDISPMODE_PERSAMPLE. For example with
1460 * 8x multisampling, subspan 0 will represent sample N (where N
1461 * is 0, 2, 4 or 6), subspan 1 will represent sample 1, 3, 5 or
1462 * 7. We can find the value of N by looking at R0.0 bits 7:6
1463 * ("Starting Sample Pair Index (SSPI)") and multiplying by two
1464 * (since samples are always delivered in pairs). That is, we
1465 * compute 2*((R0.0 & 0xc0) >> 6) == (R0.0 & 0xc0) >> 5. Then
1466 * we need to add N to the sequence (0, 0, 0, 0, 1, 1, 1, 1) in
1467 * case of SIMD8 and sequence (0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
1468 * 2, 3, 3, 3, 3) in case of SIMD16. We compute this sequence by
1469 * populating a temporary variable with the sequence (0, 1, 2, 3),
1470 * and then reading from it using vstride=1, width=4, hstride=0.
1471 * These computations hold good for 4x multisampling as well.
1473 * For 2x MSAA and SIMD16, we want to use the sequence (0, 1, 0, 1):
1474 * the first four slots are sample 0 of subspan 0; the next four
1475 * are sample 1 of subspan 0; the third group is sample 0 of
1476 * subspan 1, and finally sample 1 of subspan 1.
1479 inst
= emit(BRW_OPCODE_AND
, t1
,
1480 fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)),
1482 inst
->force_writemask_all
= true;
1483 inst
= emit(BRW_OPCODE_SHR
, t1
, t1
, fs_reg(5));
1484 inst
->force_writemask_all
= true;
1485 /* This works for both SIMD8 and SIMD16 */
1486 inst
= emit(MOV(t2
, brw_imm_v(key
->persample_2x
? 0x1010 : 0x3210)));
1487 inst
->force_writemask_all
= true;
1488 /* This special instruction takes care of setting vstride=1,
1489 * width=4, hstride=0 of t2 during an ADD instruction.
1491 emit(FS_OPCODE_SET_SAMPLE_ID
, *reg
, t1
, t2
);
1493 /* As per GL_ARB_sample_shading specification:
1494 * "When rendering to a non-multisample buffer, or if multisample
1495 * rasterization is disabled, gl_SampleID will always be zero."
1497 emit(BRW_OPCODE_MOV
, *reg
, fs_reg(0));
1504 fs_visitor::resolve_source_modifiers(fs_reg
*src
)
1506 if (!src
->abs
&& !src
->negate
)
1509 fs_reg temp
= retype(vgrf(1), src
->type
);
1510 emit(MOV(temp
, *src
));
1515 fs_visitor::fix_math_operand(fs_reg src
)
1517 /* Can't do hstride == 0 args on gen6 math, so expand it out. We
1518 * might be able to do better by doing execsize = 1 math and then
1519 * expanding that result out, but we would need to be careful with
1522 * The hardware ignores source modifiers (negate and abs) on math
1523 * instructions, so we also move to a temp to set those up.
1525 if (devinfo
->gen
== 6 && src
.file
!= UNIFORM
&& src
.file
!= IMM
&&
1526 !src
.abs
&& !src
.negate
)
1529 /* Gen7 relaxes most of the above restrictions, but still can't use IMM
1532 if (devinfo
->gen
>= 7 && src
.file
!= IMM
)
1535 fs_reg expanded
= vgrf(glsl_type::float_type
);
1536 expanded
.type
= src
.type
;
1537 emit(BRW_OPCODE_MOV
, expanded
, src
);
1542 fs_visitor::emit_math(enum opcode opcode
, fs_reg dst
, fs_reg src
)
1545 case SHADER_OPCODE_RCP
:
1546 case SHADER_OPCODE_RSQ
:
1547 case SHADER_OPCODE_SQRT
:
1548 case SHADER_OPCODE_EXP2
:
1549 case SHADER_OPCODE_LOG2
:
1550 case SHADER_OPCODE_SIN
:
1551 case SHADER_OPCODE_COS
:
1554 unreachable("not reached: bad math opcode");
1557 /* Can't do hstride == 0 args to gen6 math, so expand it out. We
1558 * might be able to do better by doing execsize = 1 math and then
1559 * expanding that result out, but we would need to be careful with
1562 * Gen 6 hardware ignores source modifiers (negate and abs) on math
1563 * instructions, so we also move to a temp to set those up.
1565 if (devinfo
->gen
== 6 || devinfo
->gen
== 7)
1566 src
= fix_math_operand(src
);
1568 fs_inst
*inst
= emit(opcode
, dst
, src
);
1570 if (devinfo
->gen
< 6) {
1572 inst
->mlen
= dispatch_width
/ 8;
1579 fs_visitor::emit_math(enum opcode opcode
, fs_reg dst
, fs_reg src0
, fs_reg src1
)
1584 if (devinfo
->gen
>= 8) {
1585 inst
= emit(opcode
, dst
, src0
, src1
);
1586 } else if (devinfo
->gen
>= 6) {
1587 src0
= fix_math_operand(src0
);
1588 src1
= fix_math_operand(src1
);
1590 inst
= emit(opcode
, dst
, src0
, src1
);
1592 /* From the Ironlake PRM, Volume 4, Part 1, Section 6.1.13
1593 * "Message Payload":
1595 * "Operand0[7]. For the INT DIV functions, this operand is the
1598 * "Operand1[7]. For the INT DIV functions, this operand is the
1601 bool is_int_div
= opcode
!= SHADER_OPCODE_POW
;
1602 fs_reg
&op0
= is_int_div
? src1
: src0
;
1603 fs_reg
&op1
= is_int_div
? src0
: src1
;
1605 emit(MOV(fs_reg(MRF
, base_mrf
+ 1, op1
.type
, dispatch_width
), op1
));
1606 inst
= emit(opcode
, dst
, op0
, reg_null_f
);
1608 inst
->base_mrf
= base_mrf
;
1609 inst
->mlen
= 2 * dispatch_width
/ 8;
1615 fs_visitor::emit_discard_jump()
1617 assert(((brw_wm_prog_data
*) this->prog_data
)->uses_kill
);
1619 /* For performance, after a discard, jump to the end of the
1620 * shader if all relevant channels have been discarded.
1622 fs_inst
*discard_jump
= emit(FS_OPCODE_DISCARD_JUMP
);
1623 discard_jump
->flag_subreg
= 1;
1625 discard_jump
->predicate
= (dispatch_width
== 8)
1626 ? BRW_PREDICATE_ALIGN1_ANY8H
1627 : BRW_PREDICATE_ALIGN1_ANY16H
;
1628 discard_jump
->predicate_inverse
= true;
1632 fs_visitor::assign_curb_setup()
1634 if (dispatch_width
== 8) {
1635 prog_data
->dispatch_grf_start_reg
= payload
.num_regs
;
1637 if (stage
== MESA_SHADER_FRAGMENT
) {
1638 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1639 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1640 } else if (stage
== MESA_SHADER_COMPUTE
) {
1641 brw_cs_prog_data
*prog_data
= (brw_cs_prog_data
*) this->prog_data
;
1642 prog_data
->dispatch_grf_start_reg_16
= payload
.num_regs
;
1644 unreachable("Unsupported shader type!");
1648 prog_data
->curb_read_length
= ALIGN(stage_prog_data
->nr_params
, 8) / 8;
1650 /* Map the offsets in the UNIFORM file to fixed HW regs. */
1651 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1652 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
1653 if (inst
->src
[i
].file
== UNIFORM
) {
1654 int uniform_nr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
1656 if (uniform_nr
>= 0 && uniform_nr
< (int) uniforms
) {
1657 constant_nr
= push_constant_loc
[uniform_nr
];
1659 /* Section 5.11 of the OpenGL 4.1 spec says:
1660 * "Out-of-bounds reads return undefined values, which include
1661 * values from other variables of the active program or zero."
1662 * Just return the first push constant.
1667 struct brw_reg brw_reg
= brw_vec1_grf(payload
.num_regs
+
1671 inst
->src
[i
].file
= HW_REG
;
1672 inst
->src
[i
].fixed_hw_reg
= byte_offset(
1673 retype(brw_reg
, inst
->src
[i
].type
),
1674 inst
->src
[i
].subreg_offset
);
1681 fs_visitor::calculate_urb_setup()
1683 assert(stage
== MESA_SHADER_FRAGMENT
);
1684 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1685 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
1687 memset(prog_data
->urb_setup
, -1,
1688 sizeof(prog_data
->urb_setup
[0]) * VARYING_SLOT_MAX
);
1691 /* Figure out where each of the incoming setup attributes lands. */
1692 if (devinfo
->gen
>= 6) {
1693 if (_mesa_bitcount_64(prog
->InputsRead
&
1694 BRW_FS_VARYING_INPUT_MASK
) <= 16) {
1695 /* The SF/SBE pipeline stage can do arbitrary rearrangement of the
1696 * first 16 varying inputs, so we can put them wherever we want.
1697 * Just put them in order.
1699 * This is useful because it means that (a) inputs not used by the
1700 * fragment shader won't take up valuable register space, and (b) we
1701 * won't have to recompile the fragment shader if it gets paired with
1702 * a different vertex (or geometry) shader.
1704 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1705 if (prog
->InputsRead
& BRW_FS_VARYING_INPUT_MASK
&
1706 BITFIELD64_BIT(i
)) {
1707 prog_data
->urb_setup
[i
] = urb_next
++;
1711 /* We have enough input varyings that the SF/SBE pipeline stage can't
1712 * arbitrarily rearrange them to suit our whim; we have to put them
1713 * in an order that matches the output of the previous pipeline stage
1714 * (geometry or vertex shader).
1716 struct brw_vue_map prev_stage_vue_map
;
1717 brw_compute_vue_map(devinfo
, &prev_stage_vue_map
,
1718 key
->input_slots_valid
);
1719 int first_slot
= 2 * BRW_SF_URB_ENTRY_READ_OFFSET
;
1720 assert(prev_stage_vue_map
.num_slots
<= first_slot
+ 32);
1721 for (int slot
= first_slot
; slot
< prev_stage_vue_map
.num_slots
;
1723 int varying
= prev_stage_vue_map
.slot_to_varying
[slot
];
1724 /* Note that varying == BRW_VARYING_SLOT_COUNT when a slot is
1727 if (varying
!= BRW_VARYING_SLOT_COUNT
&&
1728 (prog
->InputsRead
& BRW_FS_VARYING_INPUT_MASK
&
1729 BITFIELD64_BIT(varying
))) {
1730 prog_data
->urb_setup
[varying
] = slot
- first_slot
;
1733 urb_next
= prev_stage_vue_map
.num_slots
- first_slot
;
1736 /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
1737 for (unsigned int i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
1738 /* Point size is packed into the header, not as a general attribute */
1739 if (i
== VARYING_SLOT_PSIZ
)
1742 if (key
->input_slots_valid
& BITFIELD64_BIT(i
)) {
1743 /* The back color slot is skipped when the front color is
1744 * also written to. In addition, some slots can be
1745 * written in the vertex shader and not read in the
1746 * fragment shader. So the register number must always be
1747 * incremented, mapped or not.
1749 if (_mesa_varying_slot_in_fs((gl_varying_slot
) i
))
1750 prog_data
->urb_setup
[i
] = urb_next
;
1756 * It's a FS only attribute, and we did interpolation for this attribute
1757 * in SF thread. So, count it here, too.
1759 * See compile_sf_prog() for more info.
1761 if (prog
->InputsRead
& BITFIELD64_BIT(VARYING_SLOT_PNTC
))
1762 prog_data
->urb_setup
[VARYING_SLOT_PNTC
] = urb_next
++;
1765 prog_data
->num_varying_inputs
= urb_next
;
1769 fs_visitor::assign_urb_setup()
1771 assert(stage
== MESA_SHADER_FRAGMENT
);
1772 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
1774 int urb_start
= payload
.num_regs
+ prog_data
->base
.curb_read_length
;
1776 /* Offset all the urb_setup[] index by the actual position of the
1777 * setup regs, now that the location of the constants has been chosen.
1779 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1780 if (inst
->opcode
== FS_OPCODE_LINTERP
) {
1781 assert(inst
->src
[1].file
== HW_REG
);
1782 inst
->src
[1].fixed_hw_reg
.nr
+= urb_start
;
1785 if (inst
->opcode
== FS_OPCODE_CINTERP
) {
1786 assert(inst
->src
[0].file
== HW_REG
);
1787 inst
->src
[0].fixed_hw_reg
.nr
+= urb_start
;
1791 /* Each attribute is 4 setup channels, each of which is half a reg. */
1792 this->first_non_payload_grf
=
1793 urb_start
+ prog_data
->num_varying_inputs
* 2;
1797 fs_visitor::assign_vs_urb_setup()
1799 brw_vs_prog_data
*vs_prog_data
= (brw_vs_prog_data
*) prog_data
;
1800 int grf
, count
, slot
, channel
, attr
;
1802 assert(stage
== MESA_SHADER_VERTEX
);
1803 count
= _mesa_bitcount_64(vs_prog_data
->inputs_read
);
1804 if (vs_prog_data
->uses_vertexid
|| vs_prog_data
->uses_instanceid
)
1807 /* Each attribute is 4 regs. */
1808 this->first_non_payload_grf
=
1809 payload
.num_regs
+ prog_data
->curb_read_length
+ count
* 4;
1811 unsigned vue_entries
=
1812 MAX2(count
, vs_prog_data
->base
.vue_map
.num_slots
);
1814 vs_prog_data
->base
.urb_entry_size
= ALIGN(vue_entries
, 4) / 4;
1815 vs_prog_data
->base
.urb_read_length
= (count
+ 1) / 2;
1817 assert(vs_prog_data
->base
.urb_read_length
<= 15);
1819 /* Rewrite all ATTR file references to the hw grf that they land in. */
1820 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1821 for (int i
= 0; i
< inst
->sources
; i
++) {
1822 if (inst
->src
[i
].file
== ATTR
) {
1824 if (inst
->src
[i
].reg
== VERT_ATTRIB_MAX
) {
1827 /* Attributes come in in a contiguous block, ordered by their
1828 * gl_vert_attrib value. That means we can compute the slot
1829 * number for an attribute by masking out the enabled
1830 * attributes before it and counting the bits.
1832 attr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
/ 4;
1833 slot
= _mesa_bitcount_64(vs_prog_data
->inputs_read
&
1834 BITFIELD64_MASK(attr
));
1837 channel
= inst
->src
[i
].reg_offset
& 3;
1839 grf
= payload
.num_regs
+
1840 prog_data
->curb_read_length
+
1843 inst
->src
[i
].file
= HW_REG
;
1844 inst
->src
[i
].fixed_hw_reg
=
1845 retype(brw_vec8_grf(grf
, 0), inst
->src
[i
].type
);
1852 * Split large virtual GRFs into separate components if we can.
1854 * This is mostly duplicated with what brw_fs_vector_splitting does,
1855 * but that's really conservative because it's afraid of doing
1856 * splitting that doesn't result in real progress after the rest of
1857 * the optimization phases, which would cause infinite looping in
1858 * optimization. We can do it once here, safely. This also has the
1859 * opportunity to split interpolated values, or maybe even uniforms,
1860 * which we don't have at the IR level.
1862 * We want to split, because virtual GRFs are what we register
1863 * allocate and spill (due to contiguousness requirements for some
1864 * instructions), and they're what we naturally generate in the
1865 * codegen process, but most virtual GRFs don't actually need to be
1866 * contiguous sets of GRFs. If we split, we'll end up with reduced
1867 * live intervals and better dead code elimination and coalescing.
1870 fs_visitor::split_virtual_grfs()
1872 int num_vars
= this->alloc
.count
;
1874 /* Count the total number of registers */
1876 int vgrf_to_reg
[num_vars
];
1877 for (int i
= 0; i
< num_vars
; i
++) {
1878 vgrf_to_reg
[i
] = reg_count
;
1879 reg_count
+= alloc
.sizes
[i
];
1882 /* An array of "split points". For each register slot, this indicates
1883 * if this slot can be separated from the previous slot. Every time an
1884 * instruction uses multiple elements of a register (as a source or
1885 * destination), we mark the used slots as inseparable. Then we go
1886 * through and split the registers into the smallest pieces we can.
1888 bool split_points
[reg_count
];
1889 memset(split_points
, 0, sizeof(split_points
));
1891 /* Mark all used registers as fully splittable */
1892 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1893 if (inst
->dst
.file
== GRF
) {
1894 int reg
= vgrf_to_reg
[inst
->dst
.reg
];
1895 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->dst
.reg
]; j
++)
1896 split_points
[reg
+ j
] = true;
1899 for (int i
= 0; i
< inst
->sources
; i
++) {
1900 if (inst
->src
[i
].file
== GRF
) {
1901 int reg
= vgrf_to_reg
[inst
->src
[i
].reg
];
1902 for (unsigned j
= 1; j
< this->alloc
.sizes
[inst
->src
[i
].reg
]; j
++)
1903 split_points
[reg
+ j
] = true;
1908 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1909 if (inst
->dst
.file
== GRF
) {
1910 int reg
= vgrf_to_reg
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
1911 for (int j
= 1; j
< inst
->regs_written
; j
++)
1912 split_points
[reg
+ j
] = false;
1914 for (int i
= 0; i
< inst
->sources
; i
++) {
1915 if (inst
->src
[i
].file
== GRF
) {
1916 int reg
= vgrf_to_reg
[inst
->src
[i
].reg
] + inst
->src
[i
].reg_offset
;
1917 for (int j
= 1; j
< inst
->regs_read(i
); j
++)
1918 split_points
[reg
+ j
] = false;
1923 int new_virtual_grf
[reg_count
];
1924 int new_reg_offset
[reg_count
];
1927 for (int i
= 0; i
< num_vars
; i
++) {
1928 /* The first one should always be 0 as a quick sanity check. */
1929 assert(split_points
[reg
] == false);
1932 new_reg_offset
[reg
] = 0;
1937 for (unsigned j
= 1; j
< alloc
.sizes
[i
]; j
++) {
1938 /* If this is a split point, reset the offset to 0 and allocate a
1939 * new virtual GRF for the previous offset many registers
1941 if (split_points
[reg
]) {
1942 assert(offset
<= MAX_VGRF_SIZE
);
1943 int grf
= alloc
.allocate(offset
);
1944 for (int k
= reg
- offset
; k
< reg
; k
++)
1945 new_virtual_grf
[k
] = grf
;
1948 new_reg_offset
[reg
] = offset
;
1953 /* The last one gets the original register number */
1954 assert(offset
<= MAX_VGRF_SIZE
);
1955 alloc
.sizes
[i
] = offset
;
1956 for (int k
= reg
- offset
; k
< reg
; k
++)
1957 new_virtual_grf
[k
] = i
;
1959 assert(reg
== reg_count
);
1961 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
1962 if (inst
->dst
.file
== GRF
) {
1963 reg
= vgrf_to_reg
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
1964 inst
->dst
.reg
= new_virtual_grf
[reg
];
1965 inst
->dst
.reg_offset
= new_reg_offset
[reg
];
1966 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1968 for (int i
= 0; i
< inst
->sources
; i
++) {
1969 if (inst
->src
[i
].file
== GRF
) {
1970 reg
= vgrf_to_reg
[inst
->src
[i
].reg
] + inst
->src
[i
].reg_offset
;
1971 inst
->src
[i
].reg
= new_virtual_grf
[reg
];
1972 inst
->src
[i
].reg_offset
= new_reg_offset
[reg
];
1973 assert((unsigned)new_reg_offset
[reg
] < alloc
.sizes
[new_virtual_grf
[reg
]]);
1977 invalidate_live_intervals();
1981 * Remove unused virtual GRFs and compact the virtual_grf_* arrays.
1983 * During code generation, we create tons of temporary variables, many of
1984 * which get immediately killed and are never used again. Yet, in later
1985 * optimization and analysis passes, such as compute_live_intervals, we need
1986 * to loop over all the virtual GRFs. Compacting them can save a lot of
1990 fs_visitor::compact_virtual_grfs()
1992 bool progress
= false;
1993 int remap_table
[this->alloc
.count
];
1994 memset(remap_table
, -1, sizeof(remap_table
));
1996 /* Mark which virtual GRFs are used. */
1997 foreach_block_and_inst(block
, const fs_inst
, inst
, cfg
) {
1998 if (inst
->dst
.file
== GRF
)
1999 remap_table
[inst
->dst
.reg
] = 0;
2001 for (int i
= 0; i
< inst
->sources
; i
++) {
2002 if (inst
->src
[i
].file
== GRF
)
2003 remap_table
[inst
->src
[i
].reg
] = 0;
2007 /* Compact the GRF arrays. */
2009 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
2010 if (remap_table
[i
] == -1) {
2011 /* We just found an unused register. This means that we are
2012 * actually going to compact something.
2016 remap_table
[i
] = new_index
;
2017 alloc
.sizes
[new_index
] = alloc
.sizes
[i
];
2018 invalidate_live_intervals();
2023 this->alloc
.count
= new_index
;
2025 /* Patch all the instructions to use the newly renumbered registers */
2026 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2027 if (inst
->dst
.file
== GRF
)
2028 inst
->dst
.reg
= remap_table
[inst
->dst
.reg
];
2030 for (int i
= 0; i
< inst
->sources
; i
++) {
2031 if (inst
->src
[i
].file
== GRF
)
2032 inst
->src
[i
].reg
= remap_table
[inst
->src
[i
].reg
];
2036 /* Patch all the references to delta_xy, since they're used in register
2037 * allocation. If they're unused, switch them to BAD_FILE so we don't
2038 * think some random VGRF is delta_xy.
2040 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2041 if (delta_xy
[i
].file
== GRF
) {
2042 if (remap_table
[delta_xy
[i
].reg
] != -1) {
2043 delta_xy
[i
].reg
= remap_table
[delta_xy
[i
].reg
];
2045 delta_xy
[i
].file
= BAD_FILE
;
2054 * Implements array access of uniforms by inserting a
2055 * PULL_CONSTANT_LOAD instruction.
2057 * Unlike temporary GRF array access (where we don't support it due to
2058 * the difficulty of doing relative addressing on instruction
2059 * destinations), we could potentially do array access of uniforms
2060 * that were loaded in GRF space as push constants. In real-world
2061 * usage we've seen, though, the arrays being used are always larger
2062 * than we could load as push constants, so just always move all
2063 * uniform array access out to a pull constant buffer.
2066 fs_visitor::move_uniform_array_access_to_pull_constants()
2068 if (dispatch_width
!= 8)
2071 pull_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2072 memset(pull_constant_loc
, -1, sizeof(pull_constant_loc
[0]) * uniforms
);
2074 /* Walk through and find array access of uniforms. Put a copy of that
2075 * uniform in the pull constant buffer.
2077 * Note that we don't move constant-indexed accesses to arrays. No
2078 * testing has been done of the performance impact of this choice.
2080 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2081 for (int i
= 0 ; i
< inst
->sources
; i
++) {
2082 if (inst
->src
[i
].file
!= UNIFORM
|| !inst
->src
[i
].reladdr
)
2085 int uniform
= inst
->src
[i
].reg
;
2087 /* If this array isn't already present in the pull constant buffer,
2090 if (pull_constant_loc
[uniform
] == -1) {
2091 const gl_constant_value
**values
= &stage_prog_data
->param
[uniform
];
2093 assert(param_size
[uniform
]);
2095 for (int j
= 0; j
< param_size
[uniform
]; j
++) {
2096 pull_constant_loc
[uniform
+ j
] = stage_prog_data
->nr_pull_params
;
2098 stage_prog_data
->pull_param
[stage_prog_data
->nr_pull_params
++] =
2107 * Assign UNIFORM file registers to either push constants or pull constants.
2109 * We allow a fragment shader to have more than the specified minimum
2110 * maximum number of fragment shader uniform components (64). If
2111 * there are too many of these, they'd fill up all of register space.
2112 * So, this will push some of them out to the pull constant buffer and
2113 * update the program to load them.
2116 fs_visitor::assign_constant_locations()
2118 /* Only the first compile (SIMD8 mode) gets to decide on locations. */
2119 if (dispatch_width
!= 8)
2122 /* Find which UNIFORM registers are still in use. */
2123 bool is_live
[uniforms
];
2124 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2128 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2129 for (int i
= 0; i
< inst
->sources
; i
++) {
2130 if (inst
->src
[i
].file
!= UNIFORM
)
2133 int constant_nr
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
2134 if (constant_nr
>= 0 && constant_nr
< (int) uniforms
)
2135 is_live
[constant_nr
] = true;
2139 /* Only allow 16 registers (128 uniform components) as push constants.
2141 * Just demote the end of the list. We could probably do better
2142 * here, demoting things that are rarely used in the program first.
2144 * If changing this value, note the limitation about total_regs in
2147 unsigned int max_push_components
= 16 * 8;
2148 unsigned int num_push_constants
= 0;
2150 push_constant_loc
= ralloc_array(mem_ctx
, int, uniforms
);
2152 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2153 if (!is_live
[i
] || pull_constant_loc
[i
] != -1) {
2154 /* This UNIFORM register is either dead, or has already been demoted
2155 * to a pull const. Mark it as no longer living in the param[] array.
2157 push_constant_loc
[i
] = -1;
2161 if (num_push_constants
< max_push_components
) {
2162 /* Retain as a push constant. Record the location in the params[]
2165 push_constant_loc
[i
] = num_push_constants
++;
2167 /* Demote to a pull constant. */
2168 push_constant_loc
[i
] = -1;
2170 int pull_index
= stage_prog_data
->nr_pull_params
++;
2171 stage_prog_data
->pull_param
[pull_index
] = stage_prog_data
->param
[i
];
2172 pull_constant_loc
[i
] = pull_index
;
2176 stage_prog_data
->nr_params
= num_push_constants
;
2178 /* Up until now, the param[] array has been indexed by reg + reg_offset
2179 * of UNIFORM registers. Condense it to only contain the uniforms we
2180 * chose to upload as push constants.
2182 for (unsigned int i
= 0; i
< uniforms
; i
++) {
2183 int remapped
= push_constant_loc
[i
];
2188 assert(remapped
<= (int)i
);
2189 stage_prog_data
->param
[remapped
] = stage_prog_data
->param
[i
];
2194 * Replace UNIFORM register file access with either UNIFORM_PULL_CONSTANT_LOAD
2195 * or VARYING_PULL_CONSTANT_LOAD instructions which load values into VGRFs.
2198 fs_visitor::demote_pull_constants()
2200 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
2201 for (int i
= 0; i
< inst
->sources
; i
++) {
2202 if (inst
->src
[i
].file
!= UNIFORM
)
2206 unsigned location
= inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
;
2207 if (location
>= uniforms
) /* Out of bounds access */
2210 pull_index
= pull_constant_loc
[location
];
2212 if (pull_index
== -1)
2215 /* Set up the annotation tracking for new generated instructions. */
2217 current_annotation
= inst
->annotation
;
2219 fs_reg
surf_index(stage_prog_data
->binding_table
.pull_constants_start
);
2220 fs_reg dst
= vgrf(glsl_type::float_type
);
2222 /* Generate a pull load into dst. */
2223 if (inst
->src
[i
].reladdr
) {
2224 exec_list list
= VARYING_PULL_CONSTANT_LOAD(dst
,
2226 *inst
->src
[i
].reladdr
,
2228 inst
->insert_before(block
, &list
);
2229 inst
->src
[i
].reladdr
= NULL
;
2231 fs_reg offset
= fs_reg((unsigned)(pull_index
* 4) & ~15);
2233 new(mem_ctx
) fs_inst(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
, 8,
2234 dst
, surf_index
, offset
);
2235 inst
->insert_before(block
, pull
);
2236 inst
->src
[i
].set_smear(pull_index
& 3);
2239 /* Rewrite the instruction to use the temporary VGRF. */
2240 inst
->src
[i
].file
= GRF
;
2241 inst
->src
[i
].reg
= dst
.reg
;
2242 inst
->src
[i
].reg_offset
= 0;
2243 inst
->src
[i
].width
= dispatch_width
;
2246 invalidate_live_intervals();
2250 fs_visitor::opt_algebraic()
2252 bool progress
= false;
2254 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2255 switch (inst
->opcode
) {
2256 case BRW_OPCODE_MOV
:
2257 if (inst
->src
[0].file
!= IMM
)
2260 if (inst
->saturate
) {
2261 if (inst
->dst
.type
!= inst
->src
[0].type
)
2262 assert(!"unimplemented: saturate mixed types");
2264 if (brw_saturate_immediate(inst
->dst
.type
,
2265 &inst
->src
[0].fixed_hw_reg
)) {
2266 inst
->saturate
= false;
2272 case BRW_OPCODE_MUL
:
2273 if (inst
->src
[1].file
!= IMM
)
2277 if (inst
->src
[1].is_one()) {
2278 inst
->opcode
= BRW_OPCODE_MOV
;
2279 inst
->src
[1] = reg_undef
;
2285 if (inst
->src
[1].is_negative_one()) {
2286 inst
->opcode
= BRW_OPCODE_MOV
;
2287 inst
->src
[0].negate
= !inst
->src
[0].negate
;
2288 inst
->src
[1] = reg_undef
;
2294 if (inst
->src
[1].is_zero()) {
2295 inst
->opcode
= BRW_OPCODE_MOV
;
2296 inst
->src
[0] = inst
->src
[1];
2297 inst
->src
[1] = reg_undef
;
2302 if (inst
->src
[0].file
== IMM
) {
2303 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2304 inst
->opcode
= BRW_OPCODE_MOV
;
2305 inst
->src
[0].fixed_hw_reg
.dw1
.f
*= inst
->src
[1].fixed_hw_reg
.dw1
.f
;
2306 inst
->src
[1] = reg_undef
;
2311 case BRW_OPCODE_ADD
:
2312 if (inst
->src
[1].file
!= IMM
)
2316 if (inst
->src
[1].is_zero()) {
2317 inst
->opcode
= BRW_OPCODE_MOV
;
2318 inst
->src
[1] = reg_undef
;
2323 if (inst
->src
[0].file
== IMM
) {
2324 assert(inst
->src
[0].type
== BRW_REGISTER_TYPE_F
);
2325 inst
->opcode
= BRW_OPCODE_MOV
;
2326 inst
->src
[0].fixed_hw_reg
.dw1
.f
+= inst
->src
[1].fixed_hw_reg
.dw1
.f
;
2327 inst
->src
[1] = reg_undef
;
2333 if (inst
->src
[0].equals(inst
->src
[1])) {
2334 inst
->opcode
= BRW_OPCODE_MOV
;
2335 inst
->src
[1] = reg_undef
;
2340 case BRW_OPCODE_LRP
:
2341 if (inst
->src
[1].equals(inst
->src
[2])) {
2342 inst
->opcode
= BRW_OPCODE_MOV
;
2343 inst
->src
[0] = inst
->src
[1];
2344 inst
->src
[1] = reg_undef
;
2345 inst
->src
[2] = reg_undef
;
2350 case BRW_OPCODE_CMP
:
2351 if (inst
->conditional_mod
== BRW_CONDITIONAL_GE
&&
2353 inst
->src
[0].negate
&&
2354 inst
->src
[1].is_zero()) {
2355 inst
->src
[0].abs
= false;
2356 inst
->src
[0].negate
= false;
2357 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2362 case BRW_OPCODE_SEL
:
2363 if (inst
->src
[0].equals(inst
->src
[1])) {
2364 inst
->opcode
= BRW_OPCODE_MOV
;
2365 inst
->src
[1] = reg_undef
;
2366 inst
->predicate
= BRW_PREDICATE_NONE
;
2367 inst
->predicate_inverse
= false;
2369 } else if (inst
->saturate
&& inst
->src
[1].file
== IMM
) {
2370 switch (inst
->conditional_mod
) {
2371 case BRW_CONDITIONAL_LE
:
2372 case BRW_CONDITIONAL_L
:
2373 switch (inst
->src
[1].type
) {
2374 case BRW_REGISTER_TYPE_F
:
2375 if (inst
->src
[1].fixed_hw_reg
.dw1
.f
>= 1.0f
) {
2376 inst
->opcode
= BRW_OPCODE_MOV
;
2377 inst
->src
[1] = reg_undef
;
2378 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2386 case BRW_CONDITIONAL_GE
:
2387 case BRW_CONDITIONAL_G
:
2388 switch (inst
->src
[1].type
) {
2389 case BRW_REGISTER_TYPE_F
:
2390 if (inst
->src
[1].fixed_hw_reg
.dw1
.f
<= 0.0f
) {
2391 inst
->opcode
= BRW_OPCODE_MOV
;
2392 inst
->src
[1] = reg_undef
;
2393 inst
->conditional_mod
= BRW_CONDITIONAL_NONE
;
2405 case BRW_OPCODE_MAD
:
2406 if (inst
->src
[1].is_zero() || inst
->src
[2].is_zero()) {
2407 inst
->opcode
= BRW_OPCODE_MOV
;
2408 inst
->src
[1] = reg_undef
;
2409 inst
->src
[2] = reg_undef
;
2411 } else if (inst
->src
[0].is_zero()) {
2412 inst
->opcode
= BRW_OPCODE_MUL
;
2413 inst
->src
[0] = inst
->src
[2];
2414 inst
->src
[2] = reg_undef
;
2416 } else if (inst
->src
[1].is_one()) {
2417 inst
->opcode
= BRW_OPCODE_ADD
;
2418 inst
->src
[1] = inst
->src
[2];
2419 inst
->src
[2] = reg_undef
;
2421 } else if (inst
->src
[2].is_one()) {
2422 inst
->opcode
= BRW_OPCODE_ADD
;
2423 inst
->src
[2] = reg_undef
;
2425 } else if (inst
->src
[1].file
== IMM
&& inst
->src
[2].file
== IMM
) {
2426 inst
->opcode
= BRW_OPCODE_ADD
;
2427 inst
->src
[1].fixed_hw_reg
.dw1
.f
*= inst
->src
[2].fixed_hw_reg
.dw1
.f
;
2428 inst
->src
[2] = reg_undef
;
2432 case SHADER_OPCODE_RCP
: {
2433 fs_inst
*prev
= (fs_inst
*)inst
->prev
;
2434 if (prev
->opcode
== SHADER_OPCODE_SQRT
) {
2435 if (inst
->src
[0].equals(prev
->dst
)) {
2436 inst
->opcode
= SHADER_OPCODE_RSQ
;
2437 inst
->src
[0] = prev
->src
[0];
2443 case SHADER_OPCODE_BROADCAST
:
2444 if (is_uniform(inst
->src
[0])) {
2445 inst
->opcode
= BRW_OPCODE_MOV
;
2447 inst
->force_writemask_all
= true;
2449 } else if (inst
->src
[1].file
== IMM
) {
2450 inst
->opcode
= BRW_OPCODE_MOV
;
2451 inst
->src
[0] = component(inst
->src
[0],
2452 inst
->src
[1].fixed_hw_reg
.dw1
.ud
);
2454 inst
->force_writemask_all
= true;
2463 /* Swap if src[0] is immediate. */
2464 if (progress
&& inst
->is_commutative()) {
2465 if (inst
->src
[0].file
== IMM
) {
2466 fs_reg tmp
= inst
->src
[1];
2467 inst
->src
[1] = inst
->src
[0];
2476 * Optimize sample messages that have constant zero values for the trailing
2477 * texture coordinates. We can just reduce the message length for these
2478 * instructions instead of reserving a register for it. Trailing parameters
2479 * that aren't sent default to zero anyway. This will cause the dead code
2480 * eliminator to remove the MOV instruction that would otherwise be emitted to
2481 * set up the zero value.
2484 fs_visitor::opt_zero_samples()
2486 /* Gen4 infers the texturing opcode based on the message length so we can't
2489 if (devinfo
->gen
< 5)
2492 bool progress
= false;
2494 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2495 if (!inst
->is_tex())
2498 fs_inst
*load_payload
= (fs_inst
*) inst
->prev
;
2500 if (load_payload
->is_head_sentinel() ||
2501 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2504 /* We don't want to remove the message header or the first parameter.
2505 * Removing the first parameter is not allowed, see the Haswell PRM
2506 * volume 7, page 149:
2508 * "Parameter 0 is required except for the sampleinfo message, which
2509 * has no parameter 0"
2511 while (inst
->mlen
> inst
->header_size
+ dispatch_width
/ 8 &&
2512 load_payload
->src
[(inst
->mlen
- inst
->header_size
) /
2513 (dispatch_width
/ 8) +
2514 inst
->header_size
- 1].is_zero()) {
2515 inst
->mlen
-= dispatch_width
/ 8;
2521 invalidate_live_intervals();
2527 * Optimize sample messages which are followed by the final RT write.
2529 * CHV, and GEN9+ can mark a texturing SEND instruction with EOT to have its
2530 * results sent directly to the framebuffer, bypassing the EU. Recognize the
2531 * final texturing results copied to the framebuffer write payload and modify
2532 * them to write to the framebuffer directly.
2535 fs_visitor::opt_sampler_eot()
2537 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2539 if (stage
!= MESA_SHADER_FRAGMENT
)
2542 if (devinfo
->gen
< 9 && !devinfo
->is_cherryview
)
2545 /* FINISHME: It should be possible to implement this optimization when there
2546 * are multiple drawbuffers.
2548 if (key
->nr_color_regions
!= 1)
2551 /* Look for a texturing instruction immediately before the final FB_WRITE. */
2552 fs_inst
*fb_write
= (fs_inst
*) cfg
->blocks
[cfg
->num_blocks
- 1]->end();
2553 assert(fb_write
->eot
);
2554 assert(fb_write
->opcode
== FS_OPCODE_FB_WRITE
);
2556 fs_inst
*tex_inst
= (fs_inst
*) fb_write
->prev
;
2558 /* There wasn't one; nothing to do. */
2559 if (unlikely(tex_inst
->is_head_sentinel()) || !tex_inst
->is_tex())
2562 /* This optimisation doesn't seem to work for textureGather for some
2563 * reason. I can't find any documentation or known workarounds to indicate
2564 * that this is expected, but considering that it is probably pretty
2565 * unlikely that a shader would directly write out the results from
2566 * textureGather we might as well just disable it.
2568 if (tex_inst
->opcode
== SHADER_OPCODE_TG4
||
2569 tex_inst
->opcode
== SHADER_OPCODE_TG4_OFFSET
)
2572 /* If there's no header present, we need to munge the LOAD_PAYLOAD as well.
2573 * It's very likely to be the previous instruction.
2575 fs_inst
*load_payload
= (fs_inst
*) tex_inst
->prev
;
2576 if (load_payload
->is_head_sentinel() ||
2577 load_payload
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
2580 assert(!tex_inst
->eot
); /* We can't get here twice */
2581 assert((tex_inst
->offset
& (0xff << 24)) == 0);
2583 tex_inst
->offset
|= fb_write
->target
<< 24;
2584 tex_inst
->eot
= true;
2585 tex_inst
->dst
= bld
.null_reg_ud();
2586 fb_write
->remove(cfg
->blocks
[cfg
->num_blocks
- 1]);
2588 /* If a header is present, marking the eot is sufficient. Otherwise, we need
2589 * to create a new LOAD_PAYLOAD command with the same sources and a space
2590 * saved for the header. Using a new destination register not only makes sure
2591 * we have enough space, but it will make sure the dead code eliminator kills
2592 * the instruction that this will replace.
2594 if (tex_inst
->header_size
!= 0)
2597 fs_reg send_header
= bld
.vgrf(BRW_REGISTER_TYPE_F
,
2598 load_payload
->sources
+ 1);
2599 fs_reg
*new_sources
=
2600 ralloc_array(mem_ctx
, fs_reg
, load_payload
->sources
+ 1);
2602 new_sources
[0] = fs_reg();
2603 for (int i
= 0; i
< load_payload
->sources
; i
++)
2604 new_sources
[i
+1] = load_payload
->src
[i
];
2606 /* The LOAD_PAYLOAD helper seems like the obvious choice here. However, it
2607 * requires a lot of information about the sources to appropriately figure
2608 * out the number of registers needed to be used. Given this stage in our
2609 * optimization, we may not have the appropriate GRFs required by
2610 * LOAD_PAYLOAD at this point (copy propagation). Therefore, we need to
2611 * manually emit the instruction.
2613 fs_inst
*new_load_payload
= new(mem_ctx
) fs_inst(SHADER_OPCODE_LOAD_PAYLOAD
,
2614 load_payload
->exec_size
,
2617 load_payload
->sources
+ 1);
2619 new_load_payload
->regs_written
= load_payload
->regs_written
+ 1;
2620 new_load_payload
->header_size
= 1;
2622 tex_inst
->header_size
= 1;
2623 tex_inst
->insert_before(cfg
->blocks
[cfg
->num_blocks
- 1], new_load_payload
);
2624 tex_inst
->src
[0] = send_header
;
2630 fs_visitor::opt_register_renaming()
2632 bool progress
= false;
2635 int remap
[alloc
.count
];
2636 memset(remap
, -1, sizeof(int) * alloc
.count
);
2638 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
2639 if (inst
->opcode
== BRW_OPCODE_IF
|| inst
->opcode
== BRW_OPCODE_DO
) {
2641 } else if (inst
->opcode
== BRW_OPCODE_ENDIF
||
2642 inst
->opcode
== BRW_OPCODE_WHILE
) {
2646 /* Rewrite instruction sources. */
2647 for (int i
= 0; i
< inst
->sources
; i
++) {
2648 if (inst
->src
[i
].file
== GRF
&&
2649 remap
[inst
->src
[i
].reg
] != -1 &&
2650 remap
[inst
->src
[i
].reg
] != inst
->src
[i
].reg
) {
2651 inst
->src
[i
].reg
= remap
[inst
->src
[i
].reg
];
2656 const int dst
= inst
->dst
.reg
;
2659 inst
->dst
.file
== GRF
&&
2660 alloc
.sizes
[inst
->dst
.reg
] == inst
->dst
.width
/ 8 &&
2661 !inst
->is_partial_write()) {
2662 if (remap
[dst
] == -1) {
2665 remap
[dst
] = alloc
.allocate(inst
->dst
.width
/ 8);
2666 inst
->dst
.reg
= remap
[dst
];
2669 } else if (inst
->dst
.file
== GRF
&&
2671 remap
[dst
] != dst
) {
2672 inst
->dst
.reg
= remap
[dst
];
2678 invalidate_live_intervals();
2680 for (unsigned i
= 0; i
< ARRAY_SIZE(delta_xy
); i
++) {
2681 if (delta_xy
[i
].file
== GRF
&& remap
[delta_xy
[i
].reg
] != -1) {
2682 delta_xy
[i
].reg
= remap
[delta_xy
[i
].reg
];
2691 * Remove redundant or useless discard jumps.
2693 * For example, we can eliminate jumps in the following sequence:
2695 * discard-jump (redundant with the next jump)
2696 * discard-jump (useless; jumps to the next instruction)
2700 fs_visitor::opt_redundant_discard_jumps()
2702 bool progress
= false;
2704 bblock_t
*last_bblock
= cfg
->blocks
[cfg
->num_blocks
- 1];
2706 fs_inst
*placeholder_halt
= NULL
;
2707 foreach_inst_in_block_reverse(fs_inst
, inst
, last_bblock
) {
2708 if (inst
->opcode
== FS_OPCODE_PLACEHOLDER_HALT
) {
2709 placeholder_halt
= inst
;
2714 if (!placeholder_halt
)
2717 /* Delete any HALTs immediately before the placeholder halt. */
2718 for (fs_inst
*prev
= (fs_inst
*) placeholder_halt
->prev
;
2719 !prev
->is_head_sentinel() && prev
->opcode
== FS_OPCODE_DISCARD_JUMP
;
2720 prev
= (fs_inst
*) placeholder_halt
->prev
) {
2721 prev
->remove(last_bblock
);
2726 invalidate_live_intervals();
2732 fs_visitor::compute_to_mrf()
2734 bool progress
= false;
2737 /* No MRFs on Gen >= 7. */
2738 if (devinfo
->gen
>= 7)
2741 calculate_live_intervals();
2743 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2747 if (inst
->opcode
!= BRW_OPCODE_MOV
||
2748 inst
->is_partial_write() ||
2749 inst
->dst
.file
!= MRF
|| inst
->src
[0].file
!= GRF
||
2750 inst
->dst
.type
!= inst
->src
[0].type
||
2751 inst
->src
[0].abs
|| inst
->src
[0].negate
||
2752 !inst
->src
[0].is_contiguous() ||
2753 inst
->src
[0].subreg_offset
)
2756 /* Work out which hardware MRF registers are written by this
2759 int mrf_low
= inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
2761 if (inst
->dst
.reg
& BRW_MRF_COMPR4
) {
2762 mrf_high
= mrf_low
+ 4;
2763 } else if (inst
->exec_size
== 16) {
2764 mrf_high
= mrf_low
+ 1;
2769 /* Can't compute-to-MRF this GRF if someone else was going to
2772 if (this->virtual_grf_end
[inst
->src
[0].reg
] > ip
)
2775 /* Found a move of a GRF to a MRF. Let's see if we can go
2776 * rewrite the thing that made this GRF to write into the MRF.
2778 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
, block
) {
2779 if (scan_inst
->dst
.file
== GRF
&&
2780 scan_inst
->dst
.reg
== inst
->src
[0].reg
) {
2781 /* Found the last thing to write our reg we want to turn
2782 * into a compute-to-MRF.
2785 /* If this one instruction didn't populate all the
2786 * channels, bail. We might be able to rewrite everything
2787 * that writes that reg, but it would require smarter
2788 * tracking to delay the rewriting until complete success.
2790 if (scan_inst
->is_partial_write())
2793 /* Things returning more than one register would need us to
2794 * understand coalescing out more than one MOV at a time.
2796 if (scan_inst
->regs_written
> scan_inst
->dst
.width
/ 8)
2799 /* SEND instructions can't have MRF as a destination. */
2800 if (scan_inst
->mlen
)
2803 if (devinfo
->gen
== 6) {
2804 /* gen6 math instructions must have the destination be
2805 * GRF, so no compute-to-MRF for them.
2807 if (scan_inst
->is_math()) {
2812 if (scan_inst
->dst
.reg_offset
== inst
->src
[0].reg_offset
) {
2813 /* Found the creator of our MRF's source value. */
2814 scan_inst
->dst
.file
= MRF
;
2815 scan_inst
->dst
.reg
= inst
->dst
.reg
;
2816 scan_inst
->saturate
|= inst
->saturate
;
2817 inst
->remove(block
);
2823 /* We don't handle control flow here. Most computation of
2824 * values that end up in MRFs are shortly before the MRF
2827 if (block
->start() == scan_inst
)
2830 /* You can't read from an MRF, so if someone else reads our
2831 * MRF's source GRF that we wanted to rewrite, that stops us.
2833 bool interfered
= false;
2834 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
2835 if (scan_inst
->src
[i
].file
== GRF
&&
2836 scan_inst
->src
[i
].reg
== inst
->src
[0].reg
&&
2837 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
2844 if (scan_inst
->dst
.file
== MRF
) {
2845 /* If somebody else writes our MRF here, we can't
2846 * compute-to-MRF before that.
2848 int scan_mrf_low
= scan_inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
2851 if (scan_inst
->dst
.reg
& BRW_MRF_COMPR4
) {
2852 scan_mrf_high
= scan_mrf_low
+ 4;
2853 } else if (scan_inst
->exec_size
== 16) {
2854 scan_mrf_high
= scan_mrf_low
+ 1;
2856 scan_mrf_high
= scan_mrf_low
;
2859 if (mrf_low
== scan_mrf_low
||
2860 mrf_low
== scan_mrf_high
||
2861 mrf_high
== scan_mrf_low
||
2862 mrf_high
== scan_mrf_high
) {
2867 if (scan_inst
->mlen
> 0 && scan_inst
->base_mrf
!= -1) {
2868 /* Found a SEND instruction, which means that there are
2869 * live values in MRFs from base_mrf to base_mrf +
2870 * scan_inst->mlen - 1. Don't go pushing our MRF write up
2873 if (mrf_low
>= scan_inst
->base_mrf
&&
2874 mrf_low
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2877 if (mrf_high
>= scan_inst
->base_mrf
&&
2878 mrf_high
< scan_inst
->base_mrf
+ scan_inst
->mlen
) {
2886 invalidate_live_intervals();
2892 * Eliminate FIND_LIVE_CHANNEL instructions occurring outside any control
2893 * flow. We could probably do better here with some form of divergence
2897 fs_visitor::eliminate_find_live_channel()
2899 bool progress
= false;
2902 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
2903 switch (inst
->opcode
) {
2909 case BRW_OPCODE_ENDIF
:
2910 case BRW_OPCODE_WHILE
:
2914 case FS_OPCODE_DISCARD_JUMP
:
2915 /* This can potentially make control flow non-uniform until the end
2920 case SHADER_OPCODE_FIND_LIVE_CHANNEL
:
2922 inst
->opcode
= BRW_OPCODE_MOV
;
2923 inst
->src
[0] = fs_reg(0);
2925 inst
->force_writemask_all
= true;
2939 * Once we've generated code, try to convert normal FS_OPCODE_FB_WRITE
2940 * instructions to FS_OPCODE_REP_FB_WRITE.
2943 fs_visitor::emit_repclear_shader()
2945 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
2947 int color_mrf
= base_mrf
+ 2;
2949 fs_inst
*mov
= emit(MOV(vec4(brw_message_reg(color_mrf
)),
2950 fs_reg(UNIFORM
, 0, BRW_REGISTER_TYPE_F
)));
2951 mov
->force_writemask_all
= true;
2954 if (key
->nr_color_regions
== 1) {
2955 write
= emit(FS_OPCODE_REP_FB_WRITE
);
2956 write
->saturate
= key
->clamp_fragment_color
;
2957 write
->base_mrf
= color_mrf
;
2959 write
->header_size
= 0;
2962 assume(key
->nr_color_regions
> 0);
2963 for (int i
= 0; i
< key
->nr_color_regions
; ++i
) {
2964 write
= emit(FS_OPCODE_REP_FB_WRITE
);
2965 write
->saturate
= key
->clamp_fragment_color
;
2966 write
->base_mrf
= base_mrf
;
2968 write
->header_size
= 2;
2976 assign_constant_locations();
2977 assign_curb_setup();
2979 /* Now that we have the uniform assigned, go ahead and force it to a vec4. */
2980 assert(mov
->src
[0].file
== HW_REG
);
2981 mov
->src
[0] = brw_vec4_grf(mov
->src
[0].fixed_hw_reg
.nr
, 0);
2985 * Walks through basic blocks, looking for repeated MRF writes and
2986 * removing the later ones.
2989 fs_visitor::remove_duplicate_mrf_writes()
2991 fs_inst
*last_mrf_move
[16];
2992 bool progress
= false;
2994 /* Need to update the MRF tracking for compressed instructions. */
2995 if (dispatch_width
== 16)
2998 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3000 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3001 if (inst
->is_control_flow()) {
3002 memset(last_mrf_move
, 0, sizeof(last_mrf_move
));
3005 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3006 inst
->dst
.file
== MRF
) {
3007 fs_inst
*prev_inst
= last_mrf_move
[inst
->dst
.reg
];
3008 if (prev_inst
&& inst
->equals(prev_inst
)) {
3009 inst
->remove(block
);
3015 /* Clear out the last-write records for MRFs that were overwritten. */
3016 if (inst
->dst
.file
== MRF
) {
3017 last_mrf_move
[inst
->dst
.reg
] = NULL
;
3020 if (inst
->mlen
> 0 && inst
->base_mrf
!= -1) {
3021 /* Found a SEND instruction, which will include two or fewer
3022 * implied MRF writes. We could do better here.
3024 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
3025 last_mrf_move
[inst
->base_mrf
+ i
] = NULL
;
3029 /* Clear out any MRF move records whose sources got overwritten. */
3030 if (inst
->dst
.file
== GRF
) {
3031 for (unsigned int i
= 0; i
< ARRAY_SIZE(last_mrf_move
); i
++) {
3032 if (last_mrf_move
[i
] &&
3033 last_mrf_move
[i
]->src
[0].reg
== inst
->dst
.reg
) {
3034 last_mrf_move
[i
] = NULL
;
3039 if (inst
->opcode
== BRW_OPCODE_MOV
&&
3040 inst
->dst
.file
== MRF
&&
3041 inst
->src
[0].file
== GRF
&&
3042 !inst
->is_partial_write()) {
3043 last_mrf_move
[inst
->dst
.reg
] = inst
;
3048 invalidate_live_intervals();
3054 clear_deps_for_inst_src(fs_inst
*inst
, bool *deps
, int first_grf
, int grf_len
)
3056 /* Clear the flag for registers that actually got read (as expected). */
3057 for (int i
= 0; i
< inst
->sources
; i
++) {
3059 if (inst
->src
[i
].file
== GRF
) {
3060 grf
= inst
->src
[i
].reg
;
3061 } else if (inst
->src
[i
].file
== HW_REG
&&
3062 inst
->src
[i
].fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
) {
3063 grf
= inst
->src
[i
].fixed_hw_reg
.nr
;
3068 if (grf
>= first_grf
&&
3069 grf
< first_grf
+ grf_len
) {
3070 deps
[grf
- first_grf
] = false;
3071 if (inst
->exec_size
== 16)
3072 deps
[grf
- first_grf
+ 1] = false;
3078 * Implements this workaround for the original 965:
3080 * "[DevBW, DevCL] Implementation Restrictions: As the hardware does not
3081 * check for post destination dependencies on this instruction, software
3082 * must ensure that there is no destination hazard for the case of ‘write
3083 * followed by a posted write’ shown in the following example.
3086 * 2. send r3.xy <rest of send instruction>
3089 * Due to no post-destination dependency check on the ‘send’, the above
3090 * code sequence could have two instructions (1 and 2) in flight at the
3091 * same time that both consider ‘r3’ as the target of their final writes.
3094 fs_visitor::insert_gen4_pre_send_dependency_workarounds(bblock_t
*block
,
3097 int write_len
= inst
->regs_written
;
3098 int first_write_grf
= inst
->dst
.reg
;
3099 bool needs_dep
[BRW_MAX_MRF
];
3100 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3102 memset(needs_dep
, false, sizeof(needs_dep
));
3103 memset(needs_dep
, true, write_len
);
3105 clear_deps_for_inst_src(inst
, needs_dep
, first_write_grf
, write_len
);
3107 /* Walk backwards looking for writes to registers we're writing which
3108 * aren't read since being written. If we hit the start of the program,
3109 * we assume that there are no outstanding dependencies on entry to the
3112 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
, block
) {
3113 /* If we hit control flow, assume that there *are* outstanding
3114 * dependencies, and force their cleanup before our instruction.
3116 if (block
->start() == scan_inst
) {
3117 for (int i
= 0; i
< write_len
; i
++) {
3119 inst
->insert_before(block
, DEP_RESOLVE_MOV(first_write_grf
+ i
));
3125 /* We insert our reads as late as possible on the assumption that any
3126 * instruction but a MOV that might have left us an outstanding
3127 * dependency has more latency than a MOV.
3129 if (scan_inst
->dst
.file
== GRF
) {
3130 for (int i
= 0; i
< scan_inst
->regs_written
; i
++) {
3131 int reg
= scan_inst
->dst
.reg
+ i
;
3133 if (reg
>= first_write_grf
&&
3134 reg
< first_write_grf
+ write_len
&&
3135 needs_dep
[reg
- first_write_grf
]) {
3136 inst
->insert_before(block
, DEP_RESOLVE_MOV(reg
));
3137 needs_dep
[reg
- first_write_grf
] = false;
3138 if (scan_inst
->exec_size
== 16)
3139 needs_dep
[reg
- first_write_grf
+ 1] = false;
3144 /* Clear the flag for registers that actually got read (as expected). */
3145 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3147 /* Continue the loop only if we haven't resolved all the dependencies */
3149 for (i
= 0; i
< write_len
; i
++) {
3159 * Implements this workaround for the original 965:
3161 * "[DevBW, DevCL] Errata: A destination register from a send can not be
3162 * used as a destination register until after it has been sourced by an
3163 * instruction with a different destination register.
3166 fs_visitor::insert_gen4_post_send_dependency_workarounds(bblock_t
*block
, fs_inst
*inst
)
3168 int write_len
= inst
->regs_written
;
3169 int first_write_grf
= inst
->dst
.reg
;
3170 bool needs_dep
[BRW_MAX_MRF
];
3171 assert(write_len
< (int)sizeof(needs_dep
) - 1);
3173 memset(needs_dep
, false, sizeof(needs_dep
));
3174 memset(needs_dep
, true, write_len
);
3175 /* Walk forwards looking for writes to registers we're writing which aren't
3176 * read before being written.
3178 foreach_inst_in_block_starting_from(fs_inst
, scan_inst
, inst
, block
) {
3179 /* If we hit control flow, force resolve all remaining dependencies. */
3180 if (block
->end() == scan_inst
) {
3181 for (int i
= 0; i
< write_len
; i
++) {
3183 scan_inst
->insert_before(block
,
3184 DEP_RESOLVE_MOV(first_write_grf
+ i
));
3189 /* Clear the flag for registers that actually got read (as expected). */
3190 clear_deps_for_inst_src(scan_inst
, needs_dep
, first_write_grf
, write_len
);
3192 /* We insert our reads as late as possible since they're reading the
3193 * result of a SEND, which has massive latency.
3195 if (scan_inst
->dst
.file
== GRF
&&
3196 scan_inst
->dst
.reg
>= first_write_grf
&&
3197 scan_inst
->dst
.reg
< first_write_grf
+ write_len
&&
3198 needs_dep
[scan_inst
->dst
.reg
- first_write_grf
]) {
3199 scan_inst
->insert_before(block
, DEP_RESOLVE_MOV(scan_inst
->dst
.reg
));
3200 needs_dep
[scan_inst
->dst
.reg
- first_write_grf
] = false;
3203 /* Continue the loop only if we haven't resolved all the dependencies */
3205 for (i
= 0; i
< write_len
; i
++) {
3215 fs_visitor::insert_gen4_send_dependency_workarounds()
3217 if (devinfo
->gen
!= 4 || devinfo
->is_g4x
)
3220 bool progress
= false;
3222 /* Note that we're done with register allocation, so GRF fs_regs always
3223 * have a .reg_offset of 0.
3226 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
3227 if (inst
->mlen
!= 0 && inst
->dst
.file
== GRF
) {
3228 insert_gen4_pre_send_dependency_workarounds(block
, inst
);
3229 insert_gen4_post_send_dependency_workarounds(block
, inst
);
3235 invalidate_live_intervals();
3239 * Turns the generic expression-style uniform pull constant load instruction
3240 * into a hardware-specific series of instructions for loading a pull
3243 * The expression style allows the CSE pass before this to optimize out
3244 * repeated loads from the same offset, and gives the pre-register-allocation
3245 * scheduling full flexibility, while the conversion to native instructions
3246 * allows the post-register-allocation scheduler the best information
3249 * Note that execution masking for setting up pull constant loads is special:
3250 * the channels that need to be written are unrelated to the current execution
3251 * mask, since a later instruction will use one of the result channels as a
3252 * source operand for all 8 or 16 of its channels.
3255 fs_visitor::lower_uniform_pull_constant_loads()
3257 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
3258 if (inst
->opcode
!= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
)
3261 if (devinfo
->gen
>= 7) {
3262 /* The offset arg before was a vec4-aligned byte offset. We need to
3263 * turn it into a dword offset.
3265 fs_reg const_offset_reg
= inst
->src
[1];
3266 assert(const_offset_reg
.file
== IMM
&&
3267 const_offset_reg
.type
== BRW_REGISTER_TYPE_UD
);
3268 const_offset_reg
.fixed_hw_reg
.dw1
.ud
/= 4;
3269 fs_reg payload
= fs_reg(GRF
, alloc
.allocate(1));
3271 /* We have to use a message header on Skylake to get SIMD4x2 mode.
3272 * Reserve space for the register.
3274 if (devinfo
->gen
>= 9) {
3275 payload
.reg_offset
++;
3276 alloc
.sizes
[payload
.reg
] = 2;
3279 /* This is actually going to be a MOV, but since only the first dword
3280 * is accessed, we have a special opcode to do just that one. Note
3281 * that this needs to be an operation that will be considered a def
3282 * by live variable analysis, or register allocation will explode.
3284 fs_inst
*setup
= new(mem_ctx
) fs_inst(FS_OPCODE_SET_SIMD4X2_OFFSET
,
3285 8, payload
, const_offset_reg
);
3286 setup
->force_writemask_all
= true;
3288 setup
->ir
= inst
->ir
;
3289 setup
->annotation
= inst
->annotation
;
3290 inst
->insert_before(block
, setup
);
3292 /* Similarly, this will only populate the first 4 channels of the
3293 * result register (since we only use smear values from 0-3), but we
3294 * don't tell the optimizer.
3296 inst
->opcode
= FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD_GEN7
;
3297 inst
->src
[1] = payload
;
3299 invalidate_live_intervals();
3301 /* Before register allocation, we didn't tell the scheduler about the
3302 * MRF we use. We know it's safe to use this MRF because nothing
3303 * else does except for register spill/unspill, which generates and
3304 * uses its MRF within a single IR instruction.
3306 inst
->base_mrf
= 14;
3313 fs_visitor::lower_load_payload()
3315 bool progress
= false;
3317 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
3318 if (inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
)
3321 assert(inst
->dst
.file
== MRF
|| inst
->dst
.file
== GRF
);
3322 assert(inst
->saturate
== false);
3324 fs_reg dst
= inst
->dst
;
3326 /* Get rid of COMPR4. We'll add it back in if we need it */
3327 if (dst
.file
== MRF
)
3328 dst
.reg
= dst
.reg
& ~BRW_MRF_COMPR4
;
3331 for (uint8_t i
= 0; i
< inst
->header_size
; i
++) {
3332 if (inst
->src
[i
].file
!= BAD_FILE
) {
3333 fs_reg mov_dst
= retype(dst
, BRW_REGISTER_TYPE_UD
);
3334 fs_reg mov_src
= retype(inst
->src
[i
], BRW_REGISTER_TYPE_UD
);
3336 fs_inst
*mov
= MOV(mov_dst
, mov_src
);
3337 mov
->force_writemask_all
= true;
3338 inst
->insert_before(block
, mov
);
3340 dst
= offset(dst
, 1);
3343 dst
.width
= inst
->exec_size
;
3344 if (inst
->dst
.file
== MRF
&& (inst
->dst
.reg
& BRW_MRF_COMPR4
) &&
3345 inst
->exec_size
> 8) {
3346 /* In this case, the payload portion of the LOAD_PAYLOAD isn't
3347 * a straightforward copy. Instead, the result of the
3348 * LOAD_PAYLOAD is treated as interleaved and the first four
3349 * non-header sources are unpacked as:
3360 * This is used for gen <= 5 fb writes.
3362 assert(inst
->exec_size
== 16);
3363 assert(inst
->header_size
+ 4 <= inst
->sources
);
3364 for (uint8_t i
= inst
->header_size
; i
< inst
->header_size
+ 4; i
++) {
3365 if (inst
->src
[i
].file
!= BAD_FILE
) {
3366 if (devinfo
->has_compr4
) {
3367 fs_reg compr4_dst
= retype(dst
, inst
->src
[i
].type
);
3368 compr4_dst
.reg
|= BRW_MRF_COMPR4
;
3370 fs_inst
*mov
= MOV(compr4_dst
, inst
->src
[i
]);
3371 mov
->force_writemask_all
= inst
->force_writemask_all
;
3372 inst
->insert_before(block
, mov
);
3374 /* Platform doesn't have COMPR4. We have to fake it */
3375 fs_reg mov_dst
= retype(dst
, inst
->src
[i
].type
);
3378 fs_inst
*mov
= MOV(mov_dst
, half(inst
->src
[i
], 0));
3379 mov
->force_writemask_all
= inst
->force_writemask_all
;
3380 inst
->insert_before(block
, mov
);
3382 mov
= MOV(offset(mov_dst
, 4), half(inst
->src
[i
], 1));
3383 mov
->force_writemask_all
= inst
->force_writemask_all
;
3384 mov
->force_sechalf
= true;
3385 inst
->insert_before(block
, mov
);
3392 /* The loop above only ever incremented us through the first set
3393 * of 4 registers. However, thanks to the magic of COMPR4, we
3394 * actually wrote to the first 8 registers, so we need to take
3395 * that into account now.
3399 /* The COMPR4 code took care of the first 4 sources. We'll let
3400 * the regular path handle any remaining sources. Yes, we are
3401 * modifying the instruction but we're about to delete it so
3402 * this really doesn't hurt anything.
3404 inst
->header_size
+= 4;
3407 for (uint8_t i
= inst
->header_size
; i
< inst
->sources
; i
++) {
3408 if (inst
->src
[i
].file
!= BAD_FILE
) {
3409 fs_inst
*mov
= MOV(retype(dst
, inst
->src
[i
].type
),
3411 mov
->force_writemask_all
= inst
->force_writemask_all
;
3412 mov
->force_sechalf
= inst
->force_sechalf
;
3413 inst
->insert_before(block
, mov
);
3415 dst
= offset(dst
, 1);
3418 inst
->remove(block
);
3423 invalidate_live_intervals();
3429 fs_visitor::lower_integer_multiplication()
3431 bool progress
= false;
3433 /* Gen8's MUL instruction can do a 32-bit x 32-bit -> 32-bit operation
3434 * directly, but Cherryview cannot.
3436 if (devinfo
->gen
>= 8 && !devinfo
->is_cherryview
)
3439 foreach_block_and_inst_safe(block
, fs_inst
, inst
, cfg
) {
3440 if (inst
->opcode
!= BRW_OPCODE_MUL
||
3441 inst
->dst
.is_accumulator() ||
3442 (inst
->dst
.type
!= BRW_REGISTER_TYPE_D
&&
3443 inst
->dst
.type
!= BRW_REGISTER_TYPE_UD
))
3446 #define insert(instr) inst->insert_before(block, instr)
3448 /* The MUL instruction isn't commutative. On Gen <= 6, only the low
3449 * 16-bits of src0 are read, and on Gen >= 7 only the low 16-bits of
3452 * If multiplying by an immediate value that fits in 16-bits, do a
3453 * single MUL instruction with that value in the proper location.
3455 if (inst
->src
[1].file
== IMM
&&
3456 inst
->src
[1].fixed_hw_reg
.dw1
.ud
< (1 << 16)) {
3457 if (devinfo
->gen
< 7) {
3458 fs_reg
imm(GRF
, alloc
.allocate(dispatch_width
/ 8),
3459 inst
->dst
.type
, dispatch_width
);
3460 insert(MOV(imm
, inst
->src
[1]));
3461 insert(MUL(inst
->dst
, imm
, inst
->src
[0]));
3463 insert(MUL(inst
->dst
, inst
->src
[0], inst
->src
[1]));
3466 /* Gen < 8 (and some Gen8+ low-power parts like Cherryview) cannot
3467 * do 32-bit integer multiplication in one instruction, but instead
3468 * must do a sequence (which actually calculates a 64-bit result):
3470 * mul(8) acc0<1>D g3<8,8,1>D g4<8,8,1>D
3471 * mach(8) null g3<8,8,1>D g4<8,8,1>D
3472 * mov(8) g2<1>D acc0<8,8,1>D
3474 * But on Gen > 6, the ability to use second accumulator register
3475 * (acc1) for non-float data types was removed, preventing a simple
3476 * implementation in SIMD16. A 16-channel result can be calculated by
3477 * executing the three instructions twice in SIMD8, once with quarter
3478 * control of 1Q for the first eight channels and again with 2Q for
3479 * the second eight channels.
3481 * Which accumulator register is implicitly accessed (by AccWrEnable
3482 * for instance) is determined by the quarter control. Unfortunately
3483 * Ivybridge (and presumably Baytrail) has a hardware bug in which an
3484 * implicit accumulator access by an instruction with 2Q will access
3485 * acc1 regardless of whether the data type is usable in acc1.
3487 * Specifically, the 2Q mach(8) writes acc1 which does not exist for
3488 * integer data types.
3490 * Since we only want the low 32-bits of the result, we can do two
3491 * 32-bit x 16-bit multiplies (like the mul and mach are doing), and
3492 * adjust the high result and add them (like the mach is doing):
3494 * mul(8) g7<1>D g3<8,8,1>D g4.0<8,8,1>UW
3495 * mul(8) g8<1>D g3<8,8,1>D g4.1<8,8,1>UW
3496 * shl(8) g9<1>D g8<8,8,1>D 16D
3497 * add(8) g2<1>D g7<8,8,1>D g8<8,8,1>D
3499 * We avoid the shl instruction by realizing that we only want to add
3500 * the low 16-bits of the "high" result to the high 16-bits of the
3501 * "low" result and using proper regioning on the add:
3503 * mul(8) g7<1>D g3<8,8,1>D g4.0<16,8,2>UW
3504 * mul(8) g8<1>D g3<8,8,1>D g4.1<16,8,2>UW
3505 * add(8) g7.1<2>UW g7.1<16,8,2>UW g8<16,8,2>UW
3507 * Since it does not use the (single) accumulator register, we can
3508 * schedule multi-component multiplications much better.
3511 if (inst
->conditional_mod
&& inst
->dst
.is_null()) {
3512 inst
->dst
= fs_reg(GRF
, alloc
.allocate(dispatch_width
/ 8),
3513 inst
->dst
.type
, dispatch_width
);
3515 fs_reg low
= inst
->dst
;
3516 fs_reg
high(GRF
, alloc
.allocate(dispatch_width
/ 8),
3517 inst
->dst
.type
, dispatch_width
);
3519 if (brw
->gen
>= 7) {
3520 fs_reg src1_0_w
= inst
->src
[1];
3521 fs_reg src1_1_w
= inst
->src
[1];
3523 if (inst
->src
[1].file
== IMM
) {
3524 src1_0_w
.fixed_hw_reg
.dw1
.ud
&= 0xffff;
3525 src1_1_w
.fixed_hw_reg
.dw1
.ud
>>= 16;
3527 src1_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3528 src1_0_w
.stride
= 2;
3530 src1_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3531 src1_1_w
.stride
= 2;
3532 src1_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3534 insert(MUL(low
, inst
->src
[0], src1_0_w
));
3535 insert(MUL(high
, inst
->src
[0], src1_1_w
));
3537 fs_reg src0_0_w
= inst
->src
[0];
3538 fs_reg src0_1_w
= inst
->src
[0];
3540 src0_0_w
.type
= BRW_REGISTER_TYPE_UW
;
3541 src0_0_w
.stride
= 2;
3543 src0_1_w
.type
= BRW_REGISTER_TYPE_UW
;
3544 src0_1_w
.stride
= 2;
3545 src0_1_w
.subreg_offset
+= type_sz(BRW_REGISTER_TYPE_UW
);
3547 insert(MUL(low
, src0_0_w
, inst
->src
[1]));
3548 insert(MUL(high
, src0_1_w
, inst
->src
[1]));
3551 fs_reg dst
= inst
->dst
;
3552 dst
.type
= BRW_REGISTER_TYPE_UW
;
3553 dst
.subreg_offset
= 2;
3556 high
.type
= BRW_REGISTER_TYPE_UW
;
3559 low
.type
= BRW_REGISTER_TYPE_UW
;
3560 low
.subreg_offset
= 2;
3563 insert(ADD(dst
, low
, high
));
3565 if (inst
->conditional_mod
) {
3566 fs_reg
null(retype(brw_null_reg(), inst
->dst
.type
));
3567 fs_inst
*mov
= MOV(null
, inst
->dst
);
3568 mov
->conditional_mod
= inst
->conditional_mod
;
3574 inst
->remove(block
);
3579 invalidate_live_intervals();
3585 fs_visitor::dump_instructions()
3587 dump_instructions(NULL
);
3591 fs_visitor::dump_instructions(const char *name
)
3593 FILE *file
= stderr
;
3594 if (name
&& geteuid() != 0) {
3595 file
= fopen(name
, "w");
3601 calculate_register_pressure();
3602 int ip
= 0, max_pressure
= 0;
3603 foreach_block_and_inst(block
, backend_instruction
, inst
, cfg
) {
3604 max_pressure
= MAX2(max_pressure
, regs_live_at_ip
[ip
]);
3605 fprintf(file
, "{%3d} %4d: ", regs_live_at_ip
[ip
], ip
);
3606 dump_instruction(inst
, file
);
3609 fprintf(file
, "Maximum %3d registers live at once.\n", max_pressure
);
3612 foreach_in_list(backend_instruction
, inst
, &instructions
) {
3613 fprintf(file
, "%4d: ", ip
++);
3614 dump_instruction(inst
, file
);
3618 if (file
!= stderr
) {
3624 fs_visitor::dump_instruction(backend_instruction
*be_inst
)
3626 dump_instruction(be_inst
, stderr
);
3630 fs_visitor::dump_instruction(backend_instruction
*be_inst
, FILE *file
)
3632 fs_inst
*inst
= (fs_inst
*)be_inst
;
3634 if (inst
->predicate
) {
3635 fprintf(file
, "(%cf0.%d) ",
3636 inst
->predicate_inverse
? '-' : '+',
3640 fprintf(file
, "%s", brw_instruction_name(inst
->opcode
));
3642 fprintf(file
, ".sat");
3643 if (inst
->conditional_mod
) {
3644 fprintf(file
, "%s", conditional_modifier
[inst
->conditional_mod
]);
3645 if (!inst
->predicate
&&
3646 (devinfo
->gen
< 5 || (inst
->opcode
!= BRW_OPCODE_SEL
&&
3647 inst
->opcode
!= BRW_OPCODE_IF
&&
3648 inst
->opcode
!= BRW_OPCODE_WHILE
))) {
3649 fprintf(file
, ".f0.%d", inst
->flag_subreg
);
3652 fprintf(file
, "(%d) ", inst
->exec_size
);
3655 fprintf(file
, "(mlen: %d) ", inst
->mlen
);
3658 switch (inst
->dst
.file
) {
3660 fprintf(file
, "vgrf%d", inst
->dst
.reg
);
3661 if (inst
->dst
.width
!= dispatch_width
)
3662 fprintf(file
, "@%d", inst
->dst
.width
);
3663 if (alloc
.sizes
[inst
->dst
.reg
] != inst
->dst
.width
/ 8 ||
3664 inst
->dst
.subreg_offset
)
3665 fprintf(file
, "+%d.%d",
3666 inst
->dst
.reg_offset
, inst
->dst
.subreg_offset
);
3669 fprintf(file
, "m%d", inst
->dst
.reg
);
3672 fprintf(file
, "(null)");
3675 fprintf(file
, "***u%d***", inst
->dst
.reg
+ inst
->dst
.reg_offset
);
3678 fprintf(file
, "***attr%d***", inst
->dst
.reg
+ inst
->dst
.reg_offset
);
3681 if (inst
->dst
.fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
3682 switch (inst
->dst
.fixed_hw_reg
.nr
) {
3684 fprintf(file
, "null");
3686 case BRW_ARF_ADDRESS
:
3687 fprintf(file
, "a0.%d", inst
->dst
.fixed_hw_reg
.subnr
);
3689 case BRW_ARF_ACCUMULATOR
:
3690 fprintf(file
, "acc%d", inst
->dst
.fixed_hw_reg
.subnr
);
3693 fprintf(file
, "f%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
3694 inst
->dst
.fixed_hw_reg
.subnr
);
3697 fprintf(file
, "arf%d.%d", inst
->dst
.fixed_hw_reg
.nr
& 0xf,
3698 inst
->dst
.fixed_hw_reg
.subnr
);
3702 fprintf(file
, "hw_reg%d", inst
->dst
.fixed_hw_reg
.nr
);
3704 if (inst
->dst
.fixed_hw_reg
.subnr
)
3705 fprintf(file
, "+%d", inst
->dst
.fixed_hw_reg
.subnr
);
3708 fprintf(file
, "???");
3711 fprintf(file
, ":%s, ", brw_reg_type_letters(inst
->dst
.type
));
3713 for (int i
= 0; i
< inst
->sources
; i
++) {
3714 if (inst
->src
[i
].negate
)
3716 if (inst
->src
[i
].abs
)
3718 switch (inst
->src
[i
].file
) {
3720 fprintf(file
, "vgrf%d", inst
->src
[i
].reg
);
3721 if (inst
->src
[i
].width
!= dispatch_width
)
3722 fprintf(file
, "@%d", inst
->src
[i
].width
);
3723 if (alloc
.sizes
[inst
->src
[i
].reg
] != inst
->src
[i
].width
/ 8 ||
3724 inst
->src
[i
].subreg_offset
)
3725 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
3726 inst
->src
[i
].subreg_offset
);
3729 fprintf(file
, "***m%d***", inst
->src
[i
].reg
);
3732 fprintf(file
, "attr%d", inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
);
3735 fprintf(file
, "u%d", inst
->src
[i
].reg
+ inst
->src
[i
].reg_offset
);
3736 if (inst
->src
[i
].reladdr
) {
3737 fprintf(file
, "+reladdr");
3738 } else if (inst
->src
[i
].subreg_offset
) {
3739 fprintf(file
, "+%d.%d", inst
->src
[i
].reg_offset
,
3740 inst
->src
[i
].subreg_offset
);
3744 fprintf(file
, "(null)");
3747 switch (inst
->src
[i
].type
) {
3748 case BRW_REGISTER_TYPE_F
:
3749 fprintf(file
, "%ff", inst
->src
[i
].fixed_hw_reg
.dw1
.f
);
3751 case BRW_REGISTER_TYPE_W
:
3752 case BRW_REGISTER_TYPE_D
:
3753 fprintf(file
, "%dd", inst
->src
[i
].fixed_hw_reg
.dw1
.d
);
3755 case BRW_REGISTER_TYPE_UW
:
3756 case BRW_REGISTER_TYPE_UD
:
3757 fprintf(file
, "%uu", inst
->src
[i
].fixed_hw_reg
.dw1
.ud
);
3759 case BRW_REGISTER_TYPE_VF
:
3760 fprintf(file
, "[%-gF, %-gF, %-gF, %-gF]",
3761 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 0) & 0xff),
3762 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 8) & 0xff),
3763 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 16) & 0xff),
3764 brw_vf_to_float((inst
->src
[i
].fixed_hw_reg
.dw1
.ud
>> 24) & 0xff));
3767 fprintf(file
, "???");
3772 if (inst
->src
[i
].fixed_hw_reg
.negate
)
3774 if (inst
->src
[i
].fixed_hw_reg
.abs
)
3776 if (inst
->src
[i
].fixed_hw_reg
.file
== BRW_ARCHITECTURE_REGISTER_FILE
) {
3777 switch (inst
->src
[i
].fixed_hw_reg
.nr
) {
3779 fprintf(file
, "null");
3781 case BRW_ARF_ADDRESS
:
3782 fprintf(file
, "a0.%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
3784 case BRW_ARF_ACCUMULATOR
:
3785 fprintf(file
, "acc%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
3788 fprintf(file
, "f%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
3789 inst
->src
[i
].fixed_hw_reg
.subnr
);
3792 fprintf(file
, "arf%d.%d", inst
->src
[i
].fixed_hw_reg
.nr
& 0xf,
3793 inst
->src
[i
].fixed_hw_reg
.subnr
);
3797 fprintf(file
, "hw_reg%d", inst
->src
[i
].fixed_hw_reg
.nr
);
3799 if (inst
->src
[i
].fixed_hw_reg
.subnr
)
3800 fprintf(file
, "+%d", inst
->src
[i
].fixed_hw_reg
.subnr
);
3801 if (inst
->src
[i
].fixed_hw_reg
.abs
)
3805 fprintf(file
, "???");
3808 if (inst
->src
[i
].abs
)
3811 if (inst
->src
[i
].file
!= IMM
) {
3812 fprintf(file
, ":%s", brw_reg_type_letters(inst
->src
[i
].type
));
3815 if (i
< inst
->sources
- 1 && inst
->src
[i
+ 1].file
!= BAD_FILE
)
3816 fprintf(file
, ", ");
3821 if (dispatch_width
== 16 && inst
->exec_size
== 8) {
3822 if (inst
->force_sechalf
)
3823 fprintf(file
, "2ndhalf ");
3825 fprintf(file
, "1sthalf ");
3828 fprintf(file
, "\n");
3832 * Possibly returns an instruction that set up @param reg.
3834 * Sometimes we want to take the result of some expression/variable
3835 * dereference tree and rewrite the instruction generating the result
3836 * of the tree. When processing the tree, we know that the
3837 * instructions generated are all writing temporaries that are dead
3838 * outside of this tree. So, if we have some instructions that write
3839 * a temporary, we're free to point that temp write somewhere else.
3841 * Note that this doesn't guarantee that the instruction generated
3842 * only reg -- it might be the size=4 destination of a texture instruction.
3845 fs_visitor::get_instruction_generating_reg(fs_inst
*start
,
3850 end
->is_partial_write() ||
3852 !reg
.equals(end
->dst
)) {
3860 fs_visitor::setup_payload_gen6()
3863 (prog
->InputsRead
& (1 << VARYING_SLOT_POS
)) != 0;
3864 unsigned barycentric_interp_modes
=
3865 (stage
== MESA_SHADER_FRAGMENT
) ?
3866 ((brw_wm_prog_data
*) this->prog_data
)->barycentric_interp_modes
: 0;
3868 assert(devinfo
->gen
>= 6);
3870 /* R0-1: masks, pixel X/Y coordinates. */
3871 payload
.num_regs
= 2;
3872 /* R2: only for 32-pixel dispatch.*/
3874 /* R3-26: barycentric interpolation coordinates. These appear in the
3875 * same order that they appear in the brw_wm_barycentric_interp_mode
3876 * enum. Each set of coordinates occupies 2 registers if dispatch width
3877 * == 8 and 4 registers if dispatch width == 16. Coordinates only
3878 * appear if they were enabled using the "Barycentric Interpolation
3879 * Mode" bits in WM_STATE.
3881 for (int i
= 0; i
< BRW_WM_BARYCENTRIC_INTERP_MODE_COUNT
; ++i
) {
3882 if (barycentric_interp_modes
& (1 << i
)) {
3883 payload
.barycentric_coord_reg
[i
] = payload
.num_regs
;
3884 payload
.num_regs
+= 2;
3885 if (dispatch_width
== 16) {
3886 payload
.num_regs
+= 2;
3891 /* R27: interpolated depth if uses source depth */
3893 payload
.source_depth_reg
= payload
.num_regs
;
3895 if (dispatch_width
== 16) {
3896 /* R28: interpolated depth if not SIMD8. */
3900 /* R29: interpolated W set if GEN6_WM_USES_SOURCE_W. */
3902 payload
.source_w_reg
= payload
.num_regs
;
3904 if (dispatch_width
== 16) {
3905 /* R30: interpolated W if not SIMD8. */
3910 if (stage
== MESA_SHADER_FRAGMENT
) {
3911 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3912 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3913 prog_data
->uses_pos_offset
= key
->compute_pos_offset
;
3914 /* R31: MSAA position offsets. */
3915 if (prog_data
->uses_pos_offset
) {
3916 payload
.sample_pos_reg
= payload
.num_regs
;
3921 /* R32: MSAA input coverage mask */
3922 if (prog
->SystemValuesRead
& SYSTEM_BIT_SAMPLE_MASK_IN
) {
3923 assert(devinfo
->gen
>= 7);
3924 payload
.sample_mask_in_reg
= payload
.num_regs
;
3926 if (dispatch_width
== 16) {
3927 /* R33: input coverage mask if not SIMD8. */
3932 /* R34-: bary for 32-pixel. */
3933 /* R58-59: interp W for 32-pixel. */
3935 if (prog
->OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
)) {
3936 source_depth_to_render_target
= true;
3941 fs_visitor::setup_vs_payload()
3943 /* R0: thread header, R1: urb handles */
3944 payload
.num_regs
= 2;
3948 fs_visitor::setup_cs_payload()
3950 assert(brw
->gen
>= 7);
3952 payload
.num_regs
= 1;
3956 fs_visitor::assign_binding_table_offsets()
3958 assert(stage
== MESA_SHADER_FRAGMENT
);
3959 brw_wm_prog_data
*prog_data
= (brw_wm_prog_data
*) this->prog_data
;
3960 brw_wm_prog_key
*key
= (brw_wm_prog_key
*) this->key
;
3961 uint32_t next_binding_table_offset
= 0;
3963 /* If there are no color regions, we still perform an FB write to a null
3964 * renderbuffer, which we place at surface index 0.
3966 prog_data
->binding_table
.render_target_start
= next_binding_table_offset
;
3967 next_binding_table_offset
+= MAX2(key
->nr_color_regions
, 1);
3969 assign_common_binding_table_offsets(next_binding_table_offset
);
3973 fs_visitor::calculate_register_pressure()
3975 invalidate_live_intervals();
3976 calculate_live_intervals();
3978 unsigned num_instructions
= 0;
3979 foreach_block(block
, cfg
)
3980 num_instructions
+= block
->instructions
.length();
3982 regs_live_at_ip
= rzalloc_array(mem_ctx
, int, num_instructions
);
3984 for (unsigned reg
= 0; reg
< alloc
.count
; reg
++) {
3985 for (int ip
= virtual_grf_start
[reg
]; ip
<= virtual_grf_end
[reg
]; ip
++)
3986 regs_live_at_ip
[ip
] += alloc
.sizes
[reg
];
3991 fs_visitor::optimize()
3993 /* bld is the common builder object pointing at the end of the program we
3994 * used to translate it into i965 IR. For the optimization and lowering
3995 * passes coming next, any code added after the end of the program without
3996 * having explicitly called fs_builder::at() clearly points at a mistake.
3997 * Ideally optimization passes wouldn't be part of the visitor so they
3998 * wouldn't have access to bld at all, but they do, so just in case some
3999 * pass forgets to ask for a location explicitly set it to NULL here to
4002 bld
= bld
.at(NULL
, NULL
);
4004 split_virtual_grfs();
4006 move_uniform_array_access_to_pull_constants();
4007 assign_constant_locations();
4008 demote_pull_constants();
4010 #define OPT(pass, args...) ({ \
4012 bool this_progress = pass(args); \
4014 if (unlikely(INTEL_DEBUG & DEBUG_OPTIMIZER) && this_progress) { \
4015 char filename[64]; \
4016 snprintf(filename, 64, "%s%d-%04d-%02d-%02d-" #pass, \
4017 stage_abbrev, dispatch_width, shader_prog ? shader_prog->Name : 0, iteration, pass_num); \
4019 backend_shader::dump_instructions(filename); \
4022 progress = progress || this_progress; \
4026 if (unlikely(INTEL_DEBUG
& DEBUG_OPTIMIZER
)) {
4028 snprintf(filename
, 64, "%s%d-%04d-00-start",
4029 stage_abbrev
, dispatch_width
,
4030 shader_prog
? shader_prog
->Name
: 0);
4032 backend_shader::dump_instructions(filename
);
4043 OPT(remove_duplicate_mrf_writes
);
4047 OPT(opt_copy_propagate
);
4048 OPT(opt_peephole_predicated_break
);
4049 OPT(opt_cmod_propagation
);
4050 OPT(dead_code_eliminate
);
4051 OPT(opt_peephole_sel
);
4052 OPT(dead_control_flow_eliminate
, this);
4053 OPT(opt_register_renaming
);
4054 OPT(opt_redundant_discard_jumps
);
4055 OPT(opt_saturate_propagation
);
4056 OPT(opt_zero_samples
);
4057 OPT(register_coalesce
);
4058 OPT(compute_to_mrf
);
4059 OPT(eliminate_find_live_channel
);
4061 OPT(compact_virtual_grfs
);
4066 OPT(opt_sampler_eot
);
4068 if (OPT(lower_load_payload
)) {
4069 split_virtual_grfs();
4070 OPT(register_coalesce
);
4071 OPT(compute_to_mrf
);
4072 OPT(dead_code_eliminate
);
4075 OPT(opt_combine_constants
);
4076 OPT(lower_integer_multiplication
);
4078 lower_uniform_pull_constant_loads();
4082 * Three source instruction must have a GRF/MRF destination register.
4083 * ARF NULL is not allowed. Fix that up by allocating a temporary GRF.
4086 fs_visitor::fixup_3src_null_dest()
4088 foreach_block_and_inst_safe (block
, fs_inst
, inst
, cfg
) {
4089 if (inst
->is_3src() && inst
->dst
.is_null()) {
4090 inst
->dst
= fs_reg(GRF
, alloc
.allocate(dispatch_width
/ 8),
4097 fs_visitor::allocate_registers()
4099 bool allocated_without_spills
;
4101 static const enum instruction_scheduler_mode pre_modes
[] = {
4103 SCHEDULE_PRE_NON_LIFO
,
4107 /* Try each scheduling heuristic to see if it can successfully register
4108 * allocate without spilling. They should be ordered by decreasing
4109 * performance but increasing likelihood of allocating.
4111 for (unsigned i
= 0; i
< ARRAY_SIZE(pre_modes
); i
++) {
4112 schedule_instructions(pre_modes
[i
]);
4115 assign_regs_trivial();
4116 allocated_without_spills
= true;
4118 allocated_without_spills
= assign_regs(false);
4120 if (allocated_without_spills
)
4124 if (!allocated_without_spills
) {
4125 /* We assume that any spilling is worse than just dropping back to
4126 * SIMD8. There's probably actually some intermediate point where
4127 * SIMD16 with a couple of spills is still better.
4129 if (dispatch_width
== 16) {
4130 fail("Failure to register allocate. Reduce number of "
4131 "live scalar values to avoid this.");
4133 perf_debug("%s shader triggered register spilling. "
4134 "Try reducing the number of live scalar values to "
4135 "improve performance.\n", stage_name
);
4138 /* Since we're out of heuristics, just go spill registers until we
4139 * get an allocation.
4141 while (!assign_regs(true)) {
4147 /* This must come after all optimization and register allocation, since
4148 * it inserts dead code that happens to have side effects, and it does
4149 * so based on the actual physical registers in use.
4151 insert_gen4_send_dependency_workarounds();
4156 if (!allocated_without_spills
)
4157 schedule_instructions(SCHEDULE_POST
);
4159 if (last_scratch
> 0)
4160 prog_data
->total_scratch
= brw_get_scratch_size(last_scratch
);
4164 fs_visitor::run_vs()
4166 assert(stage
== MESA_SHADER_VERTEX
);
4168 assign_common_binding_table_offsets(0);
4171 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
4172 emit_shader_time_begin();
4181 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
4182 emit_shader_time_end();
4188 assign_curb_setup();
4189 assign_vs_urb_setup();
4191 fixup_3src_null_dest();
4192 allocate_registers();
4198 fs_visitor::run_fs()
4200 brw_wm_prog_data
*wm_prog_data
= (brw_wm_prog_data
*) this->prog_data
;
4201 brw_wm_prog_key
*wm_key
= (brw_wm_prog_key
*) this->key
;
4203 assert(stage
== MESA_SHADER_FRAGMENT
);
4205 sanity_param_count
= prog
->Parameters
->NumParameters
;
4207 assign_binding_table_offsets();
4209 if (devinfo
->gen
>= 6)
4210 setup_payload_gen6();
4212 setup_payload_gen4();
4216 } else if (brw
->use_rep_send
&& dispatch_width
== 16) {
4217 emit_repclear_shader();
4219 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
4220 emit_shader_time_begin();
4222 calculate_urb_setup();
4223 if (prog
->InputsRead
> 0) {
4224 if (devinfo
->gen
< 6)
4225 emit_interpolation_setup_gen4();
4227 emit_interpolation_setup_gen6();
4230 /* We handle discards by keeping track of the still-live pixels in f0.1.
4231 * Initialize it with the dispatched pixels.
4233 if (wm_prog_data
->uses_kill
) {
4234 fs_inst
*discard_init
= emit(FS_OPCODE_MOV_DISPATCH_TO_FLAGS
);
4235 discard_init
->flag_subreg
= 1;
4238 /* Generate FS IR for main(). (the visitor only descends into
4239 * functions called "main").
4246 if (wm_prog_data
->uses_kill
)
4247 emit(FS_OPCODE_PLACEHOLDER_HALT
);
4249 if (wm_key
->alpha_test_func
)
4254 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
4255 emit_shader_time_end();
4261 assign_curb_setup();
4264 fixup_3src_null_dest();
4265 allocate_registers();
4271 if (dispatch_width
== 8)
4272 wm_prog_data
->reg_blocks
= brw_register_blocks(grf_used
);
4274 wm_prog_data
->reg_blocks_16
= brw_register_blocks(grf_used
);
4276 /* If any state parameters were appended, then ParameterValues could have
4277 * been realloced, in which case the driver uniform storage set up by
4278 * _mesa_associate_uniform_storage() would point to freed memory. Make
4279 * sure that didn't happen.
4281 assert(sanity_param_count
== prog
->Parameters
->NumParameters
);
4287 fs_visitor::run_cs()
4289 assert(stage
== MESA_SHADER_COMPUTE
);
4292 sanity_param_count
= prog
->Parameters
->NumParameters
;
4294 assign_common_binding_table_offsets(0);
4298 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
4299 emit_shader_time_begin();
4306 emit_cs_terminate();
4308 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
4309 emit_shader_time_end();
4315 assign_curb_setup();
4317 fixup_3src_null_dest();
4318 allocate_registers();
4323 /* If any state parameters were appended, then ParameterValues could have
4324 * been realloced, in which case the driver uniform storage set up by
4325 * _mesa_associate_uniform_storage() would point to freed memory. Make
4326 * sure that didn't happen.
4328 assert(sanity_param_count
== prog
->Parameters
->NumParameters
);
4334 brw_wm_fs_emit(struct brw_context
*brw
,
4336 const struct brw_wm_prog_key
*key
,
4337 struct brw_wm_prog_data
*prog_data
,
4338 struct gl_fragment_program
*fp
,
4339 struct gl_shader_program
*prog
,
4340 unsigned *final_assembly_size
)
4342 bool start_busy
= false;
4343 double start_time
= 0;
4345 if (unlikely(brw
->perf_debug
)) {
4346 start_busy
= (brw
->batch
.last_bo
&&
4347 drm_intel_bo_busy(brw
->batch
.last_bo
));
4348 start_time
= get_time();
4351 struct brw_shader
*shader
= NULL
;
4353 shader
= (brw_shader
*) prog
->_LinkedShaders
[MESA_SHADER_FRAGMENT
];
4355 if (unlikely(INTEL_DEBUG
& DEBUG_WM
))
4356 brw_dump_ir("fragment", prog
, &shader
->base
, &fp
->Base
);
4358 /* Now the main event: Visit the shader IR and generate our FS IR for it.
4360 fs_visitor
v(brw
, mem_ctx
, MESA_SHADER_FRAGMENT
, key
, &prog_data
->base
,
4361 prog
, &fp
->Base
, 8);
4364 prog
->LinkStatus
= false;
4365 ralloc_strcat(&prog
->InfoLog
, v
.fail_msg
);
4368 _mesa_problem(NULL
, "Failed to compile fragment shader: %s\n",
4374 cfg_t
*simd16_cfg
= NULL
;
4375 fs_visitor
v2(brw
, mem_ctx
, MESA_SHADER_FRAGMENT
, key
, &prog_data
->base
,
4376 prog
, &fp
->Base
, 16);
4377 if (likely(!(INTEL_DEBUG
& DEBUG_NO16
) || brw
->use_rep_send
)) {
4378 if (!v
.simd16_unsupported
) {
4379 /* Try a SIMD16 compile */
4380 v2
.import_uniforms(&v
);
4382 perf_debug("SIMD16 shader failed to compile, falling back to "
4383 "SIMD8 at a 10-20%% performance cost: %s", v2
.fail_msg
);
4385 simd16_cfg
= v2
.cfg
;
4388 perf_debug("SIMD16 shader unsupported, falling back to "
4389 "SIMD8 at a 10-20%% performance cost: %s", v
.no16_msg
);
4394 int no_simd8
= (INTEL_DEBUG
& DEBUG_NO8
) || brw
->no_simd8
;
4395 if ((no_simd8
|| brw
->gen
< 5) && simd16_cfg
) {
4397 prog_data
->no_8
= true;
4400 prog_data
->no_8
= false;
4403 fs_generator
g(brw
, mem_ctx
, (void *) key
, &prog_data
->base
,
4404 &fp
->Base
, v
.promoted_constants
, v
.runtime_check_aads_emit
, "FS");
4406 if (unlikely(INTEL_DEBUG
& DEBUG_WM
)) {
4409 name
= ralloc_asprintf(mem_ctx
, "%s fragment shader %d",
4410 prog
->Label
? prog
->Label
: "unnamed",
4413 name
= ralloc_asprintf(mem_ctx
, "fragment program %d", fp
->Base
.Id
);
4415 g
.enable_debug(name
);
4419 g
.generate_code(simd8_cfg
, 8);
4421 prog_data
->prog_offset_16
= g
.generate_code(simd16_cfg
, 16);
4423 if (unlikely(brw
->perf_debug
) && shader
) {
4424 if (shader
->compiled_once
)
4425 brw_wm_debug_recompile(brw
, prog
, key
);
4426 shader
->compiled_once
= true;
4428 if (start_busy
&& !drm_intel_bo_busy(brw
->batch
.last_bo
)) {
4429 perf_debug("FS compile took %.03f ms and stalled the GPU\n",
4430 (get_time() - start_time
) * 1000);
4434 return g
.get_assembly(final_assembly_size
);
4438 brw_fs_precompile(struct gl_context
*ctx
,
4439 struct gl_shader_program
*shader_prog
,
4440 struct gl_program
*prog
)
4442 struct brw_context
*brw
= brw_context(ctx
);
4443 struct brw_wm_prog_key key
;
4445 struct gl_fragment_program
*fp
= (struct gl_fragment_program
*) prog
;
4446 struct brw_fragment_program
*bfp
= brw_fragment_program(fp
);
4447 bool program_uses_dfdy
= fp
->UsesDFdy
;
4449 memset(&key
, 0, sizeof(key
));
4453 key
.iz_lookup
|= IZ_PS_KILL_ALPHATEST_BIT
;
4455 if (fp
->Base
.OutputsWritten
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
4456 key
.iz_lookup
|= IZ_PS_COMPUTES_DEPTH_BIT
;
4458 /* Just assume depth testing. */
4459 key
.iz_lookup
|= IZ_DEPTH_TEST_ENABLE_BIT
;
4460 key
.iz_lookup
|= IZ_DEPTH_WRITE_ENABLE_BIT
;
4463 if (brw
->gen
< 6 || _mesa_bitcount_64(fp
->Base
.InputsRead
&
4464 BRW_FS_VARYING_INPUT_MASK
) > 16)
4465 key
.input_slots_valid
= fp
->Base
.InputsRead
| VARYING_BIT_POS
;
4467 brw_setup_tex_for_precompile(brw
, &key
.tex
, &fp
->Base
);
4469 if (fp
->Base
.InputsRead
& VARYING_BIT_POS
) {
4470 key
.drawable_height
= ctx
->DrawBuffer
->Height
;
4473 key
.nr_color_regions
= _mesa_bitcount_64(fp
->Base
.OutputsWritten
&
4474 ~(BITFIELD64_BIT(FRAG_RESULT_DEPTH
) |
4475 BITFIELD64_BIT(FRAG_RESULT_SAMPLE_MASK
)));
4477 if ((fp
->Base
.InputsRead
& VARYING_BIT_POS
) || program_uses_dfdy
) {
4478 key
.render_to_fbo
= _mesa_is_user_fbo(ctx
->DrawBuffer
) ||
4479 key
.nr_color_regions
> 1;
4482 key
.program_string_id
= bfp
->id
;
4484 uint32_t old_prog_offset
= brw
->wm
.base
.prog_offset
;
4485 struct brw_wm_prog_data
*old_prog_data
= brw
->wm
.prog_data
;
4487 bool success
= brw_codegen_wm_prog(brw
, shader_prog
, bfp
, &key
);
4489 brw
->wm
.base
.prog_offset
= old_prog_offset
;
4490 brw
->wm
.prog_data
= old_prog_data
;
4496 brw_setup_tex_for_precompile(struct brw_context
*brw
,
4497 struct brw_sampler_prog_key_data
*tex
,
4498 struct gl_program
*prog
)
4500 const bool has_shader_channel_select
= brw
->is_haswell
|| brw
->gen
>= 8;
4501 unsigned sampler_count
= _mesa_fls(prog
->SamplersUsed
);
4502 for (unsigned i
= 0; i
< sampler_count
; i
++) {
4503 if (!has_shader_channel_select
&& (prog
->ShadowSamplers
& (1 << i
))) {
4504 /* Assume DEPTH_TEXTURE_MODE is the default: X, X, X, 1 */
4506 MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_X
, SWIZZLE_ONE
);
4508 /* Color sampler: assume no swizzling. */
4509 tex
->swizzles
[i
] = SWIZZLE_XYZW
;