3 * Copyright © 2010-2015 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "brw_shader.h"
32 class fs_reg
: public backend_reg
{
34 DECLARE_RALLOC_CXX_OPERATORS(fs_reg
)
39 fs_reg(struct ::brw_reg reg
);
40 fs_reg(enum brw_reg_file file
, int nr
);
41 fs_reg(enum brw_reg_file file
, int nr
, enum brw_reg_type type
);
43 bool equals(const fs_reg
&r
) const;
44 bool is_contiguous() const;
47 * Return the size in bytes of a single logical component of the
48 * register assuming the given execution width.
50 unsigned component_size(unsigned width
) const;
52 /** Smear a channel of the reg to all channels. */
53 fs_reg
&set_smear(unsigned subreg
);
56 * Offset in bytes from the start of the register. Values up to a
57 * backend_reg::reg_offset unit are valid.
61 /** Register region horizontal stride */
68 assert(reg
.file
!= IMM
);
69 reg
.negate
= !reg
.negate
;
74 retype(fs_reg reg
, enum brw_reg_type type
)
81 byte_offset(fs_reg reg
, unsigned delta
)
89 const unsigned reg_size
= (reg
.file
== UNIFORM
? 4 : REG_SIZE
);
90 const unsigned suboffset
= reg
.subreg_offset
+ delta
;
91 reg
.reg_offset
+= suboffset
/ reg_size
;
92 reg
.subreg_offset
= suboffset
% reg_size
;
96 const unsigned suboffset
= reg
.subreg_offset
+ delta
;
97 reg
.nr
+= suboffset
/ REG_SIZE
;
98 reg
.subreg_offset
= suboffset
% REG_SIZE
;
103 const unsigned suboffset
= reg
.subnr
+ delta
;
104 reg
.nr
+= suboffset
/ REG_SIZE
;
105 reg
.subnr
= suboffset
% REG_SIZE
;
116 horiz_offset(const fs_reg
®
, unsigned delta
)
122 /* These only have a single component that is implicitly splatted. A
123 * horizontal offset should be a harmless no-op.
124 * XXX - Handle vector immediates correctly.
130 return byte_offset(reg
, delta
* reg
.stride
* type_sz(reg
.type
));
136 const unsigned stride
= reg
.hstride
? 1 << (reg
.hstride
- 1) : 0;
137 return byte_offset(reg
, delta
* stride
* type_sz(reg
.type
));
140 unreachable("Invalid register file");
144 offset(fs_reg reg
, unsigned width
, unsigned delta
)
155 return byte_offset(reg
, delta
* reg
.component_size(width
));
163 * Get the scalar channel of \p reg given by \p idx and replicate it to all
164 * channels of the result.
167 component(fs_reg reg
, unsigned idx
)
169 reg
= horiz_offset(reg
, idx
);
175 * Return an integer identifying the discrete address space a register is
176 * contained in. A register is by definition fully contained in the single
177 * reg_space it belongs to, so two registers with different reg_space ids are
178 * guaranteed not to overlap. Most register files are a single reg_space of
179 * its own, only the VGRF file is composed of multiple discrete address
180 * spaces, one for each VGRF allocation.
182 static inline uint32_t
183 reg_space(const fs_reg
&r
)
185 return r
.file
<< 16 | (r
.file
== VGRF
? r
.nr
: 0);
189 * Return the base offset in bytes of a register relative to the start of its
192 static inline unsigned
193 reg_offset(const fs_reg
&r
)
195 return ((r
.file
== VGRF
|| r
.file
== IMM
? 0 : r
.nr
) + r
.reg_offset
) *
196 (r
.file
== UNIFORM
? 4 : REG_SIZE
) + r
.subreg_offset
;
200 * Return whether the register region starting at \p r and spanning \p dr
201 * bytes could potentially overlap the register region starting at \p s and
202 * spanning \p ds bytes.
205 regions_overlap(const fs_reg
&r
, unsigned dr
, const fs_reg
&s
, unsigned ds
)
207 if (r
.file
== MRF
&& (r
.nr
& BRW_MRF_COMPR4
)) {
209 t
.nr
&= ~BRW_MRF_COMPR4
;
210 /* COMPR4 regions are translated by the hardware during decompression
211 * into two separate half-regions 4 MRFs apart from each other.
213 return regions_overlap(t
, dr
/ 2, s
, ds
) ||
214 regions_overlap(byte_offset(t
, 4 * REG_SIZE
), dr
/ 2, s
, ds
);
216 } else if (s
.file
== MRF
&& (s
.nr
& BRW_MRF_COMPR4
)) {
217 return regions_overlap(s
, ds
, r
, dr
);
220 return reg_space(r
) == reg_space(s
) &&
221 !(reg_offset(r
) + dr
<= reg_offset(s
) ||
222 reg_offset(s
) + ds
<= reg_offset(r
));
227 * Return whether the given register region is n-periodic, i.e. whether the
228 * original region remains invariant after shifting it by \p n scalar
232 is_periodic(const fs_reg
®
, unsigned n
)
234 if (reg
.file
== BAD_FILE
|| reg
.is_null()) {
237 } else if (reg
.file
== IMM
) {
238 const unsigned period
= (reg
.type
== BRW_REGISTER_TYPE_UV
||
239 reg
.type
== BRW_REGISTER_TYPE_V
? 8 :
240 reg
.type
== BRW_REGISTER_TYPE_VF
? 4 :
242 return n
% period
== 0;
244 } else if (reg
.file
== ARF
|| reg
.file
== FIXED_GRF
) {
245 const unsigned period
= (reg
.hstride
== 0 && reg
.vstride
== 0 ? 1 :
246 reg
.vstride
== 0 ? 1 << reg
.width
:
248 return n
% period
== 0;
251 return reg
.stride
== 0;
256 is_uniform(const fs_reg
®
)
258 return is_periodic(reg
, 1);
262 * Get the specified 8-component quarter of a register.
263 * XXX - Maybe come up with a less misleading name for this (e.g. quarter())?
266 half(const fs_reg
®
, unsigned idx
)
269 return horiz_offset(reg
, 8 * idx
);
273 * Reinterpret each channel of register \p reg as a vector of values of the
274 * given smaller type and take the i-th subcomponent from each.
277 subscript(fs_reg reg
, brw_reg_type type
, unsigned i
)
279 assert((i
+ 1) * type_sz(type
) <= type_sz(reg
.type
));
281 if (reg
.file
== ARF
|| reg
.file
== FIXED_GRF
) {
282 /* The stride is encoded inconsistently for fixed GRF and ARF registers
283 * as the log2 of the actual vertical and horizontal strides.
285 const int delta
= _mesa_logbase2(type_sz(reg
.type
)) -
286 _mesa_logbase2(type_sz(type
));
287 reg
.hstride
+= (reg
.hstride
? delta
: 0);
288 reg
.vstride
+= (reg
.vstride
? delta
: 0);
290 } else if (reg
.file
== IMM
) {
291 assert(reg
.type
== type
);
294 reg
.stride
*= type_sz(reg
.type
) / type_sz(type
);
297 return byte_offset(retype(reg
, type
), i
* type_sz(type
));
300 static const fs_reg reg_undef
;
302 class fs_inst
: public backend_instruction
{
303 fs_inst
&operator=(const fs_inst
&);
305 void init(enum opcode opcode
, uint8_t exec_width
, const fs_reg
&dst
,
306 const fs_reg
*src
, unsigned sources
);
309 DECLARE_RALLOC_CXX_OPERATORS(fs_inst
)
312 fs_inst(enum opcode opcode
, uint8_t exec_size
);
313 fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
);
314 fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
316 fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
317 const fs_reg
&src0
, const fs_reg
&src1
);
318 fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
319 const fs_reg
&src0
, const fs_reg
&src1
, const fs_reg
&src2
);
320 fs_inst(enum opcode opcode
, uint8_t exec_size
, const fs_reg
&dst
,
321 const fs_reg src
[], unsigned sources
);
322 fs_inst(const fs_inst
&that
);
325 void resize_sources(uint8_t num_sources
);
327 bool equals(fs_inst
*inst
) const;
328 bool overwrites_reg(const fs_reg
®
) const;
329 bool is_send_from_grf() const;
330 bool is_partial_write() const;
331 bool is_copy_payload(const brw::simple_allocator
&grf_alloc
) const;
332 unsigned components_read(unsigned i
) const;
333 int regs_read(int arg
) const;
334 bool can_do_source_mods(const struct brw_device_info
*devinfo
);
335 bool can_change_types() const;
336 bool has_side_effects() const;
337 bool has_source_and_destination_hazard() const;
340 * Return the subset of flag registers read by the instruction as a bitset
341 * with byte granularity.
343 unsigned flags_read(const brw_device_info
*devinfo
) const;
346 * Return the subset of flag registers updated by the instruction (either
347 * partially or fully) as a bitset with byte granularity.
349 unsigned flags_written() const;
354 uint8_t sources
; /**< Number of fs_reg sources. */
357 * Execution size of the instruction. This is used by the generator to
358 * generate the correct binary for the given fs_inst. Current valid
359 * values are 1, 8, 16.
364 * Channel group from the hardware execution and predication mask that
365 * should be applied to the instruction. The subset of channel enable
366 * signals (calculated from the EU control flow and predication state)
367 * given by [group, group + exec_size) will be used to mask GRF writes and
368 * any other side effects of the instruction.
373 bool pi_noperspective
:1; /**< Pixel interpolator noperspective flag */
377 * Make the execution of \p inst dependent on the evaluation of a possibly
378 * inverted predicate.
380 static inline fs_inst
*
381 set_predicate_inv(enum brw_predicate pred
, bool inverse
,
384 inst
->predicate
= pred
;
385 inst
->predicate_inverse
= inverse
;
390 * Make the execution of \p inst dependent on the evaluation of a predicate.
392 static inline fs_inst
*
393 set_predicate(enum brw_predicate pred
, fs_inst
*inst
)
395 return set_predicate_inv(pred
, false, inst
);
399 * Write the result of evaluating the condition given by \p mod to a flag
402 static inline fs_inst
*
403 set_condmod(enum brw_conditional_mod mod
, fs_inst
*inst
)
405 inst
->conditional_mod
= mod
;
410 * Clamp the result of \p inst to the saturation range of its destination
413 static inline fs_inst
*
414 set_saturate(bool saturate
, fs_inst
*inst
)
416 inst
->saturate
= saturate
;