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"
30 using namespace brw::surface_access
;
35 vec4_visitor::emit_nir_code()
37 if (nir
->num_uniforms
> 0)
40 nir_emit_impl(nir_shader_get_entrypoint((nir_shader
*)nir
));
44 vec4_visitor::nir_setup_uniforms()
46 uniforms
= nir
->num_uniforms
/ 16;
50 vec4_visitor::nir_emit_impl(nir_function_impl
*impl
)
52 nir_locals
= ralloc_array(mem_ctx
, dst_reg
, impl
->reg_alloc
);
53 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
54 nir_locals
[i
] = dst_reg();
57 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
58 unsigned array_elems
=
59 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
60 const unsigned num_regs
= array_elems
* DIV_ROUND_UP(reg
->bit_size
, 32);
61 nir_locals
[reg
->index
] = dst_reg(VGRF
, alloc
.allocate(num_regs
));
63 if (reg
->bit_size
== 64)
64 nir_locals
[reg
->index
].type
= BRW_REGISTER_TYPE_DF
;
67 nir_ssa_values
= ralloc_array(mem_ctx
, dst_reg
, impl
->ssa_alloc
);
69 nir_emit_cf_list(&impl
->body
);
73 vec4_visitor::nir_emit_cf_list(exec_list
*list
)
75 exec_list_validate(list
);
76 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
79 nir_emit_if(nir_cf_node_as_if(node
));
82 case nir_cf_node_loop
:
83 nir_emit_loop(nir_cf_node_as_loop(node
));
86 case nir_cf_node_block
:
87 nir_emit_block(nir_cf_node_as_block(node
));
91 unreachable("Invalid CFG node block");
97 vec4_visitor::nir_emit_if(nir_if
*if_stmt
)
99 /* First, put the condition in f0 */
100 src_reg condition
= get_nir_src(if_stmt
->condition
, BRW_REGISTER_TYPE_D
, 1);
101 vec4_instruction
*inst
= emit(MOV(dst_null_d(), condition
));
102 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
104 /* We can just predicate based on the X channel, as the condition only
105 * goes on its own line */
106 emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X
));
108 nir_emit_cf_list(&if_stmt
->then_list
);
110 /* note: if the else is empty, dead CF elimination will remove it */
111 emit(BRW_OPCODE_ELSE
);
113 nir_emit_cf_list(&if_stmt
->else_list
);
115 emit(BRW_OPCODE_ENDIF
);
119 vec4_visitor::nir_emit_loop(nir_loop
*loop
)
123 nir_emit_cf_list(&loop
->body
);
125 emit(BRW_OPCODE_WHILE
);
129 vec4_visitor::nir_emit_block(nir_block
*block
)
131 nir_foreach_instr(instr
, block
) {
132 nir_emit_instr(instr
);
137 vec4_visitor::nir_emit_instr(nir_instr
*instr
)
141 switch (instr
->type
) {
142 case nir_instr_type_load_const
:
143 nir_emit_load_const(nir_instr_as_load_const(instr
));
146 case nir_instr_type_intrinsic
:
147 nir_emit_intrinsic(nir_instr_as_intrinsic(instr
));
150 case nir_instr_type_alu
:
151 nir_emit_alu(nir_instr_as_alu(instr
));
154 case nir_instr_type_jump
:
155 nir_emit_jump(nir_instr_as_jump(instr
));
158 case nir_instr_type_tex
:
159 nir_emit_texture(nir_instr_as_tex(instr
));
162 case nir_instr_type_ssa_undef
:
163 nir_emit_undef(nir_instr_as_ssa_undef(instr
));
167 unreachable("VS instruction not yet implemented by NIR->vec4");
172 dst_reg_for_nir_reg(vec4_visitor
*v
, nir_register
*nir_reg
,
173 unsigned base_offset
, nir_src
*indirect
)
177 reg
= v
->nir_locals
[nir_reg
->index
];
178 if (nir_reg
->bit_size
== 64)
179 reg
.type
= BRW_REGISTER_TYPE_DF
;
180 reg
= offset(reg
, 8, base_offset
);
183 new(v
->mem_ctx
) src_reg(v
->get_nir_src(*indirect
,
191 vec4_visitor::get_nir_dest(const nir_dest
&dest
)
195 dst_reg(VGRF
, alloc
.allocate(DIV_ROUND_UP(dest
.ssa
.bit_size
, 32)));
196 if (dest
.ssa
.bit_size
== 64)
197 dst
.type
= BRW_REGISTER_TYPE_DF
;
198 nir_ssa_values
[dest
.ssa
.index
] = dst
;
201 return dst_reg_for_nir_reg(this, dest
.reg
.reg
, dest
.reg
.base_offset
,
207 vec4_visitor::get_nir_dest(const nir_dest
&dest
, enum brw_reg_type type
)
209 return retype(get_nir_dest(dest
), type
);
213 vec4_visitor::get_nir_dest(const nir_dest
&dest
, nir_alu_type type
)
215 return get_nir_dest(dest
, brw_type_for_nir_type(devinfo
, type
));
219 vec4_visitor::get_nir_src(const nir_src
&src
, enum brw_reg_type type
,
220 unsigned num_components
)
225 assert(src
.ssa
!= NULL
);
226 reg
= nir_ssa_values
[src
.ssa
->index
];
229 reg
= dst_reg_for_nir_reg(this, src
.reg
.reg
, src
.reg
.base_offset
,
233 reg
= retype(reg
, type
);
235 src_reg reg_as_src
= src_reg(reg
);
236 reg_as_src
.swizzle
= brw_swizzle_for_size(num_components
);
241 vec4_visitor::get_nir_src(const nir_src
&src
, nir_alu_type type
,
242 unsigned num_components
)
244 return get_nir_src(src
, brw_type_for_nir_type(devinfo
, type
),
249 vec4_visitor::get_nir_src(const nir_src
&src
, unsigned num_components
)
251 /* if type is not specified, default to signed int */
252 return get_nir_src(src
, nir_type_int32
, num_components
);
256 vec4_visitor::get_nir_src_imm(const nir_src
&src
)
258 assert(nir_src_num_components(src
) == 1);
259 assert(nir_src_bit_size(src
) == 32);
260 return nir_src_is_const(src
) ? src_reg(brw_imm_d(nir_src_as_int(src
))) :
265 vec4_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
267 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
269 if (nir_src_is_const(*offset_src
)) {
270 /* The only constant offset we should find is 0. brw_nir.c's
271 * add_const_offset_to_base() will fold other constant offsets
272 * into instr->const_index[0].
274 assert(nir_src_as_uint(*offset_src
) == 0);
278 return get_nir_src(*offset_src
, BRW_REGISTER_TYPE_UD
, 1);
282 setup_imm_df(const vec4_builder
&bld
, double v
)
284 const gen_device_info
*devinfo
= bld
.shader
->devinfo
;
285 assert(devinfo
->gen
>= 7);
287 if (devinfo
->gen
>= 8)
288 return brw_imm_df(v
);
290 /* gen7.5 does not support DF immediates straighforward but the DIM
291 * instruction allows to set the 64-bit immediate value.
293 if (devinfo
->is_haswell
) {
294 const vec4_builder ubld
= bld
.exec_all();
295 const dst_reg dst
= bld
.vgrf(BRW_REGISTER_TYPE_DF
);
296 ubld
.DIM(dst
, brw_imm_df(v
));
297 return swizzle(src_reg(dst
), BRW_SWIZZLE_XXXX
);
300 /* gen7 does not support DF immediates */
311 /* Write the low 32-bit of the constant to the X:UD channel and the
312 * high 32-bit to the Y:UD channel to build the constant in a VGRF.
313 * We have to do this twice (offset 0 and offset 1), since a DF VGRF takes
314 * two SIMD8 registers in SIMD4x2 execution. Finally, return a swizzle
315 * XXXX so any access to the VGRF only reads the constant data in these
318 const dst_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
319 for (unsigned n
= 0; n
< 2; n
++) {
320 const vec4_builder ubld
= bld
.exec_all().group(4, n
);
321 ubld
.MOV(writemask(offset(tmp
, 8, n
), WRITEMASK_X
), brw_imm_ud(di
.i1
));
322 ubld
.MOV(writemask(offset(tmp
, 8, n
), WRITEMASK_Y
), brw_imm_ud(di
.i2
));
325 return swizzle(src_reg(retype(tmp
, BRW_REGISTER_TYPE_DF
)), BRW_SWIZZLE_XXXX
);
329 vec4_visitor::nir_emit_load_const(nir_load_const_instr
*instr
)
333 if (instr
->def
.bit_size
== 64) {
334 reg
= dst_reg(VGRF
, alloc
.allocate(2));
335 reg
.type
= BRW_REGISTER_TYPE_DF
;
337 reg
= dst_reg(VGRF
, alloc
.allocate(1));
338 reg
.type
= BRW_REGISTER_TYPE_D
;
341 const vec4_builder ibld
= vec4_builder(this).at_end();
342 unsigned remaining
= brw_writemask_for_size(instr
->def
.num_components
);
344 /* @FIXME: consider emitting vector operations to save some MOVs in
345 * cases where the components are representable in 8 bits.
346 * For now, we emit a MOV for each distinct value.
348 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
349 unsigned writemask
= 1 << i
;
351 if ((remaining
& writemask
) == 0)
354 for (unsigned j
= i
; j
< instr
->def
.num_components
; j
++) {
355 if ((instr
->def
.bit_size
== 32 &&
356 instr
->value
[i
].u32
== instr
->value
[j
].u32
) ||
357 (instr
->def
.bit_size
== 64 &&
358 instr
->value
[i
].f64
== instr
->value
[j
].f64
)) {
363 reg
.writemask
= writemask
;
364 if (instr
->def
.bit_size
== 64) {
365 emit(MOV(reg
, setup_imm_df(ibld
, instr
->value
[i
].f64
)));
367 emit(MOV(reg
, brw_imm_d(instr
->value
[i
].i32
)));
370 remaining
&= ~writemask
;
373 /* Set final writemask */
374 reg
.writemask
= brw_writemask_for_size(instr
->def
.num_components
);
376 nir_ssa_values
[instr
->def
.index
] = reg
;
380 vec4_visitor::get_nir_ssbo_intrinsic_index(nir_intrinsic_instr
*instr
)
382 /* SSBO stores are weird in that their index is in src[1] */
383 const unsigned src
= instr
->intrinsic
== nir_intrinsic_store_ssbo
? 1 : 0;
386 if (nir_src_is_const(instr
->src
[src
])) {
387 unsigned index
= prog_data
->base
.binding_table
.ssbo_start
+
388 nir_src_as_uint(instr
->src
[src
]);
389 surf_index
= brw_imm_ud(index
);
391 surf_index
= src_reg(this, glsl_type::uint_type
);
392 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[src
], 1),
393 brw_imm_ud(prog_data
->base
.binding_table
.ssbo_start
)));
394 surf_index
= emit_uniformize(surf_index
);
401 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr
*instr
)
406 switch (instr
->intrinsic
) {
408 case nir_intrinsic_load_input
: {
409 /* We set EmitNoIndirectInput for VS */
410 unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
412 dest
= get_nir_dest(instr
->dest
);
413 dest
.writemask
= brw_writemask_for_size(instr
->num_components
);
415 src
= src_reg(ATTR
, instr
->const_index
[0] + load_offset
,
416 glsl_type::uvec4_type
);
417 src
= retype(src
, dest
.type
);
419 bool is_64bit
= nir_dest_bit_size(instr
->dest
) == 64;
421 dst_reg tmp
= dst_reg(this, glsl_type::dvec4_type
);
422 src
.swizzle
= BRW_SWIZZLE_XYZW
;
423 shuffle_64bit_data(tmp
, src
, false);
424 emit(MOV(dest
, src_reg(tmp
)));
426 /* Swizzle source based on component layout qualifier */
427 src
.swizzle
= BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr
));
428 emit(MOV(dest
, src
));
433 case nir_intrinsic_store_output
: {
434 unsigned store_offset
= nir_src_as_uint(instr
->src
[1]);
435 int varying
= instr
->const_index
[0] + store_offset
;
437 bool is_64bit
= nir_src_bit_size(instr
->src
[0]) == 64;
440 src
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_DF
,
441 instr
->num_components
);
442 data
= src_reg(this, glsl_type::dvec4_type
);
443 shuffle_64bit_data(dst_reg(data
), src
, true);
444 src
= retype(data
, BRW_REGISTER_TYPE_F
);
446 src
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_F
,
447 instr
->num_components
);
450 unsigned c
= nir_intrinsic_component(instr
);
451 output_reg
[varying
][c
] = dst_reg(src
);
452 output_num_components
[varying
][c
] = instr
->num_components
;
454 unsigned num_components
= instr
->num_components
;
458 output_reg
[varying
][c
] = dst_reg(src
);
459 output_num_components
[varying
][c
] = MIN2(4, num_components
);
461 if (is_64bit
&& num_components
> 4) {
462 assert(num_components
<= 8);
463 output_reg
[varying
+ 1][c
] = byte_offset(dst_reg(src
), REG_SIZE
);
464 output_num_components
[varying
+ 1][c
] = num_components
- 4;
469 case nir_intrinsic_get_buffer_size
: {
470 assert(nir_src_num_components(instr
->src
[0]) == 1);
471 unsigned ssbo_index
= nir_src_is_const(instr
->src
[0]) ?
472 nir_src_as_uint(instr
->src
[0]) : 0;
474 const unsigned index
=
475 prog_data
->base
.binding_table
.ssbo_start
+ ssbo_index
;
476 dst_reg result_dst
= get_nir_dest(instr
->dest
);
477 vec4_instruction
*inst
= new(mem_ctx
)
478 vec4_instruction(SHADER_OPCODE_GET_BUFFER_SIZE
, result_dst
);
481 inst
->mlen
= 1; /* always at least one */
482 inst
->src
[1] = brw_imm_ud(index
);
484 /* MRF for the first parameter */
485 src_reg lod
= brw_imm_d(0);
486 int param_base
= inst
->base_mrf
;
487 int writemask
= WRITEMASK_X
;
488 emit(MOV(dst_reg(MRF
, param_base
, glsl_type::int_type
, writemask
), lod
));
494 case nir_intrinsic_store_ssbo
: {
495 assert(devinfo
->gen
>= 7);
497 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
498 assert(nir_src_bit_size(instr
->src
[0]) == 32);
499 assert(nir_intrinsic_write_mask(instr
) ==
500 (1u << instr
->num_components
) - 1);
502 src_reg surf_index
= get_nir_ssbo_intrinsic_index(instr
);
503 src_reg offset_reg
= retype(get_nir_src_imm(instr
->src
[2]),
504 BRW_REGISTER_TYPE_UD
);
507 src_reg val_reg
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_F
, 4);
509 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
510 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
511 * typed and untyped messages and across hardware platforms, the
512 * current implementation of the untyped messages will transparently convert
513 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
514 * and enabling only channel X on the SEND instruction.
516 * The above, works well for full vector writes, but not for partial writes
517 * where we want to write some channels and not others, like when we have
518 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
519 * quite restrictive with regards to the channel enables we can configure in
520 * the message descriptor (not all combinations are allowed) we cannot simply
521 * implement these scenarios with a single message while keeping the
522 * aforementioned symmetry in the implementation. For now we de decided that
523 * it is better to keep the symmetry to reduce complexity, so in situations
524 * such as the one described we end up emitting two untyped write messages
525 * (one for xy and another for w).
527 * The code below packs consecutive channels into a single write message,
528 * detects gaps in the vector write and if needed, sends a second message
529 * with the remaining channels. If in the future we decide that we want to
530 * emit a single message at the expense of losing the symmetry in the
531 * implementation we can:
533 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
534 * message payload. In this mode we can write up to 8 offsets and dwords
535 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
536 * and select which of the 8 channels carry data to write by setting the
537 * appropriate writemask in the dst register of the SEND instruction.
538 * It would require to write a new generator opcode specifically for
539 * IvyBridge since we would need to prepare a SIMD8 payload that could
540 * use any channel, not just X.
542 * 2) For Haswell+: Simply send a single write message but set the writemask
543 * on the dst of the SEND instruction to select the channels we want to
544 * write. It would require to modify the current messages to receive
545 * and honor the writemask provided.
547 const vec4_builder bld
= vec4_builder(this).at_end()
548 .annotate(current_annotation
, base_ir
);
550 emit_untyped_write(bld
, surf_index
, offset_reg
, val_reg
,
551 1 /* dims */, instr
->num_components
/* size */,
556 case nir_intrinsic_load_ssbo
: {
557 assert(devinfo
->gen
>= 7);
559 /* brw_nir_lower_mem_access_bit_sizes takes care of this */
560 assert(nir_dest_bit_size(instr
->dest
) == 32);
562 src_reg surf_index
= get_nir_ssbo_intrinsic_index(instr
);
563 src_reg offset_reg
= retype(get_nir_src_imm(instr
->src
[1]),
564 BRW_REGISTER_TYPE_UD
);
566 /* Read the vector */
567 const vec4_builder bld
= vec4_builder(this).at_end()
568 .annotate(current_annotation
, base_ir
);
570 src_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
571 1 /* dims */, 4 /* size*/,
573 dst_reg dest
= get_nir_dest(instr
->dest
);
574 read_result
.type
= dest
.type
;
575 read_result
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
576 emit(MOV(dest
, read_result
));
580 case nir_intrinsic_ssbo_atomic_add
: {
581 int op
= BRW_AOP_ADD
;
583 if (nir_src_is_const(instr
->src
[2])) {
584 int add_val
= nir_src_as_int(instr
->src
[2]);
587 else if (add_val
== -1)
591 nir_emit_ssbo_atomic(op
, instr
);
594 case nir_intrinsic_ssbo_atomic_imin
:
595 nir_emit_ssbo_atomic(BRW_AOP_IMIN
, instr
);
597 case nir_intrinsic_ssbo_atomic_umin
:
598 nir_emit_ssbo_atomic(BRW_AOP_UMIN
, instr
);
600 case nir_intrinsic_ssbo_atomic_imax
:
601 nir_emit_ssbo_atomic(BRW_AOP_IMAX
, instr
);
603 case nir_intrinsic_ssbo_atomic_umax
:
604 nir_emit_ssbo_atomic(BRW_AOP_UMAX
, instr
);
606 case nir_intrinsic_ssbo_atomic_and
:
607 nir_emit_ssbo_atomic(BRW_AOP_AND
, instr
);
609 case nir_intrinsic_ssbo_atomic_or
:
610 nir_emit_ssbo_atomic(BRW_AOP_OR
, instr
);
612 case nir_intrinsic_ssbo_atomic_xor
:
613 nir_emit_ssbo_atomic(BRW_AOP_XOR
, instr
);
615 case nir_intrinsic_ssbo_atomic_exchange
:
616 nir_emit_ssbo_atomic(BRW_AOP_MOV
, instr
);
618 case nir_intrinsic_ssbo_atomic_comp_swap
:
619 nir_emit_ssbo_atomic(BRW_AOP_CMPWR
, instr
);
622 case nir_intrinsic_load_vertex_id
:
623 unreachable("should be lowered by lower_vertex_id()");
625 case nir_intrinsic_load_vertex_id_zero_base
:
626 case nir_intrinsic_load_base_vertex
:
627 case nir_intrinsic_load_instance_id
:
628 case nir_intrinsic_load_base_instance
:
629 case nir_intrinsic_load_draw_id
:
630 case nir_intrinsic_load_invocation_id
:
631 unreachable("should be lowered by brw_nir_lower_vs_inputs()");
633 case nir_intrinsic_load_uniform
: {
634 /* Offsets are in bytes but they should always be multiples of 4 */
635 assert(nir_intrinsic_base(instr
) % 4 == 0);
637 dest
= get_nir_dest(instr
->dest
);
639 src
= src_reg(dst_reg(UNIFORM
, nir_intrinsic_base(instr
) / 16));
640 src
.type
= dest
.type
;
642 /* Uniforms don't actually have to be vec4 aligned. In the case that
643 * it isn't, we have to use a swizzle to shift things around. They
644 * do still have the std140 alignment requirement that vec2's have to
645 * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
647 * The swizzle also works in the indirect case as the generator adds
648 * the swizzle to the offset for us.
650 const int type_size
= type_sz(src
.type
);
651 unsigned shift
= (nir_intrinsic_base(instr
) % 16) / type_size
;
652 assert(shift
+ instr
->num_components
<= 4);
654 if (nir_src_is_const(instr
->src
[0])) {
655 const unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
656 /* Offsets are in bytes but they should always be multiples of 4 */
657 assert(load_offset
% 4 == 0);
659 src
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
660 dest
.writemask
= brw_writemask_for_size(instr
->num_components
);
661 unsigned offset
= load_offset
+ shift
* type_size
;
662 src
.offset
= ROUND_DOWN_TO(offset
, 16);
663 shift
= (offset
% 16) / type_size
;
664 assert(shift
+ instr
->num_components
<= 4);
665 src
.swizzle
+= BRW_SWIZZLE4(shift
, shift
, shift
, shift
);
667 emit(MOV(dest
, src
));
669 /* Uniform arrays are vec4 aligned, because of std140 alignment
674 src_reg indirect
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_UD
, 1);
676 /* MOV_INDIRECT is going to stomp the whole thing anyway */
677 dest
.writemask
= WRITEMASK_XYZW
;
679 emit(SHADER_OPCODE_MOV_INDIRECT
, dest
, src
,
680 indirect
, brw_imm_ud(instr
->const_index
[1]));
685 case nir_intrinsic_load_ubo
: {
688 dest
= get_nir_dest(instr
->dest
);
690 if (nir_src_is_const(instr
->src
[0])) {
691 /* The block index is a constant, so just emit the binding table entry
694 const unsigned index
= prog_data
->base
.binding_table
.ubo_start
+
695 nir_src_as_uint(instr
->src
[0]);
696 surf_index
= brw_imm_ud(index
);
698 /* The block index is not a constant. Evaluate the index expression
699 * per-channel and add the base UBO index; we have to select a value
700 * from any live channel.
702 surf_index
= src_reg(this, glsl_type::uint_type
);
703 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[0], nir_type_int32
,
704 instr
->num_components
),
705 brw_imm_ud(prog_data
->base
.binding_table
.ubo_start
)));
706 surf_index
= emit_uniformize(surf_index
);
710 if (nir_src_is_const(instr
->src
[1])) {
711 unsigned load_offset
= nir_src_as_uint(instr
->src
[1]);
712 offset_reg
= brw_imm_ud(load_offset
& ~15);
714 offset_reg
= src_reg(this, glsl_type::uint_type
);
715 emit(MOV(dst_reg(offset_reg
),
716 get_nir_src(instr
->src
[1], nir_type_uint32
, 1)));
719 src_reg packed_consts
;
720 if (nir_dest_bit_size(instr
->dest
) == 32) {
721 packed_consts
= src_reg(this, glsl_type::vec4_type
);
722 emit_pull_constant_load_reg(dst_reg(packed_consts
),
725 NULL
, NULL
/* before_block/inst */);
727 src_reg temp
= src_reg(this, glsl_type::dvec4_type
);
728 src_reg temp_float
= retype(temp
, BRW_REGISTER_TYPE_F
);
730 emit_pull_constant_load_reg(dst_reg(temp_float
),
731 surf_index
, offset_reg
, NULL
, NULL
);
732 if (offset_reg
.file
== IMM
)
735 emit(ADD(dst_reg(offset_reg
), offset_reg
, brw_imm_ud(16u)));
736 emit_pull_constant_load_reg(dst_reg(byte_offset(temp_float
, REG_SIZE
)),
737 surf_index
, offset_reg
, NULL
, NULL
);
739 packed_consts
= src_reg(this, glsl_type::dvec4_type
);
740 shuffle_64bit_data(dst_reg(packed_consts
), temp
, false);
743 packed_consts
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
744 if (nir_src_is_const(instr
->src
[1])) {
745 unsigned load_offset
= nir_src_as_uint(instr
->src
[1]);
746 unsigned type_size
= type_sz(dest
.type
);
747 packed_consts
.swizzle
+=
748 BRW_SWIZZLE4(load_offset
% 16 / type_size
,
749 load_offset
% 16 / type_size
,
750 load_offset
% 16 / type_size
,
751 load_offset
% 16 / type_size
);
754 emit(MOV(dest
, retype(packed_consts
, dest
.type
)));
759 case nir_intrinsic_memory_barrier
: {
760 const vec4_builder bld
=
761 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
762 const dst_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
763 bld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
764 ->size_written
= 2 * REG_SIZE
;
768 case nir_intrinsic_shader_clock
: {
769 /* We cannot do anything if there is an event, so ignore it for now */
770 const src_reg shader_clock
= get_timestamp();
771 const enum brw_reg_type type
= brw_type_for_base_type(glsl_type::uvec2_type
);
773 dest
= get_nir_dest(instr
->dest
, type
);
774 emit(MOV(dest
, shader_clock
));
779 unreachable("Unknown intrinsic");
784 vec4_visitor::nir_emit_ssbo_atomic(int op
, nir_intrinsic_instr
*instr
)
787 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
788 dest
= get_nir_dest(instr
->dest
);
790 src_reg surface
= get_nir_ssbo_intrinsic_index(instr
);
791 src_reg offset
= get_nir_src(instr
->src
[1], 1);
793 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
794 data1
= get_nir_src(instr
->src
[2], 1);
796 if (op
== BRW_AOP_CMPWR
)
797 data2
= get_nir_src(instr
->src
[3], 1);
799 /* Emit the actual atomic operation operation */
800 const vec4_builder bld
=
801 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
803 src_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
805 1 /* dims */, 1 /* rsize */,
808 dest
.type
= atomic_result
.type
;
809 bld
.MOV(dest
, atomic_result
);
813 brw_swizzle_for_nir_swizzle(uint8_t swizzle
[4])
815 return BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
818 static enum brw_conditional_mod
819 brw_conditional_for_nir_comparison(nir_op op
)
825 return BRW_CONDITIONAL_L
;
830 return BRW_CONDITIONAL_GE
;
834 case nir_op_b32all_fequal2
:
835 case nir_op_b32all_iequal2
:
836 case nir_op_b32all_fequal3
:
837 case nir_op_b32all_iequal3
:
838 case nir_op_b32all_fequal4
:
839 case nir_op_b32all_iequal4
:
840 return BRW_CONDITIONAL_Z
;
844 case nir_op_b32any_fnequal2
:
845 case nir_op_b32any_inequal2
:
846 case nir_op_b32any_fnequal3
:
847 case nir_op_b32any_inequal3
:
848 case nir_op_b32any_fnequal4
:
849 case nir_op_b32any_inequal4
:
850 return BRW_CONDITIONAL_NZ
;
853 unreachable("not reached: bad operation for comparison");
858 vec4_visitor::optimize_predicate(nir_alu_instr
*instr
,
859 enum brw_predicate
*predicate
)
861 if (!instr
->src
[0].src
.is_ssa
||
862 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
865 nir_alu_instr
*cmp_instr
=
866 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
868 switch (cmp_instr
->op
) {
869 case nir_op_b32any_fnequal2
:
870 case nir_op_b32any_inequal2
:
871 case nir_op_b32any_fnequal3
:
872 case nir_op_b32any_inequal3
:
873 case nir_op_b32any_fnequal4
:
874 case nir_op_b32any_inequal4
:
875 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
877 case nir_op_b32all_fequal2
:
878 case nir_op_b32all_iequal2
:
879 case nir_op_b32all_fequal3
:
880 case nir_op_b32all_iequal3
:
881 case nir_op_b32all_fequal4
:
882 case nir_op_b32all_iequal4
:
883 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
889 unsigned size_swizzle
=
890 brw_swizzle_for_size(nir_op_infos
[cmp_instr
->op
].input_sizes
[0]);
893 assert(nir_op_infos
[cmp_instr
->op
].num_inputs
== 2);
894 for (unsigned i
= 0; i
< 2; i
++) {
895 nir_alu_type type
= nir_op_infos
[cmp_instr
->op
].input_types
[i
];
896 unsigned bit_size
= nir_src_bit_size(cmp_instr
->src
[i
].src
);
897 type
= (nir_alu_type
) (((unsigned) type
) | bit_size
);
898 op
[i
] = get_nir_src(cmp_instr
->src
[i
].src
, type
, 4);
899 unsigned base_swizzle
=
900 brw_swizzle_for_nir_swizzle(cmp_instr
->src
[i
].swizzle
);
901 op
[i
].swizzle
= brw_compose_swizzle(size_swizzle
, base_swizzle
);
902 op
[i
].abs
= cmp_instr
->src
[i
].abs
;
903 op
[i
].negate
= cmp_instr
->src
[i
].negate
;
906 emit(CMP(dst_null_d(), op
[0], op
[1],
907 brw_conditional_for_nir_comparison(cmp_instr
->op
)));
913 emit_find_msb_using_lzd(const vec4_builder
&bld
,
918 vec4_instruction
*inst
;
922 /* LZD of an absolute value source almost always does the right
923 * thing. There are two problem values:
925 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
926 * 0. However, findMSB(int(0x80000000)) == 30.
928 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
929 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
931 * For a value of zero or negative one, -1 will be returned.
933 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
934 * findMSB(-(1<<x)) should return x-1.
936 * For all negative number cases, including 0x80000000 and
937 * 0xffffffff, the correct value is obtained from LZD if instead of
938 * negating the (already negative) value the logical-not is used. A
939 * conditonal logical-not can be achieved in two instructions.
941 temp
= src_reg(bld
.vgrf(BRW_REGISTER_TYPE_D
));
943 bld
.ASR(dst_reg(temp
), src
, brw_imm_d(31));
944 bld
.XOR(dst_reg(temp
), temp
, src
);
947 bld
.LZD(retype(dst
, BRW_REGISTER_TYPE_UD
),
948 retype(temp
, BRW_REGISTER_TYPE_UD
));
950 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
951 * from the LSB side. Subtract the result from 31 to convert the MSB count
952 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
953 * which is exactly what findMSB() is supposed to return.
955 inst
= bld
.ADD(dst
, retype(src_reg(dst
), BRW_REGISTER_TYPE_D
),
957 inst
->src
[0].negate
= true;
961 vec4_visitor::emit_conversion_from_double(dst_reg dst
, src_reg src
,
964 /* BDW PRM vol 15 - workarounds:
965 * DF->f format conversion for Align16 has wrong emask calculation when
966 * source is immediate.
968 if (devinfo
->gen
== 8 && dst
.type
== BRW_REGISTER_TYPE_F
&&
969 src
.file
== BRW_IMMEDIATE_VALUE
) {
970 vec4_instruction
*inst
= emit(MOV(dst
, brw_imm_f(src
.df
)));
971 inst
->saturate
= saturate
;
977 case BRW_REGISTER_TYPE_D
:
978 op
= VEC4_OPCODE_DOUBLE_TO_D32
;
980 case BRW_REGISTER_TYPE_UD
:
981 op
= VEC4_OPCODE_DOUBLE_TO_U32
;
983 case BRW_REGISTER_TYPE_F
:
984 op
= VEC4_OPCODE_DOUBLE_TO_F32
;
987 unreachable("Unknown conversion");
990 dst_reg temp
= dst_reg(this, glsl_type::dvec4_type
);
991 emit(MOV(temp
, src
));
992 dst_reg temp2
= dst_reg(this, glsl_type::dvec4_type
);
993 emit(op
, temp2
, src_reg(temp
));
995 emit(VEC4_OPCODE_PICK_LOW_32BIT
, retype(temp2
, dst
.type
), src_reg(temp2
));
996 vec4_instruction
*inst
= emit(MOV(dst
, src_reg(retype(temp2
, dst
.type
))));
997 inst
->saturate
= saturate
;
1001 vec4_visitor::emit_conversion_to_double(dst_reg dst
, src_reg src
,
1004 dst_reg tmp_dst
= dst_reg(src_reg(this, glsl_type::dvec4_type
));
1005 src_reg tmp_src
= retype(src_reg(this, glsl_type::vec4_type
), src
.type
);
1006 emit(MOV(dst_reg(tmp_src
), src
));
1007 emit(VEC4_OPCODE_TO_DOUBLE
, tmp_dst
, tmp_src
);
1008 vec4_instruction
*inst
= emit(MOV(dst
, src_reg(tmp_dst
)));
1009 inst
->saturate
= saturate
;
1013 * Try to use an immediate value for source 1
1015 * In cases of flow control, constant propagation is sometimes unable to
1016 * determine that a register contains a constant value. To work around this,
1017 * try to emit a literal as the second source here.
1020 try_immediate_source(const nir_alu_instr
*instr
, src_reg
*op
,
1021 MAYBE_UNUSED
const gen_device_info
*devinfo
)
1023 if (nir_src_num_components(instr
->src
[1].src
) != 1 ||
1024 nir_src_bit_size(instr
->src
[1].src
) != 32 ||
1025 !nir_src_is_const(instr
->src
[1].src
))
1028 const enum brw_reg_type old_type
= op
->type
;
1031 case BRW_REGISTER_TYPE_D
:
1032 case BRW_REGISTER_TYPE_UD
: {
1033 int d
= nir_src_as_int(instr
->src
[1].src
);
1039 /* On Gen8+ a negation source modifier on a logical operation means
1040 * something different. Nothing should generate this, so assert that
1041 * it does not occur.
1043 assert(devinfo
->gen
< 8 || (instr
->op
!= nir_op_iand
&&
1044 instr
->op
!= nir_op_ior
&&
1045 instr
->op
!= nir_op_ixor
));
1049 *op
= retype(src_reg(brw_imm_d(d
)), old_type
);
1053 case BRW_REGISTER_TYPE_F
: {
1054 float f
= nir_src_as_float(instr
->src
[1].src
);
1062 *op
= src_reg(brw_imm_f(f
));
1063 assert(op
->type
== old_type
);
1068 unreachable("Non-32bit type.");
1073 vec4_visitor::nir_emit_alu(nir_alu_instr
*instr
)
1075 vec4_instruction
*inst
;
1077 nir_alu_type dst_type
= (nir_alu_type
) (nir_op_infos
[instr
->op
].output_type
|
1078 nir_dest_bit_size(instr
->dest
.dest
));
1079 dst_reg dst
= get_nir_dest(instr
->dest
.dest
, dst_type
);
1080 dst
.writemask
= instr
->dest
.write_mask
;
1083 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
1084 nir_alu_type src_type
= (nir_alu_type
)
1085 (nir_op_infos
[instr
->op
].input_types
[i
] |
1086 nir_src_bit_size(instr
->src
[i
].src
));
1087 op
[i
] = get_nir_src(instr
->src
[i
].src
, src_type
, 4);
1088 op
[i
].swizzle
= brw_swizzle_for_nir_swizzle(instr
->src
[i
].swizzle
);
1089 op
[i
].abs
= instr
->src
[i
].abs
;
1090 op
[i
].negate
= instr
->src
[i
].negate
;
1093 switch (instr
->op
) {
1096 inst
= emit(MOV(dst
, op
[0]));
1097 inst
->saturate
= instr
->dest
.saturate
;
1103 unreachable("not reached: should be handled by lower_vec_to_movs()");
1107 inst
= emit(MOV(dst
, op
[0]));
1108 inst
->saturate
= instr
->dest
.saturate
;
1114 if (nir_src_bit_size(instr
->src
[0].src
) == 64)
1115 emit_conversion_from_double(dst
, op
[0], instr
->dest
.saturate
);
1117 inst
= emit(MOV(dst
, op
[0]));
1123 emit_conversion_to_double(dst
, op
[0], instr
->dest
.saturate
);
1127 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1130 try_immediate_source(instr
, &op
[1], devinfo
);
1131 inst
= emit(ADD(dst
, op
[0], op
[1]));
1132 inst
->saturate
= instr
->dest
.saturate
;
1135 case nir_op_uadd_sat
:
1136 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1137 inst
= emit(ADD(dst
, op
[0], op
[1]));
1138 inst
->saturate
= true;
1142 try_immediate_source(instr
, &op
[1], devinfo
);
1143 inst
= emit(MUL(dst
, op
[0], op
[1]));
1144 inst
->saturate
= instr
->dest
.saturate
;
1148 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1149 if (devinfo
->gen
< 8) {
1150 /* For integer multiplication, the MUL uses the low 16 bits of one of
1151 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1152 * accumulates in the contribution of the upper 16 bits of that
1153 * operand. If we can determine that one of the args is in the low
1154 * 16 bits, though, we can just emit a single MUL.
1156 if (nir_src_is_const(instr
->src
[0].src
) &&
1157 nir_alu_instr_src_read_mask(instr
, 0) == 1 &&
1158 nir_src_comp_as_uint(instr
->src
[0].src
, 0) < (1 << 16)) {
1159 if (devinfo
->gen
< 7)
1160 emit(MUL(dst
, op
[0], op
[1]));
1162 emit(MUL(dst
, op
[1], op
[0]));
1163 } else if (nir_src_is_const(instr
->src
[1].src
) &&
1164 nir_alu_instr_src_read_mask(instr
, 1) == 1 &&
1165 nir_src_comp_as_uint(instr
->src
[1].src
, 0) < (1 << 16)) {
1166 if (devinfo
->gen
< 7)
1167 emit(MUL(dst
, op
[1], op
[0]));
1169 emit(MUL(dst
, op
[0], op
[1]));
1171 struct brw_reg acc
= retype(brw_acc_reg(8), dst
.type
);
1173 emit(MUL(acc
, op
[0], op
[1]));
1174 emit(MACH(dst_null_d(), op
[0], op
[1]));
1175 emit(MOV(dst
, src_reg(acc
)));
1178 emit(MUL(dst
, op
[0], op
[1]));
1183 case nir_op_imul_high
:
1184 case nir_op_umul_high
: {
1185 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1186 struct brw_reg acc
= retype(brw_acc_reg(8), dst
.type
);
1188 if (devinfo
->gen
>= 8)
1189 emit(MUL(acc
, op
[0], retype(op
[1], BRW_REGISTER_TYPE_UW
)));
1191 emit(MUL(acc
, op
[0], op
[1]));
1193 emit(MACH(dst
, op
[0], op
[1]));
1198 inst
= emit_math(SHADER_OPCODE_RCP
, dst
, op
[0]);
1199 inst
->saturate
= instr
->dest
.saturate
;
1203 inst
= emit_math(SHADER_OPCODE_EXP2
, dst
, op
[0]);
1204 inst
->saturate
= instr
->dest
.saturate
;
1208 inst
= emit_math(SHADER_OPCODE_LOG2
, dst
, op
[0]);
1209 inst
->saturate
= instr
->dest
.saturate
;
1213 inst
= emit_math(SHADER_OPCODE_SIN
, dst
, op
[0]);
1214 inst
->saturate
= instr
->dest
.saturate
;
1218 inst
= emit_math(SHADER_OPCODE_COS
, dst
, op
[0]);
1219 inst
->saturate
= instr
->dest
.saturate
;
1224 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1225 emit_math(SHADER_OPCODE_INT_QUOTIENT
, dst
, op
[0], op
[1]);
1230 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1231 * appears that our hardware just does the right thing for signed
1234 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1235 emit_math(SHADER_OPCODE_INT_REMAINDER
, dst
, op
[0], op
[1]);
1239 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1240 inst
= emit_math(SHADER_OPCODE_INT_REMAINDER
, dst
, op
[0], op
[1]);
1242 /* Math instructions don't support conditional mod */
1243 inst
= emit(MOV(dst_null_d(), src_reg(dst
)));
1244 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1246 /* Now, we need to determine if signs of the sources are different.
1247 * When we XOR the sources, the top bit is 0 if they are the same and 1
1248 * if they are different. We can then use a conditional modifier to
1249 * turn that into a predicate. This leads us to an XOR.l instruction.
1251 * Technically, according to the PRM, you're not allowed to use .l on a
1252 * XOR instruction. However, emperical experiments and Curro's reading
1253 * of the simulator source both indicate that it's safe.
1255 src_reg tmp
= src_reg(this, glsl_type::ivec4_type
);
1256 inst
= emit(XOR(dst_reg(tmp
), op
[0], op
[1]));
1257 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1258 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1260 /* If the result of the initial remainder operation is non-zero and the
1261 * two sources have different signs, add in a copy of op[1] to get the
1262 * final integer modulus value.
1264 inst
= emit(ADD(dst
, src_reg(dst
), op
[1]));
1265 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1270 unreachable("not reached: should be handled by ldexp_to_arith()");
1273 inst
= emit_math(SHADER_OPCODE_SQRT
, dst
, op
[0]);
1274 inst
->saturate
= instr
->dest
.saturate
;
1278 inst
= emit_math(SHADER_OPCODE_RSQ
, dst
, op
[0]);
1279 inst
->saturate
= instr
->dest
.saturate
;
1283 inst
= emit_math(SHADER_OPCODE_POW
, dst
, op
[0], op
[1]);
1284 inst
->saturate
= instr
->dest
.saturate
;
1287 case nir_op_uadd_carry
: {
1288 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1289 struct brw_reg acc
= retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD
);
1291 emit(ADDC(dst_null_ud(), op
[0], op
[1]));
1292 emit(MOV(dst
, src_reg(acc
)));
1296 case nir_op_usub_borrow
: {
1297 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1298 struct brw_reg acc
= retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD
);
1300 emit(SUBB(dst_null_ud(), op
[0], op
[1]));
1301 emit(MOV(dst
, src_reg(acc
)));
1306 inst
= emit(RNDZ(dst
, op
[0]));
1307 inst
->saturate
= instr
->dest
.saturate
;
1310 case nir_op_fceil
: {
1311 src_reg tmp
= src_reg(this, glsl_type::float_type
);
1313 brw_swizzle_for_size(instr
->src
[0].src
.is_ssa
?
1314 instr
->src
[0].src
.ssa
->num_components
:
1315 instr
->src
[0].src
.reg
.reg
->num_components
);
1317 op
[0].negate
= !op
[0].negate
;
1318 emit(RNDD(dst_reg(tmp
), op
[0]));
1320 inst
= emit(MOV(dst
, tmp
));
1321 inst
->saturate
= instr
->dest
.saturate
;
1326 inst
= emit(RNDD(dst
, op
[0]));
1327 inst
->saturate
= instr
->dest
.saturate
;
1331 inst
= emit(FRC(dst
, op
[0]));
1332 inst
->saturate
= instr
->dest
.saturate
;
1335 case nir_op_fround_even
:
1336 inst
= emit(RNDE(dst
, op
[0]));
1337 inst
->saturate
= instr
->dest
.saturate
;
1340 case nir_op_fquantize2f16
: {
1341 /* See also vec4_visitor::emit_pack_half_2x16() */
1342 src_reg tmp16
= src_reg(this, glsl_type::uvec4_type
);
1343 src_reg tmp32
= src_reg(this, glsl_type::vec4_type
);
1344 src_reg zero
= src_reg(this, glsl_type::vec4_type
);
1346 /* Check for denormal */
1347 src_reg abs_src0
= op
[0];
1348 abs_src0
.abs
= true;
1349 emit(CMP(dst_null_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1350 BRW_CONDITIONAL_L
));
1351 /* Get the appropriately signed zero */
1352 emit(AND(retype(dst_reg(zero
), BRW_REGISTER_TYPE_UD
),
1353 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1354 brw_imm_ud(0x80000000)));
1355 /* Do the actual F32 -> F16 -> F32 conversion */
1356 emit(F32TO16(dst_reg(tmp16
), op
[0]));
1357 emit(F16TO32(dst_reg(tmp32
), tmp16
));
1358 /* Select that or zero based on normal status */
1359 inst
= emit(BRW_OPCODE_SEL
, dst
, zero
, tmp32
);
1360 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1361 inst
->saturate
= instr
->dest
.saturate
;
1367 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1370 try_immediate_source(instr
, &op
[1], devinfo
);
1371 inst
= emit_minmax(BRW_CONDITIONAL_L
, dst
, op
[0], op
[1]);
1372 inst
->saturate
= instr
->dest
.saturate
;
1377 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1380 try_immediate_source(instr
, &op
[1], devinfo
);
1381 inst
= emit_minmax(BRW_CONDITIONAL_GE
, dst
, op
[0], op
[1]);
1382 inst
->saturate
= instr
->dest
.saturate
;
1386 case nir_op_fddx_coarse
:
1387 case nir_op_fddx_fine
:
1389 case nir_op_fddy_coarse
:
1390 case nir_op_fddy_fine
:
1391 unreachable("derivatives are not valid in vertex shaders");
1399 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1404 case nir_op_fne32
: {
1405 enum brw_conditional_mod conditional_mod
=
1406 brw_conditional_for_nir_comparison(instr
->op
);
1408 if (nir_src_bit_size(instr
->src
[0].src
) < 64) {
1409 try_immediate_source(instr
, &op
[1], devinfo
);
1410 emit(CMP(dst
, op
[0], op
[1], conditional_mod
));
1412 /* Produce a 32-bit boolean result from the DF comparison by selecting
1413 * only the low 32-bit in each DF produced. Do this in a temporary
1414 * so we can then move from there to the result using align16 again
1415 * to honor the original writemask.
1417 dst_reg temp
= dst_reg(this, glsl_type::dvec4_type
);
1418 emit(CMP(temp
, op
[0], op
[1], conditional_mod
));
1419 dst_reg result
= dst_reg(this, glsl_type::bvec4_type
);
1420 emit(VEC4_OPCODE_PICK_LOW_32BIT
, result
, src_reg(temp
));
1421 emit(MOV(dst
, src_reg(result
)));
1426 case nir_op_b32all_iequal2
:
1427 case nir_op_b32all_iequal3
:
1428 case nir_op_b32all_iequal4
:
1429 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1431 case nir_op_b32all_fequal2
:
1432 case nir_op_b32all_fequal3
:
1433 case nir_op_b32all_fequal4
: {
1435 brw_swizzle_for_size(nir_op_infos
[instr
->op
].input_sizes
[0]);
1437 emit(CMP(dst_null_d(), swizzle(op
[0], swiz
), swizzle(op
[1], swiz
),
1438 brw_conditional_for_nir_comparison(instr
->op
)));
1439 emit(MOV(dst
, brw_imm_d(0)));
1440 inst
= emit(MOV(dst
, brw_imm_d(~0)));
1441 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1445 case nir_op_b32any_inequal2
:
1446 case nir_op_b32any_inequal3
:
1447 case nir_op_b32any_inequal4
:
1448 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1450 case nir_op_b32any_fnequal2
:
1451 case nir_op_b32any_fnequal3
:
1452 case nir_op_b32any_fnequal4
: {
1454 brw_swizzle_for_size(nir_op_infos
[instr
->op
].input_sizes
[0]);
1456 emit(CMP(dst_null_d(), swizzle(op
[0], swiz
), swizzle(op
[1], swiz
),
1457 brw_conditional_for_nir_comparison(instr
->op
)));
1459 emit(MOV(dst
, brw_imm_d(0)));
1460 inst
= emit(MOV(dst
, brw_imm_d(~0)));
1461 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1466 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1467 if (devinfo
->gen
>= 8) {
1468 op
[0] = resolve_source_modifiers(op
[0]);
1470 emit(NOT(dst
, op
[0]));
1474 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1475 if (devinfo
->gen
>= 8) {
1476 op
[0] = resolve_source_modifiers(op
[0]);
1477 op
[1] = resolve_source_modifiers(op
[1]);
1479 try_immediate_source(instr
, &op
[1], devinfo
);
1480 emit(XOR(dst
, op
[0], op
[1]));
1484 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1485 if (devinfo
->gen
>= 8) {
1486 op
[0] = resolve_source_modifiers(op
[0]);
1487 op
[1] = resolve_source_modifiers(op
[1]);
1489 try_immediate_source(instr
, &op
[1], devinfo
);
1490 emit(OR(dst
, op
[0], op
[1]));
1494 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1495 if (devinfo
->gen
>= 8) {
1496 op
[0] = resolve_source_modifiers(op
[0]);
1497 op
[1] = resolve_source_modifiers(op
[1]);
1499 try_immediate_source(instr
, &op
[1], devinfo
);
1500 emit(AND(dst
, op
[0], op
[1]));
1506 if (nir_dest_bit_size(instr
->dest
.dest
) > 32) {
1507 assert(dst
.type
== BRW_REGISTER_TYPE_DF
);
1508 emit_conversion_to_double(dst
, negate(op
[0]), false);
1510 emit(MOV(dst
, negate(op
[0])));
1515 if (nir_src_bit_size(instr
->src
[0].src
) == 64) {
1516 /* We use a MOV with conditional_mod to check if the provided value is
1517 * 0.0. We want this to flush denormalized numbers to zero, so we set a
1518 * source modifier on the source operand to trigger this, as source
1519 * modifiers don't affect the result of the testing against 0.0.
1521 src_reg value
= op
[0];
1523 vec4_instruction
*inst
= emit(MOV(dst_null_df(), value
));
1524 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1526 src_reg one
= src_reg(this, glsl_type::ivec4_type
);
1527 emit(MOV(dst_reg(one
), brw_imm_d(~0)));
1528 inst
= emit(BRW_OPCODE_SEL
, dst
, one
, brw_imm_d(0));
1529 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1531 emit(CMP(dst
, op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
));
1536 emit(CMP(dst
, op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
));
1539 case nir_op_fnoise1_1
:
1540 case nir_op_fnoise1_2
:
1541 case nir_op_fnoise1_3
:
1542 case nir_op_fnoise1_4
:
1543 case nir_op_fnoise2_1
:
1544 case nir_op_fnoise2_2
:
1545 case nir_op_fnoise2_3
:
1546 case nir_op_fnoise2_4
:
1547 case nir_op_fnoise3_1
:
1548 case nir_op_fnoise3_2
:
1549 case nir_op_fnoise3_3
:
1550 case nir_op_fnoise3_4
:
1551 case nir_op_fnoise4_1
:
1552 case nir_op_fnoise4_2
:
1553 case nir_op_fnoise4_3
:
1554 case nir_op_fnoise4_4
:
1555 unreachable("not reached: should be handled by lower_noise");
1557 case nir_op_unpack_half_2x16_split_x
:
1558 case nir_op_unpack_half_2x16_split_y
:
1559 case nir_op_pack_half_2x16_split
:
1560 unreachable("not reached: should not occur in vertex shader");
1562 case nir_op_unpack_snorm_2x16
:
1563 case nir_op_unpack_unorm_2x16
:
1564 case nir_op_pack_snorm_2x16
:
1565 case nir_op_pack_unorm_2x16
:
1566 unreachable("not reached: should be handled by lower_packing_builtins");
1568 case nir_op_pack_uvec4_to_uint
:
1569 unreachable("not reached");
1571 case nir_op_pack_uvec2_to_uint
: {
1572 dst_reg tmp1
= dst_reg(this, glsl_type::uint_type
);
1573 tmp1
.writemask
= WRITEMASK_X
;
1574 op
[0].swizzle
= BRW_SWIZZLE_YYYY
;
1575 emit(SHL(tmp1
, op
[0], src_reg(brw_imm_ud(16u))));
1577 dst_reg tmp2
= dst_reg(this, glsl_type::uint_type
);
1578 tmp2
.writemask
= WRITEMASK_X
;
1579 op
[0].swizzle
= BRW_SWIZZLE_XXXX
;
1580 emit(AND(tmp2
, op
[0], src_reg(brw_imm_ud(0xffffu
))));
1582 emit(OR(dst
, src_reg(tmp1
), src_reg(tmp2
)));
1586 case nir_op_pack_64_2x32_split
: {
1587 dst_reg result
= dst_reg(this, glsl_type::dvec4_type
);
1588 dst_reg tmp
= dst_reg(this, glsl_type::uvec4_type
);
1589 emit(MOV(tmp
, retype(op
[0], BRW_REGISTER_TYPE_UD
)));
1590 emit(VEC4_OPCODE_SET_LOW_32BIT
, result
, src_reg(tmp
));
1591 emit(MOV(tmp
, retype(op
[1], BRW_REGISTER_TYPE_UD
)));
1592 emit(VEC4_OPCODE_SET_HIGH_32BIT
, result
, src_reg(tmp
));
1593 emit(MOV(dst
, src_reg(result
)));
1597 case nir_op_unpack_64_2x32_split_x
:
1598 case nir_op_unpack_64_2x32_split_y
: {
1599 enum opcode oper
= (instr
->op
== nir_op_unpack_64_2x32_split_x
) ?
1600 VEC4_OPCODE_PICK_LOW_32BIT
: VEC4_OPCODE_PICK_HIGH_32BIT
;
1601 dst_reg tmp
= dst_reg(this, glsl_type::dvec4_type
);
1602 emit(MOV(tmp
, op
[0]));
1603 dst_reg tmp2
= dst_reg(this, glsl_type::uvec4_type
);
1604 emit(oper
, tmp2
, src_reg(tmp
));
1605 emit(MOV(dst
, src_reg(tmp2
)));
1609 case nir_op_unpack_half_2x16
:
1610 /* As NIR does not guarantee that we have a correct swizzle outside the
1611 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1612 * uses the source operand in an operation with WRITEMASK_Y while our
1613 * source operand has only size 1, it accessed incorrect data producing
1614 * regressions in Piglit. We repeat the swizzle of the first component on the
1615 * rest of components to avoid regressions. In the vec4_visitor IR code path
1616 * this is not needed because the operand has already the correct swizzle.
1618 op
[0].swizzle
= brw_compose_swizzle(BRW_SWIZZLE_XXXX
, op
[0].swizzle
);
1619 emit_unpack_half_2x16(dst
, op
[0]);
1622 case nir_op_pack_half_2x16
:
1623 emit_pack_half_2x16(dst
, op
[0]);
1626 case nir_op_unpack_unorm_4x8
:
1627 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1628 emit_unpack_unorm_4x8(dst
, op
[0]);
1631 case nir_op_pack_unorm_4x8
:
1632 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1633 emit_pack_unorm_4x8(dst
, op
[0]);
1636 case nir_op_unpack_snorm_4x8
:
1637 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1638 emit_unpack_snorm_4x8(dst
, op
[0]);
1641 case nir_op_pack_snorm_4x8
:
1642 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1643 emit_pack_snorm_4x8(dst
, op
[0]);
1646 case nir_op_bitfield_reverse
:
1647 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1648 emit(BFREV(dst
, op
[0]));
1651 case nir_op_bit_count
:
1652 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1653 emit(CBIT(dst
, op
[0]));
1656 case nir_op_ufind_msb
:
1657 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1658 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst
, op
[0], false);
1661 case nir_op_ifind_msb
: {
1662 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1663 vec4_builder bld
= vec4_builder(this).at_end();
1666 if (devinfo
->gen
< 7) {
1667 emit_find_msb_using_lzd(bld
, dst
, op
[0], true);
1669 emit(FBH(retype(dst
, BRW_REGISTER_TYPE_UD
), op
[0]));
1671 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1672 * count from the LSB side. If FBH didn't return an error
1673 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1674 * count into an LSB count.
1676 bld
.CMP(dst_null_d(), src
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1678 inst
= bld
.ADD(dst
, src
, brw_imm_d(31));
1679 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1680 inst
->src
[0].negate
= true;
1685 case nir_op_find_lsb
: {
1686 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1687 vec4_builder bld
= vec4_builder(this).at_end();
1689 if (devinfo
->gen
< 7) {
1690 dst_reg temp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1692 /* (x & -x) generates a value that consists of only the LSB of x.
1693 * For all powers of 2, findMSB(y) == findLSB(y).
1695 src_reg src
= src_reg(retype(op
[0], BRW_REGISTER_TYPE_D
));
1696 src_reg negated_src
= src
;
1698 /* One must be negated, and the other must be non-negated. It
1699 * doesn't matter which is which.
1701 negated_src
.negate
= true;
1704 bld
.AND(temp
, src
, negated_src
);
1705 emit_find_msb_using_lzd(bld
, dst
, src_reg(temp
), false);
1707 bld
.FBL(dst
, op
[0]);
1712 case nir_op_ubitfield_extract
:
1713 case nir_op_ibitfield_extract
:
1714 unreachable("should have been lowered");
1717 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1718 op
[0] = fix_3src_operand(op
[0]);
1719 op
[1] = fix_3src_operand(op
[1]);
1720 op
[2] = fix_3src_operand(op
[2]);
1722 emit(BFE(dst
, op
[2], op
[1], op
[0]));
1726 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1727 emit(BFI1(dst
, op
[0], op
[1]));
1731 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1732 op
[0] = fix_3src_operand(op
[0]);
1733 op
[1] = fix_3src_operand(op
[1]);
1734 op
[2] = fix_3src_operand(op
[2]);
1736 emit(BFI2(dst
, op
[0], op
[1], op
[2]));
1739 case nir_op_bitfield_insert
:
1740 unreachable("not reached: should have been lowered");
1743 assert(!instr
->dest
.saturate
);
1745 /* Straightforward since the source can be assumed to be either
1746 * strictly >= 0 or strictly <= 0 depending on the setting of the
1749 inst
= emit(MOV(dst
, op
[0]));
1750 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1752 inst
= (op
[0].negate
)
1753 ? emit(MOV(dst
, brw_imm_f(-1.0f
)))
1754 : emit(MOV(dst
, brw_imm_f(1.0f
)));
1755 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1756 } else if (type_sz(op
[0].type
) < 8) {
1757 /* AND(val, 0x80000000) gives the sign bit.
1759 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1762 emit(CMP(dst_null_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
));
1764 op
[0].type
= BRW_REGISTER_TYPE_UD
;
1765 dst
.type
= BRW_REGISTER_TYPE_UD
;
1766 emit(AND(dst
, op
[0], brw_imm_ud(0x80000000u
)));
1768 inst
= emit(OR(dst
, src_reg(dst
), brw_imm_ud(0x3f800000u
)));
1769 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1770 dst
.type
= BRW_REGISTER_TYPE_F
;
1772 /* For doubles we do the same but we need to consider:
1774 * - We use a MOV with conditional_mod instead of a CMP so that we can
1775 * skip loading a 0.0 immediate. We use a source modifier on the
1776 * source of the MOV so that we flush denormalized values to 0.
1777 * Since we want to compare against 0, this won't alter the result.
1778 * - We need to extract the high 32-bit of each DF where the sign
1780 * - We need to produce a DF result.
1783 /* Check for zero */
1784 src_reg value
= op
[0];
1786 inst
= emit(MOV(dst_null_df(), value
));
1787 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1789 /* AND each high 32-bit channel with 0x80000000u */
1790 dst_reg tmp
= dst_reg(this, glsl_type::uvec4_type
);
1791 emit(VEC4_OPCODE_PICK_HIGH_32BIT
, tmp
, op
[0]);
1792 emit(AND(tmp
, src_reg(tmp
), brw_imm_ud(0x80000000u
)));
1794 /* Add 1.0 to each channel, predicated to skip the cases where the
1795 * channel's value was 0
1797 inst
= emit(OR(tmp
, src_reg(tmp
), brw_imm_ud(0x3f800000u
)));
1798 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1800 /* Now convert the result from float to double */
1801 emit_conversion_to_double(dst
, retype(src_reg(tmp
),
1802 BRW_REGISTER_TYPE_F
),
1808 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1809 try_immediate_source(instr
, &op
[1], devinfo
);
1810 emit(SHL(dst
, op
[0], op
[1]));
1814 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1815 try_immediate_source(instr
, &op
[1], devinfo
);
1816 emit(ASR(dst
, op
[0], op
[1]));
1820 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1821 try_immediate_source(instr
, &op
[1], devinfo
);
1822 emit(SHR(dst
, op
[0], op
[1]));
1826 if (type_sz(dst
.type
) == 8) {
1827 dst_reg mul_dst
= dst_reg(this, glsl_type::dvec4_type
);
1828 emit(MUL(mul_dst
, op
[1], op
[0]));
1829 inst
= emit(ADD(dst
, src_reg(mul_dst
), op
[2]));
1830 inst
->saturate
= instr
->dest
.saturate
;
1832 op
[0] = fix_3src_operand(op
[0]);
1833 op
[1] = fix_3src_operand(op
[1]);
1834 op
[2] = fix_3src_operand(op
[2]);
1836 inst
= emit(MAD(dst
, op
[2], op
[1], op
[0]));
1837 inst
->saturate
= instr
->dest
.saturate
;
1842 inst
= emit_lrp(dst
, op
[0], op
[1], op
[2]);
1843 inst
->saturate
= instr
->dest
.saturate
;
1846 case nir_op_b32csel
:
1847 enum brw_predicate predicate
;
1848 if (!optimize_predicate(instr
, &predicate
)) {
1849 emit(CMP(dst_null_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
));
1850 switch (dst
.writemask
) {
1852 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_X
;
1855 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_Y
;
1858 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_Z
;
1861 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_W
;
1864 predicate
= BRW_PREDICATE_NORMAL
;
1868 inst
= emit(BRW_OPCODE_SEL
, dst
, op
[1], op
[2]);
1869 inst
->predicate
= predicate
;
1872 case nir_op_fdot_replicated2
:
1873 inst
= emit(BRW_OPCODE_DP2
, dst
, op
[0], op
[1]);
1874 inst
->saturate
= instr
->dest
.saturate
;
1877 case nir_op_fdot_replicated3
:
1878 inst
= emit(BRW_OPCODE_DP3
, dst
, op
[0], op
[1]);
1879 inst
->saturate
= instr
->dest
.saturate
;
1882 case nir_op_fdot_replicated4
:
1883 inst
= emit(BRW_OPCODE_DP4
, dst
, op
[0], op
[1]);
1884 inst
->saturate
= instr
->dest
.saturate
;
1887 case nir_op_fdph_replicated
:
1888 inst
= emit(BRW_OPCODE_DPH
, dst
, op
[0], op
[1]);
1889 inst
->saturate
= instr
->dest
.saturate
;
1894 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1899 unreachable("not reached: should be lowered by lower_source mods");
1902 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1905 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1909 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1912 unreachable("Unimplemented ALU operation");
1915 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1916 * to sign extend the low bit to 0/~0
1918 if (devinfo
->gen
<= 5 &&
1919 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) ==
1920 BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1921 dst_reg masked
= dst_reg(this, glsl_type::int_type
);
1922 masked
.writemask
= dst
.writemask
;
1923 emit(AND(masked
, src_reg(dst
), brw_imm_d(1)));
1924 src_reg masked_neg
= src_reg(masked
);
1925 masked_neg
.negate
= true;
1926 emit(MOV(retype(dst
, BRW_REGISTER_TYPE_D
), masked_neg
));
1931 vec4_visitor::nir_emit_jump(nir_jump_instr
*instr
)
1933 switch (instr
->type
) {
1934 case nir_jump_break
:
1935 emit(BRW_OPCODE_BREAK
);
1938 case nir_jump_continue
:
1939 emit(BRW_OPCODE_CONTINUE
);
1942 case nir_jump_return
:
1945 unreachable("unknown jump");
1949 static enum ir_texture_opcode
1950 ir_texture_opcode_for_nir_texop(nir_texop texop
)
1952 enum ir_texture_opcode op
;
1955 case nir_texop_lod
: op
= ir_lod
; break;
1956 case nir_texop_query_levels
: op
= ir_query_levels
; break;
1957 case nir_texop_texture_samples
: op
= ir_texture_samples
; break;
1958 case nir_texop_tex
: op
= ir_tex
; break;
1959 case nir_texop_tg4
: op
= ir_tg4
; break;
1960 case nir_texop_txb
: op
= ir_txb
; break;
1961 case nir_texop_txd
: op
= ir_txd
; break;
1962 case nir_texop_txf
: op
= ir_txf
; break;
1963 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
1964 case nir_texop_txl
: op
= ir_txl
; break;
1965 case nir_texop_txs
: op
= ir_txs
; break;
1966 case nir_texop_samples_identical
: op
= ir_samples_identical
; break;
1968 unreachable("unknown texture opcode");
1974 static const glsl_type
*
1975 glsl_type_for_nir_alu_type(nir_alu_type alu_type
,
1976 unsigned components
)
1978 return glsl_type::get_instance(brw_glsl_base_type_for_nir_type(alu_type
),
1983 vec4_visitor::nir_emit_texture(nir_tex_instr
*instr
)
1985 unsigned texture
= instr
->texture_index
;
1986 unsigned sampler
= instr
->sampler_index
;
1987 src_reg texture_reg
= brw_imm_ud(texture
);
1988 src_reg sampler_reg
= brw_imm_ud(sampler
);
1990 const glsl_type
*coord_type
= NULL
;
1991 src_reg shadow_comparator
;
1992 src_reg offset_value
;
1994 src_reg sample_index
;
1997 const glsl_type
*dest_type
=
1998 glsl_type_for_nir_alu_type(instr
->dest_type
,
1999 nir_tex_instr_dest_size(instr
));
2000 dst_reg dest
= get_nir_dest(instr
->dest
, instr
->dest_type
);
2002 /* The hardware requires a LOD for buffer textures */
2003 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
2006 /* Load the texture operation sources */
2007 uint32_t constant_offset
= 0;
2008 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
2009 switch (instr
->src
[i
].src_type
) {
2010 case nir_tex_src_comparator
:
2011 shadow_comparator
= get_nir_src(instr
->src
[i
].src
,
2012 BRW_REGISTER_TYPE_F
, 1);
2015 case nir_tex_src_coord
: {
2016 unsigned src_size
= nir_tex_instr_src_size(instr
, i
);
2018 switch (instr
->op
) {
2020 case nir_texop_txf_ms
:
2021 case nir_texop_samples_identical
:
2022 coordinate
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
,
2024 coord_type
= glsl_type::ivec(src_size
);
2028 coordinate
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
2030 coord_type
= glsl_type::vec(src_size
);
2036 case nir_tex_src_ddx
:
2037 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
2038 nir_tex_instr_src_size(instr
, i
));
2041 case nir_tex_src_ddy
:
2042 lod2
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
2043 nir_tex_instr_src_size(instr
, i
));
2046 case nir_tex_src_lod
:
2047 switch (instr
->op
) {
2050 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 1);
2054 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
, 1);
2059 case nir_tex_src_ms_index
: {
2060 sample_index
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 1);
2064 case nir_tex_src_offset
:
2065 if (!brw_texture_offset(instr
, i
, &constant_offset
)) {
2067 get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 2);
2071 case nir_tex_src_texture_offset
: {
2072 /* Emit code to evaluate the actual indexing expression */
2073 src_reg src
= get_nir_src(instr
->src
[i
].src
, 1);
2074 src_reg
temp(this, glsl_type::uint_type
);
2075 emit(ADD(dst_reg(temp
), src
, brw_imm_ud(texture
)));
2076 texture_reg
= emit_uniformize(temp
);
2080 case nir_tex_src_sampler_offset
: {
2081 /* Emit code to evaluate the actual indexing expression */
2082 src_reg src
= get_nir_src(instr
->src
[i
].src
, 1);
2083 src_reg
temp(this, glsl_type::uint_type
);
2084 emit(ADD(dst_reg(temp
), src
, brw_imm_ud(sampler
)));
2085 sampler_reg
= emit_uniformize(temp
);
2089 case nir_tex_src_projector
:
2090 unreachable("Should be lowered by do_lower_texture_projection");
2092 case nir_tex_src_bias
:
2093 unreachable("LOD bias is not valid for vertex shaders.\n");
2096 unreachable("unknown texture source");
2100 if (instr
->op
== nir_texop_txf_ms
||
2101 instr
->op
== nir_texop_samples_identical
) {
2102 assert(coord_type
!= NULL
);
2103 if (devinfo
->gen
>= 7 &&
2104 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
2105 mcs
= emit_mcs_fetch(coord_type
, coordinate
, texture_reg
);
2107 mcs
= brw_imm_ud(0u);
2111 /* Stuff the channel select bits in the top of the texture offset */
2112 if (instr
->op
== nir_texop_tg4
) {
2113 if (instr
->component
== 1 &&
2114 (key_tex
->gather_channel_quirk_mask
& (1 << texture
))) {
2115 /* gather4 sampler is broken for green channel on RG32F --
2116 * we must ask for blue instead.
2118 constant_offset
|= 2 << 16;
2120 constant_offset
|= instr
->component
<< 16;
2124 ir_texture_opcode op
= ir_texture_opcode_for_nir_texop(instr
->op
);
2126 emit_texture(op
, dest
, dest_type
, coordinate
, instr
->coord_components
,
2128 lod
, lod2
, sample_index
,
2129 constant_offset
, offset_value
, mcs
,
2130 texture
, texture_reg
, sampler_reg
);
2134 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr
*instr
)
2136 nir_ssa_values
[instr
->def
.index
] =
2137 dst_reg(VGRF
, alloc
.allocate(DIV_ROUND_UP(instr
->def
.bit_size
, 32)));
2140 /* SIMD4x2 64bit data is stored in register space like this:
2142 * r0.0:DF x0 y0 z0 w0
2143 * r1.0:DF x1 y1 z1 w1
2145 * When we need to write data such as this to memory using 32-bit write
2146 * messages we need to shuffle it in this fashion:
2148 * r0.0:DF x0 y0 x1 y1 (to be written at base offset)
2149 * r0.0:DF z0 w0 z1 w1 (to be written at base offset + 16)
2151 * We need to do the inverse operation when we read using 32-bit messages,
2152 * which we can do by applying the same exact shuffling on the 64-bit data
2153 * read, only that because the data for each vertex is positioned differently
2154 * we need to apply different channel enables.
2156 * This function takes 64bit data and shuffles it as explained above.
2158 * The @for_write parameter is used to specify if the shuffling is being done
2159 * for proper SIMD4x2 64-bit data that needs to be shuffled prior to a 32-bit
2160 * write message (for_write = true), or instead we are doing the inverse
2161 * operation and we have just read 64-bit data using a 32-bit messages that we
2162 * need to shuffle to create valid SIMD4x2 64-bit data (for_write = false).
2164 * If @block and @ref are non-NULL, then the shuffling is done after @ref,
2165 * otherwise the instructions are emitted normally at the end. The function
2166 * returns the last instruction inserted.
2168 * Notice that @src and @dst cannot be the same register.
2171 vec4_visitor::shuffle_64bit_data(dst_reg dst
, src_reg src
, bool for_write
,
2172 bblock_t
*block
, vec4_instruction
*ref
)
2174 assert(type_sz(src
.type
) == 8);
2175 assert(type_sz(dst
.type
) == 8);
2176 assert(!regions_overlap(dst
, 2 * REG_SIZE
, src
, 2 * REG_SIZE
));
2177 assert(!ref
== !block
);
2179 const vec4_builder bld
= !ref
? vec4_builder(this).at_end() :
2180 vec4_builder(this).at(block
, ref
->next
);
2182 /* Resolve swizzle in src */
2183 vec4_instruction
*inst
;
2184 if (src
.swizzle
!= BRW_SWIZZLE_XYZW
) {
2185 dst_reg data
= dst_reg(this, glsl_type::dvec4_type
);
2186 inst
= bld
.MOV(data
, src
);
2187 src
= src_reg(data
);
2190 /* dst+0.XY = src+0.XY */
2191 inst
= bld
.group(4, 0).MOV(writemask(dst
, WRITEMASK_XY
), src
);
2193 /* dst+0.ZW = src+1.XY */
2194 inst
= bld
.group(4, for_write
? 1 : 0)
2195 .MOV(writemask(dst
, WRITEMASK_ZW
),
2196 swizzle(byte_offset(src
, REG_SIZE
), BRW_SWIZZLE_XYXY
));
2198 /* dst+1.XY = src+0.ZW */
2199 inst
= bld
.group(4, for_write
? 0 : 1)
2200 .MOV(writemask(byte_offset(dst
, REG_SIZE
), WRITEMASK_XY
),
2201 swizzle(src
, BRW_SWIZZLE_ZWZW
));
2203 /* dst+1.ZW = src+1.ZW */
2204 inst
= bld
.group(4, 1)
2205 .MOV(writemask(byte_offset(dst
, REG_SIZE
), WRITEMASK_ZW
),
2206 byte_offset(src
, REG_SIZE
));