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
25 #ifndef BRW_FS_BUILDER_H
26 #define BRW_FS_BUILDER_H
28 #include "brw_ir_fs.h"
29 #include "brw_shader.h"
30 #include "brw_context.h"
34 * Toolbox to assemble an FS IR program out of individual instructions.
36 * This object is meant to have an interface consistent with
37 * brw::vec4_builder. They cannot be fully interchangeable because
38 * brw::fs_builder generates scalar code while brw::vec4_builder generates
43 /** Type used in this IR to represent a source of an instruction. */
44 typedef fs_reg src_reg
;
46 /** Type used in this IR to represent the destination of an instruction. */
47 typedef fs_reg dst_reg
;
49 /** Type used in this IR to represent an instruction. */
50 typedef fs_inst instruction
;
53 * Construct an fs_builder that inserts instructions into \p shader.
54 * \p dispatch_width gives the native execution width of the program.
56 fs_builder(backend_shader
*shader
,
57 unsigned dispatch_width
) :
58 shader(shader
), block(NULL
), cursor(NULL
),
59 _dispatch_width(dispatch_width
),
61 force_writemask_all(false),
67 * Construct an fs_builder that inserts instructions into \p shader
68 * before instruction \p inst in basic block \p block. The default
69 * execution controls and debug annotation are initialized from the
70 * instruction passed as argument.
72 fs_builder(backend_shader
*shader
, bblock_t
*block
, fs_inst
*inst
) :
73 shader(shader
), block(block
), cursor(inst
),
74 _dispatch_width(inst
->exec_size
),
76 force_writemask_all(inst
->force_writemask_all
)
78 annotation
.str
= inst
->annotation
;
79 annotation
.ir
= inst
->ir
;
83 * Construct an fs_builder that inserts instructions before \p cursor in
84 * basic block \p block, inheriting other code generation parameters
88 at(bblock_t
*block
, exec_node
*cursor
) const
90 fs_builder bld
= *this;
97 * Construct an fs_builder appending instructions at the end of the
98 * instruction list of the shader, inheriting other code generation
99 * parameters from this.
104 return at(NULL
, (exec_node
*)&shader
->instructions
.tail_sentinel
);
108 * Construct a builder specifying the default SIMD width and group of
109 * channel enable signals, inheriting other code generation parameters
112 * \p n gives the default SIMD width, \p i gives the slot group used for
113 * predication and control flow masking in multiples of \p n channels.
116 group(unsigned n
, unsigned i
) const
118 assert(force_writemask_all
||
119 (n
<= dispatch_width() && i
< dispatch_width() / n
));
120 fs_builder bld
= *this;
121 bld
._dispatch_width
= n
;
127 * Alias for group() with width equal to eight.
130 half(unsigned i
) const
136 * Construct a builder with per-channel control flow execution masking
137 * disabled if \p b is true. If control flow execution masking is
138 * already disabled this has no effect.
141 exec_all(bool b
= true) const
143 fs_builder bld
= *this;
145 bld
.force_writemask_all
= true;
150 * Construct a builder with the given debug annotation info.
153 annotate(const char *str
, const void *ir
= NULL
) const
155 fs_builder bld
= *this;
156 bld
.annotation
.str
= str
;
157 bld
.annotation
.ir
= ir
;
162 * Get the SIMD width in use.
165 dispatch_width() const
167 return _dispatch_width
;
171 * Get the channel group in use.
180 * Allocate a virtual register of natural vector size (one for this IR)
181 * and SIMD width. \p n gives the amount of space to allocate in
182 * dispatch_width units (which is just enough space for one logical
183 * component in this IR).
186 vgrf(enum brw_reg_type type
, unsigned n
= 1) const
188 assert(dispatch_width() <= 32);
191 return dst_reg(VGRF
, shader
->alloc
.allocate(
192 DIV_ROUND_UP(n
* type_sz(type
) * dispatch_width(),
196 return retype(null_reg_ud(), type
);
200 * Create a null register of floating type.
205 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_F
));
211 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_DF
));
215 * Create a null register of signed integer type.
220 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_D
));
224 * Create a null register of unsigned integer type.
229 return dst_reg(retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
));
233 * Get the mask of SIMD channels enabled by dispatch and not yet
234 * disabled by discard.
237 sample_mask_reg() const
239 assert(shader
->stage
!= MESA_SHADER_FRAGMENT
||
240 group() + dispatch_width() <= 16);
241 if (shader
->stage
!= MESA_SHADER_FRAGMENT
) {
242 return brw_imm_d(0xffffffff);
243 } else if (((brw_wm_prog_data
*)shader
->stage_prog_data
)->uses_kill
) {
244 return brw_flag_reg(0, 1);
246 return retype(brw_vec1_grf(1, 7), BRW_REGISTER_TYPE_UD
);
251 * Insert an instruction into the program.
254 emit(const instruction
&inst
) const
256 return emit(new(shader
->mem_ctx
) instruction(inst
));
260 * Create and insert a nullary control instruction into the program.
263 emit(enum opcode opcode
) const
265 return emit(instruction(opcode
, dispatch_width()));
269 * Create and insert a nullary instruction into the program.
272 emit(enum opcode opcode
, const dst_reg
&dst
) const
274 return emit(instruction(opcode
, dispatch_width(), dst
));
278 * Create and insert a unary instruction into the program.
281 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
) const
284 case SHADER_OPCODE_RCP
:
285 case SHADER_OPCODE_RSQ
:
286 case SHADER_OPCODE_SQRT
:
287 case SHADER_OPCODE_EXP2
:
288 case SHADER_OPCODE_LOG2
:
289 case SHADER_OPCODE_SIN
:
290 case SHADER_OPCODE_COS
:
291 return emit(instruction(opcode
, dispatch_width(), dst
,
292 fix_math_operand(src0
)));
295 return emit(instruction(opcode
, dispatch_width(), dst
, src0
));
300 * Create and insert a binary instruction into the program.
303 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
304 const src_reg
&src1
) const
307 case SHADER_OPCODE_POW
:
308 case SHADER_OPCODE_INT_QUOTIENT
:
309 case SHADER_OPCODE_INT_REMAINDER
:
310 return emit(instruction(opcode
, dispatch_width(), dst
,
311 fix_math_operand(src0
),
312 fix_math_operand(src1
)));
315 return emit(instruction(opcode
, dispatch_width(), dst
, src0
, src1
));
321 * Create and insert a ternary instruction into the program.
324 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
325 const src_reg
&src1
, const src_reg
&src2
) const
329 case BRW_OPCODE_BFI2
:
332 return emit(instruction(opcode
, dispatch_width(), dst
,
333 fix_3src_operand(src0
),
334 fix_3src_operand(src1
),
335 fix_3src_operand(src2
)));
338 return emit(instruction(opcode
, dispatch_width(), dst
,
344 * Create and insert an instruction with a variable number of sources
348 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg srcs
[],
351 return emit(instruction(opcode
, dispatch_width(), dst
, srcs
, n
));
355 * Insert a preallocated instruction into the program.
358 emit(instruction
*inst
) const
360 assert(inst
->exec_size
<= 32);
361 assert(inst
->exec_size
== dispatch_width() ||
362 force_writemask_all
);
364 inst
->group
= _group
;
365 inst
->force_writemask_all
= force_writemask_all
;
366 inst
->annotation
= annotation
.str
;
367 inst
->ir
= annotation
.ir
;
370 static_cast<instruction
*>(cursor
)->insert_before(block
, inst
);
372 cursor
->insert_before(inst
);
378 * Select \p src0 if the comparison of both sources with the given
379 * conditional mod evaluates to true, otherwise select \p src1.
381 * Generally useful to get the minimum or maximum of two values.
384 emit_minmax(const dst_reg
&dst
, const src_reg
&src0
,
385 const src_reg
&src1
, brw_conditional_mod mod
) const
387 assert(mod
== BRW_CONDITIONAL_GE
|| mod
== BRW_CONDITIONAL_L
);
389 return set_condmod(mod
, SEL(dst
, fix_unsigned_negate(src0
),
390 fix_unsigned_negate(src1
)));
394 * Copy any live channel from \p src to the first channel of the result.
397 emit_uniformize(const src_reg
&src
) const
399 /* FIXME: We use a vector chan_index and dst to allow constant and
400 * copy propagration to move result all the way into the consuming
401 * instruction (typically a surface index or sampler index for a
402 * send). This uses 1 or 3 extra hw registers in 16 or 32 wide
403 * dispatch. Once we teach const/copy propagation about scalars we
404 * should go back to scalar destinations here.
406 const fs_builder ubld
= exec_all();
407 const dst_reg chan_index
= vgrf(BRW_REGISTER_TYPE_UD
);
408 const dst_reg dst
= vgrf(src
.type
);
410 ubld
.emit(SHADER_OPCODE_FIND_LIVE_CHANNEL
, chan_index
);
411 ubld
.emit(SHADER_OPCODE_BROADCAST
, dst
, src
, component(chan_index
, 0));
413 return src_reg(component(dst
, 0));
417 * Assorted arithmetic ops.
422 op(const dst_reg &dst, const src_reg &src0) const \
424 return emit(BRW_OPCODE_##op, dst, src0); \
429 op(const dst_reg &dst, const src_reg &src0, const src_reg &src1) const \
431 return emit(BRW_OPCODE_##op, dst, src0, src1); \
434 #define ALU2_ACC(op) \
436 op(const dst_reg &dst, const src_reg &src0, const src_reg &src1) const \
438 instruction *inst = emit(BRW_OPCODE_##op, dst, src0, src1); \
439 inst->writes_accumulator = true; \
445 op(const dst_reg &dst, const src_reg &src0, const src_reg &src1, \
446 const src_reg &src2) const \
448 return emit(BRW_OPCODE_##op, dst, src0, src1, src2); \
502 * CMP: Sets the low bit of the destination channels with the result
503 * of the comparison, while the upper bits are undefined, and updates
504 * the flag register with the packed 16 bits of the result.
507 CMP(const dst_reg
&dst
, const src_reg
&src0
, const src_reg
&src1
,
508 brw_conditional_mod condition
) const
510 /* Take the instruction:
512 * CMP null<d> src0<f> src1<f>
514 * Original gen4 does type conversion to the destination type
515 * before comparison, producing garbage results for floating
518 * The destination type doesn't matter on newer generations,
519 * so we set the type to match src0 so we can compact the
522 return set_condmod(condition
,
523 emit(BRW_OPCODE_CMP
, retype(dst
, src0
.type
),
524 fix_unsigned_negate(src0
),
525 fix_unsigned_negate(src1
)));
529 * Gen4 predicated IF.
532 IF(brw_predicate predicate
) const
534 return set_predicate(predicate
, emit(BRW_OPCODE_IF
));
538 * Emit a linear interpolation instruction.
541 LRP(const dst_reg
&dst
, const src_reg
&x
, const src_reg
&y
,
542 const src_reg
&a
) const
544 if (shader
->devinfo
->gen
>= 6) {
545 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
546 * we need to reorder the operands.
548 return emit(BRW_OPCODE_LRP
, dst
, a
, y
, x
);
551 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
552 const dst_reg y_times_a
= vgrf(dst
.type
);
553 const dst_reg one_minus_a
= vgrf(dst
.type
);
554 const dst_reg x_times_one_minus_a
= vgrf(dst
.type
);
556 MUL(y_times_a
, y
, a
);
557 ADD(one_minus_a
, negate(a
), brw_imm_f(1.0f
));
558 MUL(x_times_one_minus_a
, x
, src_reg(one_minus_a
));
559 return ADD(dst
, src_reg(x_times_one_minus_a
), src_reg(y_times_a
));
564 * Collect a number of registers in a contiguous range of registers.
567 LOAD_PAYLOAD(const dst_reg
&dst
, const src_reg
*src
,
568 unsigned sources
, unsigned header_size
) const
570 instruction
*inst
= emit(SHADER_OPCODE_LOAD_PAYLOAD
, dst
, src
, sources
);
571 inst
->header_size
= header_size
;
572 inst
->size_written
= header_size
* REG_SIZE
;
573 for (unsigned i
= header_size
; i
< sources
; i
++) {
574 inst
->size_written
+=
575 ALIGN(dispatch_width() * type_sz(src
[i
].type
) * dst
.stride
,
582 backend_shader
*shader
;
586 * Workaround for negation of UD registers. See comment in
587 * fs_generator::generate_code() for more details.
590 fix_unsigned_negate(const src_reg
&src
) const
592 if (src
.type
== BRW_REGISTER_TYPE_UD
&&
594 dst_reg temp
= vgrf(BRW_REGISTER_TYPE_UD
);
596 return src_reg(temp
);
603 * Workaround for source register modes not supported by the ternary
604 * instruction encoding.
607 fix_3src_operand(const src_reg
&src
) const
609 if (src
.file
== VGRF
|| src
.file
== UNIFORM
|| src
.stride
> 1) {
612 dst_reg expanded
= vgrf(src
.type
);
619 * Workaround for source register modes not supported by the math
623 fix_math_operand(const src_reg
&src
) const
625 /* Can't do hstride == 0 args on gen6 math, so expand it out. We
626 * might be able to do better by doing execsize = 1 math and then
627 * expanding that result out, but we would need to be careful with
630 * Gen6 hardware ignores source modifiers (negate and abs) on math
631 * instructions, so we also move to a temp to set those up.
633 * Gen7 relaxes most of the above restrictions, but still can't use IMM
636 if ((shader
->devinfo
->gen
== 6 &&
637 (src
.file
== IMM
|| src
.file
== UNIFORM
||
638 src
.abs
|| src
.negate
)) ||
639 (shader
->devinfo
->gen
== 7 && src
.file
== IMM
)) {
640 const dst_reg tmp
= vgrf(src
.type
);
651 unsigned _dispatch_width
;
653 bool force_writemask_all
;
655 /** Debug annotation info. */