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"
28 #include "brw_program.h"
31 using namespace brw::surface_access
;
36 vec4_visitor::emit_nir_code()
38 if (nir
->num_uniforms
> 0)
41 nir_setup_system_values();
43 /* get the main function and emit it */
44 nir_foreach_function(function
, nir
) {
45 assert(strcmp(function
->name
, "main") == 0);
46 assert(function
->impl
);
47 nir_emit_impl(function
->impl
);
52 vec4_visitor::nir_setup_system_value_intrinsic(nir_intrinsic_instr
*instr
)
56 switch (instr
->intrinsic
) {
57 case nir_intrinsic_load_vertex_id
:
58 unreachable("should be lowered by lower_vertex_id().");
60 case nir_intrinsic_load_vertex_id_zero_base
:
61 reg
= &nir_system_values
[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
];
62 if (reg
->file
== BAD_FILE
)
63 *reg
= *make_reg_for_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
);
66 case nir_intrinsic_load_base_vertex
:
67 reg
= &nir_system_values
[SYSTEM_VALUE_BASE_VERTEX
];
68 if (reg
->file
== BAD_FILE
)
69 *reg
= *make_reg_for_system_value(SYSTEM_VALUE_BASE_VERTEX
);
72 case nir_intrinsic_load_instance_id
:
73 reg
= &nir_system_values
[SYSTEM_VALUE_INSTANCE_ID
];
74 if (reg
->file
== BAD_FILE
)
75 *reg
= *make_reg_for_system_value(SYSTEM_VALUE_INSTANCE_ID
);
78 case nir_intrinsic_load_base_instance
:
79 reg
= &nir_system_values
[SYSTEM_VALUE_BASE_INSTANCE
];
80 if (reg
->file
== BAD_FILE
)
81 *reg
= *make_reg_for_system_value(SYSTEM_VALUE_BASE_INSTANCE
);
84 case nir_intrinsic_load_draw_id
:
85 reg
= &nir_system_values
[SYSTEM_VALUE_DRAW_ID
];
86 if (reg
->file
== BAD_FILE
)
87 *reg
= *make_reg_for_system_value(SYSTEM_VALUE_DRAW_ID
);
96 setup_system_values_block(nir_block
*block
, vec4_visitor
*v
)
98 nir_foreach_instr(instr
, block
) {
99 if (instr
->type
!= nir_instr_type_intrinsic
)
102 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
103 v
->nir_setup_system_value_intrinsic(intrin
);
110 vec4_visitor::nir_setup_system_values()
112 nir_system_values
= ralloc_array(mem_ctx
, dst_reg
, SYSTEM_VALUE_MAX
);
113 for (unsigned i
= 0; i
< SYSTEM_VALUE_MAX
; i
++) {
114 nir_system_values
[i
] = dst_reg();
117 nir_foreach_function(function
, nir
) {
118 assert(strcmp(function
->name
, "main") == 0);
119 assert(function
->impl
);
120 nir_foreach_block(block
, function
->impl
) {
121 setup_system_values_block(block
, this);
127 vec4_visitor::nir_setup_uniforms()
129 uniforms
= nir
->num_uniforms
/ 16;
133 vec4_visitor::nir_emit_impl(nir_function_impl
*impl
)
135 nir_locals
= ralloc_array(mem_ctx
, dst_reg
, impl
->reg_alloc
);
136 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
137 nir_locals
[i
] = dst_reg();
140 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
141 unsigned array_elems
=
142 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
144 nir_locals
[reg
->index
] = dst_reg(VGRF
, alloc
.allocate(array_elems
));
147 nir_ssa_values
= ralloc_array(mem_ctx
, dst_reg
, impl
->ssa_alloc
);
149 nir_emit_cf_list(&impl
->body
);
153 vec4_visitor::nir_emit_cf_list(exec_list
*list
)
155 exec_list_validate(list
);
156 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
157 switch (node
->type
) {
159 nir_emit_if(nir_cf_node_as_if(node
));
162 case nir_cf_node_loop
:
163 nir_emit_loop(nir_cf_node_as_loop(node
));
166 case nir_cf_node_block
:
167 nir_emit_block(nir_cf_node_as_block(node
));
171 unreachable("Invalid CFG node block");
177 vec4_visitor::nir_emit_if(nir_if
*if_stmt
)
179 /* First, put the condition in f0 */
180 src_reg condition
= get_nir_src(if_stmt
->condition
, BRW_REGISTER_TYPE_D
, 1);
181 vec4_instruction
*inst
= emit(MOV(dst_null_d(), condition
));
182 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
184 /* We can just predicate based on the X channel, as the condition only
185 * goes on its own line */
186 emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X
));
188 nir_emit_cf_list(&if_stmt
->then_list
);
190 /* note: if the else is empty, dead CF elimination will remove it */
191 emit(BRW_OPCODE_ELSE
);
193 nir_emit_cf_list(&if_stmt
->else_list
);
195 emit(BRW_OPCODE_ENDIF
);
199 vec4_visitor::nir_emit_loop(nir_loop
*loop
)
203 nir_emit_cf_list(&loop
->body
);
205 emit(BRW_OPCODE_WHILE
);
209 vec4_visitor::nir_emit_block(nir_block
*block
)
211 nir_foreach_instr(instr
, block
) {
212 nir_emit_instr(instr
);
217 vec4_visitor::nir_emit_instr(nir_instr
*instr
)
221 switch (instr
->type
) {
222 case nir_instr_type_load_const
:
223 nir_emit_load_const(nir_instr_as_load_const(instr
));
226 case nir_instr_type_intrinsic
:
227 nir_emit_intrinsic(nir_instr_as_intrinsic(instr
));
230 case nir_instr_type_alu
:
231 nir_emit_alu(nir_instr_as_alu(instr
));
234 case nir_instr_type_jump
:
235 nir_emit_jump(nir_instr_as_jump(instr
));
238 case nir_instr_type_tex
:
239 nir_emit_texture(nir_instr_as_tex(instr
));
242 case nir_instr_type_ssa_undef
:
243 nir_emit_undef(nir_instr_as_ssa_undef(instr
));
247 fprintf(stderr
, "VS instruction not yet implemented by NIR->vec4\n");
253 dst_reg_for_nir_reg(vec4_visitor
*v
, nir_register
*nir_reg
,
254 unsigned base_offset
, nir_src
*indirect
)
258 reg
= v
->nir_locals
[nir_reg
->index
];
259 reg
= offset(reg
, base_offset
);
262 new(v
->mem_ctx
) src_reg(v
->get_nir_src(*indirect
,
270 vec4_visitor::get_nir_dest(const nir_dest
&dest
)
273 dst_reg dst
= dst_reg(VGRF
, alloc
.allocate(1));
274 nir_ssa_values
[dest
.ssa
.index
] = dst
;
277 return dst_reg_for_nir_reg(this, dest
.reg
.reg
, dest
.reg
.base_offset
,
283 vec4_visitor::get_nir_dest(const nir_dest
&dest
, enum brw_reg_type type
)
285 return retype(get_nir_dest(dest
), type
);
289 vec4_visitor::get_nir_dest(const nir_dest
&dest
, nir_alu_type type
)
291 return get_nir_dest(dest
, brw_type_for_nir_type(type
));
295 vec4_visitor::get_nir_src(const nir_src
&src
, enum brw_reg_type type
,
296 unsigned num_components
)
301 assert(src
.ssa
!= NULL
);
302 reg
= nir_ssa_values
[src
.ssa
->index
];
305 reg
= dst_reg_for_nir_reg(this, src
.reg
.reg
, src
.reg
.base_offset
,
309 reg
= retype(reg
, type
);
311 src_reg reg_as_src
= src_reg(reg
);
312 reg_as_src
.swizzle
= brw_swizzle_for_size(num_components
);
317 vec4_visitor::get_nir_src(const nir_src
&src
, nir_alu_type type
,
318 unsigned num_components
)
320 return get_nir_src(src
, brw_type_for_nir_type(type
), num_components
);
324 vec4_visitor::get_nir_src(const nir_src
&src
, unsigned num_components
)
326 /* if type is not specified, default to signed int */
327 return get_nir_src(src
, nir_type_int
, num_components
);
331 vec4_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
333 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
334 nir_const_value
*const_value
= nir_src_as_const_value(*offset_src
);
337 /* The only constant offset we should find is 0. brw_nir.c's
338 * add_const_offset_to_base() will fold other constant offsets
339 * into instr->const_index[0].
341 assert(const_value
->u32
[0] == 0);
345 return get_nir_src(*offset_src
, BRW_REGISTER_TYPE_UD
, 1);
349 vec4_visitor::nir_emit_load_const(nir_load_const_instr
*instr
)
351 dst_reg reg
= dst_reg(VGRF
, alloc
.allocate(1));
352 reg
.type
= BRW_REGISTER_TYPE_D
;
354 unsigned remaining
= brw_writemask_for_size(instr
->def
.num_components
);
356 /* @FIXME: consider emitting vector operations to save some MOVs in
357 * cases where the components are representable in 8 bits.
358 * For now, we emit a MOV for each distinct value.
360 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
361 unsigned writemask
= 1 << i
;
363 if ((remaining
& writemask
) == 0)
366 for (unsigned j
= i
; j
< instr
->def
.num_components
; j
++) {
367 if (instr
->value
.u32
[i
] == instr
->value
.u32
[j
]) {
372 reg
.writemask
= writemask
;
373 emit(MOV(reg
, brw_imm_d(instr
->value
.i32
[i
])));
375 remaining
&= ~writemask
;
378 /* Set final writemask */
379 reg
.writemask
= brw_writemask_for_size(instr
->def
.num_components
);
381 nir_ssa_values
[instr
->def
.index
] = reg
;
385 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr
*instr
)
390 switch (instr
->intrinsic
) {
392 case nir_intrinsic_load_input
: {
393 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
395 /* We set EmitNoIndirectInput for VS */
396 assert(const_offset
);
398 src
= src_reg(ATTR
, instr
->const_index
[0] + const_offset
->u32
[0],
399 glsl_type::uvec4_type
);
400 /* Swizzle source based on component layout qualifier */
401 src
.swizzle
= BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr
));
403 dest
= get_nir_dest(instr
->dest
, src
.type
);
404 dest
.writemask
= brw_writemask_for_size(instr
->num_components
);
406 emit(MOV(dest
, src
));
410 case nir_intrinsic_store_output
: {
411 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
412 assert(const_offset
);
414 int varying
= instr
->const_index
[0] + const_offset
->u32
[0];
416 src
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_F
,
417 instr
->num_components
);
419 if (varying
>= VARYING_SLOT_VAR0
) {
420 unsigned c
= nir_intrinsic_component(instr
);
421 unsigned v
= varying
- VARYING_SLOT_VAR0
;
422 output_generic_reg
[v
][c
] = dst_reg(src
);
423 output_generic_num_components
[v
][c
] = instr
->num_components
;
425 output_reg
[varying
] = dst_reg(src
);
430 case nir_intrinsic_get_buffer_size
: {
431 nir_const_value
*const_uniform_block
= nir_src_as_const_value(instr
->src
[0]);
432 unsigned ssbo_index
= const_uniform_block
? const_uniform_block
->u32
[0] : 0;
434 const unsigned index
=
435 prog_data
->base
.binding_table
.ssbo_start
+ ssbo_index
;
436 dst_reg result_dst
= get_nir_dest(instr
->dest
);
437 vec4_instruction
*inst
= new(mem_ctx
)
438 vec4_instruction(VS_OPCODE_GET_BUFFER_SIZE
, result_dst
);
441 inst
->mlen
= 1; /* always at least one */
442 inst
->src
[1] = brw_imm_ud(index
);
444 /* MRF for the first parameter */
445 src_reg lod
= brw_imm_d(0);
446 int param_base
= inst
->base_mrf
;
447 int writemask
= WRITEMASK_X
;
448 emit(MOV(dst_reg(MRF
, param_base
, glsl_type::int_type
, writemask
), lod
));
452 brw_mark_surface_used(&prog_data
->base
, index
);
456 case nir_intrinsic_store_ssbo
: {
457 assert(devinfo
->gen
>= 7);
461 nir_const_value
*const_uniform_block
=
462 nir_src_as_const_value(instr
->src
[1]);
463 if (const_uniform_block
) {
464 unsigned index
= prog_data
->base
.binding_table
.ssbo_start
+
465 const_uniform_block
->u32
[0];
466 surf_index
= brw_imm_ud(index
);
467 brw_mark_surface_used(&prog_data
->base
, index
);
469 surf_index
= src_reg(this, glsl_type::uint_type
);
470 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[1], 1),
471 brw_imm_ud(prog_data
->base
.binding_table
.ssbo_start
)));
472 surf_index
= emit_uniformize(surf_index
);
474 brw_mark_surface_used(&prog_data
->base
,
475 prog_data
->base
.binding_table
.ssbo_start
+
476 nir
->info
.num_ssbos
- 1);
481 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[2]);
483 offset_reg
= brw_imm_ud(const_offset
->u32
[0]);
485 offset_reg
= get_nir_src(instr
->src
[2], 1);
489 src_reg val_reg
= get_nir_src(instr
->src
[0], 4);
492 unsigned write_mask
= instr
->const_index
[0];
494 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
495 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
496 * typed and untyped messages and across hardware platforms, the
497 * current implementation of the untyped messages will transparently convert
498 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
499 * and enabling only channel X on the SEND instruction.
501 * The above, works well for full vector writes, but not for partial writes
502 * where we want to write some channels and not others, like when we have
503 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
504 * quite restrictive with regards to the channel enables we can configure in
505 * the message descriptor (not all combinations are allowed) we cannot simply
506 * implement these scenarios with a single message while keeping the
507 * aforementioned symmetry in the implementation. For now we de decided that
508 * it is better to keep the symmetry to reduce complexity, so in situations
509 * such as the one described we end up emitting two untyped write messages
510 * (one for xy and another for w).
512 * The code below packs consecutive channels into a single write message,
513 * detects gaps in the vector write and if needed, sends a second message
514 * with the remaining channels. If in the future we decide that we want to
515 * emit a single message at the expense of losing the symmetry in the
516 * implementation we can:
518 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
519 * message payload. In this mode we can write up to 8 offsets and dwords
520 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
521 * and select which of the 8 channels carry data to write by setting the
522 * appropriate writemask in the dst register of the SEND instruction.
523 * It would require to write a new generator opcode specifically for
524 * IvyBridge since we would need to prepare a SIMD8 payload that could
525 * use any channel, not just X.
527 * 2) For Haswell+: Simply send a single write message but set the writemask
528 * on the dst of the SEND instruction to select the channels we want to
529 * write. It would require to modify the current messages to receive
530 * and honor the writemask provided.
532 const vec4_builder bld
= vec4_builder(this).at_end()
533 .annotate(current_annotation
, base_ir
);
535 int swizzle
[4] = { 0, 0, 0, 0};
536 int num_channels
= 0;
537 unsigned skipped_channels
= 0;
538 int num_components
= instr
->num_components
;
539 for (int i
= 0; i
< num_components
; i
++) {
540 /* Check if this channel needs to be written. If so, record the
541 * channel we need to take the data from in the swizzle array
543 int component_mask
= 1 << i
;
544 int write_test
= write_mask
& component_mask
;
546 swizzle
[num_channels
++] = i
;
548 /* If we don't have to write this channel it means we have a gap in the
549 * vector, so write the channels we accumulated until now, if any. Do
550 * the same if this was the last component in the vector.
552 if (!write_test
|| i
== num_components
- 1) {
553 if (num_channels
> 0) {
554 /* We have channels to write, so update the offset we need to
555 * write at to skip the channels we skipped, if any.
557 if (skipped_channels
> 0) {
558 if (offset_reg
.file
== IMM
) {
559 offset_reg
.ud
+= 4 * skipped_channels
;
561 emit(ADD(dst_reg(offset_reg
), offset_reg
,
562 brw_imm_ud(4 * skipped_channels
)));
566 /* Swizzle the data register so we take the data from the channels
567 * we need to write and send the write message. This will write
568 * num_channels consecutive dwords starting at offset.
571 BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
572 emit_untyped_write(bld
, surf_index
, offset_reg
, val_reg
,
573 1 /* dims */, num_channels
/* size */,
576 /* If we have to do a second write we will have to update the
577 * offset so that we jump over the channels we have just written
580 skipped_channels
= num_channels
;
582 /* Restart the count for the next write message */
586 /* We did not write the current channel, so increase skipped count */
594 case nir_intrinsic_load_ssbo
: {
595 assert(devinfo
->gen
>= 7);
597 nir_const_value
*const_uniform_block
=
598 nir_src_as_const_value(instr
->src
[0]);
601 if (const_uniform_block
) {
602 unsigned index
= prog_data
->base
.binding_table
.ssbo_start
+
603 const_uniform_block
->u32
[0];
604 surf_index
= brw_imm_ud(index
);
606 brw_mark_surface_used(&prog_data
->base
, index
);
608 surf_index
= src_reg(this, glsl_type::uint_type
);
609 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[0], 1),
610 brw_imm_ud(prog_data
->base
.binding_table
.ssbo_start
)));
611 surf_index
= emit_uniformize(surf_index
);
613 /* Assume this may touch any UBO. It would be nice to provide
614 * a tighter bound, but the array information is already lowered away.
616 brw_mark_surface_used(&prog_data
->base
,
617 prog_data
->base
.binding_table
.ssbo_start
+
618 nir
->info
.num_ssbos
- 1);
622 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
624 offset_reg
= brw_imm_ud(const_offset
->u32
[0]);
626 offset_reg
= get_nir_src(instr
->src
[1], 1);
629 /* Read the vector */
630 const vec4_builder bld
= vec4_builder(this).at_end()
631 .annotate(current_annotation
, base_ir
);
633 src_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
634 1 /* dims */, 4 /* size*/,
636 dst_reg dest
= get_nir_dest(instr
->dest
);
637 read_result
.type
= dest
.type
;
638 read_result
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
639 emit(MOV(dest
, read_result
));
644 case nir_intrinsic_ssbo_atomic_add
:
645 nir_emit_ssbo_atomic(BRW_AOP_ADD
, instr
);
647 case nir_intrinsic_ssbo_atomic_imin
:
648 nir_emit_ssbo_atomic(BRW_AOP_IMIN
, instr
);
650 case nir_intrinsic_ssbo_atomic_umin
:
651 nir_emit_ssbo_atomic(BRW_AOP_UMIN
, instr
);
653 case nir_intrinsic_ssbo_atomic_imax
:
654 nir_emit_ssbo_atomic(BRW_AOP_IMAX
, instr
);
656 case nir_intrinsic_ssbo_atomic_umax
:
657 nir_emit_ssbo_atomic(BRW_AOP_UMAX
, instr
);
659 case nir_intrinsic_ssbo_atomic_and
:
660 nir_emit_ssbo_atomic(BRW_AOP_AND
, instr
);
662 case nir_intrinsic_ssbo_atomic_or
:
663 nir_emit_ssbo_atomic(BRW_AOP_OR
, instr
);
665 case nir_intrinsic_ssbo_atomic_xor
:
666 nir_emit_ssbo_atomic(BRW_AOP_XOR
, instr
);
668 case nir_intrinsic_ssbo_atomic_exchange
:
669 nir_emit_ssbo_atomic(BRW_AOP_MOV
, instr
);
671 case nir_intrinsic_ssbo_atomic_comp_swap
:
672 nir_emit_ssbo_atomic(BRW_AOP_CMPWR
, instr
);
675 case nir_intrinsic_load_vertex_id
:
676 unreachable("should be lowered by lower_vertex_id()");
678 case nir_intrinsic_load_vertex_id_zero_base
:
679 case nir_intrinsic_load_base_vertex
:
680 case nir_intrinsic_load_instance_id
:
681 case nir_intrinsic_load_base_instance
:
682 case nir_intrinsic_load_draw_id
:
683 case nir_intrinsic_load_invocation_id
: {
684 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
685 src_reg val
= src_reg(nir_system_values
[sv
]);
686 assert(val
.file
!= BAD_FILE
);
687 dest
= get_nir_dest(instr
->dest
, val
.type
);
688 emit(MOV(dest
, val
));
692 case nir_intrinsic_load_uniform
: {
693 /* Offsets are in bytes but they should always be multiples of 4 */
694 assert(nir_intrinsic_base(instr
) % 4 == 0);
696 dest
= get_nir_dest(instr
->dest
);
698 src
= src_reg(dst_reg(UNIFORM
, nir_intrinsic_base(instr
) / 16));
699 src
.type
= dest
.type
;
701 /* Uniforms don't actually have to be vec4 aligned. In the case that
702 * it isn't, we have to use a swizzle to shift things around. They
703 * do still have the std140 alignment requirement that vec2's have to
704 * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
706 * The swizzle also works in the indirect case as the generator adds
707 * the swizzle to the offset for us.
709 unsigned shift
= (nir_intrinsic_base(instr
) % 16) / 4;
710 assert(shift
+ instr
->num_components
<= 4);
712 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
714 /* Offsets are in bytes but they should always be multiples of 4 */
715 assert(const_offset
->u32
[0] % 4 == 0);
717 unsigned offset
= const_offset
->u32
[0] + shift
* 4;
718 src
.offset
= ROUND_DOWN_TO(offset
, 16);
719 shift
= (offset
% 16) / 4;
720 src
.swizzle
+= BRW_SWIZZLE4(shift
, shift
, shift
, shift
);
722 emit(MOV(dest
, src
));
724 src
.swizzle
+= BRW_SWIZZLE4(shift
, shift
, shift
, shift
);
726 src_reg indirect
= get_nir_src(instr
->src
[0], BRW_REGISTER_TYPE_UD
, 1);
728 /* MOV_INDIRECT is going to stomp the whole thing anyway */
729 dest
.writemask
= WRITEMASK_XYZW
;
731 emit(SHADER_OPCODE_MOV_INDIRECT
, dest
, src
,
732 indirect
, brw_imm_ud(instr
->const_index
[1]));
737 case nir_intrinsic_atomic_counter_read
:
738 case nir_intrinsic_atomic_counter_inc
:
739 case nir_intrinsic_atomic_counter_dec
: {
740 unsigned surf_index
= prog_data
->base
.binding_table
.abo_start
+
741 (unsigned) instr
->const_index
[0];
742 const vec4_builder bld
=
743 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
745 /* Get some metadata from the image intrinsic. */
746 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
748 /* Get the arguments of the atomic intrinsic. */
749 src_reg offset
= get_nir_src(instr
->src
[0], nir_type_int
,
750 instr
->num_components
);
751 const src_reg surface
= brw_imm_ud(surf_index
);
752 const src_reg src0
= (info
->num_srcs
>= 2
753 ? get_nir_src(instr
->src
[1]) : src_reg());
754 const src_reg src1
= (info
->num_srcs
>= 3
755 ? get_nir_src(instr
->src
[2]) : src_reg());
759 dest
= get_nir_dest(instr
->dest
);
761 if (instr
->intrinsic
== nir_intrinsic_atomic_counter_read
) {
762 tmp
= emit_untyped_read(bld
, surface
, offset
, 1, 1);
764 tmp
= emit_untyped_atomic(bld
, surface
, offset
,
767 get_atomic_counter_op(instr
->intrinsic
));
770 bld
.MOV(retype(dest
, tmp
.type
), tmp
);
771 brw_mark_surface_used(stage_prog_data
, surf_index
);
775 case nir_intrinsic_load_ubo
: {
776 nir_const_value
*const_block_index
= nir_src_as_const_value(instr
->src
[0]);
779 dest
= get_nir_dest(instr
->dest
);
781 if (const_block_index
) {
782 /* The block index is a constant, so just emit the binding table entry
785 const unsigned index
= prog_data
->base
.binding_table
.ubo_start
+
786 const_block_index
->u32
[0];
787 surf_index
= brw_imm_ud(index
);
788 brw_mark_surface_used(&prog_data
->base
, index
);
790 /* The block index is not a constant. Evaluate the index expression
791 * per-channel and add the base UBO index; we have to select a value
792 * from any live channel.
794 surf_index
= src_reg(this, glsl_type::uint_type
);
795 emit(ADD(dst_reg(surf_index
), get_nir_src(instr
->src
[0], nir_type_int
,
796 instr
->num_components
),
797 brw_imm_ud(prog_data
->base
.binding_table
.ubo_start
)));
798 surf_index
= emit_uniformize(surf_index
);
800 /* Assume this may touch any UBO. It would be nice to provide
801 * a tighter bound, but the array information is already lowered away.
803 brw_mark_surface_used(&prog_data
->base
,
804 prog_data
->base
.binding_table
.ubo_start
+
805 nir
->info
.num_ubos
- 1);
809 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
811 offset
= brw_imm_ud(const_offset
->u32
[0] & ~15);
813 offset
= get_nir_src(instr
->src
[1], nir_type_int
, 1);
816 src_reg packed_consts
= src_reg(this, glsl_type::vec4_type
);
817 packed_consts
.type
= dest
.type
;
819 emit_pull_constant_load_reg(dst_reg(packed_consts
),
822 NULL
, NULL
/* before_block/inst */);
824 packed_consts
.swizzle
= brw_swizzle_for_size(instr
->num_components
);
826 packed_consts
.swizzle
+= BRW_SWIZZLE4(const_offset
->u32
[0] % 16 / 4,
827 const_offset
->u32
[0] % 16 / 4,
828 const_offset
->u32
[0] % 16 / 4,
829 const_offset
->u32
[0] % 16 / 4);
832 emit(MOV(dest
, packed_consts
));
836 case nir_intrinsic_memory_barrier
: {
837 const vec4_builder bld
=
838 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
839 const dst_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
840 bld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
841 ->size_written
= 2 * REG_SIZE
;
845 case nir_intrinsic_shader_clock
: {
846 /* We cannot do anything if there is an event, so ignore it for now */
847 const src_reg shader_clock
= get_timestamp();
848 const enum brw_reg_type type
= brw_type_for_base_type(glsl_type::uvec2_type
);
850 dest
= get_nir_dest(instr
->dest
, type
);
851 emit(MOV(dest
, shader_clock
));
856 unreachable("Unknown intrinsic");
861 vec4_visitor::nir_emit_ssbo_atomic(int op
, nir_intrinsic_instr
*instr
)
864 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
865 dest
= get_nir_dest(instr
->dest
);
868 nir_const_value
*const_surface
= nir_src_as_const_value(instr
->src
[0]);
870 unsigned surf_index
= prog_data
->base
.binding_table
.ssbo_start
+
871 const_surface
->u32
[0];
872 surface
= brw_imm_ud(surf_index
);
873 brw_mark_surface_used(&prog_data
->base
, surf_index
);
875 surface
= src_reg(this, glsl_type::uint_type
);
876 emit(ADD(dst_reg(surface
), get_nir_src(instr
->src
[0]),
877 brw_imm_ud(prog_data
->base
.binding_table
.ssbo_start
)));
879 /* Assume this may touch any UBO. This is the same we do for other
880 * UBO/SSBO accesses with non-constant surface.
882 brw_mark_surface_used(&prog_data
->base
,
883 prog_data
->base
.binding_table
.ssbo_start
+
884 nir
->info
.num_ssbos
- 1);
887 src_reg offset
= get_nir_src(instr
->src
[1], 1);
888 src_reg data1
= get_nir_src(instr
->src
[2], 1);
890 if (op
== BRW_AOP_CMPWR
)
891 data2
= get_nir_src(instr
->src
[3], 1);
893 /* Emit the actual atomic operation operation */
894 const vec4_builder bld
=
895 vec4_builder(this).at_end().annotate(current_annotation
, base_ir
);
897 src_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
899 1 /* dims */, 1 /* rsize */,
902 dest
.type
= atomic_result
.type
;
903 bld
.MOV(dest
, atomic_result
);
907 brw_swizzle_for_nir_swizzle(uint8_t swizzle
[4])
909 return BRW_SWIZZLE4(swizzle
[0], swizzle
[1], swizzle
[2], swizzle
[3]);
912 static enum brw_conditional_mod
913 brw_conditional_for_nir_comparison(nir_op op
)
919 return BRW_CONDITIONAL_L
;
924 return BRW_CONDITIONAL_GE
;
928 case nir_op_ball_fequal2
:
929 case nir_op_ball_iequal2
:
930 case nir_op_ball_fequal3
:
931 case nir_op_ball_iequal3
:
932 case nir_op_ball_fequal4
:
933 case nir_op_ball_iequal4
:
934 return BRW_CONDITIONAL_Z
;
938 case nir_op_bany_fnequal2
:
939 case nir_op_bany_inequal2
:
940 case nir_op_bany_fnequal3
:
941 case nir_op_bany_inequal3
:
942 case nir_op_bany_fnequal4
:
943 case nir_op_bany_inequal4
:
944 return BRW_CONDITIONAL_NZ
;
947 unreachable("not reached: bad operation for comparison");
952 vec4_visitor::optimize_predicate(nir_alu_instr
*instr
,
953 enum brw_predicate
*predicate
)
955 if (!instr
->src
[0].src
.is_ssa
||
956 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
959 nir_alu_instr
*cmp_instr
=
960 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
962 switch (cmp_instr
->op
) {
963 case nir_op_bany_fnequal2
:
964 case nir_op_bany_inequal2
:
965 case nir_op_bany_fnequal3
:
966 case nir_op_bany_inequal3
:
967 case nir_op_bany_fnequal4
:
968 case nir_op_bany_inequal4
:
969 *predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
971 case nir_op_ball_fequal2
:
972 case nir_op_ball_iequal2
:
973 case nir_op_ball_fequal3
:
974 case nir_op_ball_iequal3
:
975 case nir_op_ball_fequal4
:
976 case nir_op_ball_iequal4
:
977 *predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
983 unsigned size_swizzle
=
984 brw_swizzle_for_size(nir_op_infos
[cmp_instr
->op
].input_sizes
[0]);
987 assert(nir_op_infos
[cmp_instr
->op
].num_inputs
== 2);
988 for (unsigned i
= 0; i
< 2; i
++) {
989 op
[i
] = get_nir_src(cmp_instr
->src
[i
].src
,
990 nir_op_infos
[cmp_instr
->op
].input_types
[i
], 4);
991 unsigned base_swizzle
=
992 brw_swizzle_for_nir_swizzle(cmp_instr
->src
[i
].swizzle
);
993 op
[i
].swizzle
= brw_compose_swizzle(size_swizzle
, base_swizzle
);
994 op
[i
].abs
= cmp_instr
->src
[i
].abs
;
995 op
[i
].negate
= cmp_instr
->src
[i
].negate
;
998 emit(CMP(dst_null_d(), op
[0], op
[1],
999 brw_conditional_for_nir_comparison(cmp_instr
->op
)));
1005 emit_find_msb_using_lzd(const vec4_builder
&bld
,
1010 vec4_instruction
*inst
;
1014 /* LZD of an absolute value source almost always does the right
1015 * thing. There are two problem values:
1017 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
1018 * 0. However, findMSB(int(0x80000000)) == 30.
1020 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
1021 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1023 * For a value of zero or negative one, -1 will be returned.
1025 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
1026 * findMSB(-(1<<x)) should return x-1.
1028 * For all negative number cases, including 0x80000000 and
1029 * 0xffffffff, the correct value is obtained from LZD if instead of
1030 * negating the (already negative) value the logical-not is used. A
1031 * conditonal logical-not can be achieved in two instructions.
1033 temp
= src_reg(bld
.vgrf(BRW_REGISTER_TYPE_D
));
1035 bld
.ASR(dst_reg(temp
), src
, brw_imm_d(31));
1036 bld
.XOR(dst_reg(temp
), temp
, src
);
1039 bld
.LZD(retype(dst
, BRW_REGISTER_TYPE_UD
),
1040 retype(temp
, BRW_REGISTER_TYPE_UD
));
1042 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
1043 * from the LSB side. Subtract the result from 31 to convert the MSB count
1044 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
1045 * which is exactly what findMSB() is supposed to return.
1047 inst
= bld
.ADD(dst
, retype(src_reg(dst
), BRW_REGISTER_TYPE_D
),
1049 inst
->src
[0].negate
= true;
1053 vec4_visitor::nir_emit_alu(nir_alu_instr
*instr
)
1055 vec4_instruction
*inst
;
1057 dst_reg dst
= get_nir_dest(instr
->dest
.dest
,
1058 nir_op_infos
[instr
->op
].output_type
);
1059 dst
.writemask
= instr
->dest
.write_mask
;
1062 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
1063 op
[i
] = get_nir_src(instr
->src
[i
].src
,
1064 nir_op_infos
[instr
->op
].input_types
[i
], 4);
1065 op
[i
].swizzle
= brw_swizzle_for_nir_swizzle(instr
->src
[i
].swizzle
);
1066 op
[i
].abs
= instr
->src
[i
].abs
;
1067 op
[i
].negate
= instr
->src
[i
].negate
;
1070 switch (instr
->op
) {
1073 inst
= emit(MOV(dst
, op
[0]));
1074 inst
->saturate
= instr
->dest
.saturate
;
1080 unreachable("not reached: should be handled by lower_vec_to_movs()");
1084 inst
= emit(MOV(dst
, op
[0]));
1085 inst
->saturate
= instr
->dest
.saturate
;
1090 inst
= emit(MOV(dst
, op
[0]));
1096 inst
= emit(ADD(dst
, op
[0], op
[1]));
1097 inst
->saturate
= instr
->dest
.saturate
;
1101 inst
= emit(MUL(dst
, op
[0], op
[1]));
1102 inst
->saturate
= instr
->dest
.saturate
;
1106 if (devinfo
->gen
< 8) {
1107 nir_const_value
*value0
= nir_src_as_const_value(instr
->src
[0].src
);
1108 nir_const_value
*value1
= nir_src_as_const_value(instr
->src
[1].src
);
1110 /* For integer multiplication, the MUL uses the low 16 bits of one of
1111 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1112 * accumulates in the contribution of the upper 16 bits of that
1113 * operand. If we can determine that one of the args is in the low
1114 * 16 bits, though, we can just emit a single MUL.
1116 if (value0
&& value0
->u32
[0] < (1 << 16)) {
1117 if (devinfo
->gen
< 7)
1118 emit(MUL(dst
, op
[0], op
[1]));
1120 emit(MUL(dst
, op
[1], op
[0]));
1121 } else if (value1
&& value1
->u32
[0] < (1 << 16)) {
1122 if (devinfo
->gen
< 7)
1123 emit(MUL(dst
, op
[1], op
[0]));
1125 emit(MUL(dst
, op
[0], op
[1]));
1127 struct brw_reg acc
= retype(brw_acc_reg(8), dst
.type
);
1129 emit(MUL(acc
, op
[0], op
[1]));
1130 emit(MACH(dst_null_d(), op
[0], op
[1]));
1131 emit(MOV(dst
, src_reg(acc
)));
1134 emit(MUL(dst
, op
[0], op
[1]));
1139 case nir_op_imul_high
:
1140 case nir_op_umul_high
: {
1141 struct brw_reg acc
= retype(brw_acc_reg(8), dst
.type
);
1143 if (devinfo
->gen
>= 8)
1144 emit(MUL(acc
, op
[0], retype(op
[1], BRW_REGISTER_TYPE_UW
)));
1146 emit(MUL(acc
, op
[0], op
[1]));
1148 emit(MACH(dst
, op
[0], op
[1]));
1153 inst
= emit_math(SHADER_OPCODE_RCP
, dst
, op
[0]);
1154 inst
->saturate
= instr
->dest
.saturate
;
1158 inst
= emit_math(SHADER_OPCODE_EXP2
, dst
, op
[0]);
1159 inst
->saturate
= instr
->dest
.saturate
;
1163 inst
= emit_math(SHADER_OPCODE_LOG2
, dst
, op
[0]);
1164 inst
->saturate
= instr
->dest
.saturate
;
1168 inst
= emit_math(SHADER_OPCODE_SIN
, dst
, op
[0]);
1169 inst
->saturate
= instr
->dest
.saturate
;
1173 inst
= emit_math(SHADER_OPCODE_COS
, dst
, op
[0]);
1174 inst
->saturate
= instr
->dest
.saturate
;
1179 emit_math(SHADER_OPCODE_INT_QUOTIENT
, dst
, op
[0], op
[1]);
1184 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1185 * appears that our hardware just does the right thing for signed
1188 emit_math(SHADER_OPCODE_INT_REMAINDER
, dst
, op
[0], op
[1]);
1192 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1193 inst
= emit_math(SHADER_OPCODE_INT_REMAINDER
, dst
, op
[0], op
[1]);
1195 /* Math instructions don't support conditional mod */
1196 inst
= emit(MOV(dst_null_d(), src_reg(dst
)));
1197 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1199 /* Now, we need to determine if signs of the sources are different.
1200 * When we XOR the sources, the top bit is 0 if they are the same and 1
1201 * if they are different. We can then use a conditional modifier to
1202 * turn that into a predicate. This leads us to an XOR.l instruction.
1204 * Technically, according to the PRM, you're not allowed to use .l on a
1205 * XOR instruction. However, emperical experiments and Curro's reading
1206 * of the simulator source both indicate that it's safe.
1208 src_reg tmp
= src_reg(this, glsl_type::ivec4_type
);
1209 inst
= emit(XOR(dst_reg(tmp
), op
[0], op
[1]));
1210 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1211 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1213 /* If the result of the initial remainder operation is non-zero and the
1214 * two sources have different signs, add in a copy of op[1] to get the
1215 * final integer modulus value.
1217 inst
= emit(ADD(dst
, src_reg(dst
), op
[1]));
1218 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1223 unreachable("not reached: should be handled by ldexp_to_arith()");
1226 inst
= emit_math(SHADER_OPCODE_SQRT
, dst
, op
[0]);
1227 inst
->saturate
= instr
->dest
.saturate
;
1231 inst
= emit_math(SHADER_OPCODE_RSQ
, dst
, op
[0]);
1232 inst
->saturate
= instr
->dest
.saturate
;
1236 inst
= emit_math(SHADER_OPCODE_POW
, dst
, op
[0], op
[1]);
1237 inst
->saturate
= instr
->dest
.saturate
;
1240 case nir_op_uadd_carry
: {
1241 struct brw_reg acc
= retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD
);
1243 emit(ADDC(dst_null_ud(), op
[0], op
[1]));
1244 emit(MOV(dst
, src_reg(acc
)));
1248 case nir_op_usub_borrow
: {
1249 struct brw_reg acc
= retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD
);
1251 emit(SUBB(dst_null_ud(), op
[0], op
[1]));
1252 emit(MOV(dst
, src_reg(acc
)));
1257 inst
= emit(RNDZ(dst
, op
[0]));
1258 inst
->saturate
= instr
->dest
.saturate
;
1261 case nir_op_fceil
: {
1262 src_reg tmp
= src_reg(this, glsl_type::float_type
);
1264 brw_swizzle_for_size(instr
->src
[0].src
.is_ssa
?
1265 instr
->src
[0].src
.ssa
->num_components
:
1266 instr
->src
[0].src
.reg
.reg
->num_components
);
1268 op
[0].negate
= !op
[0].negate
;
1269 emit(RNDD(dst_reg(tmp
), op
[0]));
1271 inst
= emit(MOV(dst
, tmp
));
1272 inst
->saturate
= instr
->dest
.saturate
;
1277 inst
= emit(RNDD(dst
, op
[0]));
1278 inst
->saturate
= instr
->dest
.saturate
;
1282 inst
= emit(FRC(dst
, op
[0]));
1283 inst
->saturate
= instr
->dest
.saturate
;
1286 case nir_op_fround_even
:
1287 inst
= emit(RNDE(dst
, op
[0]));
1288 inst
->saturate
= instr
->dest
.saturate
;
1291 case nir_op_fquantize2f16
: {
1292 /* See also vec4_visitor::emit_pack_half_2x16() */
1293 src_reg tmp16
= src_reg(this, glsl_type::uvec4_type
);
1294 src_reg tmp32
= src_reg(this, glsl_type::vec4_type
);
1295 src_reg zero
= src_reg(this, glsl_type::vec4_type
);
1297 /* Check for denormal */
1298 src_reg abs_src0
= op
[0];
1299 abs_src0
.abs
= true;
1300 emit(CMP(dst_null_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1301 BRW_CONDITIONAL_L
));
1302 /* Get the appropriately signed zero */
1303 emit(AND(retype(dst_reg(zero
), BRW_REGISTER_TYPE_UD
),
1304 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1305 brw_imm_ud(0x80000000)));
1306 /* Do the actual F32 -> F16 -> F32 conversion */
1307 emit(F32TO16(dst_reg(tmp16
), op
[0]));
1308 emit(F16TO32(dst_reg(tmp32
), tmp16
));
1309 /* Select that or zero based on normal status */
1310 inst
= emit(BRW_OPCODE_SEL
, dst
, zero
, tmp32
);
1311 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1312 inst
->saturate
= instr
->dest
.saturate
;
1319 inst
= emit_minmax(BRW_CONDITIONAL_L
, dst
, op
[0], op
[1]);
1320 inst
->saturate
= instr
->dest
.saturate
;
1326 inst
= emit_minmax(BRW_CONDITIONAL_GE
, dst
, op
[0], op
[1]);
1327 inst
->saturate
= instr
->dest
.saturate
;
1331 case nir_op_fddx_coarse
:
1332 case nir_op_fddx_fine
:
1334 case nir_op_fddy_coarse
:
1335 case nir_op_fddy_fine
:
1336 unreachable("derivatives are not valid in vertex shaders");
1348 emit(CMP(dst
, op
[0], op
[1],
1349 brw_conditional_for_nir_comparison(instr
->op
)));
1352 case nir_op_ball_fequal2
:
1353 case nir_op_ball_iequal2
:
1354 case nir_op_ball_fequal3
:
1355 case nir_op_ball_iequal3
:
1356 case nir_op_ball_fequal4
:
1357 case nir_op_ball_iequal4
: {
1359 brw_swizzle_for_size(nir_op_infos
[instr
->op
].input_sizes
[0]);
1361 emit(CMP(dst_null_d(), swizzle(op
[0], swiz
), swizzle(op
[1], swiz
),
1362 brw_conditional_for_nir_comparison(instr
->op
)));
1363 emit(MOV(dst
, brw_imm_d(0)));
1364 inst
= emit(MOV(dst
, brw_imm_d(~0)));
1365 inst
->predicate
= BRW_PREDICATE_ALIGN16_ALL4H
;
1369 case nir_op_bany_fnequal2
:
1370 case nir_op_bany_inequal2
:
1371 case nir_op_bany_fnequal3
:
1372 case nir_op_bany_inequal3
:
1373 case nir_op_bany_fnequal4
:
1374 case nir_op_bany_inequal4
: {
1376 brw_swizzle_for_size(nir_op_infos
[instr
->op
].input_sizes
[0]);
1378 emit(CMP(dst_null_d(), swizzle(op
[0], swiz
), swizzle(op
[1], swiz
),
1379 brw_conditional_for_nir_comparison(instr
->op
)));
1381 emit(MOV(dst
, brw_imm_d(0)));
1382 inst
= emit(MOV(dst
, brw_imm_d(~0)));
1383 inst
->predicate
= BRW_PREDICATE_ALIGN16_ANY4H
;
1388 if (devinfo
->gen
>= 8) {
1389 op
[0] = resolve_source_modifiers(op
[0]);
1391 emit(NOT(dst
, op
[0]));
1395 if (devinfo
->gen
>= 8) {
1396 op
[0] = resolve_source_modifiers(op
[0]);
1397 op
[1] = resolve_source_modifiers(op
[1]);
1399 emit(XOR(dst
, op
[0], op
[1]));
1403 if (devinfo
->gen
>= 8) {
1404 op
[0] = resolve_source_modifiers(op
[0]);
1405 op
[1] = resolve_source_modifiers(op
[1]);
1407 emit(OR(dst
, op
[0], op
[1]));
1411 if (devinfo
->gen
>= 8) {
1412 op
[0] = resolve_source_modifiers(op
[0]);
1413 op
[1] = resolve_source_modifiers(op
[1]);
1415 emit(AND(dst
, op
[0], op
[1]));
1420 emit(MOV(dst
, negate(op
[0])));
1424 emit(CMP(dst
, op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
));
1428 emit(CMP(dst
, op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
));
1431 case nir_op_fnoise1_1
:
1432 case nir_op_fnoise1_2
:
1433 case nir_op_fnoise1_3
:
1434 case nir_op_fnoise1_4
:
1435 case nir_op_fnoise2_1
:
1436 case nir_op_fnoise2_2
:
1437 case nir_op_fnoise2_3
:
1438 case nir_op_fnoise2_4
:
1439 case nir_op_fnoise3_1
:
1440 case nir_op_fnoise3_2
:
1441 case nir_op_fnoise3_3
:
1442 case nir_op_fnoise3_4
:
1443 case nir_op_fnoise4_1
:
1444 case nir_op_fnoise4_2
:
1445 case nir_op_fnoise4_3
:
1446 case nir_op_fnoise4_4
:
1447 unreachable("not reached: should be handled by lower_noise");
1449 case nir_op_unpack_half_2x16_split_x
:
1450 case nir_op_unpack_half_2x16_split_y
:
1451 case nir_op_pack_half_2x16_split
:
1452 unreachable("not reached: should not occur in vertex shader");
1454 case nir_op_unpack_snorm_2x16
:
1455 case nir_op_unpack_unorm_2x16
:
1456 case nir_op_pack_snorm_2x16
:
1457 case nir_op_pack_unorm_2x16
:
1458 unreachable("not reached: should be handled by lower_packing_builtins");
1460 case nir_op_pack_uvec4_to_uint
:
1461 unreachable("not reached");
1463 case nir_op_pack_uvec2_to_uint
: {
1464 dst_reg tmp1
= dst_reg(this, glsl_type::uint_type
);
1465 tmp1
.writemask
= WRITEMASK_X
;
1466 op
[0].swizzle
= BRW_SWIZZLE_YYYY
;
1467 emit(SHL(tmp1
, op
[0], src_reg(brw_imm_ud(16u))));
1469 dst_reg tmp2
= dst_reg(this, glsl_type::uint_type
);
1470 tmp2
.writemask
= WRITEMASK_X
;
1471 op
[0].swizzle
= BRW_SWIZZLE_XXXX
;
1472 emit(AND(tmp2
, op
[0], src_reg(brw_imm_ud(0xffffu
))));
1474 emit(OR(dst
, src_reg(tmp1
), src_reg(tmp2
)));
1478 case nir_op_unpack_half_2x16
:
1479 /* As NIR does not guarantee that we have a correct swizzle outside the
1480 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1481 * uses the source operand in an operation with WRITEMASK_Y while our
1482 * source operand has only size 1, it accessed incorrect data producing
1483 * regressions in Piglit. We repeat the swizzle of the first component on the
1484 * rest of components to avoid regressions. In the vec4_visitor IR code path
1485 * this is not needed because the operand has already the correct swizzle.
1487 op
[0].swizzle
= brw_compose_swizzle(BRW_SWIZZLE_XXXX
, op
[0].swizzle
);
1488 emit_unpack_half_2x16(dst
, op
[0]);
1491 case nir_op_pack_half_2x16
:
1492 emit_pack_half_2x16(dst
, op
[0]);
1495 case nir_op_unpack_unorm_4x8
:
1496 emit_unpack_unorm_4x8(dst
, op
[0]);
1499 case nir_op_pack_unorm_4x8
:
1500 emit_pack_unorm_4x8(dst
, op
[0]);
1503 case nir_op_unpack_snorm_4x8
:
1504 emit_unpack_snorm_4x8(dst
, op
[0]);
1507 case nir_op_pack_snorm_4x8
:
1508 emit_pack_snorm_4x8(dst
, op
[0]);
1511 case nir_op_bitfield_reverse
:
1512 emit(BFREV(dst
, op
[0]));
1515 case nir_op_bit_count
:
1516 emit(CBIT(dst
, op
[0]));
1519 case nir_op_ufind_msb
:
1520 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst
, op
[0], false);
1523 case nir_op_ifind_msb
: {
1524 vec4_builder bld
= vec4_builder(this).at_end();
1527 if (devinfo
->gen
< 7) {
1528 emit_find_msb_using_lzd(bld
, dst
, op
[0], true);
1530 emit(FBH(retype(dst
, BRW_REGISTER_TYPE_UD
), op
[0]));
1532 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1533 * count from the LSB side. If FBH didn't return an error
1534 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1535 * count into an LSB count.
1537 bld
.CMP(dst_null_d(), src
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1539 inst
= bld
.ADD(dst
, src
, brw_imm_d(31));
1540 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1541 inst
->src
[0].negate
= true;
1546 case nir_op_find_lsb
: {
1547 vec4_builder bld
= vec4_builder(this).at_end();
1549 if (devinfo
->gen
< 7) {
1550 dst_reg temp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1552 /* (x & -x) generates a value that consists of only the LSB of x.
1553 * For all powers of 2, findMSB(y) == findLSB(y).
1555 src_reg src
= src_reg(retype(op
[0], BRW_REGISTER_TYPE_D
));
1556 src_reg negated_src
= src
;
1558 /* One must be negated, and the other must be non-negated. It
1559 * doesn't matter which is which.
1561 negated_src
.negate
= true;
1564 bld
.AND(temp
, src
, negated_src
);
1565 emit_find_msb_using_lzd(bld
, dst
, src_reg(temp
), false);
1567 bld
.FBL(dst
, op
[0]);
1572 case nir_op_ubitfield_extract
:
1573 case nir_op_ibitfield_extract
:
1574 unreachable("should have been lowered");
1577 op
[0] = fix_3src_operand(op
[0]);
1578 op
[1] = fix_3src_operand(op
[1]);
1579 op
[2] = fix_3src_operand(op
[2]);
1581 emit(BFE(dst
, op
[2], op
[1], op
[0]));
1585 emit(BFI1(dst
, op
[0], op
[1]));
1589 op
[0] = fix_3src_operand(op
[0]);
1590 op
[1] = fix_3src_operand(op
[1]);
1591 op
[2] = fix_3src_operand(op
[2]);
1593 emit(BFI2(dst
, op
[0], op
[1], op
[2]));
1596 case nir_op_bitfield_insert
:
1597 unreachable("not reached: should have been lowered");
1600 /* AND(val, 0x80000000) gives the sign bit.
1602 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1605 emit(CMP(dst_null_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
));
1607 op
[0].type
= BRW_REGISTER_TYPE_UD
;
1608 dst
.type
= BRW_REGISTER_TYPE_UD
;
1609 emit(AND(dst
, op
[0], brw_imm_ud(0x80000000u
)));
1611 inst
= emit(OR(dst
, src_reg(dst
), brw_imm_ud(0x3f800000u
)));
1612 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1613 dst
.type
= BRW_REGISTER_TYPE_F
;
1615 if (instr
->dest
.saturate
) {
1616 inst
= emit(MOV(dst
, src_reg(dst
)));
1617 inst
->saturate
= true;
1622 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1623 * -> non-negative val generates 0x00000000.
1624 * Predicated OR sets 1 if val is positive.
1626 emit(CMP(dst_null_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_G
));
1627 emit(ASR(dst
, op
[0], brw_imm_d(31)));
1628 inst
= emit(OR(dst
, src_reg(dst
), brw_imm_d(1)));
1629 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1633 emit(SHL(dst
, op
[0], op
[1]));
1637 emit(ASR(dst
, op
[0], op
[1]));
1641 emit(SHR(dst
, op
[0], op
[1]));
1645 op
[0] = fix_3src_operand(op
[0]);
1646 op
[1] = fix_3src_operand(op
[1]);
1647 op
[2] = fix_3src_operand(op
[2]);
1649 inst
= emit(MAD(dst
, op
[2], op
[1], op
[0]));
1650 inst
->saturate
= instr
->dest
.saturate
;
1654 inst
= emit_lrp(dst
, op
[0], op
[1], op
[2]);
1655 inst
->saturate
= instr
->dest
.saturate
;
1659 enum brw_predicate predicate
;
1660 if (!optimize_predicate(instr
, &predicate
)) {
1661 emit(CMP(dst_null_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
));
1662 switch (dst
.writemask
) {
1664 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_X
;
1667 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_Y
;
1670 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_Z
;
1673 predicate
= BRW_PREDICATE_ALIGN16_REPLICATE_W
;
1676 predicate
= BRW_PREDICATE_NORMAL
;
1680 inst
= emit(BRW_OPCODE_SEL
, dst
, op
[1], op
[2]);
1681 inst
->predicate
= predicate
;
1684 case nir_op_fdot_replicated2
:
1685 inst
= emit(BRW_OPCODE_DP2
, dst
, op
[0], op
[1]);
1686 inst
->saturate
= instr
->dest
.saturate
;
1689 case nir_op_fdot_replicated3
:
1690 inst
= emit(BRW_OPCODE_DP3
, dst
, op
[0], op
[1]);
1691 inst
->saturate
= instr
->dest
.saturate
;
1694 case nir_op_fdot_replicated4
:
1695 inst
= emit(BRW_OPCODE_DP4
, dst
, op
[0], op
[1]);
1696 inst
->saturate
= instr
->dest
.saturate
;
1699 case nir_op_fdph_replicated
:
1700 inst
= emit(BRW_OPCODE_DPH
, dst
, op
[0], op
[1]);
1701 inst
->saturate
= instr
->dest
.saturate
;
1709 unreachable("not reached: should be lowered by lower_source mods");
1712 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1715 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1719 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1722 unreachable("Unimplemented ALU operation");
1725 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1726 * to sign extend the low bit to 0/~0
1728 if (devinfo
->gen
<= 5 &&
1729 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) ==
1730 BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1731 dst_reg masked
= dst_reg(this, glsl_type::int_type
);
1732 masked
.writemask
= dst
.writemask
;
1733 emit(AND(masked
, src_reg(dst
), brw_imm_d(1)));
1734 src_reg masked_neg
= src_reg(masked
);
1735 masked_neg
.negate
= true;
1736 emit(MOV(retype(dst
, BRW_REGISTER_TYPE_D
), masked_neg
));
1741 vec4_visitor::nir_emit_jump(nir_jump_instr
*instr
)
1743 switch (instr
->type
) {
1744 case nir_jump_break
:
1745 emit(BRW_OPCODE_BREAK
);
1748 case nir_jump_continue
:
1749 emit(BRW_OPCODE_CONTINUE
);
1752 case nir_jump_return
:
1755 unreachable("unknown jump");
1759 enum ir_texture_opcode
1760 ir_texture_opcode_for_nir_texop(nir_texop texop
)
1762 enum ir_texture_opcode op
;
1765 case nir_texop_lod
: op
= ir_lod
; break;
1766 case nir_texop_query_levels
: op
= ir_query_levels
; break;
1767 case nir_texop_texture_samples
: op
= ir_texture_samples
; break;
1768 case nir_texop_tex
: op
= ir_tex
; break;
1769 case nir_texop_tg4
: op
= ir_tg4
; break;
1770 case nir_texop_txb
: op
= ir_txb
; break;
1771 case nir_texop_txd
: op
= ir_txd
; break;
1772 case nir_texop_txf
: op
= ir_txf
; break;
1773 case nir_texop_txf_ms
: op
= ir_txf_ms
; break;
1774 case nir_texop_txl
: op
= ir_txl
; break;
1775 case nir_texop_txs
: op
= ir_txs
; break;
1776 case nir_texop_samples_identical
: op
= ir_samples_identical
; break;
1778 unreachable("unknown texture opcode");
1784 glsl_type_for_nir_alu_type(nir_alu_type alu_type
,
1785 unsigned components
)
1788 case nir_type_float
:
1789 return glsl_type::vec(components
);
1791 return glsl_type::ivec(components
);
1793 return glsl_type::uvec(components
);
1795 return glsl_type::bvec(components
);
1797 return glsl_type::error_type
;
1800 return glsl_type::error_type
;
1804 vec4_visitor::nir_emit_texture(nir_tex_instr
*instr
)
1806 unsigned texture
= instr
->texture_index
;
1807 unsigned sampler
= instr
->sampler_index
;
1808 src_reg texture_reg
= brw_imm_ud(texture
);
1809 src_reg sampler_reg
= brw_imm_ud(sampler
);
1811 const glsl_type
*coord_type
= NULL
;
1812 src_reg shadow_comparitor
;
1813 src_reg offset_value
;
1815 src_reg sample_index
;
1818 const glsl_type
*dest_type
=
1819 glsl_type_for_nir_alu_type(instr
->dest_type
,
1820 nir_tex_instr_dest_size(instr
));
1821 dst_reg dest
= get_nir_dest(instr
->dest
, instr
->dest_type
);
1823 /* The hardware requires a LOD for buffer textures */
1824 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
1827 /* Load the texture operation sources */
1828 uint32_t constant_offset
= 0;
1829 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1830 switch (instr
->src
[i
].src_type
) {
1831 case nir_tex_src_comparitor
:
1832 shadow_comparitor
= get_nir_src(instr
->src
[i
].src
,
1833 BRW_REGISTER_TYPE_F
, 1);
1836 case nir_tex_src_coord
: {
1837 unsigned src_size
= nir_tex_instr_src_size(instr
, i
);
1839 switch (instr
->op
) {
1841 case nir_texop_txf_ms
:
1842 case nir_texop_samples_identical
:
1843 coordinate
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
,
1845 coord_type
= glsl_type::ivec(src_size
);
1849 coordinate
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
1851 coord_type
= glsl_type::vec(src_size
);
1857 case nir_tex_src_ddx
:
1858 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
1859 nir_tex_instr_src_size(instr
, i
));
1862 case nir_tex_src_ddy
:
1863 lod2
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
,
1864 nir_tex_instr_src_size(instr
, i
));
1867 case nir_tex_src_lod
:
1868 switch (instr
->op
) {
1871 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 1);
1875 lod
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_F
, 1);
1880 case nir_tex_src_ms_index
: {
1881 sample_index
= get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 1);
1885 case nir_tex_src_offset
: {
1886 nir_const_value
*const_offset
=
1887 nir_src_as_const_value(instr
->src
[i
].src
);
1889 constant_offset
= brw_texture_offset(const_offset
->i32
, 3);
1892 get_nir_src(instr
->src
[i
].src
, BRW_REGISTER_TYPE_D
, 2);
1897 case nir_tex_src_texture_offset
: {
1898 /* The highest texture which may be used by this operation is
1899 * the last element of the array. Mark it here, because the generator
1900 * doesn't have enough information to determine the bound.
1902 uint32_t array_size
= instr
->texture_array_size
;
1903 uint32_t max_used
= texture
+ array_size
- 1;
1904 if (instr
->op
== nir_texop_tg4
) {
1905 max_used
+= prog_data
->base
.binding_table
.gather_texture_start
;
1907 max_used
+= prog_data
->base
.binding_table
.texture_start
;
1910 brw_mark_surface_used(&prog_data
->base
, max_used
);
1912 /* Emit code to evaluate the actual indexing expression */
1913 src_reg src
= get_nir_src(instr
->src
[i
].src
, 1);
1914 src_reg
temp(this, glsl_type::uint_type
);
1915 emit(ADD(dst_reg(temp
), src
, brw_imm_ud(texture
)));
1916 texture_reg
= emit_uniformize(temp
);
1920 case nir_tex_src_sampler_offset
: {
1921 /* Emit code to evaluate the actual indexing expression */
1922 src_reg src
= get_nir_src(instr
->src
[i
].src
, 1);
1923 src_reg
temp(this, glsl_type::uint_type
);
1924 emit(ADD(dst_reg(temp
), src
, brw_imm_ud(sampler
)));
1925 sampler_reg
= emit_uniformize(temp
);
1929 case nir_tex_src_projector
:
1930 unreachable("Should be lowered by do_lower_texture_projection");
1932 case nir_tex_src_bias
:
1933 unreachable("LOD bias is not valid for vertex shaders.\n");
1936 unreachable("unknown texture source");
1940 if (instr
->op
== nir_texop_txf_ms
||
1941 instr
->op
== nir_texop_samples_identical
) {
1942 assert(coord_type
!= NULL
);
1943 if (devinfo
->gen
>= 7 &&
1944 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
1945 mcs
= emit_mcs_fetch(coord_type
, coordinate
, texture_reg
);
1947 mcs
= brw_imm_ud(0u);
1951 /* Stuff the channel select bits in the top of the texture offset */
1952 if (instr
->op
== nir_texop_tg4
) {
1953 if (instr
->component
== 1 &&
1954 (key_tex
->gather_channel_quirk_mask
& (1 << texture
))) {
1955 /* gather4 sampler is broken for green channel on RG32F --
1956 * we must ask for blue instead.
1958 constant_offset
|= 2 << 16;
1960 constant_offset
|= instr
->component
<< 16;
1964 ir_texture_opcode op
= ir_texture_opcode_for_nir_texop(instr
->op
);
1966 emit_texture(op
, dest
, dest_type
, coordinate
, instr
->coord_components
,
1968 lod
, lod2
, sample_index
,
1969 constant_offset
, offset_value
, mcs
,
1970 texture
, texture_reg
, sampler
, sampler_reg
);
1974 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr
*instr
)
1976 nir_ssa_values
[instr
->def
.index
] = dst_reg(VGRF
, alloc
.allocate(1));