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_VEC4_BUILDER_H
26 #define BRW_VEC4_BUILDER_H
28 #include "brw_ir_vec4.h"
29 #include "brw_ir_allocator.h"
30 #include "brw_context.h"
34 * Toolbox to assemble a VEC4 IR program out of individual instructions.
36 * This object is meant to have an interface consistent with
37 * brw::fs_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 brw::src_reg src_reg
;
46 /** Type used in this IR to represent the destination of an instruction. */
47 typedef brw::dst_reg dst_reg
;
49 /** Type used in this IR to represent an instruction. */
50 typedef vec4_instruction instruction
;
53 * Construct a vec4_builder that inserts instructions into \p shader.
55 vec4_builder(backend_shader
*shader
) :
56 shader(shader
), block(NULL
), cursor(NULL
),
57 force_writemask_all(false),
63 * Construct a vec4_builder that inserts instructions into \p shader
64 * before instruction \p inst in basic block \p block. The default
65 * execution controls and debug annotation are initialized from the
66 * instruction passed as argument.
68 vec4_builder(backend_shader
*shader
, bblock_t
*block
, instruction
*inst
) :
69 shader(shader
), block(block
), cursor(inst
),
70 force_writemask_all(inst
->force_writemask_all
)
72 annotation
.str
= inst
->annotation
;
73 annotation
.ir
= inst
->ir
;
77 * Construct a vec4_builder that inserts instructions before \p cursor
78 * in basic block \p block, inheriting other code generation parameters
82 at(bblock_t
*block
, exec_node
*cursor
) const
84 vec4_builder bld
= *this;
91 * Construct a vec4_builder appending instructions at the end of the
92 * instruction list of the shader, inheriting other code generation
93 * parameters from this.
98 return at(NULL
, (exec_node
*)&shader
->instructions
.tail_sentinel
);
102 * Construct a builder with per-channel control flow execution masking
103 * disabled if \p b is true. If control flow execution masking is
104 * already disabled this has no effect.
107 exec_all(bool b
= true) const
109 vec4_builder bld
= *this;
111 bld
.force_writemask_all
= true;
116 * Construct a builder with the given debug annotation info.
119 annotate(const char *str
, const void *ir
= NULL
) const
121 vec4_builder bld
= *this;
122 bld
.annotation
.str
= str
;
123 bld
.annotation
.ir
= ir
;
128 * Get the SIMD width in use.
131 dispatch_width() const
137 * Allocate a virtual register of natural vector size (four for this IR)
138 * and SIMD width. \p n gives the amount of space to allocate in
139 * dispatch_width units (which is just enough space for four logical
140 * components in this IR).
143 vgrf(enum brw_reg_type type
, unsigned n
= 1) const
145 assert(dispatch_width() <= 32);
148 return retype(dst_reg(VGRF
, shader
->alloc
.allocate(
149 n
* DIV_ROUND_UP(type_sz(type
), 4))),
152 return retype(null_reg_ud(), type
);
156 * Create a null register of floating type.
161 return dst_reg(retype(brw_null_vec(dispatch_width()),
162 BRW_REGISTER_TYPE_F
));
166 * Create a null register of signed integer type.
171 return dst_reg(retype(brw_null_vec(dispatch_width()),
172 BRW_REGISTER_TYPE_D
));
176 * Create a null register of unsigned integer type.
181 return dst_reg(retype(brw_null_vec(dispatch_width()),
182 BRW_REGISTER_TYPE_UD
));
186 * Insert an instruction into the program.
189 emit(const instruction
&inst
) const
191 return emit(new(shader
->mem_ctx
) instruction(inst
));
195 * Create and insert a nullary control instruction into the program.
198 emit(enum opcode opcode
) const
200 return emit(instruction(opcode
));
204 * Create and insert a nullary instruction into the program.
207 emit(enum opcode opcode
, const dst_reg
&dst
) const
209 return emit(instruction(opcode
, dst
));
213 * Create and insert a unary instruction into the program.
216 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
) const
219 case SHADER_OPCODE_RCP
:
220 case SHADER_OPCODE_RSQ
:
221 case SHADER_OPCODE_SQRT
:
222 case SHADER_OPCODE_EXP2
:
223 case SHADER_OPCODE_LOG2
:
224 case SHADER_OPCODE_SIN
:
225 case SHADER_OPCODE_COS
:
226 return fix_math_instruction(
227 emit(instruction(opcode
, dst
,
228 fix_math_operand(src0
))));
231 return emit(instruction(opcode
, dst
, src0
));
236 * Create and insert a binary instruction into the program.
239 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
240 const src_reg
&src1
) const
243 case SHADER_OPCODE_POW
:
244 case SHADER_OPCODE_INT_QUOTIENT
:
245 case SHADER_OPCODE_INT_REMAINDER
:
246 return fix_math_instruction(
247 emit(instruction(opcode
, dst
,
248 fix_math_operand(src0
),
249 fix_math_operand(src1
))));
252 return emit(instruction(opcode
, dst
, src0
, src1
));
257 * Create and insert a ternary instruction into the program.
260 emit(enum opcode opcode
, const dst_reg
&dst
, const src_reg
&src0
,
261 const src_reg
&src1
, const src_reg
&src2
) const
265 case BRW_OPCODE_BFI2
:
268 return emit(instruction(opcode
, dst
,
269 fix_3src_operand(src0
),
270 fix_3src_operand(src1
),
271 fix_3src_operand(src2
)));
274 return emit(instruction(opcode
, dst
, src0
, src1
, src2
));
279 * Insert a preallocated instruction into the program.
282 emit(instruction
*inst
) const
284 inst
->force_writemask_all
= force_writemask_all
;
285 inst
->annotation
= annotation
.str
;
286 inst
->ir
= annotation
.ir
;
289 static_cast<instruction
*>(cursor
)->insert_before(block
, inst
);
291 cursor
->insert_before(inst
);
297 * Select \p src0 if the comparison of both sources with the given
298 * conditional mod evaluates to true, otherwise select \p src1.
300 * Generally useful to get the minimum or maximum of two values.
303 emit_minmax(const dst_reg
&dst
, const src_reg
&src0
,
304 const src_reg
&src1
, brw_conditional_mod mod
) const
306 assert(mod
== BRW_CONDITIONAL_GE
|| mod
== BRW_CONDITIONAL_L
);
308 return set_condmod(mod
, SEL(dst
, fix_unsigned_negate(src0
),
309 fix_unsigned_negate(src1
)));
313 * Copy any live channel from \p src to the first channel of the result.
316 emit_uniformize(const src_reg
&src
) const
318 const vec4_builder ubld
= exec_all();
319 const dst_reg chan_index
=
320 writemask(vgrf(BRW_REGISTER_TYPE_UD
), WRITEMASK_X
);
321 const dst_reg dst
= vgrf(src
.type
);
323 ubld
.emit(SHADER_OPCODE_FIND_LIVE_CHANNEL
, chan_index
);
324 ubld
.emit(SHADER_OPCODE_BROADCAST
, dst
, src
, src_reg(chan_index
));
330 * Assorted arithmetic ops.
335 op(const dst_reg &dst, const src_reg &src0) const \
337 return emit(BRW_OPCODE_##op, dst, src0); \
342 op(const dst_reg &dst, const src_reg &src0, const src_reg &src1) const \
344 return emit(BRW_OPCODE_##op, dst, src0, src1); \
347 #define ALU2_ACC(op) \
349 op(const dst_reg &dst, const src_reg &src0, const src_reg &src1) const \
351 instruction *inst = emit(BRW_OPCODE_##op, dst, src0, src1); \
352 inst->writes_accumulator = true; \
358 op(const dst_reg &dst, const src_reg &src0, const src_reg &src1, \
359 const src_reg &src2) const \
361 return emit(BRW_OPCODE_##op, dst, src0, src1, src2); \
415 * CMP: Sets the low bit of the destination channels with the result
416 * of the comparison, while the upper bits are undefined, and updates
417 * the flag register with the packed 16 bits of the result.
420 CMP(const dst_reg
&dst
, const src_reg
&src0
, const src_reg
&src1
,
421 brw_conditional_mod condition
) const
423 /* Take the instruction:
425 * CMP null<d> src0<f> src1<f>
427 * Original gen4 does type conversion to the destination type
428 * before comparison, producing garbage results for floating
431 * The destination type doesn't matter on newer generations,
432 * so we set the type to match src0 so we can compact the
435 return set_condmod(condition
,
436 emit(BRW_OPCODE_CMP
, retype(dst
, src0
.type
),
437 fix_unsigned_negate(src0
),
438 fix_unsigned_negate(src1
)));
442 * Gen4 predicated IF.
445 IF(brw_predicate predicate
) const
447 return set_predicate(predicate
, emit(BRW_OPCODE_IF
));
451 * Gen6 IF with embedded comparison.
454 IF(const src_reg
&src0
, const src_reg
&src1
,
455 brw_conditional_mod condition
) const
457 assert(shader
->devinfo
->gen
== 6);
458 return set_condmod(condition
,
461 fix_unsigned_negate(src0
),
462 fix_unsigned_negate(src1
)));
466 * Emit a linear interpolation instruction.
469 LRP(const dst_reg
&dst
, const src_reg
&x
, const src_reg
&y
,
470 const src_reg
&a
) const
472 if (shader
->devinfo
->gen
>= 6) {
473 /* The LRP instruction actually does op1 * op0 + op2 * (1 - op0), so
474 * we need to reorder the operands.
476 return emit(BRW_OPCODE_LRP
, dst
, a
, y
, x
);
479 /* We can't use the LRP instruction. Emit x*(1-a) + y*a. */
480 const dst_reg y_times_a
= vgrf(dst
.type
);
481 const dst_reg one_minus_a
= vgrf(dst
.type
);
482 const dst_reg x_times_one_minus_a
= vgrf(dst
.type
);
484 MUL(y_times_a
, y
, a
);
485 ADD(one_minus_a
, negate(a
), brw_imm_f(1.0f
));
486 MUL(x_times_one_minus_a
, x
, src_reg(one_minus_a
));
487 return ADD(dst
, src_reg(x_times_one_minus_a
), src_reg(y_times_a
));
491 backend_shader
*shader
;
495 * Workaround for negation of UD registers. See comment in
496 * fs_generator::generate_code() for the details.
499 fix_unsigned_negate(const src_reg
&src
) const
501 if (src
.type
== BRW_REGISTER_TYPE_UD
&& src
.negate
) {
502 dst_reg temp
= vgrf(BRW_REGISTER_TYPE_UD
);
504 return src_reg(temp
);
511 * Workaround for register access modes not supported by the ternary
512 * instruction encoding.
515 fix_3src_operand(const src_reg
&src
) const
517 /* Using vec4 uniforms in SIMD4x2 programs is difficult. You'd like to be
518 * able to use vertical stride of zero to replicate the vec4 uniform, like
520 * g3<0;4,1>:f - [0, 4][1, 5][2, 6][3, 7]
522 * But you can't, since vertical stride is always four in three-source
523 * instructions. Instead, insert a MOV instruction to do the replication so
524 * that the three-source instruction can consume it.
527 /* The MOV is only needed if the source is a uniform or immediate. */
528 if (src
.file
!= UNIFORM
&& src
.file
!= IMM
)
531 if (src
.file
== UNIFORM
&& brw_is_single_value_swizzle(src
.swizzle
))
534 const dst_reg expanded
= vgrf(src
.type
);
535 emit(VEC4_OPCODE_UNPACK_UNIFORM
, expanded
, src
);
536 return src_reg(expanded
);
540 * Workaround for register access modes not supported by the math
544 fix_math_operand(const src_reg
&src
) const
546 /* The gen6 math instruction ignores the source modifiers --
547 * swizzle, abs, negate, and at least some parts of the register
548 * region description.
550 * Rather than trying to enumerate all these cases, *always* expand the
551 * operand to a temp GRF for gen6.
553 * For gen7, keep the operand as-is, except if immediate, which gen7 still
556 if (shader
->devinfo
->gen
== 6 ||
557 (shader
->devinfo
->gen
== 7 && src
.file
== IMM
)) {
558 const dst_reg tmp
= vgrf(src
.type
);
567 * Workaround other weirdness of the math instruction.
570 fix_math_instruction(instruction
*inst
) const
572 if (shader
->devinfo
->gen
== 6 &&
573 inst
->dst
.writemask
!= WRITEMASK_XYZW
) {
574 const dst_reg tmp
= vgrf(inst
->dst
.type
);
575 MOV(inst
->dst
, src_reg(tmp
));
578 } else if (shader
->devinfo
->gen
< 6) {
579 const unsigned sources
= (inst
->src
[1].file
== BAD_FILE
? 1 : 2);
581 inst
->mlen
= sources
;
590 bool force_writemask_all
;
592 /** Debug annotation info. */