2 * Copyright © 2015 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 #include "brw_vec4_builder.h"
27 #include "brw_vec4_surface_builder.h"
31 using namespace brw::surface_access
;
36 vec4_visitor::emit_nir_code()
38 if (nir
->num_uniforms
> 0)
41 nir_emit_impl(nir_shader_get_entrypoint((nir_shader
*)nir
));
45 vec4_visitor::nir_setup_uniforms()
47 uniforms
= nir
->num_uniforms
/ 16;
51 vec4_visitor::nir_emit_impl(nir_function_impl
*impl
)
53 nir_locals
= ralloc_array(mem_ctx
, dst_reg
, impl
->reg_alloc
);
54 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
55 nir_locals
[i
] = dst_reg();
58 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
59 unsigned array_elems
=
60 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
61 const unsigned num_regs
= array_elems
* DIV_ROUND_UP(reg
->bit_size
, 32);
62 nir_locals
[reg
->index
] = dst_reg(VGRF
, alloc
.allocate(num_regs
));
64 if (reg
->bit_size
== 64)
65 nir_locals
[reg
->index
].type
= BRW_REGISTER_TYPE_DF
;
68 nir_ssa_values
= ralloc_array(mem_ctx
, dst_reg
, impl
->ssa_alloc
);
70 nir_emit_cf_list(&impl
->body
);
74 vec4_visitor::nir_emit_cf_list(exec_list
*list
)
76 exec_list_validate(list
);
77 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
80 nir_emit_if(nir_cf_node_as_if(node
));
83 case nir_cf_node_loop
:
84 nir_emit_loop(nir_cf_node_as_loop(node
));
87 case nir_cf_node_block
:
88 nir_emit_block(nir_cf_node_as_block(node
));
92 unreachable("Invalid CFG node block");
98 vec4_visitor::nir_emit_if(nir_if
*if_stmt
)
100 /* First, put the condition in f0 */
101 src_reg condition
= get_nir_src(if_stmt
->condition
, BRW_REGISTER_TYPE_D
, 1);
102 vec4_instruction
*inst
= emit(MOV(dst_null_d(), condition
));
103 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
105 /* We can just predicate based on the X channel, as the condition only
106 * goes on its own line */
107 emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X
));
109 nir_emit_cf_list(&if_stmt
->then_list
);
111 /* note: if the else is empty, dead CF elimination will remove it */
112 emit(BRW_OPCODE_ELSE
);
114 nir_emit_cf_list(&if_stmt
->else_list
);
116 emit(BRW_OPCODE_ENDIF
);
120 vec4_visitor::nir_emit_loop(nir_loop
*loop
)
124 nir_emit_cf_list(&loop
->body
);
126 emit(BRW_OPCODE_WHILE
);
130 vec4_visitor::nir_emit_block(nir_block
*block
)
132 nir_foreach_instr(instr
, block
) {
133 nir_emit_instr(instr
);
138 vec4_visitor::nir_emit_instr(nir_instr
*instr
)
142 switch (instr
->type
) {
143 case nir_instr_type_load_const
:
144 nir_emit_load_const(nir_instr_as_load_const(instr
));
147 case nir_instr_type_intrinsic
:
148 nir_emit_intrinsic(nir_instr_as_intrinsic(instr
));
151 case nir_instr_type_alu
:
152 nir_emit_alu(nir_instr_as_alu(instr
));
155 case nir_instr_type_jump
:
156 nir_emit_jump(nir_instr_as_jump(instr
));
159 case nir_instr_type_tex
:
160 nir_emit_texture(nir_instr_as_tex(instr
));
163 case nir_instr_type_ssa_undef
:
164 nir_emit_undef(nir_instr_as_ssa_undef(instr
));
168 unreachable("VS instruction not yet implemented by NIR->vec4");
173 dst_reg_for_nir_reg(vec4_visitor
*v
, nir_register
*nir_reg
,
174 unsigned base_offset
, nir_src
*indirect
)
178 reg
= v
->nir_locals
[nir_reg
->index
];
179 if (nir_reg
->bit_size
== 64)
180 reg
.type
= BRW_REGISTER_TYPE_DF
;
181 reg
= offset(reg
, 8, base_offset
);
184 new(v
->mem_ctx
) src_reg(v
->get_nir_src(*indirect
,
192 vec4_visitor::get_nir_dest(const nir_dest
&dest
)
196 dst_reg(VGRF
, alloc
.allocate(DIV_ROUND_UP(dest
.ssa
.bit_size
, 32)));
197 if (dest
.ssa
.bit_size
== 64)
198 dst
.type
= BRW_REGISTER_TYPE_DF
;
199 nir_ssa_values
[dest
.ssa
.index
] = dst
;
202 return dst_reg_for_nir_reg(this, dest
.reg
.reg
, dest
.reg
.base_offset
,
208 vec4_visitor::get_nir_dest(const nir_dest
&dest
, enum brw_reg_type type
)
210 return retype(get_nir_dest(dest
), type
);
214 vec4_visitor::get_nir_dest(const nir_dest
&dest
, nir_alu_type type
)
216 return get_nir_dest(dest
, brw_type_for_nir_type(devinfo
, type
));
220 vec4_visitor::get_nir_src(const nir_src
&src
, enum brw_reg_type type
,
221 unsigned num_components
)
226 assert(src
.ssa
!= NULL
);
227 reg
= nir_ssa_values
[src
.ssa
->index
];
230 reg
= dst_reg_for_nir_reg(this, src
.reg
.reg
, src
.reg
.base_offset
,
234 reg
= retype(reg
, type
);
236 src_reg reg_as_src
= src_reg(reg
);
237 reg_as_src
.swizzle
= brw_swizzle_for_size(num_components
);
242 vec4_visitor::get_nir_src(const nir_src
&src
, nir_alu_type type
,
243 unsigned num_components
)
245 return get_nir_src(src
, brw_type_for_nir_type(devinfo
, type
),
250 vec4_visitor::get_nir_src(const nir_src
&src
, unsigned num_components
)
252 /* if type is not specified, default to signed int */
253 return get_nir_src(src
, nir_type_int32
, num_components
);
257 vec4_visitor::get_nir_src_imm(const nir_src
&src
)
259 assert(nir_src_num_components(src
) == 1);
260 assert(nir_src_bit_size(src
) == 32);
261 return nir_src_is_const(src
) ? src_reg(brw_imm_d(nir_src_as_int(src
))) :
266 vec4_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
268 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
270 if (nir_src_is_const(*offset_src
)) {
271 /* The only constant offset we should find is 0. brw_nir.c's
272 * add_const_offset_to_base() will fold other constant offsets
273 * into instr->const_index[0].
275 assert(nir_src_as_uint(*offset_src
) == 0);
279 return get_nir_src(*offset_src
, BRW_REGISTER_TYPE_UD
, 1);
283 setup_imm_df(const vec4_builder
&bld
, double v
)
285 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
286 assert(devinfo
->gen
>= 7);
288 if (devinfo
->gen
>= 8)
289 return brw_imm_df(v
);
291 /* gen7.5 does not support DF immediates straighforward but the DIM
292 * instruction allows to set the 64-bit immediate value.
294 if (devinfo
->is_haswell
) {
295 const vec4_builder ubld
= bld
.exec_all();
296 const dst_reg dst
= bld
.vgrf(BRW_REGISTER_TYPE_DF
);
297 ubld
.DIM(dst
, brw_imm_df(v
));
298 return swizzle(src_reg(dst
), BRW_SWIZZLE_XXXX
);
301 /* gen7 does not support DF immediates */
312 /* Write the low 32-bit of the constant to the X:UD channel and the
313 * high 32-bit to the Y:UD channel to build the constant in a VGRF.
314 * We have to do this twice (offset 0 and offset 1), since a DF VGRF takes
315 * two SIMD8 registers in SIMD4x2 execution. Finally, return a swizzle
316 * XXXX so any access to the VGRF only reads the constant data in these
319 const dst_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
320 for (unsigned n
= 0; n
< 2; n
++) {
321 const vec4_builder ubld
= bld
.exec_all().group(4, n
);
322 ubld
.MOV(writemask(offset(tmp
, 8, n
), WRITEMASK_X
), brw_imm_ud(di
.i1
));
323 ubld
.MOV(writemask(offset(tmp
, 8, n
), WRITEMASK_Y
), brw_imm_ud(di
.i2
));
326 return swizzle(src_reg(retype(tmp
, BRW_REGISTER_TYPE_DF
)), BRW_SWIZZLE_XXXX
);
330 vec4_visitor::nir_emit_load_const(nir_load_const_instr
*instr
)
334 if (instr
->def
.bit_size
== 64) {
335 reg
= dst_reg(VGRF
, alloc
.allocate(2));
336 reg
.type
= BRW_REGISTER_TYPE_DF
;
338 reg
= dst_reg(VGRF
, alloc
.allocate(1));
339 reg
.type
= BRW_REGISTER_TYPE_D
;
342 const vec4_builder ibld
= vec4_builder(this).at_end();
343 unsigned remaining
= brw_writemask_for_size(instr
->def
.num_components
);
345 /* @FIXME: consider emitting vector operations to save some MOVs in
346 * cases where the components are representable in 8 bits.
347 * For now, we emit a MOV for each distinct value.
349 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
350 unsigned writemask
= 1 << i
;
352 if ((remaining
& writemask
) == 0)
355 for (unsigned j
= i
; j
< instr
->def
.num_components
; j
++) {
356 if ((instr
->def
.bit_size
== 32 &&
357 instr
->value
[i
].u32
== instr
->value
[j
].u32
) ||
358 (instr
->def
.bit_size
== 64 &&
359 instr
->value
[i
].f64
== instr
->value
[j
].f64
)) {
364 reg
.writemask
= writemask
;
365 if (instr
->def
.bit_size
== 64) {
366 emit(MOV(reg
, setup_imm_df(ibld
, instr
->value
[i
].f64
)));
368 emit(MOV(reg
, brw_imm_d(instr
->value
[i
].i32
)));
371 remaining
&= ~writemask
;
374 /* Set final writemask */
375 reg
.writemask
= brw_writemask_for_size(instr
->def
.num_components
);
377 nir_ssa_values
[instr
->def
.index
] = reg
;
381 vec4_visitor::get_nir_ssbo_intrinsic_index(nir_intrinsic_instr
*instr
)
383 /* SSBO stores are weird in that their index is in src[1] */
384 const unsigned src
= instr
->intrinsic
== nir_intrinsic_store_ssbo
? 1 : 0;
387 if (nir_src_is_const(instr
->src
[src
])) {
388 unsigned index
= prog_data
->base
.binding_table
.ssbo_start
+
389 nir_src_as_uint(instr
->src
[src
]);
390 surf_index
= brw_imm_ud(index
);
392 surf_index
= src_reg(this, glsl_type::uint_type
);
393 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[src
], 1),
394 brw_imm_ud(prog_data
->base
.binding_table
.ssbo_start
)));
395 surf_index
= emit_uniformize(surf_index
);
402 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr
*instr
)
407 switch (instr
->intrinsic
) {
409 case nir_intrinsic_load_input
: {
410 /* We set EmitNoIndirectInput for VS */
411 unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
413 dest
= get_nir_dest(instr
->dest
);
414 dest
.writemask
= brw_writemask_for_size(instr
->num_components
);
416 src
= src_reg(ATTR
, instr
->const_index
[0] + load_offset
,
417 glsl_type::uvec4_type
);
418 src
= retype(src
, dest
.type
);
420 bool is_64bit
= nir_dest_bit_size(instr
->dest
) == 64;
422 dst_reg tmp
= dst_reg(this, glsl_type::dvec4_type
);
423 src
.swizzle
= BRW_SWIZZLE_XYZW
;
424 shuffle_64bit_data(tmp
, src
, false);
425 emit(MOV(dest
, src_reg(tmp
)));
427 /* Swizzle source based on component layout qualifier */
428 src
.swizzle
= BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr
));
429 emit(MOV(dest
, src
));
434 case nir_intrinsic_store_output
: {
435 unsigned store_offset
= nir_src_as_uint(instr
->src
[1]);
436 int varying
= instr
->const_index
[0] + store_offset
;
438 bool is_64bit
= nir_src_bit_size(instr
->src
[0]) == 64;
441 src
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_DF
,
442 instr
->num_components
);
443 data
= src_reg(this, glsl_type::dvec4_type
);
444 shuffle_64bit_data(dst_reg(data
), src
, true);
445 src
= retype(data
, BRW_REGISTER_TYPE_F
);
447 src
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_F
,
448 instr
->num_components
);
451 unsigned c
= nir_intrinsic_component(instr
);
452 output_reg
[varying
][c
] = dst_reg(src
);
453 output_num_components
[varying
][c
] = instr
->num_components
;
455 unsigned num_components
= instr
->num_components
;
459 output_reg
[varying
][c
] = dst_reg(src
);
460 output_num_components
[varying
][c
] = MIN2(4, num_components
);
462 if (is_64bit
&& num_components
> 4) {
463 assert(num_components
<= 8);
464 output_reg
[varying
+ 1][c
] = byte_offset(dst_reg(src
), REG_SIZE
);
465 output_num_components
[varying
+ 1][c
] = num_components
- 4;
470 case nir_intrinsic_get_buffer_size
: {
471 assert(nir_src_num_components(instr
->src
[0]) == 1);
472 unsigned ssbo_index
= nir_src_is_const(instr
->src
[0]) ?
473 nir_src_as_uint(instr
->src
[0]) : 0;
475 const unsigned index
=
476 prog_data
->base
.binding_table
.ssbo_start
+ ssbo_index
;
477 dst_reg result_dst
= get_nir_dest(instr
->dest
);
478 vec4_instruction
*inst
= new(mem_ctx
)
479 vec4_instruction(SHADER_OPCODE_GET_BUFFER_SIZE
, result_dst
);
482 inst
->mlen
= 1; /* always at least one */
483 inst
->src
[1] = brw_imm_ud(index
);
485 /* MRF for the first parameter */
486 src_reg lod
= brw_imm_d(0);
487 int param_base
= inst
->base_mrf
;
488 int writemask
= WRITEMASK_X
;
489 emit(MOV(dst_reg(MRF
, param_base
, glsl_type::int_type
, writemask
), lod
));
495 case nir_intrinsic_store_ssbo
: {
496 assert(devinfo
->gen
>= 7);
498 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
499 assert(nir_src_bit_size(instr
->src
[0]) == 32);
500 assert(nir_intrinsic_write_mask(instr
) ==
501 (1u << instr
->num_components
) - 1);
503 src_reg surf_index
= get_nir_ssbo_intrinsic_index(instr
);
504 src_reg offset_reg
= retype(get_nir_src_imm(instr
->src
[2]),
505 BRW_REGISTER_TYPE_UD
);
508 src_reg val_reg
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_F
, 4);
510 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
511 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
512 * typed and untyped messages and across hardware platforms, the
513 * current implementation of the untyped messages will transparently convert
514 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
515 * and enabling only channel X on the SEND instruction.
517 * The above, works well for full vector writes, but not for partial writes
518 * where we want to write some channels and not others, like when we have
519 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
520 * quite restrictive with regards to the channel enables we can configure in
521 * the message descriptor (not all combinations are allowed) we cannot simply
522 * implement these scenarios with a single message while keeping the
523 * aforementioned symmetry in the implementation. For now we de decided that
524 * it is better to keep the symmetry to reduce complexity, so in situations
525 * such as the one described we end up emitting two untyped write messages
526 * (one for xy and another for w).
528 * The code below packs consecutive channels into a single write message,
529 * detects gaps in the vector write and if needed, sends a second message
530 * with the remaining channels. If in the future we decide that we want to
531 * emit a single message at the expense of losing the symmetry in the
532 * implementation we can:
534 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
535 * message payload. In this mode we can write up to 8 offsets and dwords
536 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
537 * and select which of the 8 channels carry data to write by setting the
538 * appropriate writemask in the dst register of the SEND instruction.
539 * It would require to write a new generator opcode specifically for
540 * IvyBridge since we would need to prepare a SIMD8 payload that could
541 * use any channel, not just X.
543 * 2) For Haswell+: Simply send a single write message but set the writemask
544 * on the dst of the SEND instruction to select the channels we want to
545 * write. It would require to modify the current messages to receive
546 * and honor the writemask provided.
548 const vec4_builder bld
= vec4_builder(this).at_end()
549 .annotate(current_annotation
, base_ir
);
551 emit_untyped_write(bld
, surf_index
, offset_reg
, val_reg
,
552 1 /* dims */, instr
->num_components
/* size */,
557 case nir_intrinsic_load_ssbo
: {
558 assert(devinfo
->gen
>= 7);
560 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
561 assert(nir_dest_bit_size(instr
->dest
) == 32);
563 src_reg surf_index
= get_nir_ssbo_intrinsic_index(instr
);
564 src_reg offset_reg
= retype(get_nir_src_imm(instr
->src
[1]),
565 BRW_REGISTER_TYPE_UD
);
567 /* Read the vector */
568 const vec4_builder bld
= vec4_builder(this).at_end()
569 .annotate(current_annotation
, base_ir
);
571 src_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
572 1 /* dims */, 4 /* size*/,
574 dst_reg dest
= get_nir_dest(instr
->dest
);
575 read_result
.type
= dest
.type
;
576 read_result
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
577 emit(MOV(dest
, read_result
));
581 case nir_intrinsic_ssbo_atomic_add
: {
582 int op
= BRW_AOP_ADD
;
584 if (nir_src_is_const(instr
->src
[2])) {
585 int add_val
= nir_src_as_int(instr
->src
[2]);
588 else if (add_val
== -1)
592 nir_emit_ssbo_atomic(op
, instr
);
595 case nir_intrinsic_ssbo_atomic_imin
:
596 nir_emit_ssbo_atomic(BRW_AOP_IMIN
, instr
);
598 case nir_intrinsic_ssbo_atomic_umin
:
599 nir_emit_ssbo_atomic(BRW_AOP_UMIN
, instr
);
601 case nir_intrinsic_ssbo_atomic_imax
:
602 nir_emit_ssbo_atomic(BRW_AOP_IMAX
, instr
);
604 case nir_intrinsic_ssbo_atomic_umax
:
605 nir_emit_ssbo_atomic(BRW_AOP_UMAX
, instr
);
607 case nir_intrinsic_ssbo_atomic_and
:
608 nir_emit_ssbo_atomic(BRW_AOP_AND
, instr
);
610 case nir_intrinsic_ssbo_atomic_or
:
611 nir_emit_ssbo_atomic(BRW_AOP_OR
, instr
);
613 case nir_intrinsic_ssbo_atomic_xor
:
614 nir_emit_ssbo_atomic(BRW_AOP_XOR
, instr
);
616 case nir_intrinsic_ssbo_atomic_exchange
:
617 nir_emit_ssbo_atomic(BRW_AOP_MOV
, instr
);
619 case nir_intrinsic_ssbo_atomic_comp_swap
:
620 nir_emit_ssbo_atomic(BRW_AOP_CMPWR
, instr
);
623 case nir_intrinsic_load_vertex_id
:
624 unreachable("should be lowered by lower_vertex_id()");
626 case nir_intrinsic_load_vertex_id_zero_base
:
627 case nir_intrinsic_load_base_vertex
:
628 case nir_intrinsic_load_instance_id
:
629 case nir_intrinsic_load_base_instance
:
630 case nir_intrinsic_load_draw_id
:
631 case nir_intrinsic_load_invocation_id
:
632 unreachable("should be lowered by brw_nir_lower_vs_inputs()");
634 case nir_intrinsic_load_uniform
: {
635 /* Offsets are in bytes but they should always be multiples of 4 */
636 assert(nir_intrinsic_base(instr
) % 4 == 0);
638 dest
= get_nir_dest(instr
->dest
);
640 src
= src_reg(dst_reg(UNIFORM
, nir_intrinsic_base(instr
) / 16));
641 src
.type
= dest
.type
;
643 /* Uniforms don't actually have to be vec4 aligned. In the case that
644 * it isn't, we have to use a swizzle to shift things around. They
645 * do still have the std140 alignment requirement that vec2's have to
646 * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
648 * The swizzle also works in the indirect case as the generator adds
649 * the swizzle to the offset for us.
651 const int type_size
= type_sz(src
.type
);
652 unsigned shift
= (nir_intrinsic_base(instr
) % 16) / type_size
;
653 assert(shift
+ instr
->num_components
<= 4);
655 if (nir_src_is_const(instr
->src
[0])) {
656 const unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
657 /* Offsets are in bytes but they should always be multiples of 4 */
658 assert(load_offset
% 4 == 0);
660 src
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
661 dest
.writemask
= brw_writemask_for_size(instr
->num_components
);
662 unsigned offset
= load_offset
+ shift
* type_size
;
663 src
.offset
= ROUND_DOWN_TO(offset
, 16);
664 shift
= (offset
% 16) / type_size
;
665 assert(shift
+ instr
->num_components
<= 4);
666 src
.swizzle
+= BRW_SWIZZLE4(shift
, shift
, shift
, shift
);
668 emit(MOV(dest
, src
));
670 /* Uniform arrays are vec4 aligned, because of std140 alignment
675 src_reg indirect
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_UD
, 1);
677 /* MOV_INDIRECT is going to stomp the whole thing anyway */
678 dest
.writemask
= WRITEMASK_XYZW
;
680 emit(SHADER_OPCODE_MOV_INDIRECT
, dest
, src
,
681 indirect
, brw_imm_ud(instr
->const_index
[1]));
686 case nir_intrinsic_load_ubo
: {
689 dest
= get_nir_dest(instr
->dest
);
691 if (nir_src_is_const(instr
->src
[0])) {
692 /* The block index is a constant, so just emit the binding table entry
695 const unsigned index
= prog_data
->base
.binding_table
.ubo_start
+
696 nir_src_as_uint(instr
->src
[0]);
697 surf_index
= brw_imm_ud(index
);
699 /* The block index is not a constant. Evaluate the index expression
700 * per-channel and add the base UBO index; we have to select a value
701 * from any live channel.
703 surf_index
= src_reg(this, glsl_type::uint_type
);
704 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[0], nir_type_int32
,
705 instr
->num_components
),
706 brw_imm_ud(prog_data
->base
.binding_table
.ubo_start
)));
707 surf_index
= emit_uniformize(surf_index
);
711 if (nir_src_is_const(instr
->src
[1])) {
712 unsigned load_offset
= nir_src_as_uint(instr
->src
[1]);
713 offset_reg
= brw_imm_ud(load_offset
& ~15);
715 offset_reg
= src_reg(this, glsl_type::uint_type
);
716 emit(MOV(dst_reg(offset_reg
),
717 get_nir_src(instr
->src
[1], nir_type_uint32
, 1)));
720 src_reg packed_consts
;
721 if (nir_dest_bit_size(instr
->dest
) == 32) {
722 packed_consts
= src_reg(this, glsl_type::vec4_type
);
723 emit_pull_constant_load_reg(dst_reg(packed_consts
),
726 NULL
, NULL
/* before_block/inst */);
728 src_reg temp
= src_reg(this, glsl_type::dvec4_type
);
729 src_reg temp_float
= retype(temp
, BRW_REGISTER_TYPE_F
);
731 emit_pull_constant_load_reg(dst_reg(temp_float
),
732 surf_index
, offset_reg
, NULL
, NULL
);
733 if (offset_reg
.file
== IMM
)
736 emit(ADD(dst_reg(offset_reg
), offset_reg
, brw_imm_ud(16u)));
737 emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float
, REG_SIZE
)),
738 surf_index
, offset_reg
, NULL
, NULL
);
740 packed_consts
= src_reg(this, glsl_type::dvec4_type
);
741 shuffle_64bit_data(dst_reg(packed_consts
), temp
, false);
744 packed_consts
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
745 if (nir_src_is_const(instr
->src
[1])) {
746 unsigned load_offset
= nir_src_as_uint(instr
->src
[1]);
747 unsigned type_size
= type_sz(dest
.type
);
748 packed_consts
.swizzle
+=
749 BRW_SWIZZLE4(load_offset
% 16 / type_size
,
750 load_offset
% 16 / type_size
,
751 load_offset
% 16 / type_size
,
752 load_offset
% 16 / type_size
);
755 emit(MOV(dest
, retype(packed_consts
, dest
.type
)));
760 case nir_intrinsic_memory_barrier
: {
761 const vec4_builder bld
=
762 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
763 const dst_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
764 bld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
, brw_vec8_grf(0, 0))
765 ->size_written
= 2 * REG_SIZE
;
769 case nir_intrinsic_shader_clock
: {
770 /* We cannot do anything if there is an event, so ignore it for now */
771 const src_reg shader_clock
= get_timestamp();
772 const enum brw_reg_type type
= brw_type_for_base_type(glsl_type::uvec2_type
);
774 dest
= get_nir_dest(instr
->dest
, type
);
775 emit(MOV(dest
, shader_clock
));
780 unreachable("Unknown intrinsic");
785 vec4_visitor::nir_emit_ssbo_atomic(int op
, nir_intrinsic_instr
*instr
)
788 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
789 dest
= get_nir_dest(instr
->dest
);
791 src_reg surface
= get_nir_ssbo_intrinsic_index(instr
);
792 src_reg offset
= get_nir_src(instr
->src
[1], 1);
794 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
795 data1
= get_nir_src(instr
->src
[2], 1);
797 if (op
== BRW_AOP_CMPWR
)
798 data2
= get_nir_src(instr
->src
[3], 1);
800 /* Emit the actual atomic operation operation */
801 const vec4_builder bld
=
802 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
804 src_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
806 1 /* dims */, 1 /* rsize */,
809 dest
.type
= atomic_result
.type
;
810 bld
.MOV(dest
, atomic_result
);
814 brw_swizzle_for_nir_swizzle(uint8_t swizzle
[4])
816 return BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
819 static enum brw_conditional_mod
820 brw_conditional_for_nir_comparison(nir_op op
)
826 return BRW_CONDITIONAL_L
;
831 return BRW_CONDITIONAL_GE
;
835 case nir_op_b32all_fequal2
:
836 case nir_op_b32all_iequal2
:
837 case nir_op_b32all_fequal3
:
838 case nir_op_b32all_iequal3
:
839 case nir_op_b32all_fequal4
:
840 case nir_op_b32all_iequal4
:
841 return BRW_CONDITIONAL_Z
;
845 case nir_op_b32any_fnequal2
:
846 case nir_op_b32any_inequal2
:
847 case nir_op_b32any_fnequal3
:
848 case nir_op_b32any_inequal3
:
849 case nir_op_b32any_fnequal4
:
850 case nir_op_b32any_inequal4
:
851 return BRW_CONDITIONAL_NZ
;
854 unreachable("not reached: bad operation for comparison");
859 vec4_visitor::optimize_predicate(nir_alu_instr
*instr
,
860 enum brw_predicate
*predicate
)
862 if (!instr
->src
[0].src
.is_ssa
||
863 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
866 nir_alu_instr
*cmp_instr
=
867 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
869 switch (cmp_instr
->op
) {
870 case nir_op_b32any_fnequal2
:
871 case nir_op_b32any_inequal2
:
872 case nir_op_b32any_fnequal3
:
873 case nir_op_b32any_inequal3
:
874 case nir_op_b32any_fnequal4
:
875 case nir_op_b32any_inequal4
:
876 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
878 case nir_op_b32all_fequal2
:
879 case nir_op_b32all_iequal2
:
880 case nir_op_b32all_fequal3
:
881 case nir_op_b32all_iequal3
:
882 case nir_op_b32all_fequal4
:
883 case nir_op_b32all_iequal4
:
884 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
890 unsigned size_swizzle
=
891 brw_swizzle_for_size(nir_op_infos
[cmp_instr
->op
].input_sizes
[0]);
894 assert(nir_op_infos
[cmp_instr
->op
].num_inputs
== 2);
895 for (unsigned i
= 0; i
< 2; i
++) {
896 nir_alu_type type
= nir_op_infos
[cmp_instr
->op
].input_types
[i
];
897 unsigned bit_size
= nir_src_bit_size(cmp_instr
->src
[i
].src
);
898 type
= (nir_alu_type
) (((unsigned) type
) | bit_size
);
899 op
[i
] = get_nir_src(cmp_instr
->src
[i
].src
, type
, 4);
900 unsigned base_swizzle
=
901 brw_swizzle_for_nir_swizzle(cmp_instr
->src
[i
].swizzle
);
902 op
[i
].swizzle
= brw_compose_swizzle(size_swizzle
, base_swizzle
);
903 op
[i
].abs
= cmp_instr
->src
[i
].abs
;
904 op
[i
].negate
= cmp_instr
->src
[i
].negate
;
907 emit(CMP(dst_null_d(), op
[0], op
[1],
908 brw_conditional_for_nir_comparison(cmp_instr
->op
)));
914 emit_find_msb_using_lzd(const vec4_builder
&bld
,
919 vec4_instruction
*inst
;
923 /* LZD of an absolute value source almost always does the right
924 * thing. There are two problem values:
926 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
927 * 0. However, findMSB(int(0x80000000)) == 30.
929 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
930 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
932 * For a value of zero or negative one, -1 will be returned.
934 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
935 * findMSB(-(1<<x)) should return x-1.
937 * For all negative number cases, including 0x80000000 and
938 * 0xffffffff, the correct value is obtained from LZD if instead of
939 * negating the (already negative) value the logical-not is used. A
940 * conditonal logical-not can be achieved in two instructions.
942 temp
= src_reg(bld
.vgrf(BRW_REGISTER_TYPE_D
));
944 bld
.ASR(dst_reg(temp
), src
, brw_imm_d(31));
945 bld
.XOR(dst_reg(temp
), temp
, src
);
948 bld
.LZD(retype(dst
, BRW_REGISTER_TYPE_UD
),
949 retype(temp
, BRW_REGISTER_TYPE_UD
));
951 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
952 * from the LSB side. Subtract the result from 31 to convert the MSB count
953 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
954 * which is exactly what findMSB() is supposed to return.
956 inst
= bld
.ADD(dst
, retype(src_reg(dst
), BRW_REGISTER_TYPE_D
),
958 inst
->src
[0].negate
= true;
962 vec4_visitor::emit_conversion_from_double(dst_reg dst
, src_reg src
,
965 /* BDW PRM vol 15 - workarounds:
966 * DF->f format conversion for Align16 has wrong emask calculation when
967 * source is immediate.
969 if (devinfo
->gen
== 8 && dst
.type
== BRW_REGISTER_TYPE_F
&&
970 src
.file
== BRW_IMMEDIATE_VALUE
) {
971 vec4_instruction
*inst
= emit(MOV(dst
, brw_imm_f(src
.df
)));
972 inst
->saturate
= saturate
;
978 case BRW_REGISTER_TYPE_D
:
979 op
= VEC4_OPCODE_DOUBLE_TO_D32
;
981 case BRW_REGISTER_TYPE_UD
:
982 op
= VEC4_OPCODE_DOUBLE_TO_U32
;
984 case BRW_REGISTER_TYPE_F
:
985 op
= VEC4_OPCODE_DOUBLE_TO_F32
;
988 unreachable("Unknown conversion");
991 dst_reg temp
= dst_reg(this, glsl_type::dvec4_type
);
992 emit(MOV(temp
, src
));
993 dst_reg temp2
= dst_reg(this, glsl_type::dvec4_type
);
994 emit(op
, temp2
, src_reg(temp
));
996 emit(VEC4_OPCODE_PICK_LOW_32BIT
, retype(temp2
, dst
.type
), src_reg(temp2
));
997 vec4_instruction
*inst
= emit(MOV(dst
, src_reg(retype(temp2
, dst
.type
))));
998 inst
->saturate
= saturate
;
1002 vec4_visitor::emit_conversion_to_double(dst_reg dst
, src_reg src
,
1005 dst_reg tmp_dst
= dst_reg(src_reg(this, glsl_type::dvec4_type
));
1006 src_reg tmp_src
= retype(src_reg(this, glsl_type::vec4_type
), src
.type
);
1007 emit(MOV(dst_reg(tmp_src
), src
));
1008 emit(VEC4_OPCODE_TO_DOUBLE
, tmp_dst
, tmp_src
);
1009 vec4_instruction
*inst
= emit(MOV(dst
, src_reg(tmp_dst
)));
1010 inst
->saturate
= saturate
;
1014 * Try to use an immediate value for a source
1016 * In cases of flow control, constant propagation is sometimes unable to
1017 * determine that a register contains a constant value. To work around this,
1018 * try to emit a literal as one of the sources. If \c try_src0_also is set,
1019 * \c op[0] will also be tried for an immediate value.
1021 * If \c op[0] is modified, the operands will be exchanged so that \c op[1]
1022 * will always be the immediate value.
1024 * \return The index of the source that was modified, 0 or 1, if successful.
1027 * \param op - Operands to the instruction
1028 * \param try_src0_also - True if \c op[0] should also be a candidate for
1029 * getting an immediate value. This should only be set
1030 * for commutative operations.
1033 try_immediate_source(const nir_alu_instr
*instr
, src_reg
*op
,
1035 ASSERTED
const gen_device_info
*devinfo
)
1039 /* MOV should be the only single-source instruction passed to this
1040 * function. Any other unary instruction with a constant source should
1041 * have been constant-folded away!
1043 assert(nir_op_infos
[instr
->op
].num_inputs
> 1 ||
1044 instr
->op
== nir_op_mov
);
1046 if (instr
->op
!= nir_op_mov
&&
1047 nir_src_bit_size(instr
->src
[1].src
) == 32 &&
1048 nir_src_is_const(instr
->src
[1].src
)) {
1050 } else if (try_src0_also
&&
1051 nir_src_bit_size(instr
->src
[0].src
) == 32 &&
1052 nir_src_is_const(instr
->src
[0].src
)) {
1058 const enum brw_reg_type old_type
= op
[idx
].type
;
1061 case BRW_REGISTER_TYPE_D
:
1062 case BRW_REGISTER_TYPE_UD
: {
1063 int first_comp
= -1;
1066 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
1067 if (nir_alu_instr_channel_used(instr
, idx
, i
)) {
1068 if (first_comp
< 0) {
1070 d
= nir_src_comp_as_int(instr
->src
[idx
].src
,
1071 instr
->src
[idx
].swizzle
[i
]);
1072 } else if (d
!= nir_src_comp_as_int(instr
->src
[idx
].src
,
1073 instr
->src
[idx
].swizzle
[i
])) {
1082 if (op
[idx
].negate
) {
1083 /* On Gen8+ a negation source modifier on a logical operation means
1084 * something different. Nothing should generate this, so assert that
1085 * it does not occur.
1087 assert(devinfo
->gen
< 8 || (instr
->op
!= nir_op_iand
&&
1088 instr
->op
!= nir_op_ior
&&
1089 instr
->op
!= nir_op_ixor
));
1093 op
[idx
] = retype(src_reg(brw_imm_d(d
)), old_type
);
1097 case BRW_REGISTER_TYPE_F
: {
1098 int first_comp
= -1;
1099 float f
[4] = { 0.0f
, 0.0f
, 0.0f
, 0.0f
};
1100 bool is_scalar
= true;
1102 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
1103 if (nir_alu_instr_channel_used(instr
, idx
, i
)) {
1104 f
[i
] = nir_src_comp_as_float(instr
->src
[idx
].src
,
1105 instr
->src
[idx
].swizzle
[i
]);
1106 if (first_comp
< 0) {
1108 } else if (f
[first_comp
] != f
[i
]) {
1116 f
[first_comp
] = fabs(f
[first_comp
]);
1119 f
[first_comp
] = -f
[first_comp
];
1121 op
[idx
] = src_reg(brw_imm_f(f
[first_comp
]));
1122 assert(op
[idx
].type
== old_type
);
1124 uint8_t vf_values
[4] = { 0, 0, 0, 0 };
1126 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
1133 const int vf
= brw_float_to_vf(f
[i
]);
1140 op
[idx
] = src_reg(brw_imm_vf4(vf_values
[0], vf_values
[1],
1141 vf_values
[2], vf_values
[3]));
1147 unreachable("Non-32bit type.");
1150 /* If the instruction has more than one source, the instruction format only
1151 * allows source 1 to be an immediate value. If the immediate value was
1152 * source 0, then the sources must be exchanged.
1154 if (idx
== 0 && instr
->op
!= nir_op_mov
) {
1155 src_reg tmp
= op
[0];
1164 vec4_visitor::fix_float_operands(src_reg op
[3], nir_alu_instr
*instr
)
1166 bool fixed
[3] = { false, false, false };
1168 for (unsigned i
= 0; i
< 2; i
++) {
1169 if (!nir_src_is_const(instr
->src
[i
].src
))
1172 for (unsigned j
= i
+ 1; j
< 3; j
++) {
1176 if (!nir_src_is_const(instr
->src
[j
].src
))
1179 if (nir_alu_srcs_equal(instr
, instr
, i
, j
)) {
1181 op
[i
] = fix_3src_operand(op
[i
]);
1187 } else if (nir_alu_srcs_negative_equal(instr
, instr
, i
, j
)) {
1189 op
[i
] = fix_3src_operand(op
[i
]);
1192 op
[j
].negate
= !op
[j
].negate
;
1200 for (unsigned i
= 0; i
< 3; i
++) {
1202 op
[i
] = fix_3src_operand(op
[i
]);
1207 vec4_visitor::nir_emit_alu(nir_alu_instr
*instr
)
1209 vec4_instruction
*inst
;
1211 nir_alu_type dst_type
= (nir_alu_type
) (nir_op_infos
[instr
->op
].output_type
|
1212 nir_dest_bit_size(instr
->dest
.dest
));
1213 dst_reg dst
= get_nir_dest(instr
->dest
.dest
, dst_type
);
1214 dst
.writemask
= instr
->dest
.write_mask
;
1217 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
1218 nir_alu_type src_type
= (nir_alu_type
)
1219 (nir_op_infos
[instr
->op
].input_types
[i
] |
1220 nir_src_bit_size(instr
->src
[i
].src
));
1221 op
[i
] = get_nir_src(instr
->src
[i
].src
, src_type
, 4);
1222 op
[i
].swizzle
= brw_swizzle_for_nir_swizzle(instr
->src
[i
].swizzle
);
1223 op
[i
].abs
= instr
->src
[i
].abs
;
1224 op
[i
].negate
= instr
->src
[i
].negate
;
1227 switch (instr
->op
) {
1229 try_immediate_source(instr
, &op
[0], true, devinfo
);
1230 inst
= emit(MOV(dst
, op
[0]));
1231 inst
->saturate
= instr
->dest
.saturate
;
1237 unreachable("not reached: should be handled by lower_vec_to_movs()");
1241 inst
= emit(MOV(dst
, op
[0]));
1242 inst
->saturate
= instr
->dest
.saturate
;
1248 if (nir_src_bit_size(instr
->src
[0].src
) == 64)
1249 emit_conversion_from_double(dst
, op
[0], instr
->dest
.saturate
);
1251 inst
= emit(MOV(dst
, op
[0]));
1257 emit_conversion_to_double(dst
, op
[0], instr
->dest
.saturate
);
1261 inst
= emit(MOV(dst
, op
[0]));
1262 inst
->saturate
= true;
1267 op
[0].negate
= true;
1268 inst
= emit(MOV(dst
, op
[0]));
1269 if (instr
->op
== nir_op_fneg
)
1270 inst
->saturate
= instr
->dest
.saturate
;
1275 op
[0].negate
= false;
1277 inst
= emit(MOV(dst
, op
[0]));
1278 if (instr
->op
== nir_op_fabs
)
1279 inst
->saturate
= instr
->dest
.saturate
;
1283 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1286 try_immediate_source(instr
, op
, true, devinfo
);
1287 inst
= emit(ADD(dst
, op
[0], op
[1]));
1288 inst
->saturate
= instr
->dest
.saturate
;
1291 case nir_op_uadd_sat
:
1292 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1293 inst
= emit(ADD(dst
, op
[0], op
[1]));
1294 inst
->saturate
= true;
1298 try_immediate_source(instr
, op
, true, devinfo
);
1299 inst
= emit(MUL(dst
, op
[0], op
[1]));
1300 inst
->saturate
= instr
->dest
.saturate
;
1304 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1305 if (devinfo
->gen
< 8) {
1306 /* For integer multiplication, the MUL uses the low 16 bits of one of
1307 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1308 * accumulates in the contribution of the upper 16 bits of that
1309 * operand. If we can determine that one of the args is in the low
1310 * 16 bits, though, we can just emit a single MUL.
1312 if (nir_src_is_const(instr
->src
[0].src
) &&
1313 nir_alu_instr_src_read_mask(instr
, 0) == 1 &&
1314 nir_src_comp_as_uint(instr
->src
[0].src
, 0) < (1 << 16)) {
1315 if (devinfo
->gen
< 7)
1316 emit(MUL(dst
, op
[0], op
[1]));
1318 emit(MUL(dst
, op
[1], op
[0]));
1319 } else if (nir_src_is_const(instr
->src
[1].src
) &&
1320 nir_alu_instr_src_read_mask(instr
, 1) == 1 &&
1321 nir_src_comp_as_uint(instr
->src
[1].src
, 0) < (1 << 16)) {
1322 if (devinfo
->gen
< 7)
1323 emit(MUL(dst
, op
[1], op
[0]));
1325 emit(MUL(dst
, op
[0], op
[1]));
1327 struct brw_reg acc
= retype(brw_acc_reg(8), dst
.type
);
1329 emit(MUL(acc
, op
[0], op
[1]));
1330 emit(MACH(dst_null_d(), op
[0], op
[1]));
1331 emit(MOV(dst
, src_reg(acc
)));
1334 emit(MUL(dst
, op
[0], op
[1]));
1339 case nir_op_imul_high
:
1340 case nir_op_umul_high
: {
1341 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1342 struct brw_reg acc
= retype(brw_acc_reg(8), dst
.type
);
1344 if (devinfo
->gen
>= 8)
1345 emit(MUL(acc
, op
[0], retype(op
[1], BRW_REGISTER_TYPE_UW
)));
1347 emit(MUL(acc
, op
[0], op
[1]));
1349 emit(MACH(dst
, op
[0], op
[1]));
1354 inst
= emit_math(SHADER_OPCODE_RCP
, dst
, op
[0]);
1355 inst
->saturate
= instr
->dest
.saturate
;
1359 inst
= emit_math(SHADER_OPCODE_EXP2
, dst
, op
[0]);
1360 inst
->saturate
= instr
->dest
.saturate
;
1364 inst
= emit_math(SHADER_OPCODE_LOG2
, dst
, op
[0]);
1365 inst
->saturate
= instr
->dest
.saturate
;
1369 inst
= emit_math(SHADER_OPCODE_SIN
, dst
, op
[0]);
1370 inst
->saturate
= instr
->dest
.saturate
;
1374 inst
= emit_math(SHADER_OPCODE_COS
, dst
, op
[0]);
1375 inst
->saturate
= instr
->dest
.saturate
;
1380 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1381 emit_math(SHADER_OPCODE_INT_QUOTIENT
, dst
, op
[0], op
[1]);
1386 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1387 * appears that our hardware just does the right thing for signed
1390 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1391 emit_math(SHADER_OPCODE_INT_REMAINDER
, dst
, op
[0], op
[1]);
1395 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1396 inst
= emit_math(SHADER_OPCODE_INT_REMAINDER
, dst
, op
[0], op
[1]);
1398 /* Math instructions don't support conditional mod */
1399 inst
= emit(MOV(dst_null_d(), src_reg(dst
)));
1400 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1402 /* Now, we need to determine if signs of the sources are different.
1403 * When we XOR the sources, the top bit is 0 if they are the same and 1
1404 * if they are different. We can then use a conditional modifier to
1405 * turn that into a predicate. This leads us to an XOR.l instruction.
1407 * Technically, according to the PRM, you're not allowed to use .l on a
1408 * XOR instruction. However, emperical experiments and Curro's reading
1409 * of the simulator source both indicate that it's safe.
1411 src_reg tmp
= src_reg(this, glsl_type::ivec4_type
);
1412 inst
= emit(XOR(dst_reg(tmp
), op
[0], op
[1]));
1413 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1414 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1416 /* If the result of the initial remainder operation is non-zero and the
1417 * two sources have different signs, add in a copy of op[1] to get the
1418 * final integer modulus value.
1420 inst
= emit(ADD(dst
, src_reg(dst
), op
[1]));
1421 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1426 unreachable("not reached: should be handled by ldexp_to_arith()");
1429 inst
= emit_math(SHADER_OPCODE_SQRT
, dst
, op
[0]);
1430 inst
->saturate
= instr
->dest
.saturate
;
1434 inst
= emit_math(SHADER_OPCODE_RSQ
, dst
, op
[0]);
1435 inst
->saturate
= instr
->dest
.saturate
;
1439 inst
= emit_math(SHADER_OPCODE_POW
, dst
, op
[0], op
[1]);
1440 inst
->saturate
= instr
->dest
.saturate
;
1443 case nir_op_uadd_carry
: {
1444 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1445 struct brw_reg acc
= retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD
);
1447 emit(ADDC(dst_null_ud(), op
[0], op
[1]));
1448 emit(MOV(dst
, src_reg(acc
)));
1452 case nir_op_usub_borrow
: {
1453 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1454 struct brw_reg acc
= retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD
);
1456 emit(SUBB(dst_null_ud(), op
[0], op
[1]));
1457 emit(MOV(dst
, src_reg(acc
)));
1462 inst
= emit(RNDZ(dst
, op
[0]));
1463 inst
->saturate
= instr
->dest
.saturate
;
1466 case nir_op_fceil
: {
1467 src_reg tmp
= src_reg(this, glsl_type::float_type
);
1469 brw_swizzle_for_size(instr
->src
[0].src
.is_ssa
?
1470 instr
->src
[0].src
.ssa
->num_components
:
1471 instr
->src
[0].src
.reg
.reg
->num_components
);
1473 op
[0].negate
= !op
[0].negate
;
1474 emit(RNDD(dst_reg(tmp
), op
[0]));
1476 inst
= emit(MOV(dst
, tmp
));
1477 inst
->saturate
= instr
->dest
.saturate
;
1482 inst
= emit(RNDD(dst
, op
[0]));
1483 inst
->saturate
= instr
->dest
.saturate
;
1487 inst
= emit(FRC(dst
, op
[0]));
1488 inst
->saturate
= instr
->dest
.saturate
;
1491 case nir_op_fround_even
:
1492 inst
= emit(RNDE(dst
, op
[0]));
1493 inst
->saturate
= instr
->dest
.saturate
;
1496 case nir_op_fquantize2f16
: {
1497 /* See also vec4_visitor::emit_pack_half_2x16() */
1498 src_reg tmp16
= src_reg(this, glsl_type::uvec4_type
);
1499 src_reg tmp32
= src_reg(this, glsl_type::vec4_type
);
1500 src_reg zero
= src_reg(this, glsl_type::vec4_type
);
1502 /* Check for denormal */
1503 src_reg abs_src0
= op
[0];
1504 abs_src0
.abs
= true;
1505 emit(CMP(dst_null_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1506 BRW_CONDITIONAL_L
));
1507 /* Get the appropriately signed zero */
1508 emit(AND(retype(dst_reg(zero
), BRW_REGISTER_TYPE_UD
),
1509 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1510 brw_imm_ud(0x80000000)));
1511 /* Do the actual F32 -> F16 -> F32 conversion */
1512 emit(F32TO16(dst_reg(tmp16
), op
[0]));
1513 emit(F16TO32(dst_reg(tmp32
), tmp16
));
1514 /* Select that or zero based on normal status */
1515 inst
= emit(BRW_OPCODE_SEL
, dst
, zero
, tmp32
);
1516 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1517 inst
->saturate
= instr
->dest
.saturate
;
1523 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1526 try_immediate_source(instr
, op
, true, devinfo
);
1527 inst
= emit_minmax(BRW_CONDITIONAL_L
, dst
, op
[0], op
[1]);
1528 inst
->saturate
= instr
->dest
.saturate
;
1533 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1536 try_immediate_source(instr
, op
, true, devinfo
);
1537 inst
= emit_minmax(BRW_CONDITIONAL_GE
, dst
, op
[0], op
[1]);
1538 inst
->saturate
= instr
->dest
.saturate
;
1542 case nir_op_fddx_coarse
:
1543 case nir_op_fddx_fine
:
1545 case nir_op_fddy_coarse
:
1546 case nir_op_fddy_fine
:
1547 unreachable("derivatives are not valid in vertex shaders");
1555 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1560 case nir_op_fne32
: {
1561 enum brw_conditional_mod conditional_mod
=
1562 brw_conditional_for_nir_comparison(instr
->op
);
1564 if (nir_src_bit_size(instr
->src
[0].src
) < 64) {
1565 /* If the order of the sources is changed due to an immediate value,
1566 * then the condition must also be changed.
1568 if (try_immediate_source(instr
, op
, true, devinfo
) == 0)
1569 conditional_mod
= brw_swap_cmod(conditional_mod
);
1571 emit(CMP(dst
, op
[0], op
[1], conditional_mod
));
1573 /* Produce a 32-bit boolean result from the DF comparison by selecting
1574 * only the low 32-bit in each DF produced. Do this in a temporary
1575 * so we can then move from there to the result using align16 again
1576 * to honor the original writemask.
1578 dst_reg temp
= dst_reg(this, glsl_type::dvec4_type
);
1579 emit(CMP(temp
, op
[0], op
[1], conditional_mod
));
1580 dst_reg result
= dst_reg(this, glsl_type::bvec4_type
);
1581 emit(VEC4_OPCODE_PICK_LOW_32BIT
, result
, src_reg(temp
));
1582 emit(MOV(dst
, src_reg(result
)));
1587 case nir_op_b32all_iequal2
:
1588 case nir_op_b32all_iequal3
:
1589 case nir_op_b32all_iequal4
:
1590 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1592 case nir_op_b32all_fequal2
:
1593 case nir_op_b32all_fequal3
:
1594 case nir_op_b32all_fequal4
: {
1596 brw_swizzle_for_size(nir_op_infos
[instr
->op
].input_sizes
[0]);
1598 emit(CMP(dst_null_d(), swizzle(op
[0], swiz
), swizzle(op
[1], swiz
),
1599 brw_conditional_for_nir_comparison(instr
->op
)));
1600 emit(MOV(dst
, brw_imm_d(0)));
1601 inst
= emit(MOV(dst
, brw_imm_d(~0)));
1602 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1606 case nir_op_b32any_inequal2
:
1607 case nir_op_b32any_inequal3
:
1608 case nir_op_b32any_inequal4
:
1609 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1611 case nir_op_b32any_fnequal2
:
1612 case nir_op_b32any_fnequal3
:
1613 case nir_op_b32any_fnequal4
: {
1615 brw_swizzle_for_size(nir_op_infos
[instr
->op
].input_sizes
[0]);
1617 emit(CMP(dst_null_d(), swizzle(op
[0], swiz
), swizzle(op
[1], swiz
),
1618 brw_conditional_for_nir_comparison(instr
->op
)));
1620 emit(MOV(dst
, brw_imm_d(0)));
1621 inst
= emit(MOV(dst
, brw_imm_d(~0)));
1622 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1627 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1628 if (devinfo
->gen
>= 8) {
1629 op
[0] = resolve_source_modifiers(op
[0]);
1631 emit(NOT(dst
, op
[0]));
1635 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1636 if (devinfo
->gen
>= 8) {
1637 op
[0] = resolve_source_modifiers(op
[0]);
1638 op
[1] = resolve_source_modifiers(op
[1]);
1640 try_immediate_source(instr
, op
, true, devinfo
);
1641 emit(XOR(dst
, op
[0], op
[1]));
1645 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1646 if (devinfo
->gen
>= 8) {
1647 op
[0] = resolve_source_modifiers(op
[0]);
1648 op
[1] = resolve_source_modifiers(op
[1]);
1650 try_immediate_source(instr
, op
, true, devinfo
);
1651 emit(OR(dst
, op
[0], op
[1]));
1655 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1656 if (devinfo
->gen
>= 8) {
1657 op
[0] = resolve_source_modifiers(op
[0]);
1658 op
[1] = resolve_source_modifiers(op
[1]);
1660 try_immediate_source(instr
, op
, true, devinfo
);
1661 emit(AND(dst
, op
[0], op
[1]));
1667 if (nir_dest_bit_size(instr
->dest
.dest
) > 32) {
1668 assert(dst
.type
== BRW_REGISTER_TYPE_DF
);
1669 emit_conversion_to_double(dst
, negate(op
[0]), false);
1671 emit(MOV(dst
, negate(op
[0])));
1676 if (nir_src_bit_size(instr
->src
[0].src
) == 64) {
1677 /* We use a MOV with conditional_mod to check if the provided value is
1678 * 0.0. We want this to flush denormalized numbers to zero, so we set a
1679 * source modifier on the source operand to trigger this, as source
1680 * modifiers don't affect the result of the testing against 0.0.
1682 src_reg value
= op
[0];
1684 vec4_instruction
*inst
= emit(MOV(dst_null_df(), value
));
1685 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1687 src_reg one
= src_reg(this, glsl_type::ivec4_type
);
1688 emit(MOV(dst_reg(one
), brw_imm_d(~0)));
1689 inst
= emit(BRW_OPCODE_SEL
, dst
, one
, brw_imm_d(0));
1690 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1692 emit(CMP(dst
, op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
));
1697 emit(CMP(dst
, op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
));
1700 case nir_op_fnoise1_1
:
1701 case nir_op_fnoise1_2
:
1702 case nir_op_fnoise1_3
:
1703 case nir_op_fnoise1_4
:
1704 case nir_op_fnoise2_1
:
1705 case nir_op_fnoise2_2
:
1706 case nir_op_fnoise2_3
:
1707 case nir_op_fnoise2_4
:
1708 case nir_op_fnoise3_1
:
1709 case nir_op_fnoise3_2
:
1710 case nir_op_fnoise3_3
:
1711 case nir_op_fnoise3_4
:
1712 case nir_op_fnoise4_1
:
1713 case nir_op_fnoise4_2
:
1714 case nir_op_fnoise4_3
:
1715 case nir_op_fnoise4_4
:
1716 unreachable("not reached: should be handled by lower_noise");
1718 case nir_op_unpack_half_2x16_split_x
:
1719 case nir_op_unpack_half_2x16_split_y
:
1720 case nir_op_pack_half_2x16_split
:
1721 unreachable("not reached: should not occur in vertex shader");
1723 case nir_op_unpack_snorm_2x16
:
1724 case nir_op_unpack_unorm_2x16
:
1725 case nir_op_pack_snorm_2x16
:
1726 case nir_op_pack_unorm_2x16
:
1727 unreachable("not reached: should be handled by lower_packing_builtins");
1729 case nir_op_pack_uvec4_to_uint
:
1730 unreachable("not reached");
1732 case nir_op_pack_uvec2_to_uint
: {
1733 dst_reg tmp1
= dst_reg(this, glsl_type::uint_type
);
1734 tmp1
.writemask
= WRITEMASK_X
;
1735 op
[0].swizzle
= BRW_SWIZZLE_YYYY
;
1736 emit(SHL(tmp1
, op
[0], src_reg(brw_imm_ud(16u))));
1738 dst_reg tmp2
= dst_reg(this, glsl_type::uint_type
);
1739 tmp2
.writemask
= WRITEMASK_X
;
1740 op
[0].swizzle
= BRW_SWIZZLE_XXXX
;
1741 emit(AND(tmp2
, op
[0], src_reg(brw_imm_ud(0xffffu
))));
1743 emit(OR(dst
, src_reg(tmp1
), src_reg(tmp2
)));
1747 case nir_op_pack_64_2x32_split
: {
1748 dst_reg result
= dst_reg(this, glsl_type::dvec4_type
);
1749 dst_reg tmp
= dst_reg(this, glsl_type::uvec4_type
);
1750 emit(MOV(tmp
, retype(op
[0], BRW_REGISTER_TYPE_UD
)));
1751 emit(VEC4_OPCODE_SET_LOW_32BIT
, result
, src_reg(tmp
));
1752 emit(MOV(tmp
, retype(op
[1], BRW_REGISTER_TYPE_UD
)));
1753 emit(VEC4_OPCODE_SET_HIGH_32BIT
, result
, src_reg(tmp
));
1754 emit(MOV(dst
, src_reg(result
)));
1758 case nir_op_unpack_64_2x32_split_x
:
1759 case nir_op_unpack_64_2x32_split_y
: {
1760 enum opcode oper
= (instr
->op
== nir_op_unpack_64_2x32_split_x
) ?
1761 VEC4_OPCODE_PICK_LOW_32BIT
: VEC4_OPCODE_PICK_HIGH_32BIT
;
1762 dst_reg tmp
= dst_reg(this, glsl_type::dvec4_type
);
1763 emit(MOV(tmp
, op
[0]));
1764 dst_reg tmp2
= dst_reg(this, glsl_type::uvec4_type
);
1765 emit(oper
, tmp2
, src_reg(tmp
));
1766 emit(MOV(dst
, src_reg(tmp2
)));
1770 case nir_op_unpack_half_2x16
:
1771 /* As NIR does not guarantee that we have a correct swizzle outside the
1772 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1773 * uses the source operand in an operation with WRITEMASK_Y while our
1774 * source operand has only size 1, it accessed incorrect data producing
1775 * regressions in Piglit. We repeat the swizzle of the first component on the
1776 * rest of components to avoid regressions. In the vec4_visitor IR code path
1777 * this is not needed because the operand has already the correct swizzle.
1779 op
[0].swizzle
= brw_compose_swizzle(BRW_SWIZZLE_XXXX
, op
[0].swizzle
);
1780 emit_unpack_half_2x16(dst
, op
[0]);
1783 case nir_op_pack_half_2x16
:
1784 emit_pack_half_2x16(dst
, op
[0]);
1787 case nir_op_unpack_unorm_4x8
:
1788 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1789 emit_unpack_unorm_4x8(dst
, op
[0]);
1792 case nir_op_pack_unorm_4x8
:
1793 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1794 emit_pack_unorm_4x8(dst
, op
[0]);
1797 case nir_op_unpack_snorm_4x8
:
1798 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1799 emit_unpack_snorm_4x8(dst
, op
[0]);
1802 case nir_op_pack_snorm_4x8
:
1803 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1804 emit_pack_snorm_4x8(dst
, op
[0]);
1807 case nir_op_bitfield_reverse
:
1808 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1809 emit(BFREV(dst
, op
[0]));
1812 case nir_op_bit_count
:
1813 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1814 emit(CBIT(dst
, op
[0]));
1817 case nir_op_ufind_msb
:
1818 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1819 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst
, op
[0], false);
1822 case nir_op_ifind_msb
: {
1823 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1824 vec4_builder bld
= vec4_builder(this).at_end();
1827 if (devinfo
->gen
< 7) {
1828 emit_find_msb_using_lzd(bld
, dst
, op
[0], true);
1830 emit(FBH(retype(dst
, BRW_REGISTER_TYPE_UD
), op
[0]));
1832 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1833 * count from the LSB side. If FBH didn't return an error
1834 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1835 * count into an LSB count.
1837 bld
.CMP(dst_null_d(), src
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1839 inst
= bld
.ADD(dst
, src
, brw_imm_d(31));
1840 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1841 inst
->src
[0].negate
= true;
1846 case nir_op_find_lsb
: {
1847 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1848 vec4_builder bld
= vec4_builder(this).at_end();
1850 if (devinfo
->gen
< 7) {
1851 dst_reg temp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1853 /* (x & -x) generates a value that consists of only the LSB of x.
1854 * For all powers of 2, findMSB(y) == findLSB(y).
1856 src_reg src
= src_reg(retype(op
[0], BRW_REGISTER_TYPE_D
));
1857 src_reg negated_src
= src
;
1859 /* One must be negated, and the other must be non-negated. It
1860 * doesn't matter which is which.
1862 negated_src
.negate
= true;
1865 bld
.AND(temp
, src
, negated_src
);
1866 emit_find_msb_using_lzd(bld
, dst
, src_reg(temp
), false);
1868 bld
.FBL(dst
, op
[0]);
1873 case nir_op_ubitfield_extract
:
1874 case nir_op_ibitfield_extract
:
1875 unreachable("should have been lowered");
1878 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1879 op
[0] = fix_3src_operand(op
[0]);
1880 op
[1] = fix_3src_operand(op
[1]);
1881 op
[2] = fix_3src_operand(op
[2]);
1883 emit(BFE(dst
, op
[2], op
[1], op
[0]));
1887 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1888 emit(BFI1(dst
, op
[0], op
[1]));
1892 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1893 op
[0] = fix_3src_operand(op
[0]);
1894 op
[1] = fix_3src_operand(op
[1]);
1895 op
[2] = fix_3src_operand(op
[2]);
1897 emit(BFI2(dst
, op
[0], op
[1], op
[2]));
1900 case nir_op_bitfield_insert
:
1901 unreachable("not reached: should have been lowered");
1904 assert(!instr
->dest
.saturate
);
1906 /* Straightforward since the source can be assumed to be either
1907 * strictly >= 0 or strictly <= 0 depending on the setting of the
1910 inst
= emit(MOV(dst
, op
[0]));
1911 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1913 inst
= (op
[0].negate
)
1914 ? emit(MOV(dst
, brw_imm_f(-1.0f
)))
1915 : emit(MOV(dst
, brw_imm_f(1.0f
)));
1916 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1917 } else if (type_sz(op
[0].type
) < 8) {
1918 /* AND(val, 0x80000000) gives the sign bit.
1920 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1923 emit(CMP(dst_null_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
));
1925 op
[0].type
= BRW_REGISTER_TYPE_UD
;
1926 dst
.type
= BRW_REGISTER_TYPE_UD
;
1927 emit(AND(dst
, op
[0], brw_imm_ud(0x80000000u
)));
1929 inst
= emit(OR(dst
, src_reg(dst
), brw_imm_ud(0x3f800000u
)));
1930 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1931 dst
.type
= BRW_REGISTER_TYPE_F
;
1933 /* For doubles we do the same but we need to consider:
1935 * - We use a MOV with conditional_mod instead of a CMP so that we can
1936 * skip loading a 0.0 immediate. We use a source modifier on the
1937 * source of the MOV so that we flush denormalized values to 0.
1938 * Since we want to compare against 0, this won't alter the result.
1939 * - We need to extract the high 32-bit of each DF where the sign
1941 * - We need to produce a DF result.
1944 /* Check for zero */
1945 src_reg value
= op
[0];
1947 inst
= emit(MOV(dst_null_df(), value
));
1948 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1950 /* AND each high 32-bit channel with 0x80000000u */
1951 dst_reg tmp
= dst_reg(this, glsl_type::uvec4_type
);
1952 emit(VEC4_OPCODE_PICK_HIGH_32BIT
, tmp
, op
[0]);
1953 emit(AND(tmp
, src_reg(tmp
), brw_imm_ud(0x80000000u
)));
1955 /* Add 1.0 to each channel, predicated to skip the cases where the
1956 * channel's value was 0
1958 inst
= emit(OR(tmp
, src_reg(tmp
), brw_imm_ud(0x3f800000u
)));
1959 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1961 /* Now convert the result from float to double */
1962 emit_conversion_to_double(dst
, retype(src_reg(tmp
),
1963 BRW_REGISTER_TYPE_F
),
1969 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1970 try_immediate_source(instr
, op
, false, devinfo
);
1971 emit(SHL(dst
, op
[0], op
[1]));
1975 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1976 try_immediate_source(instr
, op
, false, devinfo
);
1977 emit(ASR(dst
, op
[0], op
[1]));
1981 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1982 try_immediate_source(instr
, op
, false, devinfo
);
1983 emit(SHR(dst
, op
[0], op
[1]));
1987 if (type_sz(dst
.type
) == 8) {
1988 dst_reg mul_dst
= dst_reg(this, glsl_type::dvec4_type
);
1989 emit(MUL(mul_dst
, op
[1], op
[0]));
1990 inst
= emit(ADD(dst
, src_reg(mul_dst
), op
[2]));
1991 inst
->saturate
= instr
->dest
.saturate
;
1993 fix_float_operands(op
, instr
);
1994 inst
= emit(MAD(dst
, op
[2], op
[1], op
[0]));
1995 inst
->saturate
= instr
->dest
.saturate
;
2000 fix_float_operands(op
, instr
);
2001 inst
= emit(LRP(dst
, op
[2], op
[1], op
[0]));
2002 inst
->saturate
= instr
->dest
.saturate
;
2005 case nir_op_b32csel
:
2006 enum brw_predicate predicate
;
2007 if (!optimize_predicate(instr
, &predicate
)) {
2008 emit(CMP(dst_null_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
));
2009 switch (dst
.writemask
) {
2011 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_X
;
2014 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_Y
;
2017 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_Z
;
2020 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_W
;
2023 predicate
= BRW_PREDICATE_NORMAL
;
2027 inst
= emit(BRW_OPCODE_SEL
, dst
, op
[1], op
[2]);
2028 inst
->predicate
= predicate
;
2031 case nir_op_fdot_replicated2
:
2032 try_immediate_source(instr
, op
, true, devinfo
);
2033 inst
= emit(BRW_OPCODE_DP2
, dst
, op
[0], op
[1]);
2034 inst
->saturate
= instr
->dest
.saturate
;
2037 case nir_op_fdot_replicated3
:
2038 try_immediate_source(instr
, op
, true, devinfo
);
2039 inst
= emit(BRW_OPCODE_DP3
, dst
, op
[0], op
[1]);
2040 inst
->saturate
= instr
->dest
.saturate
;
2043 case nir_op_fdot_replicated4
:
2044 try_immediate_source(instr
, op
, true, devinfo
);
2045 inst
= emit(BRW_OPCODE_DP4
, dst
, op
[0], op
[1]);
2046 inst
->saturate
= instr
->dest
.saturate
;
2049 case nir_op_fdph_replicated
:
2050 try_immediate_source(instr
, op
, true, devinfo
);
2051 inst
= emit(BRW_OPCODE_DPH
, dst
, op
[0], op
[1]);
2052 inst
->saturate
= instr
->dest
.saturate
;
2056 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
2059 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
2063 unreachable("not reached: should be handled by ir_sub_to_add_neg");
2066 unreachable("Unimplemented ALU operation");
2069 /* If we need to do a boolean resolve, replace the result with -(x & 1)
2070 * to sign extend the low bit to 0/~0
2072 if (devinfo
->gen
<= 5 &&
2073 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) ==
2074 BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
2075 dst_reg masked
= dst_reg(this, glsl_type::int_type
);
2076 masked
.writemask
= dst
.writemask
;
2077 emit(AND(masked
, src_reg(dst
), brw_imm_d(1)));
2078 src_reg masked_neg
= src_reg(masked
);
2079 masked_neg
.negate
= true;
2080 emit(MOV(retype(dst
, BRW_REGISTER_TYPE_D
), masked_neg
));
2085 vec4_visitor::nir_emit_jump(nir_jump_instr
*instr
)
2087 switch (instr
->type
) {
2088 case nir_jump_break
:
2089 emit(BRW_OPCODE_BREAK
);
2092 case nir_jump_continue
:
2093 emit(BRW_OPCODE_CONTINUE
);
2096 case nir_jump_return
:
2099 unreachable("unknown jump");
2103 static enum ir_texture_opcode
2104 ir_texture_opcode_for_nir_texop(nir_texop texop
)
2106 enum ir_texture_opcode op
;
2109 case nir_texop_lod
: op
= ir_lod
; break;
2110 case nir_texop_query_levels
: op
= ir_query_levels
; break;
2111 case nir_texop_texture_samples
: op
= ir_texture_samples
; break;
2112 case nir_texop_tex
: op
= ir_tex
; break;
2113 case nir_texop_tg4
: op
= ir_tg4
; break;
2114 case nir_texop_txb
: op
= ir_txb
; break;
2115 case nir_texop_txd
: op
= ir_txd
; break;
2116 case nir_texop_txf
: op
= ir_txf
; break;
2117 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
2118 case nir_texop_txl
: op
= ir_txl
; break;
2119 case nir_texop_txs
: op
= ir_txs
; break;
2120 case nir_texop_samples_identical
: op
= ir_samples_identical
; break;
2122 unreachable("unknown texture opcode");
2128 static const glsl_type
*
2129 glsl_type_for_nir_alu_type(nir_alu_type alu_type
,
2130 unsigned components
)
2132 return glsl_type::get_instance(brw_glsl_base_type_for_nir_type(alu_type
),
2137 vec4_visitor::nir_emit_texture(nir_tex_instr
*instr
)
2139 unsigned texture
= instr
->texture_index
;
2140 unsigned sampler
= instr
->sampler_index
;
2141 src_reg texture_reg
= brw_imm_ud(texture
);
2142 src_reg sampler_reg
= brw_imm_ud(sampler
);
2144 const glsl_type
*coord_type
= NULL
;
2145 src_reg shadow_comparator
;
2146 src_reg offset_value
;
2148 src_reg sample_index
;
2151 const glsl_type
*dest_type
=
2152 glsl_type_for_nir_alu_type(instr
->dest_type
,
2153 nir_tex_instr_dest_size(instr
));
2154 dst_reg dest
= get_nir_dest(instr
->dest
, instr
->dest_type
);
2156 /* The hardware requires a LOD for buffer textures */
2157 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
2160 /* Load the texture operation sources */
2161 uint32_t constant_offset
= 0;
2162 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
2163 switch (instr
->src
[i
].src_type
) {
2164 case nir_tex_src_comparator
:
2165 shadow_comparator
= get_nir_src(instr
->src
[i
].src
,
2166 BRW_REGISTER_TYPE_F
, 1);
2169 case nir_tex_src_coord
: {
2170 unsigned src_size
= nir_tex_instr_src_size(instr
, i
);
2172 switch (instr
->op
) {
2174 case nir_texop_txf_ms
:
2175 case nir_texop_samples_identical
:
2176 coordinate
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
,
2178 coord_type
= glsl_type::ivec(src_size
);
2182 coordinate
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
2184 coord_type
= glsl_type::vec(src_size
);
2190 case nir_tex_src_ddx
:
2191 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
2192 nir_tex_instr_src_size(instr
, i
));
2195 case nir_tex_src_ddy
:
2196 lod2
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
2197 nir_tex_instr_src_size(instr
, i
));
2200 case nir_tex_src_lod
:
2201 switch (instr
->op
) {
2204 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 1);
2208 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
, 1);
2213 case nir_tex_src_ms_index
: {
2214 sample_index
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 1);
2218 case nir_tex_src_offset
:
2219 if (!brw_texture_offset(instr
, i
, &constant_offset
)) {
2221 get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 2);
2225 case nir_tex_src_texture_offset
: {
2226 /* Emit code to evaluate the actual indexing expression */
2227 src_reg src
= get_nir_src(instr
->src
[i
].src
, 1);
2228 src_reg
temp(this, glsl_type::uint_type
);
2229 emit(ADD(dst_reg(temp
), src
, brw_imm_ud(texture
)));
2230 texture_reg
= emit_uniformize(temp
);
2234 case nir_tex_src_sampler_offset
: {
2235 /* Emit code to evaluate the actual indexing expression */
2236 src_reg src
= get_nir_src(instr
->src
[i
].src
, 1);
2237 src_reg
temp(this, glsl_type::uint_type
);
2238 emit(ADD(dst_reg(temp
), src
, brw_imm_ud(sampler
)));
2239 sampler_reg
= emit_uniformize(temp
);
2243 case nir_tex_src_projector
:
2244 unreachable("Should be lowered by do_lower_texture_projection");
2246 case nir_tex_src_bias
:
2247 unreachable("LOD bias is not valid for vertex shaders.\n");
2250 unreachable("unknown texture source");
2254 if (instr
->op
== nir_texop_txf_ms
||
2255 instr
->op
== nir_texop_samples_identical
) {
2256 assert(coord_type
!= NULL
);
2257 if (devinfo
->gen
>= 7 &&
2258 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
2259 mcs
= emit_mcs_fetch(coord_type
, coordinate
, texture_reg
);
2261 mcs
= brw_imm_ud(0u);
2265 /* Stuff the channel select bits in the top of the texture offset */
2266 if (instr
->op
== nir_texop_tg4
) {
2267 if (instr
->component
== 1 &&
2268 (key_tex
->gather_channel_quirk_mask
& (1 << texture
))) {
2269 /* gather4 sampler is broken for green channel on RG32F --
2270 * we must ask for blue instead.
2272 constant_offset
|= 2 << 16;
2274 constant_offset
|= instr
->component
<< 16;
2278 ir_texture_opcode op
= ir_texture_opcode_for_nir_texop(instr
->op
);
2280 emit_texture(op
, dest
, dest_type
, coordinate
, instr
->coord_components
,
2282 lod
, lod2
, sample_index
,
2283 constant_offset
, offset_value
, mcs
,
2284 texture
, texture_reg
, sampler_reg
);
2288 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr
*instr
)
2290 nir_ssa_values
[instr
->def
.index
] =
2291 dst_reg(VGRF
, alloc
.allocate(DIV_ROUND_UP(instr
->def
.bit_size
, 32)));
2294 /* SIMD4x2 64bit data is stored in register space like this:
2296 * r0.0:DF x0 y0 z0 w0
2297 * r1.0:DF x1 y1 z1 w1
2299 * When we need to write data such as this to memory using 32-bit write
2300 * messages we need to shuffle it in this fashion:
2302 * r0.0:DF x0 y0 x1 y1 (to be written at base offset)
2303 * r0.0:DF z0 w0 z1 w1 (to be written at base offset + 16)
2305 * We need to do the inverse operation when we read using 32-bit messages,
2306 * which we can do by applying the same exact shuffling on the 64-bit data
2307 * read, only that because the data for each vertex is positioned differently
2308 * we need to apply different channel enables.
2310 * This function takes 64bit data and shuffles it as explained above.
2312 * The @for_write parameter is used to specify if the shuffling is being done
2313 * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
2314 * write message (for_write = true), or instead we are doing the inverse
2315 * operation and we have just read 64-bit data using a 32-bit messages that we
2316 * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
2318 * If @block and @ref are non-NULL, then the shuffling is done after @ref,
2319 * otherwise the instructions are emitted normally at the end. The function
2320 * returns the last instruction inserted.
2322 * Notice that @src and @dst cannot be the same register.
2325 vec4_visitor::shuffle_64bit_data(dst_reg dst
, src_reg src
, bool for_write
,
2326 bblock_t
*block
, vec4_instruction
*ref
)
2328 assert(type_sz(src
.type
) == 8);
2329 assert(type_sz(dst
.type
) == 8);
2330 assert(!regions_overlap(dst
, 2 * REG_SIZE
, src
, 2 * REG_SIZE
));
2331 assert(!ref
== !block
);
2333 const vec4_builder bld
= !ref
? vec4_builder(this).at_end() :
2334 vec4_builder(this).at(block
, ref
->next
);
2336 /* Resolve swizzle in src */
2337 vec4_instruction
*inst
;
2338 if (src
.swizzle
!= BRW_SWIZZLE_XYZW
) {
2339 dst_reg data
= dst_reg(this, glsl_type::dvec4_type
);
2340 inst
= bld
.MOV(data
, src
);
2341 src
= src_reg(data
);
2344 /* dst+0.XY = src+0.XY */
2345 inst
= bld
.group(4, 0).MOV(writemask(dst
, WRITEMASK_XY
), src
);
2347 /* dst+0.ZW = src+1.XY */
2348 inst
= bld
.group(4, for_write
? 1 : 0)
2349 .MOV(writemask(dst
, WRITEMASK_ZW
),
2350 swizzle(byte_offset(src
, REG_SIZE
), BRW_SWIZZLE_XYXY
));
2352 /* dst+1.XY = src+0.ZW */
2353 inst
= bld
.group(4, for_write
? 0 : 1)
2354 .MOV(writemask(byte_offset(dst
, REG_SIZE
), WRITEMASK_XY
),
2355 swizzle(src
, BRW_SWIZZLE_ZWZW
));
2357 /* dst+1.ZW = src+1.ZW */
2358 inst
= bld
.group(4, 1)
2359 .MOV(writemask(byte_offset(dst
, REG_SIZE
), WRITEMASK_ZW
),
2360 byte_offset(src
, REG_SIZE
));