2 * Copyright © 2010 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
24 #include "compiler/glsl/ir.h"
28 #include "nir_search_helpers.h"
29 #include "util/u_math.h"
30 #include "util/bitscan.h"
35 fs_visitor::emit_nir_code()
37 emit_shader_float_controls_execution_mode();
39 /* emit the arrays used for inputs and outputs - load/store intrinsics will
40 * be converted to reads/writes of these arrays
44 nir_emit_system_values();
45 last_scratch
= ALIGN(nir
->scratch_size
, 4) * dispatch_width
;
47 nir_emit_impl(nir_shader_get_entrypoint((nir_shader
*)nir
));
51 fs_visitor::nir_setup_outputs()
53 if (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_FRAGMENT
)
56 unsigned vec4s
[VARYING_SLOT_TESS_MAX
] = { 0, };
58 /* Calculate the size of output registers in a separate pass, before
59 * allocating them. With ARB_enhanced_layouts, multiple output variables
60 * may occupy the same slot, but have different type sizes.
62 nir_foreach_variable(var
, &nir
->outputs
) {
63 const int loc
= var
->data
.driver_location
;
64 const unsigned var_vec4s
=
65 var
->data
.compact
? DIV_ROUND_UP(glsl_get_length(var
->type
), 4)
66 : type_size_vec4(var
->type
, true);
67 vec4s
[loc
] = MAX2(vec4s
[loc
], var_vec4s
);
70 for (unsigned loc
= 0; loc
< ARRAY_SIZE(vec4s
);) {
71 if (vec4s
[loc
] == 0) {
76 unsigned reg_size
= vec4s
[loc
];
78 /* Check if there are any ranges that start within this range and extend
79 * past it. If so, include them in this allocation.
81 for (unsigned i
= 1; i
< reg_size
; i
++)
82 reg_size
= MAX2(vec4s
[i
+ loc
] + i
, reg_size
);
84 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4 * reg_size
);
85 for (unsigned i
= 0; i
< reg_size
; i
++)
86 outputs
[loc
+ i
] = offset(reg
, bld
, 4 * i
);
93 fs_visitor::nir_setup_uniforms()
95 /* Only the first compile gets to set up uniforms. */
96 if (push_constant_loc
) {
97 assert(pull_constant_loc
);
101 uniforms
= nir
->num_uniforms
/ 4;
103 if (stage
== MESA_SHADER_COMPUTE
) {
104 /* Add uniforms for builtins after regular NIR uniforms. */
105 assert(uniforms
== prog_data
->nr_params
);
108 if (brw_cs_prog_data(prog_data
)->uses_variable_group_size
) {
109 param
= brw_stage_prog_data_add_params(prog_data
, 3);
110 for (unsigned i
= 0; i
< 3; i
++) {
111 param
[i
] = (BRW_PARAM_BUILTIN_WORK_GROUP_SIZE_X
+ i
);
112 group_size
[i
] = fs_reg(UNIFORM
, uniforms
++, BRW_REGISTER_TYPE_UD
);
116 /* Subgroup ID must be the last uniform on the list. This will make
117 * easier later to split between cross thread and per thread
120 param
= brw_stage_prog_data_add_params(prog_data
, 1);
121 *param
= BRW_PARAM_BUILTIN_SUBGROUP_ID
;
122 subgroup_id
= fs_reg(UNIFORM
, uniforms
++, BRW_REGISTER_TYPE_UD
);
127 emit_system_values_block(nir_block
*block
, fs_visitor
*v
)
131 nir_foreach_instr(instr
, block
) {
132 if (instr
->type
!= nir_instr_type_intrinsic
)
135 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
136 switch (intrin
->intrinsic
) {
137 case nir_intrinsic_load_vertex_id
:
138 case nir_intrinsic_load_base_vertex
:
139 unreachable("should be lowered by nir_lower_system_values().");
141 case nir_intrinsic_load_vertex_id_zero_base
:
142 case nir_intrinsic_load_is_indexed_draw
:
143 case nir_intrinsic_load_first_vertex
:
144 case nir_intrinsic_load_instance_id
:
145 case nir_intrinsic_load_base_instance
:
146 case nir_intrinsic_load_draw_id
:
147 unreachable("should be lowered by brw_nir_lower_vs_inputs().");
149 case nir_intrinsic_load_invocation_id
:
150 if (v
->stage
== MESA_SHADER_TESS_CTRL
)
152 assert(v
->stage
== MESA_SHADER_GEOMETRY
);
153 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
154 if (reg
->file
== BAD_FILE
) {
155 const fs_builder abld
= v
->bld
.annotate("gl_InvocationID", NULL
);
156 fs_reg
g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
157 fs_reg iid
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
158 abld
.SHR(iid
, g1
, brw_imm_ud(27u));
163 case nir_intrinsic_load_sample_pos
:
164 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
165 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
166 if (reg
->file
== BAD_FILE
)
167 *reg
= *v
->emit_samplepos_setup();
170 case nir_intrinsic_load_sample_id
:
171 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
172 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
173 if (reg
->file
== BAD_FILE
)
174 *reg
= *v
->emit_sampleid_setup();
177 case nir_intrinsic_load_sample_mask_in
:
178 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
179 assert(v
->devinfo
->gen
>= 7);
180 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
181 if (reg
->file
== BAD_FILE
)
182 *reg
= *v
->emit_samplemaskin_setup();
185 case nir_intrinsic_load_work_group_id
:
186 assert(v
->stage
== MESA_SHADER_COMPUTE
);
187 reg
= &v
->nir_system_values
[SYSTEM_VALUE_WORK_GROUP_ID
];
188 if (reg
->file
== BAD_FILE
)
189 *reg
= *v
->emit_cs_work_group_id_setup();
192 case nir_intrinsic_load_helper_invocation
:
193 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
194 reg
= &v
->nir_system_values
[SYSTEM_VALUE_HELPER_INVOCATION
];
195 if (reg
->file
== BAD_FILE
) {
196 const fs_builder abld
=
197 v
->bld
.annotate("gl_HelperInvocation", NULL
);
199 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
200 * pixel mask is in g1.7 of the thread payload.
202 * We move the per-channel pixel enable bit to the low bit of each
203 * channel by shifting the byte containing the pixel mask by the
204 * vector immediate 0x76543210UV.
206 * The region of <1,8,0> reads only 1 byte (the pixel masks for
207 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
208 * masks for 2 and 3) in SIMD16.
210 fs_reg shifted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
212 for (unsigned i
= 0; i
< DIV_ROUND_UP(v
->dispatch_width
, 16); i
++) {
213 const fs_builder hbld
= abld
.group(MIN2(16, v
->dispatch_width
), i
);
214 hbld
.SHR(offset(shifted
, hbld
, i
),
215 stride(retype(brw_vec1_grf(1 + i
, 7),
216 BRW_REGISTER_TYPE_UB
),
218 brw_imm_v(0x76543210));
221 /* A set bit in the pixel mask means the channel is enabled, but
222 * that is the opposite of gl_HelperInvocation so we need to invert
225 * The negate source-modifier bit of logical instructions on Gen8+
226 * performs 1's complement negation, so we can use that instead of
229 fs_reg inverted
= negate(shifted
);
230 if (v
->devinfo
->gen
< 8) {
231 inverted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
232 abld
.NOT(inverted
, shifted
);
235 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
236 * with 1 and negating.
238 fs_reg anded
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
239 abld
.AND(anded
, inverted
, brw_imm_uw(1));
241 fs_reg dst
= abld
.vgrf(BRW_REGISTER_TYPE_D
, 1);
242 abld
.MOV(dst
, negate(retype(anded
, BRW_REGISTER_TYPE_D
)));
256 fs_visitor::nir_emit_system_values()
258 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
259 for (unsigned i
= 0; i
< SYSTEM_VALUE_MAX
; i
++) {
260 nir_system_values
[i
] = fs_reg();
263 /* Always emit SUBGROUP_INVOCATION. Dead code will clean it up if we
264 * never end up using it.
267 const fs_builder abld
= bld
.annotate("gl_SubgroupInvocation", NULL
);
268 fs_reg
®
= nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
];
269 reg
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
271 const fs_builder allbld8
= abld
.group(8, 0).exec_all();
272 allbld8
.MOV(reg
, brw_imm_v(0x76543210));
273 if (dispatch_width
> 8)
274 allbld8
.ADD(byte_offset(reg
, 16), reg
, brw_imm_uw(8u));
275 if (dispatch_width
> 16) {
276 const fs_builder allbld16
= abld
.group(16, 0).exec_all();
277 allbld16
.ADD(byte_offset(reg
, 32), reg
, brw_imm_uw(16u));
281 nir_function_impl
*impl
= nir_shader_get_entrypoint((nir_shader
*)nir
);
282 nir_foreach_block(block
, impl
)
283 emit_system_values_block(block
, this);
287 * Returns a type based on a reference_type (word, float, half-float) and a
290 * Reference BRW_REGISTER_TYPE are HF,F,DF,W,D,UW,UD.
292 * @FIXME: 64-bit return types are always DF on integer types to maintain
293 * compability with uses of DF previously to the introduction of int64
297 brw_reg_type_from_bit_size(const unsigned bit_size
,
298 const brw_reg_type reference_type
)
300 switch(reference_type
) {
301 case BRW_REGISTER_TYPE_HF
:
302 case BRW_REGISTER_TYPE_F
:
303 case BRW_REGISTER_TYPE_DF
:
306 return BRW_REGISTER_TYPE_HF
;
308 return BRW_REGISTER_TYPE_F
;
310 return BRW_REGISTER_TYPE_DF
;
312 unreachable("Invalid bit size");
314 case BRW_REGISTER_TYPE_B
:
315 case BRW_REGISTER_TYPE_W
:
316 case BRW_REGISTER_TYPE_D
:
317 case BRW_REGISTER_TYPE_Q
:
320 return BRW_REGISTER_TYPE_B
;
322 return BRW_REGISTER_TYPE_W
;
324 return BRW_REGISTER_TYPE_D
;
326 return BRW_REGISTER_TYPE_Q
;
328 unreachable("Invalid bit size");
330 case BRW_REGISTER_TYPE_UB
:
331 case BRW_REGISTER_TYPE_UW
:
332 case BRW_REGISTER_TYPE_UD
:
333 case BRW_REGISTER_TYPE_UQ
:
336 return BRW_REGISTER_TYPE_UB
;
338 return BRW_REGISTER_TYPE_UW
;
340 return BRW_REGISTER_TYPE_UD
;
342 return BRW_REGISTER_TYPE_UQ
;
344 unreachable("Invalid bit size");
347 unreachable("Unknown type");
352 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
354 nir_locals
= ralloc_array(mem_ctx
, fs_reg
, impl
->reg_alloc
);
355 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
356 nir_locals
[i
] = fs_reg();
359 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
360 unsigned array_elems
=
361 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
362 unsigned size
= array_elems
* reg
->num_components
;
363 const brw_reg_type reg_type
= reg
->bit_size
== 8 ? BRW_REGISTER_TYPE_B
:
364 brw_reg_type_from_bit_size(reg
->bit_size
, BRW_REGISTER_TYPE_F
);
365 nir_locals
[reg
->index
] = bld
.vgrf(reg_type
, size
);
368 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
371 nir_emit_cf_list(&impl
->body
);
375 fs_visitor::nir_emit_cf_list(exec_list
*list
)
377 exec_list_validate(list
);
378 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
379 switch (node
->type
) {
381 nir_emit_if(nir_cf_node_as_if(node
));
384 case nir_cf_node_loop
:
385 nir_emit_loop(nir_cf_node_as_loop(node
));
388 case nir_cf_node_block
:
389 nir_emit_block(nir_cf_node_as_block(node
));
393 unreachable("Invalid CFG node block");
399 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
404 /* If the condition has the form !other_condition, use other_condition as
405 * the source, but invert the predicate on the if instruction.
407 nir_alu_instr
*cond
= nir_src_as_alu_instr(if_stmt
->condition
);
408 if (cond
!= NULL
&& cond
->op
== nir_op_inot
) {
409 assert(!cond
->src
[0].negate
);
410 assert(!cond
->src
[0].abs
);
413 cond_reg
= get_nir_src(cond
->src
[0].src
);
416 cond_reg
= get_nir_src(if_stmt
->condition
);
419 /* first, put the condition into f0 */
420 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
421 retype(cond_reg
, BRW_REGISTER_TYPE_D
));
422 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
424 bld
.IF(BRW_PREDICATE_NORMAL
)->predicate_inverse
= invert
;
426 nir_emit_cf_list(&if_stmt
->then_list
);
428 if (!nir_cf_list_is_empty_block(&if_stmt
->else_list
)) {
429 bld
.emit(BRW_OPCODE_ELSE
);
430 nir_emit_cf_list(&if_stmt
->else_list
);
433 bld
.emit(BRW_OPCODE_ENDIF
);
435 if (devinfo
->gen
< 7)
436 limit_dispatch_width(16, "Non-uniform control flow unsupported "
441 fs_visitor::nir_emit_loop(nir_loop
*loop
)
443 bld
.emit(BRW_OPCODE_DO
);
445 nir_emit_cf_list(&loop
->body
);
447 bld
.emit(BRW_OPCODE_WHILE
);
449 if (devinfo
->gen
< 7)
450 limit_dispatch_width(16, "Non-uniform control flow unsupported "
455 fs_visitor::nir_emit_block(nir_block
*block
)
457 nir_foreach_instr(instr
, block
) {
458 nir_emit_instr(instr
);
463 fs_visitor::nir_emit_instr(nir_instr
*instr
)
465 const fs_builder abld
= bld
.annotate(NULL
, instr
);
467 switch (instr
->type
) {
468 case nir_instr_type_alu
:
469 nir_emit_alu(abld
, nir_instr_as_alu(instr
), true);
472 case nir_instr_type_deref
:
473 unreachable("All derefs should've been lowered");
476 case nir_instr_type_intrinsic
:
478 case MESA_SHADER_VERTEX
:
479 nir_emit_vs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
481 case MESA_SHADER_TESS_CTRL
:
482 nir_emit_tcs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
484 case MESA_SHADER_TESS_EVAL
:
485 nir_emit_tes_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
487 case MESA_SHADER_GEOMETRY
:
488 nir_emit_gs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
490 case MESA_SHADER_FRAGMENT
:
491 nir_emit_fs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
493 case MESA_SHADER_COMPUTE
:
494 nir_emit_cs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
497 unreachable("unsupported shader stage");
501 case nir_instr_type_tex
:
502 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
505 case nir_instr_type_load_const
:
506 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
509 case nir_instr_type_ssa_undef
:
510 /* We create a new VGRF for undefs on every use (by handling
511 * them in get_nir_src()), rather than for each definition.
512 * This helps register coalescing eliminate MOVs from undef.
516 case nir_instr_type_jump
:
517 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
521 unreachable("unknown instruction type");
526 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
530 fs_visitor::optimize_extract_to_float(nir_alu_instr
*instr
,
531 const fs_reg
&result
)
533 if (!instr
->src
[0].src
.is_ssa
||
534 !instr
->src
[0].src
.ssa
->parent_instr
)
537 if (instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
540 nir_alu_instr
*src0
=
541 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
543 if (src0
->op
!= nir_op_extract_u8
&& src0
->op
!= nir_op_extract_u16
&&
544 src0
->op
!= nir_op_extract_i8
&& src0
->op
!= nir_op_extract_i16
)
547 /* If either opcode has source modifiers, bail.
549 * TODO: We can potentially handle source modifiers if both of the opcodes
550 * we're combining are signed integers.
552 if (instr
->src
[0].abs
|| instr
->src
[0].negate
||
553 src0
->src
[0].abs
|| src0
->src
[0].negate
)
556 unsigned element
= nir_src_as_uint(src0
->src
[1].src
);
558 /* Element type to extract.*/
559 const brw_reg_type type
= brw_int_type(
560 src0
->op
== nir_op_extract_u16
|| src0
->op
== nir_op_extract_i16
? 2 : 1,
561 src0
->op
== nir_op_extract_i16
|| src0
->op
== nir_op_extract_i8
);
563 fs_reg op0
= get_nir_src(src0
->src
[0].src
);
564 op0
.type
= brw_type_for_nir_type(devinfo
,
565 (nir_alu_type
)(nir_op_infos
[src0
->op
].input_types
[0] |
566 nir_src_bit_size(src0
->src
[0].src
)));
567 op0
= offset(op0
, bld
, src0
->src
[0].swizzle
[0]);
569 set_saturate(instr
->dest
.saturate
,
570 bld
.MOV(result
, subscript(op0
, type
, element
)));
575 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
576 const fs_reg
&result
)
578 nir_intrinsic_instr
*src0
= nir_src_as_intrinsic(instr
->src
[0].src
);
579 if (src0
== NULL
|| src0
->intrinsic
!= nir_intrinsic_load_front_face
)
582 if (!nir_src_is_const(instr
->src
[1].src
) ||
583 !nir_src_is_const(instr
->src
[2].src
))
586 const float value1
= nir_src_as_float(instr
->src
[1].src
);
587 const float value2
= nir_src_as_float(instr
->src
[2].src
);
588 if (fabsf(value1
) != 1.0f
|| fabsf(value2
) != 1.0f
)
591 /* nir_opt_algebraic should have gotten rid of bcsel(b, a, a) */
592 assert(value1
== -value2
);
594 fs_reg tmp
= vgrf(glsl_type::int_type
);
596 if (devinfo
->gen
>= 12) {
597 /* Bit 15 of g1.1 is 0 if the polygon is front facing. */
598 fs_reg g1
= fs_reg(retype(brw_vec1_grf(1, 1), BRW_REGISTER_TYPE_W
));
600 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
602 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
603 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
605 * and negate the result for (gl_FrontFacing ? -1.0 : 1.0).
607 bld
.OR(subscript(tmp
, BRW_REGISTER_TYPE_W
, 1),
608 g1
, brw_imm_uw(0x3f80));
611 bld
.MOV(tmp
, negate(tmp
));
613 } else if (devinfo
->gen
>= 6) {
614 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
615 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
617 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
619 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
620 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
622 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
624 * This negation looks like it's safe in practice, because bits 0:4 will
625 * surely be TRIANGLES
628 if (value1
== -1.0f
) {
632 bld
.OR(subscript(tmp
, BRW_REGISTER_TYPE_W
, 1),
633 g0
, brw_imm_uw(0x3f80));
635 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
636 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
638 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
640 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
641 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
643 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
645 * This negation looks like it's safe in practice, because bits 0:4 will
646 * surely be TRIANGLES
649 if (value1
== -1.0f
) {
653 bld
.OR(tmp
, g1_6
, brw_imm_d(0x3f800000));
655 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, brw_imm_d(0xbf800000));
661 emit_find_msb_using_lzd(const fs_builder
&bld
,
662 const fs_reg
&result
,
670 /* LZD of an absolute value source almost always does the right
671 * thing. There are two problem values:
673 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
674 * 0. However, findMSB(int(0x80000000)) == 30.
676 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
677 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
679 * For a value of zero or negative one, -1 will be returned.
681 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
682 * findMSB(-(1<<x)) should return x-1.
684 * For all negative number cases, including 0x80000000 and
685 * 0xffffffff, the correct value is obtained from LZD if instead of
686 * negating the (already negative) value the logical-not is used. A
687 * conditonal logical-not can be achieved in two instructions.
689 temp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
691 bld
.ASR(temp
, src
, brw_imm_d(31));
692 bld
.XOR(temp
, temp
, src
);
695 bld
.LZD(retype(result
, BRW_REGISTER_TYPE_UD
),
696 retype(temp
, BRW_REGISTER_TYPE_UD
));
698 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
699 * from the LSB side. Subtract the result from 31 to convert the MSB
700 * count into an LSB count. If no bits are set, LZD will return 32.
701 * 31-32 = -1, which is exactly what findMSB() is supposed to return.
703 inst
= bld
.ADD(result
, retype(result
, BRW_REGISTER_TYPE_D
), brw_imm_d(31));
704 inst
->src
[0].negate
= true;
708 brw_rnd_mode_from_nir_op (const nir_op op
) {
710 case nir_op_f2f16_rtz
:
711 return BRW_RND_MODE_RTZ
;
712 case nir_op_f2f16_rtne
:
713 return BRW_RND_MODE_RTNE
;
715 unreachable("Operation doesn't support rounding mode");
720 brw_rnd_mode_from_execution_mode(unsigned execution_mode
)
722 if (nir_has_any_rounding_mode_rtne(execution_mode
))
723 return BRW_RND_MODE_RTNE
;
724 if (nir_has_any_rounding_mode_rtz(execution_mode
))
725 return BRW_RND_MODE_RTZ
;
726 return BRW_RND_MODE_UNSPECIFIED
;
730 fs_visitor::prepare_alu_destination_and_sources(const fs_builder
&bld
,
731 nir_alu_instr
*instr
,
736 need_dest
? get_nir_dest(instr
->dest
.dest
) : bld
.null_reg_ud();
738 result
.type
= brw_type_for_nir_type(devinfo
,
739 (nir_alu_type
)(nir_op_infos
[instr
->op
].output_type
|
740 nir_dest_bit_size(instr
->dest
.dest
)));
742 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
743 op
[i
] = get_nir_src(instr
->src
[i
].src
);
744 op
[i
].type
= brw_type_for_nir_type(devinfo
,
745 (nir_alu_type
)(nir_op_infos
[instr
->op
].input_types
[i
] |
746 nir_src_bit_size(instr
->src
[i
].src
)));
747 op
[i
].abs
= instr
->src
[i
].abs
;
748 op
[i
].negate
= instr
->src
[i
].negate
;
751 /* Move and vecN instrutions may still be vectored. Return the raw,
752 * vectored source and destination so that fs_visitor::nir_emit_alu can
753 * handle it. Other callers should not have to handle these kinds of
766 /* At this point, we have dealt with any instruction that operates on
767 * more than a single channel. Therefore, we can just adjust the source
768 * and destination registers for that channel and emit the instruction.
770 unsigned channel
= 0;
771 if (nir_op_infos
[instr
->op
].output_size
== 0) {
772 /* Since NIR is doing the scalarizing for us, we should only ever see
773 * vectorized operations with a single channel.
775 assert(util_bitcount(instr
->dest
.write_mask
) == 1);
776 channel
= ffs(instr
->dest
.write_mask
) - 1;
778 result
= offset(result
, bld
, channel
);
781 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
782 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
783 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
790 fs_visitor::resolve_inot_sources(const fs_builder
&bld
, nir_alu_instr
*instr
,
793 for (unsigned i
= 0; i
< 2; i
++) {
794 nir_alu_instr
*inot_instr
= nir_src_as_alu_instr(instr
->src
[i
].src
);
796 if (inot_instr
!= NULL
&& inot_instr
->op
== nir_op_inot
&&
797 !inot_instr
->src
[0].abs
&& !inot_instr
->src
[0].negate
) {
798 /* The source of the inot is now the source of instr. */
799 prepare_alu_destination_and_sources(bld
, inot_instr
, &op
[i
], false);
801 assert(!op
[i
].negate
);
804 op
[i
] = resolve_source_modifiers(op
[i
]);
810 fs_visitor::try_emit_b2fi_of_inot(const fs_builder
&bld
,
812 nir_alu_instr
*instr
)
814 if (devinfo
->gen
< 6 || devinfo
->gen
>= 12)
817 nir_alu_instr
*inot_instr
= nir_src_as_alu_instr(instr
->src
[0].src
);
819 if (inot_instr
== NULL
|| inot_instr
->op
!= nir_op_inot
)
822 /* HF is also possible as a destination on BDW+. For nir_op_b2i, the set
823 * of valid size-changing combinations is a bit more complex.
825 * The source restriction is just because I was lazy about generating the
828 if (nir_dest_bit_size(instr
->dest
.dest
) != 32 ||
829 nir_src_bit_size(inot_instr
->src
[0].src
) != 32)
832 /* b2[fi](inot(a)) maps a=0 => 1, a=-1 => 0. Since a can only be 0 or -1,
833 * this is float(1 + a).
837 prepare_alu_destination_and_sources(bld
, inot_instr
, &op
, false);
839 /* Ignore the saturate modifier, if there is one. The result of the
840 * arithmetic can only be 0 or 1, so the clamping will do nothing anyway.
842 bld
.ADD(result
, op
, brw_imm_d(1));
848 * Emit code for nir_op_fsign possibly fused with a nir_op_fmul
850 * If \c instr is not the \c nir_op_fsign, then \c fsign_src is the index of
851 * the source of \c instr that is a \c nir_op_fsign.
854 fs_visitor::emit_fsign(const fs_builder
&bld
, const nir_alu_instr
*instr
,
855 fs_reg result
, fs_reg
*op
, unsigned fsign_src
)
859 assert(instr
->op
== nir_op_fsign
|| instr
->op
== nir_op_fmul
);
860 assert(fsign_src
< nir_op_infos
[instr
->op
].num_inputs
);
862 if (instr
->op
!= nir_op_fsign
) {
863 const nir_alu_instr
*const fsign_instr
=
864 nir_src_as_alu_instr(instr
->src
[fsign_src
].src
);
866 assert(!fsign_instr
->dest
.saturate
);
868 /* op[fsign_src] has the nominal result of the fsign, and op[1 -
869 * fsign_src] has the other multiply source. This must be rearranged so
870 * that op[0] is the source of the fsign op[1] is the other multiply
876 op
[0] = get_nir_src(fsign_instr
->src
[0].src
);
878 const nir_alu_type t
=
879 (nir_alu_type
)(nir_op_infos
[instr
->op
].input_types
[0] |
880 nir_src_bit_size(fsign_instr
->src
[0].src
));
882 op
[0].type
= brw_type_for_nir_type(devinfo
, t
);
883 op
[0].abs
= fsign_instr
->src
[0].abs
;
884 op
[0].negate
= fsign_instr
->src
[0].negate
;
886 unsigned channel
= 0;
887 if (nir_op_infos
[instr
->op
].output_size
== 0) {
888 /* Since NIR is doing the scalarizing for us, we should only ever see
889 * vectorized operations with a single channel.
891 assert(util_bitcount(instr
->dest
.write_mask
) == 1);
892 channel
= ffs(instr
->dest
.write_mask
) - 1;
895 op
[0] = offset(op
[0], bld
, fsign_instr
->src
[0].swizzle
[channel
]);
897 /* Resolve any source modifiers. We could do slightly better on Gen8+
898 * if the only source modifier is negation, but *shrug*.
900 if (op
[1].negate
|| op
[1].abs
) {
901 fs_reg tmp
= bld
.vgrf(op
[1].type
);
907 assert(!instr
->dest
.saturate
);
911 /* Straightforward since the source can be assumed to be either strictly
912 * >= 0 or strictly <= 0 depending on the setting of the negate flag.
914 set_condmod(BRW_CONDITIONAL_NZ
, bld
.MOV(result
, op
[0]));
916 if (instr
->op
== nir_op_fsign
) {
917 inst
= (op
[0].negate
)
918 ? bld
.MOV(result
, brw_imm_f(-1.0f
))
919 : bld
.MOV(result
, brw_imm_f(1.0f
));
921 op
[1].negate
= (op
[0].negate
!= op
[1].negate
);
922 inst
= bld
.MOV(result
, op
[1]);
925 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
926 } else if (type_sz(op
[0].type
) == 2) {
927 /* AND(val, 0x8000) gives the sign bit.
929 * Predicated OR ORs 1.0 (0x3c00) with the sign bit if val is not zero.
931 fs_reg zero
= retype(brw_imm_uw(0), BRW_REGISTER_TYPE_HF
);
932 bld
.CMP(bld
.null_reg_f(), op
[0], zero
, BRW_CONDITIONAL_NZ
);
934 op
[0].type
= BRW_REGISTER_TYPE_UW
;
935 result
.type
= BRW_REGISTER_TYPE_UW
;
936 bld
.AND(result
, op
[0], brw_imm_uw(0x8000u
));
938 if (instr
->op
== nir_op_fsign
)
939 inst
= bld
.OR(result
, result
, brw_imm_uw(0x3c00u
));
941 /* Use XOR here to get the result sign correct. */
942 inst
= bld
.XOR(result
, result
, retype(op
[1], BRW_REGISTER_TYPE_UW
));
945 inst
->predicate
= BRW_PREDICATE_NORMAL
;
946 } else if (type_sz(op
[0].type
) == 4) {
947 /* AND(val, 0x80000000) gives the sign bit.
949 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
952 bld
.CMP(bld
.null_reg_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
954 op
[0].type
= BRW_REGISTER_TYPE_UD
;
955 result
.type
= BRW_REGISTER_TYPE_UD
;
956 bld
.AND(result
, op
[0], brw_imm_ud(0x80000000u
));
958 if (instr
->op
== nir_op_fsign
)
959 inst
= bld
.OR(result
, result
, brw_imm_ud(0x3f800000u
));
961 /* Use XOR here to get the result sign correct. */
962 inst
= bld
.XOR(result
, result
, retype(op
[1], BRW_REGISTER_TYPE_UD
));
965 inst
->predicate
= BRW_PREDICATE_NORMAL
;
967 /* For doubles we do the same but we need to consider:
969 * - 2-src instructions can't operate with 64-bit immediates
970 * - The sign is encoded in the high 32-bit of each DF
971 * - We need to produce a DF result.
974 fs_reg zero
= vgrf(glsl_type::double_type
);
975 bld
.MOV(zero
, setup_imm_df(bld
, 0.0));
976 bld
.CMP(bld
.null_reg_df(), op
[0], zero
, BRW_CONDITIONAL_NZ
);
978 bld
.MOV(result
, zero
);
980 fs_reg r
= subscript(result
, BRW_REGISTER_TYPE_UD
, 1);
981 bld
.AND(r
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 1),
982 brw_imm_ud(0x80000000u
));
984 if (instr
->op
== nir_op_fsign
) {
985 set_predicate(BRW_PREDICATE_NORMAL
,
986 bld
.OR(r
, r
, brw_imm_ud(0x3ff00000u
)));
988 /* This could be done better in some cases. If the scale is an
989 * immediate with the low 32-bits all 0, emitting a separate XOR and
990 * OR would allow an algebraic optimization to remove the OR. There
991 * are currently zero instances of fsign(double(x))*IMM in shader-db
992 * or any test suite, so it is hard to care at this time.
994 fs_reg result_int64
= retype(result
, BRW_REGISTER_TYPE_UQ
);
995 inst
= bld
.XOR(result_int64
, result_int64
,
996 retype(op
[1], BRW_REGISTER_TYPE_UQ
));
1002 * Deteremine whether sources of a nir_op_fmul can be fused with a nir_op_fsign
1004 * Checks the operands of a \c nir_op_fmul to determine whether or not
1005 * \c emit_fsign could fuse the multiplication with the \c sign() calculation.
1007 * \param instr The multiplication instruction
1009 * \param fsign_src The source of \c instr that may or may not be a
1013 can_fuse_fmul_fsign(nir_alu_instr
*instr
, unsigned fsign_src
)
1015 assert(instr
->op
== nir_op_fmul
);
1017 nir_alu_instr
*const fsign_instr
=
1018 nir_src_as_alu_instr(instr
->src
[fsign_src
].src
);
1022 * 1. instr->src[fsign_src] must be a nir_op_fsign.
1023 * 2. The nir_op_fsign can only be used by this multiplication.
1024 * 3. The source that is the nir_op_fsign does not have source modifiers.
1025 * \c emit_fsign only examines the source modifiers of the source of the
1028 * The nir_op_fsign must also not have the saturate modifier, but steps
1029 * have already been taken (in nir_opt_algebraic) to ensure that.
1031 return fsign_instr
!= NULL
&& fsign_instr
->op
== nir_op_fsign
&&
1032 is_used_once(fsign_instr
) &&
1033 !instr
->src
[fsign_src
].abs
&& !instr
->src
[fsign_src
].negate
;
1037 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
,
1040 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
1042 unsigned execution_mode
=
1043 bld
.shader
->nir
->info
.float_controls_execution_mode
;
1046 fs_reg result
= prepare_alu_destination_and_sources(bld
, instr
, op
, need_dest
);
1048 switch (instr
->op
) {
1053 fs_reg temp
= result
;
1054 bool need_extra_copy
= false;
1055 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
1056 if (!instr
->src
[i
].src
.is_ssa
&&
1057 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
1058 need_extra_copy
= true;
1059 temp
= bld
.vgrf(result
.type
, 4);
1064 for (unsigned i
= 0; i
< 4; i
++) {
1065 if (!(instr
->dest
.write_mask
& (1 << i
)))
1068 if (instr
->op
== nir_op_mov
) {
1069 inst
= bld
.MOV(offset(temp
, bld
, i
),
1070 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
1072 inst
= bld
.MOV(offset(temp
, bld
, i
),
1073 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
1075 inst
->saturate
= instr
->dest
.saturate
;
1078 /* In this case the source and destination registers were the same,
1079 * so we need to insert an extra set of moves in order to deal with
1082 if (need_extra_copy
) {
1083 for (unsigned i
= 0; i
< 4; i
++) {
1084 if (!(instr
->dest
.write_mask
& (1 << i
)))
1087 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
1095 if (optimize_extract_to_float(instr
, result
))
1097 inst
= bld
.MOV(result
, op
[0]);
1098 inst
->saturate
= instr
->dest
.saturate
;
1101 case nir_op_f2f16_rtne
:
1102 case nir_op_f2f16_rtz
:
1103 case nir_op_f2f16
: {
1104 brw_rnd_mode rnd
= BRW_RND_MODE_UNSPECIFIED
;
1106 if (nir_op_f2f16
== instr
->op
)
1107 rnd
= brw_rnd_mode_from_execution_mode(execution_mode
);
1109 rnd
= brw_rnd_mode_from_nir_op(instr
->op
);
1111 if (BRW_RND_MODE_UNSPECIFIED
!= rnd
)
1112 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(), brw_imm_d(rnd
));
1114 /* In theory, it would be better to use BRW_OPCODE_F32TO16. Depending
1115 * on the HW gen, it is a special hw opcode or just a MOV, and
1116 * brw_F32TO16 (at brw_eu_emit) would do the work to chose.
1118 * But if we want to use that opcode, we need to provide support on
1119 * different optimizations and lowerings. As right now HF support is
1120 * only for gen8+, it will be better to use directly the MOV, and use
1121 * BRW_OPCODE_F32TO16 when/if we work for HF support on gen7.
1123 assert(type_sz(op
[0].type
) < 8); /* brw_nir_lower_conversions */
1124 inst
= bld
.MOV(result
, op
[0]);
1125 inst
->saturate
= instr
->dest
.saturate
;
1136 if (try_emit_b2fi_of_inot(bld
, result
, instr
))
1138 op
[0].type
= BRW_REGISTER_TYPE_D
;
1139 op
[0].negate
= !op
[0].negate
;
1162 if (result
.type
== BRW_REGISTER_TYPE_B
||
1163 result
.type
== BRW_REGISTER_TYPE_UB
||
1164 result
.type
== BRW_REGISTER_TYPE_HF
)
1165 assert(type_sz(op
[0].type
) < 8); /* brw_nir_lower_conversions */
1167 if (op
[0].type
== BRW_REGISTER_TYPE_B
||
1168 op
[0].type
== BRW_REGISTER_TYPE_UB
||
1169 op
[0].type
== BRW_REGISTER_TYPE_HF
)
1170 assert(type_sz(result
.type
) < 8); /* brw_nir_lower_conversions */
1172 inst
= bld
.MOV(result
, op
[0]);
1173 inst
->saturate
= instr
->dest
.saturate
;
1177 inst
= bld
.MOV(result
, op
[0]);
1178 inst
->saturate
= true;
1183 op
[0].negate
= true;
1184 inst
= bld
.MOV(result
, op
[0]);
1185 if (instr
->op
== nir_op_fneg
)
1186 inst
->saturate
= instr
->dest
.saturate
;
1191 op
[0].negate
= false;
1193 inst
= bld
.MOV(result
, op
[0]);
1194 if (instr
->op
== nir_op_fabs
)
1195 inst
->saturate
= instr
->dest
.saturate
;
1199 if (nir_has_any_rounding_mode_enabled(execution_mode
)) {
1201 brw_rnd_mode_from_execution_mode(execution_mode
);
1202 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(),
1206 if (op
[0].type
== BRW_REGISTER_TYPE_HF
)
1207 assert(type_sz(result
.type
) < 8); /* brw_nir_lower_conversions */
1209 inst
= bld
.MOV(result
, op
[0]);
1210 inst
->saturate
= instr
->dest
.saturate
;
1214 emit_fsign(bld
, instr
, result
, op
, 0);
1218 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
1219 inst
->saturate
= instr
->dest
.saturate
;
1223 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
1224 inst
->saturate
= instr
->dest
.saturate
;
1228 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
1229 inst
->saturate
= instr
->dest
.saturate
;
1233 inst
= bld
.emit(SHADER_OPCODE_SIN
, result
, op
[0]);
1234 inst
->saturate
= instr
->dest
.saturate
;
1238 inst
= bld
.emit(SHADER_OPCODE_COS
, result
, op
[0]);
1239 inst
->saturate
= instr
->dest
.saturate
;
1243 if (fs_key
->high_quality_derivatives
) {
1244 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
1246 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
1248 inst
->saturate
= instr
->dest
.saturate
;
1250 case nir_op_fddx_fine
:
1251 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
1252 inst
->saturate
= instr
->dest
.saturate
;
1254 case nir_op_fddx_coarse
:
1255 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
1256 inst
->saturate
= instr
->dest
.saturate
;
1259 if (fs_key
->high_quality_derivatives
) {
1260 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0]);
1262 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0]);
1264 inst
->saturate
= instr
->dest
.saturate
;
1266 case nir_op_fddy_fine
:
1267 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0]);
1268 inst
->saturate
= instr
->dest
.saturate
;
1270 case nir_op_fddy_coarse
:
1271 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0]);
1272 inst
->saturate
= instr
->dest
.saturate
;
1276 if (nir_has_any_rounding_mode_enabled(execution_mode
)) {
1278 brw_rnd_mode_from_execution_mode(execution_mode
);
1279 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(),
1284 inst
= bld
.ADD(result
, op
[0], op
[1]);
1285 inst
->saturate
= instr
->dest
.saturate
;
1288 case nir_op_iadd_sat
:
1289 case nir_op_uadd_sat
:
1290 inst
= bld
.ADD(result
, op
[0], op
[1]);
1291 inst
->saturate
= true;
1294 case nir_op_isub_sat
:
1295 bld
.emit(SHADER_OPCODE_ISUB_SAT
, result
, op
[0], op
[1]);
1298 case nir_op_usub_sat
:
1299 bld
.emit(SHADER_OPCODE_USUB_SAT
, result
, op
[0], op
[1]);
1304 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1305 inst
= bld
.AVG(result
, op
[0], op
[1]);
1309 case nir_op_uhadd
: {
1310 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1311 fs_reg tmp
= bld
.vgrf(result
.type
);
1313 if (devinfo
->gen
>= 8) {
1314 op
[0] = resolve_source_modifiers(op
[0]);
1315 op
[1] = resolve_source_modifiers(op
[1]);
1318 /* AVG(x, y) - ((x ^ y) & 1) */
1319 bld
.XOR(tmp
, op
[0], op
[1]);
1320 bld
.AND(tmp
, tmp
, retype(brw_imm_ud(1), result
.type
));
1321 bld
.AVG(result
, op
[0], op
[1]);
1322 inst
= bld
.ADD(result
, result
, tmp
);
1323 inst
->src
[1].negate
= true;
1328 for (unsigned i
= 0; i
< 2; i
++) {
1329 if (can_fuse_fmul_fsign(instr
, i
)) {
1330 emit_fsign(bld
, instr
, result
, op
, i
);
1335 /* We emit the rounding mode after the previous fsign optimization since
1336 * it won't result in a MUL, but will try to negate the value by other
1339 if (nir_has_any_rounding_mode_enabled(execution_mode
)) {
1341 brw_rnd_mode_from_execution_mode(execution_mode
);
1342 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(),
1346 inst
= bld
.MUL(result
, op
[0], op
[1]);
1347 inst
->saturate
= instr
->dest
.saturate
;
1350 case nir_op_imul_2x32_64
:
1351 case nir_op_umul_2x32_64
:
1352 bld
.MUL(result
, op
[0], op
[1]);
1355 case nir_op_imul_32x16
:
1356 case nir_op_umul_32x16
: {
1357 const bool ud
= instr
->op
== nir_op_umul_32x16
;
1359 assert(nir_dest_bit_size(instr
->dest
.dest
) == 32);
1361 /* Before Gen7, the order of the 32-bit source and the 16-bit source was
1362 * swapped. The extension isn't enabled on those platforms, so don't
1363 * pretend to support the differences.
1365 assert(devinfo
->gen
>= 7);
1367 if (op
[1].file
== IMM
)
1368 op
[1] = ud
? brw_imm_uw(op
[1].ud
) : brw_imm_w(op
[1].d
);
1370 const enum brw_reg_type word_type
=
1371 ud
? BRW_REGISTER_TYPE_UW
: BRW_REGISTER_TYPE_W
;
1373 op
[1] = subscript(op
[1], word_type
, 0);
1376 const enum brw_reg_type dword_type
=
1377 ud
? BRW_REGISTER_TYPE_UD
: BRW_REGISTER_TYPE_D
;
1379 bld
.MUL(result
, retype(op
[0], dword_type
), op
[1]);
1384 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1385 bld
.MUL(result
, op
[0], op
[1]);
1388 case nir_op_imul_high
:
1389 case nir_op_umul_high
:
1390 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1391 bld
.emit(SHADER_OPCODE_MULH
, result
, op
[0], op
[1]);
1396 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1397 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
1400 case nir_op_uadd_carry
:
1401 unreachable("Should have been lowered by carry_to_arith().");
1403 case nir_op_usub_borrow
:
1404 unreachable("Should have been lowered by borrow_to_arith().");
1408 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1409 * appears that our hardware just does the right thing for signed
1412 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1413 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
1417 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1418 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
1420 /* Math instructions don't support conditional mod */
1421 inst
= bld
.MOV(bld
.null_reg_d(), result
);
1422 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1424 /* Now, we need to determine if signs of the sources are different.
1425 * When we XOR the sources, the top bit is 0 if they are the same and 1
1426 * if they are different. We can then use a conditional modifier to
1427 * turn that into a predicate. This leads us to an XOR.l instruction.
1429 * Technically, according to the PRM, you're not allowed to use .l on a
1430 * XOR instruction. However, emperical experiments and Curro's reading
1431 * of the simulator source both indicate that it's safe.
1433 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1434 inst
= bld
.XOR(tmp
, op
[0], op
[1]);
1435 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1436 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1438 /* If the result of the initial remainder operation is non-zero and the
1439 * two sources have different signs, add in a copy of op[1] to get the
1440 * final integer modulus value.
1442 inst
= bld
.ADD(result
, result
, op
[1]);
1443 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1450 case nir_op_fne32
: {
1451 fs_reg dest
= result
;
1453 const uint32_t bit_size
= nir_src_bit_size(instr
->src
[0].src
);
1455 dest
= bld
.vgrf(op
[0].type
, 1);
1457 bld
.CMP(dest
, op
[0], op
[1], brw_cmod_for_nir_comparison(instr
->op
));
1459 if (bit_size
> 32) {
1460 bld
.MOV(result
, subscript(dest
, BRW_REGISTER_TYPE_UD
, 0));
1461 } else if(bit_size
< 32) {
1462 /* When we convert the result to 32-bit we need to be careful and do
1463 * it as a signed conversion to get sign extension (for 32-bit true)
1465 const brw_reg_type src_type
=
1466 brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_D
);
1468 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), retype(dest
, src_type
));
1478 case nir_op_ine32
: {
1479 fs_reg dest
= result
;
1481 /* On Gen11 we have an additional issue being that src1 cannot be a byte
1482 * type. So we convert both operands for the comparison.
1485 temp_op
[0] = bld
.fix_byte_src(op
[0]);
1486 temp_op
[1] = bld
.fix_byte_src(op
[1]);
1488 const uint32_t bit_size
= type_sz(temp_op
[0].type
) * 8;
1490 dest
= bld
.vgrf(temp_op
[0].type
, 1);
1492 bld
.CMP(dest
, temp_op
[0], temp_op
[1],
1493 brw_cmod_for_nir_comparison(instr
->op
));
1495 if (bit_size
> 32) {
1496 bld
.MOV(result
, subscript(dest
, BRW_REGISTER_TYPE_UD
, 0));
1497 } else if (bit_size
< 32) {
1498 /* When we convert the result to 32-bit we need to be careful and do
1499 * it as a signed conversion to get sign extension (for 32-bit true)
1501 const brw_reg_type src_type
=
1502 brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_D
);
1504 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), retype(dest
, src_type
));
1510 if (devinfo
->gen
>= 8) {
1511 nir_alu_instr
*inot_src_instr
= nir_src_as_alu_instr(instr
->src
[0].src
);
1513 if (inot_src_instr
!= NULL
&&
1514 (inot_src_instr
->op
== nir_op_ior
||
1515 inot_src_instr
->op
== nir_op_ixor
||
1516 inot_src_instr
->op
== nir_op_iand
) &&
1517 !inot_src_instr
->src
[0].abs
&&
1518 !inot_src_instr
->src
[0].negate
&&
1519 !inot_src_instr
->src
[1].abs
&&
1520 !inot_src_instr
->src
[1].negate
) {
1521 /* The sources of the source logical instruction are now the
1522 * sources of the instruction that will be generated.
1524 prepare_alu_destination_and_sources(bld
, inot_src_instr
, op
, false);
1525 resolve_inot_sources(bld
, inot_src_instr
, op
);
1527 /* Smash all of the sources and destination to be signed. This
1528 * doesn't matter for the operation of the instruction, but cmod
1529 * propagation fails on unsigned sources with negation (due to
1530 * fs_inst::can_do_cmod returning false).
1533 brw_type_for_nir_type(devinfo
,
1534 (nir_alu_type
)(nir_type_int
|
1535 nir_dest_bit_size(instr
->dest
.dest
)));
1537 brw_type_for_nir_type(devinfo
,
1538 (nir_alu_type
)(nir_type_int
|
1539 nir_src_bit_size(inot_src_instr
->src
[0].src
)));
1541 brw_type_for_nir_type(devinfo
,
1542 (nir_alu_type
)(nir_type_int
|
1543 nir_src_bit_size(inot_src_instr
->src
[1].src
)));
1545 /* For XOR, only invert one of the sources. Arbitrarily choose
1548 op
[0].negate
= !op
[0].negate
;
1549 if (inot_src_instr
->op
!= nir_op_ixor
)
1550 op
[1].negate
= !op
[1].negate
;
1552 switch (inot_src_instr
->op
) {
1554 bld
.AND(result
, op
[0], op
[1]);
1558 bld
.OR(result
, op
[0], op
[1]);
1562 bld
.XOR(result
, op
[0], op
[1]);
1566 unreachable("impossible opcode");
1569 op
[0] = resolve_source_modifiers(op
[0]);
1571 bld
.NOT(result
, op
[0]);
1574 if (devinfo
->gen
>= 8) {
1575 resolve_inot_sources(bld
, instr
, op
);
1577 bld
.XOR(result
, op
[0], op
[1]);
1580 if (devinfo
->gen
>= 8) {
1581 resolve_inot_sources(bld
, instr
, op
);
1583 bld
.OR(result
, op
[0], op
[1]);
1586 if (devinfo
->gen
>= 8) {
1587 resolve_inot_sources(bld
, instr
, op
);
1589 bld
.AND(result
, op
[0], op
[1]);
1595 case nir_op_b32all_fequal2
:
1596 case nir_op_b32all_iequal2
:
1597 case nir_op_b32all_fequal3
:
1598 case nir_op_b32all_iequal3
:
1599 case nir_op_b32all_fequal4
:
1600 case nir_op_b32all_iequal4
:
1601 case nir_op_b32any_fnequal2
:
1602 case nir_op_b32any_inequal2
:
1603 case nir_op_b32any_fnequal3
:
1604 case nir_op_b32any_inequal3
:
1605 case nir_op_b32any_fnequal4
:
1606 case nir_op_b32any_inequal4
:
1607 unreachable("Lowered by nir_lower_alu_reductions");
1609 case nir_op_fnoise1_1
:
1610 case nir_op_fnoise1_2
:
1611 case nir_op_fnoise1_3
:
1612 case nir_op_fnoise1_4
:
1613 case nir_op_fnoise2_1
:
1614 case nir_op_fnoise2_2
:
1615 case nir_op_fnoise2_3
:
1616 case nir_op_fnoise2_4
:
1617 case nir_op_fnoise3_1
:
1618 case nir_op_fnoise3_2
:
1619 case nir_op_fnoise3_3
:
1620 case nir_op_fnoise3_4
:
1621 case nir_op_fnoise4_1
:
1622 case nir_op_fnoise4_2
:
1623 case nir_op_fnoise4_3
:
1624 case nir_op_fnoise4_4
:
1625 unreachable("not reached: should be handled by lower_noise");
1628 unreachable("not reached: should be handled by ldexp_to_arith()");
1631 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
1632 inst
->saturate
= instr
->dest
.saturate
;
1636 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
1637 inst
->saturate
= instr
->dest
.saturate
;
1641 case nir_op_f2b32
: {
1642 uint32_t bit_size
= nir_src_bit_size(instr
->src
[0].src
);
1643 if (bit_size
== 64) {
1644 /* two-argument instructions can't take 64-bit immediates */
1648 if (instr
->op
== nir_op_f2b32
) {
1649 zero
= vgrf(glsl_type::double_type
);
1650 tmp
= vgrf(glsl_type::double_type
);
1651 bld
.MOV(zero
, setup_imm_df(bld
, 0.0));
1653 zero
= vgrf(glsl_type::int64_t_type
);
1654 tmp
= vgrf(glsl_type::int64_t_type
);
1655 bld
.MOV(zero
, brw_imm_q(0));
1658 /* A SIMD16 execution needs to be split in two instructions, so use
1659 * a vgrf instead of the flag register as dst so instruction splitting
1662 bld
.CMP(tmp
, op
[0], zero
, BRW_CONDITIONAL_NZ
);
1663 bld
.MOV(result
, subscript(tmp
, BRW_REGISTER_TYPE_UD
, 0));
1666 if (bit_size
== 32) {
1667 zero
= instr
->op
== nir_op_f2b32
? brw_imm_f(0.0f
) : brw_imm_d(0);
1669 assert(bit_size
== 16);
1670 zero
= instr
->op
== nir_op_f2b32
?
1671 retype(brw_imm_w(0), BRW_REGISTER_TYPE_HF
) : brw_imm_w(0);
1673 bld
.CMP(result
, op
[0], zero
, BRW_CONDITIONAL_NZ
);
1679 inst
= bld
.RNDZ(result
, op
[0]);
1680 if (devinfo
->gen
< 6) {
1681 set_condmod(BRW_CONDITIONAL_R
, inst
);
1682 set_predicate(BRW_PREDICATE_NORMAL
,
1683 bld
.ADD(result
, result
, brw_imm_f(1.0f
)));
1684 inst
= bld
.MOV(result
, result
); /* for potential saturation */
1686 inst
->saturate
= instr
->dest
.saturate
;
1689 case nir_op_fceil
: {
1690 op
[0].negate
= !op
[0].negate
;
1691 fs_reg temp
= vgrf(glsl_type::float_type
);
1692 bld
.RNDD(temp
, op
[0]);
1694 inst
= bld
.MOV(result
, temp
);
1695 inst
->saturate
= instr
->dest
.saturate
;
1699 inst
= bld
.RNDD(result
, op
[0]);
1700 inst
->saturate
= instr
->dest
.saturate
;
1703 inst
= bld
.FRC(result
, op
[0]);
1704 inst
->saturate
= instr
->dest
.saturate
;
1706 case nir_op_fround_even
:
1707 inst
= bld
.RNDE(result
, op
[0]);
1708 if (devinfo
->gen
< 6) {
1709 set_condmod(BRW_CONDITIONAL_R
, inst
);
1710 set_predicate(BRW_PREDICATE_NORMAL
,
1711 bld
.ADD(result
, result
, brw_imm_f(1.0f
)));
1712 inst
= bld
.MOV(result
, result
); /* for potential saturation */
1714 inst
->saturate
= instr
->dest
.saturate
;
1717 case nir_op_fquantize2f16
: {
1718 fs_reg tmp16
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1719 fs_reg tmp32
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1720 fs_reg zero
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1722 /* The destination stride must be at least as big as the source stride. */
1723 tmp16
.type
= BRW_REGISTER_TYPE_W
;
1726 /* Check for denormal */
1727 fs_reg abs_src0
= op
[0];
1728 abs_src0
.abs
= true;
1729 bld
.CMP(bld
.null_reg_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1731 /* Get the appropriately signed zero */
1732 bld
.AND(retype(zero
, BRW_REGISTER_TYPE_UD
),
1733 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1734 brw_imm_ud(0x80000000));
1735 /* Do the actual F32 -> F16 -> F32 conversion */
1736 bld
.emit(BRW_OPCODE_F32TO16
, tmp16
, op
[0]);
1737 bld
.emit(BRW_OPCODE_F16TO32
, tmp32
, tmp16
);
1738 /* Select that or zero based on normal status */
1739 inst
= bld
.SEL(result
, zero
, tmp32
);
1740 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1741 inst
->saturate
= instr
->dest
.saturate
;
1748 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
1749 inst
->saturate
= instr
->dest
.saturate
;
1755 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
1756 inst
->saturate
= instr
->dest
.saturate
;
1759 case nir_op_pack_snorm_2x16
:
1760 case nir_op_pack_snorm_4x8
:
1761 case nir_op_pack_unorm_2x16
:
1762 case nir_op_pack_unorm_4x8
:
1763 case nir_op_unpack_snorm_2x16
:
1764 case nir_op_unpack_snorm_4x8
:
1765 case nir_op_unpack_unorm_2x16
:
1766 case nir_op_unpack_unorm_4x8
:
1767 case nir_op_unpack_half_2x16
:
1768 case nir_op_pack_half_2x16
:
1769 unreachable("not reached: should be handled by lower_packing_builtins");
1771 case nir_op_unpack_half_2x16_split_x_flush_to_zero
:
1772 assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
& execution_mode
);
1774 case nir_op_unpack_half_2x16_split_x
:
1775 inst
= bld
.emit(BRW_OPCODE_F16TO32
, result
,
1776 subscript(op
[0], BRW_REGISTER_TYPE_UW
, 0));
1777 inst
->saturate
= instr
->dest
.saturate
;
1780 case nir_op_unpack_half_2x16_split_y_flush_to_zero
:
1781 assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
& execution_mode
);
1783 case nir_op_unpack_half_2x16_split_y
:
1784 inst
= bld
.emit(BRW_OPCODE_F16TO32
, result
,
1785 subscript(op
[0], BRW_REGISTER_TYPE_UW
, 1));
1786 inst
->saturate
= instr
->dest
.saturate
;
1789 case nir_op_pack_64_2x32_split
:
1790 case nir_op_pack_32_2x16_split
:
1791 bld
.emit(FS_OPCODE_PACK
, result
, op
[0], op
[1]);
1794 case nir_op_unpack_64_2x32_split_x
:
1795 case nir_op_unpack_64_2x32_split_y
: {
1796 if (instr
->op
== nir_op_unpack_64_2x32_split_x
)
1797 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 0));
1799 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 1));
1803 case nir_op_unpack_32_2x16_split_x
:
1804 case nir_op_unpack_32_2x16_split_y
: {
1805 if (instr
->op
== nir_op_unpack_32_2x16_split_x
)
1806 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UW
, 0));
1808 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UW
, 1));
1813 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
1814 inst
->saturate
= instr
->dest
.saturate
;
1817 case nir_op_bitfield_reverse
:
1818 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1819 bld
.BFREV(result
, op
[0]);
1822 case nir_op_bit_count
:
1823 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1824 bld
.CBIT(result
, op
[0]);
1827 case nir_op_ufind_msb
: {
1828 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1829 emit_find_msb_using_lzd(bld
, result
, op
[0], false);
1834 assert(nir_dest_bit_size(instr
->dest
.dest
) == 32);
1835 bld
.LZD(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1838 case nir_op_ifind_msb
: {
1839 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1841 if (devinfo
->gen
< 7) {
1842 emit_find_msb_using_lzd(bld
, result
, op
[0], true);
1844 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1846 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1847 * count from the LSB side. If FBH didn't return an error
1848 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1849 * count into an LSB count.
1851 bld
.CMP(bld
.null_reg_d(), result
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1853 inst
= bld
.ADD(result
, result
, brw_imm_d(31));
1854 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1855 inst
->src
[0].negate
= true;
1860 case nir_op_find_lsb
:
1861 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1863 if (devinfo
->gen
< 7) {
1864 fs_reg temp
= vgrf(glsl_type::int_type
);
1866 /* (x & -x) generates a value that consists of only the LSB of x.
1867 * For all powers of 2, findMSB(y) == findLSB(y).
1869 fs_reg src
= retype(op
[0], BRW_REGISTER_TYPE_D
);
1870 fs_reg negated_src
= src
;
1872 /* One must be negated, and the other must be non-negated. It
1873 * doesn't matter which is which.
1875 negated_src
.negate
= true;
1878 bld
.AND(temp
, src
, negated_src
);
1879 emit_find_msb_using_lzd(bld
, result
, temp
, false);
1881 bld
.FBL(result
, op
[0]);
1885 case nir_op_ubitfield_extract
:
1886 case nir_op_ibitfield_extract
:
1887 unreachable("should have been lowered");
1890 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1891 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1894 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1895 bld
.BFI1(result
, op
[0], op
[1]);
1898 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1899 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1902 case nir_op_bitfield_insert
:
1903 unreachable("not reached: should have been lowered");
1906 bld
.SHL(result
, op
[0], op
[1]);
1909 bld
.ASR(result
, op
[0], op
[1]);
1912 bld
.SHR(result
, op
[0], op
[1]);
1916 bld
.ROL(result
, op
[0], op
[1]);
1919 bld
.ROR(result
, op
[0], op
[1]);
1922 case nir_op_pack_half_2x16_split
:
1923 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1927 if (nir_has_any_rounding_mode_enabled(execution_mode
)) {
1929 brw_rnd_mode_from_execution_mode(execution_mode
);
1930 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(),
1934 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1935 inst
->saturate
= instr
->dest
.saturate
;
1939 if (nir_has_any_rounding_mode_enabled(execution_mode
)) {
1941 brw_rnd_mode_from_execution_mode(execution_mode
);
1942 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(),
1946 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1947 inst
->saturate
= instr
->dest
.saturate
;
1950 case nir_op_b32csel
:
1951 if (optimize_frontfacing_ternary(instr
, result
))
1954 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1955 inst
= bld
.SEL(result
, op
[1], op
[2]);
1956 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1959 case nir_op_extract_u8
:
1960 case nir_op_extract_i8
: {
1961 unsigned byte
= nir_src_as_uint(instr
->src
[1].src
);
1966 * There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.
1967 * Use two instructions and a word or DWord intermediate integer type.
1969 if (nir_dest_bit_size(instr
->dest
.dest
) == 64) {
1970 const brw_reg_type type
= brw_int_type(1, instr
->op
== nir_op_extract_i8
);
1972 if (instr
->op
== nir_op_extract_i8
) {
1973 /* If we need to sign extend, extract to a word first */
1974 fs_reg w_temp
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
1975 bld
.MOV(w_temp
, subscript(op
[0], type
, byte
));
1976 bld
.MOV(result
, w_temp
);
1977 } else if (byte
& 1) {
1978 /* Extract the high byte from the word containing the desired byte
1982 subscript(op
[0], BRW_REGISTER_TYPE_UW
, byte
/ 2),
1985 /* Otherwise use an AND with 0xff and a word type */
1987 subscript(op
[0], BRW_REGISTER_TYPE_UW
, byte
/ 2),
1991 const brw_reg_type type
= brw_int_type(1, instr
->op
== nir_op_extract_i8
);
1992 bld
.MOV(result
, subscript(op
[0], type
, byte
));
1997 case nir_op_extract_u16
:
1998 case nir_op_extract_i16
: {
1999 const brw_reg_type type
= brw_int_type(2, instr
->op
== nir_op_extract_i16
);
2000 unsigned word
= nir_src_as_uint(instr
->src
[1].src
);
2001 bld
.MOV(result
, subscript(op
[0], type
, word
));
2006 unreachable("unhandled instruction");
2009 /* If we need to do a boolean resolve, replace the result with -(x & 1)
2010 * to sign extend the low bit to 0/~0
2012 if (devinfo
->gen
<= 5 &&
2013 !result
.is_null() &&
2014 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
2015 fs_reg masked
= vgrf(glsl_type::int_type
);
2016 bld
.AND(masked
, result
, brw_imm_d(1));
2017 masked
.negate
= true;
2018 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
2023 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
2024 nir_load_const_instr
*instr
)
2026 const brw_reg_type reg_type
=
2027 brw_reg_type_from_bit_size(instr
->def
.bit_size
, BRW_REGISTER_TYPE_D
);
2028 fs_reg reg
= bld
.vgrf(reg_type
, instr
->def
.num_components
);
2030 switch (instr
->def
.bit_size
) {
2032 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
2033 bld
.MOV(offset(reg
, bld
, i
), setup_imm_b(bld
, instr
->value
[i
].i8
));
2037 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
2038 bld
.MOV(offset(reg
, bld
, i
), brw_imm_w(instr
->value
[i
].i16
));
2042 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
2043 bld
.MOV(offset(reg
, bld
, i
), brw_imm_d(instr
->value
[i
].i32
));
2047 assert(devinfo
->gen
>= 7);
2048 if (devinfo
->gen
== 7) {
2049 /* We don't get 64-bit integer types until gen8 */
2050 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
2051 bld
.MOV(retype(offset(reg
, bld
, i
), BRW_REGISTER_TYPE_DF
),
2052 setup_imm_df(bld
, instr
->value
[i
].f64
));
2055 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
2056 bld
.MOV(offset(reg
, bld
, i
), brw_imm_q(instr
->value
[i
].i64
));
2061 unreachable("Invalid bit size");
2064 nir_ssa_values
[instr
->def
.index
] = reg
;
2068 fs_visitor::get_nir_src(const nir_src
&src
)
2072 if (src
.ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
2073 const brw_reg_type reg_type
=
2074 brw_reg_type_from_bit_size(src
.ssa
->bit_size
, BRW_REGISTER_TYPE_D
);
2075 reg
= bld
.vgrf(reg_type
, src
.ssa
->num_components
);
2077 reg
= nir_ssa_values
[src
.ssa
->index
];
2080 /* We don't handle indirects on locals */
2081 assert(src
.reg
.indirect
== NULL
);
2082 reg
= offset(nir_locals
[src
.reg
.reg
->index
], bld
,
2083 src
.reg
.base_offset
* src
.reg
.reg
->num_components
);
2086 if (nir_src_bit_size(src
) == 64 && devinfo
->gen
== 7) {
2087 /* The only 64-bit type available on gen7 is DF, so use that. */
2088 reg
.type
= BRW_REGISTER_TYPE_DF
;
2090 /* To avoid floating-point denorm flushing problems, set the type by
2091 * default to an integer type - instructions that need floating point
2092 * semantics will set this to F if they need to
2094 reg
.type
= brw_reg_type_from_bit_size(nir_src_bit_size(src
),
2095 BRW_REGISTER_TYPE_D
);
2102 * Return an IMM for constants; otherwise call get_nir_src() as normal.
2104 * This function should not be called on any value which may be 64 bits.
2105 * We could theoretically support 64-bit on gen8+ but we choose not to
2106 * because it wouldn't work in general (no gen7 support) and there are
2107 * enough restrictions in 64-bit immediates that you can't take the return
2108 * value and treat it the same as the result of get_nir_src().
2111 fs_visitor::get_nir_src_imm(const nir_src
&src
)
2113 assert(nir_src_bit_size(src
) == 32);
2114 return nir_src_is_const(src
) ?
2115 fs_reg(brw_imm_d(nir_src_as_int(src
))) : get_nir_src(src
);
2119 fs_visitor::get_nir_dest(const nir_dest
&dest
)
2122 const brw_reg_type reg_type
=
2123 brw_reg_type_from_bit_size(dest
.ssa
.bit_size
,
2124 dest
.ssa
.bit_size
== 8 ?
2125 BRW_REGISTER_TYPE_D
:
2126 BRW_REGISTER_TYPE_F
);
2127 nir_ssa_values
[dest
.ssa
.index
] =
2128 bld
.vgrf(reg_type
, dest
.ssa
.num_components
);
2129 bld
.UNDEF(nir_ssa_values
[dest
.ssa
.index
]);
2130 return nir_ssa_values
[dest
.ssa
.index
];
2132 /* We don't handle indirects on locals */
2133 assert(dest
.reg
.indirect
== NULL
);
2134 return offset(nir_locals
[dest
.reg
.reg
->index
], bld
,
2135 dest
.reg
.base_offset
* dest
.reg
.reg
->num_components
);
2140 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
2143 for (unsigned i
= 0; i
< 4; i
++) {
2144 if (!((wr_mask
>> i
) & 1))
2147 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
2148 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
2149 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
2150 if (new_inst
->src
[j
].file
== VGRF
)
2151 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
2158 emit_pixel_interpolater_send(const fs_builder
&bld
,
2163 glsl_interp_mode interpolation
)
2165 struct brw_wm_prog_data
*wm_prog_data
=
2166 brw_wm_prog_data(bld
.shader
->stage_prog_data
);
2168 fs_inst
*inst
= bld
.emit(opcode
, dst
, src
, desc
);
2169 /* 2 floats per slot returned */
2170 inst
->size_written
= 2 * dst
.component_size(inst
->exec_size
);
2171 inst
->pi_noperspective
= interpolation
== INTERP_MODE_NOPERSPECTIVE
;
2173 wm_prog_data
->pulls_bary
= true;
2179 * Computes 1 << x, given a D/UD register containing some value x.
2182 intexp2(const fs_builder
&bld
, const fs_reg
&x
)
2184 assert(x
.type
== BRW_REGISTER_TYPE_UD
|| x
.type
== BRW_REGISTER_TYPE_D
);
2186 fs_reg result
= bld
.vgrf(x
.type
, 1);
2187 fs_reg one
= bld
.vgrf(x
.type
, 1);
2189 bld
.MOV(one
, retype(brw_imm_d(1), one
.type
));
2190 bld
.SHL(result
, one
, x
);
2195 fs_visitor::emit_gs_end_primitive(const nir_src
&vertex_count_nir_src
)
2197 assert(stage
== MESA_SHADER_GEOMETRY
);
2199 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
2201 if (gs_compile
->control_data_header_size_bits
== 0)
2204 /* We can only do EndPrimitive() functionality when the control data
2205 * consists of cut bits. Fortunately, the only time it isn't is when the
2206 * output type is points, in which case EndPrimitive() is a no-op.
2208 if (gs_prog_data
->control_data_format
!=
2209 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
2213 /* Cut bits use one bit per vertex. */
2214 assert(gs_compile
->control_data_bits_per_vertex
== 1);
2216 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
2217 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
2219 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
2220 * vertex n, 0 otherwise. So all we need to do here is mark bit
2221 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
2222 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
2223 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
2225 * Note that if EndPrimitive() is called before emitting any vertices, this
2226 * will cause us to set bit 31 of the control_data_bits register to 1.
2227 * That's fine because:
2229 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
2230 * output, so the hardware will ignore cut bit 31.
2232 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
2233 * last vertex, so setting cut bit 31 has no effect (since the primitive
2234 * is automatically ended when the GS terminates).
2236 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
2237 * control_data_bits register to 0 when the first vertex is emitted.
2240 const fs_builder abld
= bld
.annotate("end primitive");
2242 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
2243 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2244 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
2245 fs_reg mask
= intexp2(abld
, prev_count
);
2246 /* Note: we're relying on the fact that the GEN SHL instruction only pays
2247 * attention to the lower 5 bits of its second source argument, so on this
2248 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
2249 * ((vertex_count - 1) % 32).
2251 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
2255 fs_visitor::emit_gs_control_data_bits(const fs_reg
&vertex_count
)
2257 assert(stage
== MESA_SHADER_GEOMETRY
);
2258 assert(gs_compile
->control_data_bits_per_vertex
!= 0);
2260 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
2262 const fs_builder abld
= bld
.annotate("emit control data bits");
2263 const fs_builder fwa_bld
= bld
.exec_all();
2265 /* We use a single UD register to accumulate control data bits (32 bits
2266 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
2269 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
2270 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
2271 * use the Channel Mask phase to enable/disable which DWord within that
2272 * group to write. (Remember, different SIMD8 channels may have emitted
2273 * different numbers of vertices, so we may need per-slot offsets.)
2275 * Channel masking presents an annoying problem: we may have to replicate
2276 * the data up to 4 times:
2278 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
2280 * To avoid penalizing shaders that emit a small number of vertices, we
2281 * can avoid these sometimes: if the size of the control data header is
2282 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
2283 * land in the same 128-bit group, so we can skip per-slot offsets.
2285 * Similarly, if the control data header is <= 32 bits, there is only one
2286 * DWord, so we can skip channel masks.
2288 enum opcode opcode
= SHADER_OPCODE_URB_WRITE_SIMD8
;
2290 fs_reg channel_mask
, per_slot_offset
;
2292 if (gs_compile
->control_data_header_size_bits
> 32) {
2293 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
2294 channel_mask
= vgrf(glsl_type::uint_type
);
2297 if (gs_compile
->control_data_header_size_bits
> 128) {
2298 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
;
2299 per_slot_offset
= vgrf(glsl_type::uint_type
);
2302 /* Figure out which DWord we're trying to write to using the formula:
2304 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
2306 * Since bits_per_vertex is a power of two, and is known at compile
2307 * time, this can be optimized to:
2309 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
2311 if (opcode
!= SHADER_OPCODE_URB_WRITE_SIMD8
) {
2312 fs_reg dword_index
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2313 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2314 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
2315 unsigned log2_bits_per_vertex
=
2316 util_last_bit(gs_compile
->control_data_bits_per_vertex
);
2317 abld
.SHR(dword_index
, prev_count
, brw_imm_ud(6u - log2_bits_per_vertex
));
2319 if (per_slot_offset
.file
!= BAD_FILE
) {
2320 /* Set the per-slot offset to dword_index / 4, so that we'll write to
2321 * the appropriate OWord within the control data header.
2323 abld
.SHR(per_slot_offset
, dword_index
, brw_imm_ud(2u));
2326 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
2327 * write to the appropriate DWORD within the OWORD.
2329 fs_reg channel
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2330 fwa_bld
.AND(channel
, dword_index
, brw_imm_ud(3u));
2331 channel_mask
= intexp2(fwa_bld
, channel
);
2332 /* Then the channel masks need to be in bits 23:16. */
2333 fwa_bld
.SHL(channel_mask
, channel_mask
, brw_imm_ud(16u));
2336 /* Store the control data bits in the message payload and send it. */
2338 if (channel_mask
.file
!= BAD_FILE
)
2339 mlen
+= 4; /* channel masks, plus 3 extra copies of the data */
2340 if (per_slot_offset
.file
!= BAD_FILE
)
2343 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
2344 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, mlen
);
2346 sources
[i
++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
2347 if (per_slot_offset
.file
!= BAD_FILE
)
2348 sources
[i
++] = per_slot_offset
;
2349 if (channel_mask
.file
!= BAD_FILE
)
2350 sources
[i
++] = channel_mask
;
2352 sources
[i
++] = this->control_data_bits
;
2355 abld
.LOAD_PAYLOAD(payload
, sources
, mlen
, mlen
);
2356 fs_inst
*inst
= abld
.emit(opcode
, reg_undef
, payload
);
2358 /* We need to increment Global Offset by 256-bits to make room for
2359 * Broadwell's extra "Vertex Count" payload at the beginning of the
2360 * URB entry. Since this is an OWord message, Global Offset is counted
2361 * in 128-bit units, so we must set it to 2.
2363 if (gs_prog_data
->static_vertex_count
== -1)
2368 fs_visitor::set_gs_stream_control_data_bits(const fs_reg
&vertex_count
,
2371 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
2373 /* Note: we are calling this *before* increasing vertex_count, so
2374 * this->vertex_count == vertex_count - 1 in the formula above.
2377 /* Stream mode uses 2 bits per vertex */
2378 assert(gs_compile
->control_data_bits_per_vertex
== 2);
2380 /* Must be a valid stream */
2381 assert(stream_id
< MAX_VERTEX_STREAMS
);
2383 /* Control data bits are initialized to 0 so we don't have to set any
2384 * bits when sending vertices to stream 0.
2389 const fs_builder abld
= bld
.annotate("set stream control data bits", NULL
);
2391 /* reg::sid = stream_id */
2392 fs_reg sid
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2393 abld
.MOV(sid
, brw_imm_ud(stream_id
));
2395 /* reg:shift_count = 2 * (vertex_count - 1) */
2396 fs_reg shift_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2397 abld
.SHL(shift_count
, vertex_count
, brw_imm_ud(1u));
2399 /* Note: we're relying on the fact that the GEN SHL instruction only pays
2400 * attention to the lower 5 bits of its second source argument, so on this
2401 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
2402 * stream_id << ((2 * (vertex_count - 1)) % 32).
2404 fs_reg mask
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2405 abld
.SHL(mask
, sid
, shift_count
);
2406 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
2410 fs_visitor::emit_gs_vertex(const nir_src
&vertex_count_nir_src
,
2413 assert(stage
== MESA_SHADER_GEOMETRY
);
2415 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
2417 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
2418 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
2420 /* Haswell and later hardware ignores the "Render Stream Select" bits
2421 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
2422 * and instead sends all primitives down the pipeline for rasterization.
2423 * If the SOL stage is enabled, "Render Stream Select" is honored and
2424 * primitives bound to non-zero streams are discarded after stream output.
2426 * Since the only purpose of primives sent to non-zero streams is to
2427 * be recorded by transform feedback, we can simply discard all geometry
2428 * bound to these streams when transform feedback is disabled.
2430 if (stream_id
> 0 && !nir
->info
.has_transform_feedback_varyings
)
2433 /* If we're outputting 32 control data bits or less, then we can wait
2434 * until the shader is over to output them all. Otherwise we need to
2435 * output them as we go. Now is the time to do it, since we're about to
2436 * output the vertex_count'th vertex, so it's guaranteed that the
2437 * control data bits associated with the (vertex_count - 1)th vertex are
2440 if (gs_compile
->control_data_header_size_bits
> 32) {
2441 const fs_builder abld
=
2442 bld
.annotate("emit vertex: emit control data bits");
2444 /* Only emit control data bits if we've finished accumulating a batch
2445 * of 32 bits. This is the case when:
2447 * (vertex_count * bits_per_vertex) % 32 == 0
2449 * (in other words, when the last 5 bits of vertex_count *
2450 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
2451 * integer n (which is always the case, since bits_per_vertex is
2452 * always 1 or 2), this is equivalent to requiring that the last 5-n
2453 * bits of vertex_count are 0:
2455 * vertex_count & (2^(5-n) - 1) == 0
2457 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
2460 * vertex_count & (32 / bits_per_vertex - 1) == 0
2462 * TODO: If vertex_count is an immediate, we could do some of this math
2463 * at compile time...
2466 abld
.AND(bld
.null_reg_d(), vertex_count
,
2467 brw_imm_ud(32u / gs_compile
->control_data_bits_per_vertex
- 1u));
2468 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
2470 abld
.IF(BRW_PREDICATE_NORMAL
);
2471 /* If vertex_count is 0, then no control data bits have been
2472 * accumulated yet, so we can skip emitting them.
2474 abld
.CMP(bld
.null_reg_d(), vertex_count
, brw_imm_ud(0u),
2475 BRW_CONDITIONAL_NEQ
);
2476 abld
.IF(BRW_PREDICATE_NORMAL
);
2477 emit_gs_control_data_bits(vertex_count
);
2478 abld
.emit(BRW_OPCODE_ENDIF
);
2480 /* Reset control_data_bits to 0 so we can start accumulating a new
2483 * Note: in the case where vertex_count == 0, this neutralizes the
2484 * effect of any call to EndPrimitive() that the shader may have
2485 * made before outputting its first vertex.
2487 inst
= abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
2488 inst
->force_writemask_all
= true;
2489 abld
.emit(BRW_OPCODE_ENDIF
);
2492 emit_urb_writes(vertex_count
);
2494 /* In stream mode we have to set control data bits for all vertices
2495 * unless we have disabled control data bits completely (which we do
2496 * do for GL_POINTS outputs that don't use streams).
2498 if (gs_compile
->control_data_header_size_bits
> 0 &&
2499 gs_prog_data
->control_data_format
==
2500 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
2501 set_gs_stream_control_data_bits(vertex_count
, stream_id
);
2506 fs_visitor::emit_gs_input_load(const fs_reg
&dst
,
2507 const nir_src
&vertex_src
,
2508 unsigned base_offset
,
2509 const nir_src
&offset_src
,
2510 unsigned num_components
,
2511 unsigned first_component
)
2513 assert(type_sz(dst
.type
) == 4);
2514 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
2515 const unsigned push_reg_count
= gs_prog_data
->base
.urb_read_length
* 8;
2517 /* TODO: figure out push input layout for invocations == 1 */
2518 if (gs_prog_data
->invocations
== 1 &&
2519 nir_src_is_const(offset_src
) && nir_src_is_const(vertex_src
) &&
2520 4 * (base_offset
+ nir_src_as_uint(offset_src
)) < push_reg_count
) {
2521 int imm_offset
= (base_offset
+ nir_src_as_uint(offset_src
)) * 4 +
2522 nir_src_as_uint(vertex_src
) * push_reg_count
;
2523 for (unsigned i
= 0; i
< num_components
; i
++) {
2524 bld
.MOV(offset(dst
, bld
, i
),
2525 fs_reg(ATTR
, imm_offset
+ i
+ first_component
, dst
.type
));
2530 /* Resort to the pull model. Ensure the VUE handles are provided. */
2531 assert(gs_prog_data
->base
.include_vue_handles
);
2533 unsigned first_icp_handle
= gs_prog_data
->include_primitive_id
? 3 : 2;
2534 fs_reg icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2536 if (gs_prog_data
->invocations
== 1) {
2537 if (nir_src_is_const(vertex_src
)) {
2538 /* The vertex index is constant; just select the proper URB handle. */
2540 retype(brw_vec8_grf(first_icp_handle
+ nir_src_as_uint(vertex_src
), 0),
2541 BRW_REGISTER_TYPE_UD
);
2543 /* The vertex index is non-constant. We need to use indirect
2544 * addressing to fetch the proper URB handle.
2546 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2547 * indicating that channel <n> should read the handle from
2548 * DWord <n>. We convert that to bytes by multiplying by 4.
2550 * Next, we convert the vertex index to bytes by multiplying
2551 * by 32 (shifting by 5), and add the two together. This is
2552 * the final indirect byte offset.
2554 fs_reg sequence
= bld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
2555 fs_reg channel_offsets
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2556 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2557 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2559 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2560 bld
.MOV(sequence
, fs_reg(brw_imm_v(0x76543210)));
2561 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2562 bld
.SHL(channel_offsets
, sequence
, brw_imm_ud(2u));
2563 /* Convert vertex_index to bytes (multiply by 32) */
2564 bld
.SHL(vertex_offset_bytes
,
2565 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2567 bld
.ADD(icp_offset_bytes
, vertex_offset_bytes
, channel_offsets
);
2569 /* Use first_icp_handle as the base offset. There is one register
2570 * of URB handles per vertex, so inform the register allocator that
2571 * we might read up to nir->info.gs.vertices_in registers.
2573 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2574 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
2575 fs_reg(icp_offset_bytes
),
2576 brw_imm_ud(nir
->info
.gs
.vertices_in
* REG_SIZE
));
2579 assert(gs_prog_data
->invocations
> 1);
2581 if (nir_src_is_const(vertex_src
)) {
2582 unsigned vertex
= nir_src_as_uint(vertex_src
);
2583 assert(devinfo
->gen
>= 9 || vertex
<= 5);
2585 retype(brw_vec1_grf(first_icp_handle
+ vertex
/ 8, vertex
% 8),
2586 BRW_REGISTER_TYPE_UD
));
2588 /* The vertex index is non-constant. We need to use indirect
2589 * addressing to fetch the proper URB handle.
2592 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2594 /* Convert vertex_index to bytes (multiply by 4) */
2595 bld
.SHL(icp_offset_bytes
,
2596 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2599 /* Use first_icp_handle as the base offset. There is one DWord
2600 * of URB handles per vertex, so inform the register allocator that
2601 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2603 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2604 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
2605 fs_reg(icp_offset_bytes
),
2606 brw_imm_ud(DIV_ROUND_UP(nir
->info
.gs
.vertices_in
, 8) *
2612 fs_reg indirect_offset
= get_nir_src(offset_src
);
2614 if (nir_src_is_const(offset_src
)) {
2615 /* Constant indexing - use global offset. */
2616 if (first_component
!= 0) {
2617 unsigned read_components
= num_components
+ first_component
;
2618 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2619 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
, icp_handle
);
2620 inst
->size_written
= read_components
*
2621 tmp
.component_size(inst
->exec_size
);
2622 for (unsigned i
= 0; i
< num_components
; i
++) {
2623 bld
.MOV(offset(dst
, bld
, i
),
2624 offset(tmp
, bld
, i
+ first_component
));
2627 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
, icp_handle
);
2628 inst
->size_written
= num_components
*
2629 dst
.component_size(inst
->exec_size
);
2631 inst
->offset
= base_offset
+ nir_src_as_uint(offset_src
);
2634 /* Indirect indexing - use per-slot offsets as well. */
2635 const fs_reg srcs
[] = { icp_handle
, indirect_offset
};
2636 unsigned read_components
= num_components
+ first_component
;
2637 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2638 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2639 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2640 if (first_component
!= 0) {
2641 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2643 inst
->size_written
= read_components
*
2644 tmp
.component_size(inst
->exec_size
);
2645 for (unsigned i
= 0; i
< num_components
; i
++) {
2646 bld
.MOV(offset(dst
, bld
, i
),
2647 offset(tmp
, bld
, i
+ first_component
));
2650 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
, payload
);
2651 inst
->size_written
= num_components
*
2652 dst
.component_size(inst
->exec_size
);
2654 inst
->offset
= base_offset
;
2660 fs_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
2662 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
2664 if (nir_src_is_const(*offset_src
)) {
2665 /* The only constant offset we should find is 0. brw_nir.c's
2666 * add_const_offset_to_base() will fold other constant offsets
2667 * into instr->const_index[0].
2669 assert(nir_src_as_uint(*offset_src
) == 0);
2673 return get_nir_src(*offset_src
);
2677 fs_visitor::nir_emit_vs_intrinsic(const fs_builder
&bld
,
2678 nir_intrinsic_instr
*instr
)
2680 assert(stage
== MESA_SHADER_VERTEX
);
2683 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2684 dest
= get_nir_dest(instr
->dest
);
2686 switch (instr
->intrinsic
) {
2687 case nir_intrinsic_load_vertex_id
:
2688 case nir_intrinsic_load_base_vertex
:
2689 unreachable("should be lowered by nir_lower_system_values()");
2691 case nir_intrinsic_load_input
: {
2692 assert(nir_dest_bit_size(instr
->dest
) == 32);
2693 fs_reg src
= fs_reg(ATTR
, nir_intrinsic_base(instr
) * 4, dest
.type
);
2694 src
= offset(src
, bld
, nir_intrinsic_component(instr
));
2695 src
= offset(src
, bld
, nir_src_as_uint(instr
->src
[0]));
2697 for (unsigned i
= 0; i
< instr
->num_components
; i
++)
2698 bld
.MOV(offset(dest
, bld
, i
), offset(src
, bld
, i
));
2702 case nir_intrinsic_load_vertex_id_zero_base
:
2703 case nir_intrinsic_load_instance_id
:
2704 case nir_intrinsic_load_base_instance
:
2705 case nir_intrinsic_load_draw_id
:
2706 case nir_intrinsic_load_first_vertex
:
2707 case nir_intrinsic_load_is_indexed_draw
:
2708 unreachable("lowered by brw_nir_lower_vs_inputs");
2711 nir_emit_intrinsic(bld
, instr
);
2717 fs_visitor::get_tcs_single_patch_icp_handle(const fs_builder
&bld
,
2718 nir_intrinsic_instr
*instr
)
2720 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
2721 const nir_src
&vertex_src
= instr
->src
[0];
2722 nir_intrinsic_instr
*vertex_intrin
= nir_src_as_intrinsic(vertex_src
);
2725 if (nir_src_is_const(vertex_src
)) {
2726 /* Emit a MOV to resolve <0,1,0> regioning. */
2727 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2728 unsigned vertex
= nir_src_as_uint(vertex_src
);
2730 retype(brw_vec1_grf(1 + (vertex
>> 3), vertex
& 7),
2731 BRW_REGISTER_TYPE_UD
));
2732 } else if (tcs_prog_data
->instances
== 1 && vertex_intrin
&&
2733 vertex_intrin
->intrinsic
== nir_intrinsic_load_invocation_id
) {
2734 /* For the common case of only 1 instance, an array index of
2735 * gl_InvocationID means reading g1. Skip all the indirect work.
2737 icp_handle
= retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
);
2739 /* The vertex index is non-constant. We need to use indirect
2740 * addressing to fetch the proper URB handle.
2742 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2744 /* Each ICP handle is a single DWord (4 bytes) */
2745 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2746 bld
.SHL(vertex_offset_bytes
,
2747 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2750 /* Start at g1. We might read up to 4 registers. */
2751 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2752 retype(brw_vec8_grf(1, 0), icp_handle
.type
), vertex_offset_bytes
,
2753 brw_imm_ud(4 * REG_SIZE
));
2760 fs_visitor::get_tcs_eight_patch_icp_handle(const fs_builder
&bld
,
2761 nir_intrinsic_instr
*instr
)
2763 struct brw_tcs_prog_key
*tcs_key
= (struct brw_tcs_prog_key
*) key
;
2764 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
2765 const nir_src
&vertex_src
= instr
->src
[0];
2767 unsigned first_icp_handle
= tcs_prog_data
->include_primitive_id
? 3 : 2;
2769 if (nir_src_is_const(vertex_src
)) {
2770 return fs_reg(retype(brw_vec8_grf(first_icp_handle
+
2771 nir_src_as_uint(vertex_src
), 0),
2772 BRW_REGISTER_TYPE_UD
));
2775 /* The vertex index is non-constant. We need to use indirect
2776 * addressing to fetch the proper URB handle.
2778 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2779 * indicating that channel <n> should read the handle from
2780 * DWord <n>. We convert that to bytes by multiplying by 4.
2782 * Next, we convert the vertex index to bytes by multiplying
2783 * by 32 (shifting by 5), and add the two together. This is
2784 * the final indirect byte offset.
2786 fs_reg icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2787 fs_reg sequence
= bld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
2788 fs_reg channel_offsets
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2789 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2790 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2792 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2793 bld
.MOV(sequence
, fs_reg(brw_imm_v(0x76543210)));
2794 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2795 bld
.SHL(channel_offsets
, sequence
, brw_imm_ud(2u));
2796 /* Convert vertex_index to bytes (multiply by 32) */
2797 bld
.SHL(vertex_offset_bytes
,
2798 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2800 bld
.ADD(icp_offset_bytes
, vertex_offset_bytes
, channel_offsets
);
2802 /* Use first_icp_handle as the base offset. There is one register
2803 * of URB handles per vertex, so inform the register allocator that
2804 * we might read up to nir->info.gs.vertices_in registers.
2806 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2807 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
2808 icp_offset_bytes
, brw_imm_ud(tcs_key
->input_vertices
* REG_SIZE
));
2814 fs_visitor::get_tcs_output_urb_handle()
2816 struct brw_vue_prog_data
*vue_prog_data
= brw_vue_prog_data(prog_data
);
2818 if (vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_SINGLE_PATCH
) {
2819 return retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
);
2821 assert(vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
);
2822 return retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
);
2827 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder
&bld
,
2828 nir_intrinsic_instr
*instr
)
2830 assert(stage
== MESA_SHADER_TESS_CTRL
);
2831 struct brw_tcs_prog_key
*tcs_key
= (struct brw_tcs_prog_key
*) key
;
2832 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
2833 struct brw_vue_prog_data
*vue_prog_data
= &tcs_prog_data
->base
;
2836 vue_prog_data
->dispatch_mode
== DISPATCH_MODE_TCS_8_PATCH
;
2839 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2840 dst
= get_nir_dest(instr
->dest
);
2842 switch (instr
->intrinsic
) {
2843 case nir_intrinsic_load_primitive_id
:
2844 bld
.MOV(dst
, fs_reg(eight_patch
? brw_vec8_grf(2, 0)
2845 : brw_vec1_grf(0, 1)));
2847 case nir_intrinsic_load_invocation_id
:
2848 bld
.MOV(retype(dst
, invocation_id
.type
), invocation_id
);
2850 case nir_intrinsic_load_patch_vertices_in
:
2851 bld
.MOV(retype(dst
, BRW_REGISTER_TYPE_D
),
2852 brw_imm_d(tcs_key
->input_vertices
));
2855 case nir_intrinsic_control_barrier
: {
2856 if (tcs_prog_data
->instances
== 1)
2859 fs_reg m0
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2860 fs_reg m0_2
= component(m0
, 2);
2862 const fs_builder chanbld
= bld
.exec_all().group(1, 0);
2864 /* Zero the message header */
2865 bld
.exec_all().MOV(m0
, brw_imm_ud(0u));
2867 if (devinfo
->gen
< 11) {
2868 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2869 chanbld
.AND(m0_2
, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
),
2870 brw_imm_ud(INTEL_MASK(16, 13)));
2872 /* Shift it up to bits 27:24. */
2873 chanbld
.SHL(m0_2
, m0_2
, brw_imm_ud(11));
2875 chanbld
.AND(m0_2
, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
),
2876 brw_imm_ud(INTEL_MASK(30, 24)));
2879 /* Set the Barrier Count and the enable bit */
2880 if (devinfo
->gen
< 11) {
2881 chanbld
.OR(m0_2
, m0_2
,
2882 brw_imm_ud(tcs_prog_data
->instances
<< 9 | (1 << 15)));
2884 chanbld
.OR(m0_2
, m0_2
,
2885 brw_imm_ud(tcs_prog_data
->instances
<< 8 | (1 << 15)));
2888 bld
.emit(SHADER_OPCODE_BARRIER
, bld
.null_reg_ud(), m0
);
2892 case nir_intrinsic_load_input
:
2893 unreachable("nir_lower_io should never give us these.");
2896 case nir_intrinsic_load_per_vertex_input
: {
2897 assert(nir_dest_bit_size(instr
->dest
) == 32);
2898 fs_reg indirect_offset
= get_indirect_offset(instr
);
2899 unsigned imm_offset
= instr
->const_index
[0];
2903 eight_patch
? get_tcs_eight_patch_icp_handle(bld
, instr
)
2904 : get_tcs_single_patch_icp_handle(bld
, instr
);
2906 /* We can only read two double components with each URB read, so
2907 * we send two read messages in that case, each one loading up to
2908 * two double components.
2910 unsigned num_components
= instr
->num_components
;
2911 unsigned first_component
= nir_intrinsic_component(instr
);
2913 if (indirect_offset
.file
== BAD_FILE
) {
2914 /* Constant indexing - use global offset. */
2915 if (first_component
!= 0) {
2916 unsigned read_components
= num_components
+ first_component
;
2917 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2918 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
, icp_handle
);
2919 for (unsigned i
= 0; i
< num_components
; i
++) {
2920 bld
.MOV(offset(dst
, bld
, i
),
2921 offset(tmp
, bld
, i
+ first_component
));
2924 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
, icp_handle
);
2926 inst
->offset
= imm_offset
;
2929 /* Indirect indexing - use per-slot offsets as well. */
2930 const fs_reg srcs
[] = { icp_handle
, indirect_offset
};
2931 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2932 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2933 if (first_component
!= 0) {
2934 unsigned read_components
= num_components
+ first_component
;
2935 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2936 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2938 for (unsigned i
= 0; i
< num_components
; i
++) {
2939 bld
.MOV(offset(dst
, bld
, i
),
2940 offset(tmp
, bld
, i
+ first_component
));
2943 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
,
2946 inst
->offset
= imm_offset
;
2949 inst
->size_written
= (num_components
+ first_component
) *
2950 inst
->dst
.component_size(inst
->exec_size
);
2952 /* Copy the temporary to the destination to deal with writemasking.
2954 * Also attempt to deal with gl_PointSize being in the .w component.
2956 if (inst
->offset
== 0 && indirect_offset
.file
== BAD_FILE
) {
2957 assert(type_sz(dst
.type
) == 4);
2958 inst
->dst
= bld
.vgrf(dst
.type
, 4);
2959 inst
->size_written
= 4 * REG_SIZE
;
2960 bld
.MOV(dst
, offset(inst
->dst
, bld
, 3));
2965 case nir_intrinsic_load_output
:
2966 case nir_intrinsic_load_per_vertex_output
: {
2967 assert(nir_dest_bit_size(instr
->dest
) == 32);
2968 fs_reg indirect_offset
= get_indirect_offset(instr
);
2969 unsigned imm_offset
= instr
->const_index
[0];
2970 unsigned first_component
= nir_intrinsic_component(instr
);
2972 struct brw_reg output_handles
= get_tcs_output_urb_handle();
2975 if (indirect_offset
.file
== BAD_FILE
) {
2976 /* This MOV replicates the output handle to all enabled channels
2977 * is SINGLE_PATCH mode.
2979 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2980 bld
.MOV(patch_handle
, output_handles
);
2983 if (first_component
!= 0) {
2984 unsigned read_components
=
2985 instr
->num_components
+ first_component
;
2986 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2987 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
,
2989 inst
->size_written
= read_components
* REG_SIZE
;
2990 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2991 bld
.MOV(offset(dst
, bld
, i
),
2992 offset(tmp
, bld
, i
+ first_component
));
2995 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
,
2997 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2999 inst
->offset
= imm_offset
;
3003 /* Indirect indexing - use per-slot offsets as well. */
3004 const fs_reg srcs
[] = { output_handles
, indirect_offset
};
3005 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
3006 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
3007 if (first_component
!= 0) {
3008 unsigned read_components
=
3009 instr
->num_components
+ first_component
;
3010 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
3011 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
3013 inst
->size_written
= read_components
* REG_SIZE
;
3014 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
3015 bld
.MOV(offset(dst
, bld
, i
),
3016 offset(tmp
, bld
, i
+ first_component
));
3019 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
,
3021 inst
->size_written
= instr
->num_components
* REG_SIZE
;
3023 inst
->offset
= imm_offset
;
3029 case nir_intrinsic_store_output
:
3030 case nir_intrinsic_store_per_vertex_output
: {
3031 assert(nir_src_bit_size(instr
->src
[0]) == 32);
3032 fs_reg value
= get_nir_src(instr
->src
[0]);
3033 fs_reg indirect_offset
= get_indirect_offset(instr
);
3034 unsigned imm_offset
= instr
->const_index
[0];
3035 unsigned mask
= instr
->const_index
[1];
3036 unsigned header_regs
= 0;
3037 struct brw_reg output_handles
= get_tcs_output_urb_handle();
3040 srcs
[header_regs
++] = output_handles
;
3042 if (indirect_offset
.file
!= BAD_FILE
) {
3043 srcs
[header_regs
++] = indirect_offset
;
3049 unsigned num_components
= util_last_bit(mask
);
3052 /* We can only pack two 64-bit components in a single message, so send
3053 * 2 messages if we have more components
3055 unsigned first_component
= nir_intrinsic_component(instr
);
3056 mask
= mask
<< first_component
;
3058 if (mask
!= WRITEMASK_XYZW
) {
3059 srcs
[header_regs
++] = brw_imm_ud(mask
<< 16);
3060 opcode
= indirect_offset
.file
!= BAD_FILE
?
3061 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
3062 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
3064 opcode
= indirect_offset
.file
!= BAD_FILE
?
3065 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
3066 SHADER_OPCODE_URB_WRITE_SIMD8
;
3069 for (unsigned i
= 0; i
< num_components
; i
++) {
3070 if (!(mask
& (1 << (i
+ first_component
))))
3073 srcs
[header_regs
+ i
+ first_component
] = offset(value
, bld
, i
);
3076 unsigned mlen
= header_regs
+ num_components
+ first_component
;
3078 bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
3079 bld
.LOAD_PAYLOAD(payload
, srcs
, mlen
, header_regs
);
3081 fs_inst
*inst
= bld
.emit(opcode
, bld
.null_reg_ud(), payload
);
3082 inst
->offset
= imm_offset
;
3088 nir_emit_intrinsic(bld
, instr
);
3094 fs_visitor::nir_emit_tes_intrinsic(const fs_builder
&bld
,
3095 nir_intrinsic_instr
*instr
)
3097 assert(stage
== MESA_SHADER_TESS_EVAL
);
3098 struct brw_tes_prog_data
*tes_prog_data
= brw_tes_prog_data(prog_data
);
3101 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3102 dest
= get_nir_dest(instr
->dest
);
3104 switch (instr
->intrinsic
) {
3105 case nir_intrinsic_load_primitive_id
:
3106 bld
.MOV(dest
, fs_reg(brw_vec1_grf(0, 1)));
3108 case nir_intrinsic_load_tess_coord
:
3109 /* gl_TessCoord is part of the payload in g1-3 */
3110 for (unsigned i
= 0; i
< 3; i
++) {
3111 bld
.MOV(offset(dest
, bld
, i
), fs_reg(brw_vec8_grf(1 + i
, 0)));
3115 case nir_intrinsic_load_input
:
3116 case nir_intrinsic_load_per_vertex_input
: {
3117 assert(nir_dest_bit_size(instr
->dest
) == 32);
3118 fs_reg indirect_offset
= get_indirect_offset(instr
);
3119 unsigned imm_offset
= instr
->const_index
[0];
3120 unsigned first_component
= nir_intrinsic_component(instr
);
3123 if (indirect_offset
.file
== BAD_FILE
) {
3124 /* Arbitrarily only push up to 32 vec4 slots worth of data,
3125 * which is 16 registers (since each holds 2 vec4 slots).
3127 const unsigned max_push_slots
= 32;
3128 if (imm_offset
< max_push_slots
) {
3129 fs_reg src
= fs_reg(ATTR
, imm_offset
/ 2, dest
.type
);
3130 for (int i
= 0; i
< instr
->num_components
; i
++) {
3131 unsigned comp
= 4 * (imm_offset
% 2) + i
+ first_component
;
3132 bld
.MOV(offset(dest
, bld
, i
), component(src
, comp
));
3135 tes_prog_data
->base
.urb_read_length
=
3136 MAX2(tes_prog_data
->base
.urb_read_length
,
3137 (imm_offset
/ 2) + 1);
3139 /* Replicate the patch handle to all enabled channels */
3140 const fs_reg srcs
[] = {
3141 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)
3143 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
3144 bld
.LOAD_PAYLOAD(patch_handle
, srcs
, ARRAY_SIZE(srcs
), 0);
3146 if (first_component
!= 0) {
3147 unsigned read_components
=
3148 instr
->num_components
+ first_component
;
3149 fs_reg tmp
= bld
.vgrf(dest
.type
, read_components
);
3150 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
,
3152 inst
->size_written
= read_components
* REG_SIZE
;
3153 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
3154 bld
.MOV(offset(dest
, bld
, i
),
3155 offset(tmp
, bld
, i
+ first_component
));
3158 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dest
,
3160 inst
->size_written
= instr
->num_components
* REG_SIZE
;
3163 inst
->offset
= imm_offset
;
3166 /* Indirect indexing - use per-slot offsets as well. */
3168 /* We can only read two double components with each URB read, so
3169 * we send two read messages in that case, each one loading up to
3170 * two double components.
3172 unsigned num_components
= instr
->num_components
;
3173 const fs_reg srcs
[] = {
3174 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
3177 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
3178 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
3180 if (first_component
!= 0) {
3181 unsigned read_components
=
3182 num_components
+ first_component
;
3183 fs_reg tmp
= bld
.vgrf(dest
.type
, read_components
);
3184 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
3186 for (unsigned i
= 0; i
< num_components
; i
++) {
3187 bld
.MOV(offset(dest
, bld
, i
),
3188 offset(tmp
, bld
, i
+ first_component
));
3191 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dest
,
3195 inst
->offset
= imm_offset
;
3196 inst
->size_written
= (num_components
+ first_component
) *
3197 inst
->dst
.component_size(inst
->exec_size
);
3202 nir_emit_intrinsic(bld
, instr
);
3208 fs_visitor::nir_emit_gs_intrinsic(const fs_builder
&bld
,
3209 nir_intrinsic_instr
*instr
)
3211 assert(stage
== MESA_SHADER_GEOMETRY
);
3212 fs_reg indirect_offset
;
3215 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3216 dest
= get_nir_dest(instr
->dest
);
3218 switch (instr
->intrinsic
) {
3219 case nir_intrinsic_load_primitive_id
:
3220 assert(stage
== MESA_SHADER_GEOMETRY
);
3221 assert(brw_gs_prog_data(prog_data
)->include_primitive_id
);
3222 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
3223 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD
));
3226 case nir_intrinsic_load_input
:
3227 unreachable("load_input intrinsics are invalid for the GS stage");
3229 case nir_intrinsic_load_per_vertex_input
:
3230 emit_gs_input_load(dest
, instr
->src
[0], instr
->const_index
[0],
3231 instr
->src
[1], instr
->num_components
,
3232 nir_intrinsic_component(instr
));
3235 case nir_intrinsic_emit_vertex_with_counter
:
3236 emit_gs_vertex(instr
->src
[0], instr
->const_index
[0]);
3239 case nir_intrinsic_end_primitive_with_counter
:
3240 emit_gs_end_primitive(instr
->src
[0]);
3243 case nir_intrinsic_set_vertex_count
:
3244 bld
.MOV(this->final_gs_vertex_count
, get_nir_src(instr
->src
[0]));
3247 case nir_intrinsic_load_invocation_id
: {
3248 fs_reg val
= nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
3249 assert(val
.file
!= BAD_FILE
);
3250 dest
.type
= val
.type
;
3256 nir_emit_intrinsic(bld
, instr
);
3262 * Fetch the current render target layer index.
3265 fetch_render_target_array_index(const fs_builder
&bld
)
3267 if (bld
.shader
->devinfo
->gen
>= 6) {
3268 /* The render target array index is provided in the thread payload as
3269 * bits 26:16 of r0.0.
3271 const fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3272 bld
.AND(idx
, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE
, 0, 1),
3276 /* Pre-SNB we only ever render into the first layer of the framebuffer
3277 * since layered rendering is not implemented.
3279 return brw_imm_ud(0);
3284 * Fake non-coherent framebuffer read implemented using TXF to fetch from the
3285 * framebuffer at the current fragment coordinates and sample index.
3288 fs_visitor::emit_non_coherent_fb_read(const fs_builder
&bld
, const fs_reg
&dst
,
3291 const struct gen_device_info
*devinfo
= bld
.shader
->devinfo
;
3293 assert(bld
.shader
->stage
== MESA_SHADER_FRAGMENT
);
3294 const brw_wm_prog_key
*wm_key
=
3295 reinterpret_cast<const brw_wm_prog_key
*>(key
);
3296 assert(!wm_key
->coherent_fb_fetch
);
3297 const struct brw_wm_prog_data
*wm_prog_data
=
3298 brw_wm_prog_data(stage_prog_data
);
3300 /* Calculate the surface index relative to the start of the texture binding
3301 * table block, since that's what the texturing messages expect.
3303 const unsigned surface
= target
+
3304 wm_prog_data
->binding_table
.render_target_read_start
-
3305 wm_prog_data
->base
.binding_table
.texture_start
;
3307 /* Calculate the fragment coordinates. */
3308 const fs_reg coords
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 3);
3309 bld
.MOV(offset(coords
, bld
, 0), pixel_x
);
3310 bld
.MOV(offset(coords
, bld
, 1), pixel_y
);
3311 bld
.MOV(offset(coords
, bld
, 2), fetch_render_target_array_index(bld
));
3313 /* Calculate the sample index and MCS payload when multisampling. Luckily
3314 * the MCS fetch message behaves deterministically for UMS surfaces, so it
3315 * shouldn't be necessary to recompile based on whether the framebuffer is
3318 if (wm_key
->multisample_fbo
&&
3319 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
].file
== BAD_FILE
)
3320 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
] = *emit_sampleid_setup();
3322 const fs_reg sample
= nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
3323 const fs_reg mcs
= wm_key
->multisample_fbo
?
3324 emit_mcs_fetch(coords
, 3, brw_imm_ud(surface
), fs_reg()) : fs_reg();
3326 /* Use either a normal or a CMS texel fetch message depending on whether
3327 * the framebuffer is single or multisample. On SKL+ use the wide CMS
3328 * message just in case the framebuffer uses 16x multisampling, it should
3329 * be equivalent to the normal CMS fetch for lower multisampling modes.
3331 const opcode op
= !wm_key
->multisample_fbo
? SHADER_OPCODE_TXF_LOGICAL
:
3332 devinfo
->gen
>= 9 ? SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
3333 SHADER_OPCODE_TXF_CMS_LOGICAL
;
3335 /* Emit the instruction. */
3336 fs_reg srcs
[TEX_LOGICAL_NUM_SRCS
];
3337 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = coords
;
3338 srcs
[TEX_LOGICAL_SRC_LOD
] = brw_imm_ud(0);
3339 srcs
[TEX_LOGICAL_SRC_SAMPLE_INDEX
] = sample
;
3340 srcs
[TEX_LOGICAL_SRC_MCS
] = mcs
;
3341 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(surface
);
3342 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = brw_imm_ud(0);
3343 srcs
[TEX_LOGICAL_SRC_COORD_COMPONENTS
] = brw_imm_ud(3);
3344 srcs
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
] = brw_imm_ud(0);
3346 fs_inst
*inst
= bld
.emit(op
, dst
, srcs
, ARRAY_SIZE(srcs
));
3347 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
3353 * Actual coherent framebuffer read implemented using the native render target
3354 * read message. Requires SKL+.
3357 emit_coherent_fb_read(const fs_builder
&bld
, const fs_reg
&dst
, unsigned target
)
3359 assert(bld
.shader
->devinfo
->gen
>= 9);
3360 fs_inst
*inst
= bld
.emit(FS_OPCODE_FB_READ_LOGICAL
, dst
);
3361 inst
->target
= target
;
3362 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
3368 alloc_temporary(const fs_builder
&bld
, unsigned size
, fs_reg
*regs
, unsigned n
)
3370 if (n
&& regs
[0].file
!= BAD_FILE
) {
3374 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
3376 for (unsigned i
= 0; i
< n
; i
++)
3384 alloc_frag_output(fs_visitor
*v
, unsigned location
)
3386 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
3387 const brw_wm_prog_key
*const key
=
3388 reinterpret_cast<const brw_wm_prog_key
*>(v
->key
);
3389 const unsigned l
= GET_FIELD(location
, BRW_NIR_FRAG_OUTPUT_LOCATION
);
3390 const unsigned i
= GET_FIELD(location
, BRW_NIR_FRAG_OUTPUT_INDEX
);
3392 if (i
> 0 || (key
->force_dual_color_blend
&& l
== FRAG_RESULT_DATA1
))
3393 return alloc_temporary(v
->bld
, 4, &v
->dual_src_output
, 1);
3395 else if (l
== FRAG_RESULT_COLOR
)
3396 return alloc_temporary(v
->bld
, 4, v
->outputs
,
3397 MAX2(key
->nr_color_regions
, 1));
3399 else if (l
== FRAG_RESULT_DEPTH
)
3400 return alloc_temporary(v
->bld
, 1, &v
->frag_depth
, 1);
3402 else if (l
== FRAG_RESULT_STENCIL
)
3403 return alloc_temporary(v
->bld
, 1, &v
->frag_stencil
, 1);
3405 else if (l
== FRAG_RESULT_SAMPLE_MASK
)
3406 return alloc_temporary(v
->bld
, 1, &v
->sample_mask
, 1);
3408 else if (l
>= FRAG_RESULT_DATA0
&&
3409 l
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
)
3410 return alloc_temporary(v
->bld
, 4,
3411 &v
->outputs
[l
- FRAG_RESULT_DATA0
], 1);
3414 unreachable("Invalid location");
3418 fs_visitor::nir_emit_fs_intrinsic(const fs_builder
&bld
,
3419 nir_intrinsic_instr
*instr
)
3421 assert(stage
== MESA_SHADER_FRAGMENT
);
3424 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3425 dest
= get_nir_dest(instr
->dest
);
3427 switch (instr
->intrinsic
) {
3428 case nir_intrinsic_load_front_face
:
3429 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
3430 *emit_frontfacing_interpolation());
3433 case nir_intrinsic_load_sample_pos
: {
3434 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
3435 assert(sample_pos
.file
!= BAD_FILE
);
3436 dest
.type
= sample_pos
.type
;
3437 bld
.MOV(dest
, sample_pos
);
3438 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
3442 case nir_intrinsic_load_layer_id
:
3443 dest
.type
= BRW_REGISTER_TYPE_UD
;
3444 bld
.MOV(dest
, fetch_render_target_array_index(bld
));
3447 case nir_intrinsic_is_helper_invocation
: {
3448 /* Unlike the regular gl_HelperInvocation, that is defined at dispatch,
3449 * the helperInvocationEXT() (aka SpvOpIsHelperInvocationEXT) takes into
3450 * consideration demoted invocations. That information is stored in
3453 dest
.type
= BRW_REGISTER_TYPE_UD
;
3455 bld
.MOV(dest
, brw_imm_ud(0));
3457 fs_inst
*mov
= bld
.MOV(dest
, brw_imm_ud(~0));
3458 mov
->predicate
= BRW_PREDICATE_NORMAL
;
3459 mov
->predicate_inverse
= true;
3460 mov
->flag_subreg
= sample_mask_flag_subreg(this);
3464 case nir_intrinsic_load_helper_invocation
:
3465 case nir_intrinsic_load_sample_mask_in
:
3466 case nir_intrinsic_load_sample_id
: {
3467 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
3468 fs_reg val
= nir_system_values
[sv
];
3469 assert(val
.file
!= BAD_FILE
);
3470 dest
.type
= val
.type
;
3475 case nir_intrinsic_store_output
: {
3476 const fs_reg src
= get_nir_src(instr
->src
[0]);
3477 const unsigned store_offset
= nir_src_as_uint(instr
->src
[1]);
3478 const unsigned location
= nir_intrinsic_base(instr
) +
3479 SET_FIELD(store_offset
, BRW_NIR_FRAG_OUTPUT_LOCATION
);
3480 const fs_reg new_dest
= retype(alloc_frag_output(this, location
),
3483 for (unsigned j
= 0; j
< instr
->num_components
; j
++)
3484 bld
.MOV(offset(new_dest
, bld
, nir_intrinsic_component(instr
) + j
),
3485 offset(src
, bld
, j
));
3490 case nir_intrinsic_load_output
: {
3491 const unsigned l
= GET_FIELD(nir_intrinsic_base(instr
),
3492 BRW_NIR_FRAG_OUTPUT_LOCATION
);
3493 assert(l
>= FRAG_RESULT_DATA0
);
3494 const unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
3495 const unsigned target
= l
- FRAG_RESULT_DATA0
+ load_offset
;
3496 const fs_reg tmp
= bld
.vgrf(dest
.type
, 4);
3498 if (reinterpret_cast<const brw_wm_prog_key
*>(key
)->coherent_fb_fetch
)
3499 emit_coherent_fb_read(bld
, tmp
, target
);
3501 emit_non_coherent_fb_read(bld
, tmp
, target
);
3503 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3504 bld
.MOV(offset(dest
, bld
, j
),
3505 offset(tmp
, bld
, nir_intrinsic_component(instr
) + j
));
3511 case nir_intrinsic_demote
:
3512 case nir_intrinsic_discard
:
3513 case nir_intrinsic_demote_if
:
3514 case nir_intrinsic_discard_if
: {
3515 /* We track our discarded pixels in f0.1/f1.0. By predicating on it, we
3516 * can update just the flag bits that aren't yet discarded. If there's
3517 * no condition, we emit a CMP of g0 != g0, so all currently executing
3518 * channels will get turned off.
3520 fs_inst
*cmp
= NULL
;
3521 if (instr
->intrinsic
== nir_intrinsic_demote_if
||
3522 instr
->intrinsic
== nir_intrinsic_discard_if
) {
3523 nir_alu_instr
*alu
= nir_src_as_alu_instr(instr
->src
[0]);
3526 alu
->op
!= nir_op_bcsel
&&
3527 (devinfo
->gen
> 5 ||
3528 (alu
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) != BRW_NIR_BOOLEAN_NEEDS_RESOLVE
||
3529 alu
->op
== nir_op_fne32
|| alu
->op
== nir_op_feq32
||
3530 alu
->op
== nir_op_flt32
|| alu
->op
== nir_op_fge32
||
3531 alu
->op
== nir_op_ine32
|| alu
->op
== nir_op_ieq32
||
3532 alu
->op
== nir_op_ilt32
|| alu
->op
== nir_op_ige32
||
3533 alu
->op
== nir_op_ult32
|| alu
->op
== nir_op_uge32
)) {
3534 /* Re-emit the instruction that generated the Boolean value, but
3535 * do not store it. Since this instruction will be conditional,
3536 * other instructions that want to use the real Boolean value may
3537 * get garbage. This was a problem for piglit's fs-discard-exit-2
3540 * Ideally we'd detect that the instruction cannot have a
3541 * conditional modifier before emitting the instructions. Alas,
3542 * that is nigh impossible. Instead, we're going to assume the
3543 * instruction (or last instruction) generated can have a
3544 * conditional modifier. If it cannot, fallback to the old-style
3545 * compare, and hope dead code elimination will clean up the
3546 * extra instructions generated.
3548 nir_emit_alu(bld
, alu
, false);
3550 cmp
= (fs_inst
*) instructions
.get_tail();
3551 if (cmp
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
3552 if (cmp
->can_do_cmod())
3553 cmp
->conditional_mod
= BRW_CONDITIONAL_Z
;
3557 /* The old sequence that would have been generated is,
3558 * basically, bool_result == false. This is equivalent to
3559 * !bool_result, so negate the old modifier.
3561 cmp
->conditional_mod
= brw_negate_cmod(cmp
->conditional_mod
);
3566 cmp
= bld
.CMP(bld
.null_reg_f(), get_nir_src(instr
->src
[0]),
3567 brw_imm_d(0), BRW_CONDITIONAL_Z
);
3570 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
3571 BRW_REGISTER_TYPE_UW
));
3572 cmp
= bld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
, BRW_CONDITIONAL_NZ
);
3575 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
3576 cmp
->flag_subreg
= sample_mask_flag_subreg(this);
3578 if (devinfo
->gen
>= 6) {
3579 /* Due to the way we implement discard, the jump will only happen
3580 * when the whole quad is discarded. So we can do this even for
3581 * demote as it won't break its uniformity promises.
3583 emit_discard_jump();
3586 if (devinfo
->gen
< 7)
3587 limit_dispatch_width(
3588 16, "Fragment discard/demote not implemented in SIMD32 mode.\n");
3592 case nir_intrinsic_load_input
: {
3593 /* load_input is only used for flat inputs */
3594 assert(nir_dest_bit_size(instr
->dest
) == 32);
3595 unsigned base
= nir_intrinsic_base(instr
);
3596 unsigned comp
= nir_intrinsic_component(instr
);
3597 unsigned num_components
= instr
->num_components
;
3599 /* Special case fields in the VUE header */
3600 if (base
== VARYING_SLOT_LAYER
)
3602 else if (base
== VARYING_SLOT_VIEWPORT
)
3605 for (unsigned int i
= 0; i
< num_components
; i
++) {
3606 bld
.MOV(offset(dest
, bld
, i
),
3607 retype(component(interp_reg(base
, comp
+ i
), 3), dest
.type
));
3612 case nir_intrinsic_load_fs_input_interp_deltas
: {
3613 assert(stage
== MESA_SHADER_FRAGMENT
);
3614 assert(nir_src_as_uint(instr
->src
[0]) == 0);
3615 fs_reg interp
= interp_reg(nir_intrinsic_base(instr
),
3616 nir_intrinsic_component(instr
));
3617 dest
.type
= BRW_REGISTER_TYPE_F
;
3618 bld
.MOV(offset(dest
, bld
, 0), component(interp
, 3));
3619 bld
.MOV(offset(dest
, bld
, 1), component(interp
, 1));
3620 bld
.MOV(offset(dest
, bld
, 2), component(interp
, 0));
3624 case nir_intrinsic_load_barycentric_pixel
:
3625 case nir_intrinsic_load_barycentric_centroid
:
3626 case nir_intrinsic_load_barycentric_sample
: {
3627 /* Use the delta_xy values computed from the payload */
3628 const glsl_interp_mode interp_mode
=
3629 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(instr
);
3630 enum brw_barycentric_mode bary
=
3631 brw_barycentric_mode(interp_mode
, instr
->intrinsic
);
3632 const fs_reg srcs
[] = { offset(this->delta_xy
[bary
], bld
, 0),
3633 offset(this->delta_xy
[bary
], bld
, 1) };
3634 bld
.LOAD_PAYLOAD(dest
, srcs
, ARRAY_SIZE(srcs
), 0);
3638 case nir_intrinsic_load_barycentric_at_sample
: {
3639 const glsl_interp_mode interpolation
=
3640 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(instr
);
3642 if (nir_src_is_const(instr
->src
[0])) {
3643 unsigned msg_data
= nir_src_as_uint(instr
->src
[0]) << 4;
3645 emit_pixel_interpolater_send(bld
,
3646 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3649 brw_imm_ud(msg_data
),
3652 const fs_reg sample_src
= retype(get_nir_src(instr
->src
[0]),
3653 BRW_REGISTER_TYPE_UD
);
3655 if (nir_src_is_dynamically_uniform(instr
->src
[0])) {
3656 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
3657 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
3658 bld
.exec_all().group(1, 0)
3659 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
3660 emit_pixel_interpolater_send(bld
,
3661 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3664 component(msg_data
, 0),
3667 /* Make a loop that sends a message to the pixel interpolater
3668 * for the sample number in each live channel. If there are
3669 * multiple channels with the same sample number then these
3670 * will be handled simultaneously with a single interation of
3673 bld
.emit(BRW_OPCODE_DO
);
3675 /* Get the next live sample number into sample_id_reg */
3676 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
3678 /* Set the flag register so that we can perform the send
3679 * message on all channels that have the same sample number
3681 bld
.CMP(bld
.null_reg_ud(),
3682 sample_src
, sample_id
,
3683 BRW_CONDITIONAL_EQ
);
3684 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
3685 bld
.exec_all().group(1, 0)
3686 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
3688 emit_pixel_interpolater_send(bld
,
3689 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3692 component(msg_data
, 0),
3694 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
3696 /* Continue the loop if there are any live channels left */
3697 set_predicate_inv(BRW_PREDICATE_NORMAL
,
3699 bld
.emit(BRW_OPCODE_WHILE
));
3705 case nir_intrinsic_load_barycentric_at_offset
: {
3706 const glsl_interp_mode interpolation
=
3707 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(instr
);
3709 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
3712 assert(nir_src_bit_size(instr
->src
[0]) == 32);
3713 unsigned off_x
= MIN2((int)(const_offset
[0].f32
* 16), 7) & 0xf;
3714 unsigned off_y
= MIN2((int)(const_offset
[1].f32
* 16), 7) & 0xf;
3716 emit_pixel_interpolater_send(bld
,
3717 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
,
3720 brw_imm_ud(off_x
| (off_y
<< 4)),
3723 fs_reg src
= vgrf(glsl_type::ivec2_type
);
3724 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
3725 BRW_REGISTER_TYPE_F
);
3726 for (int i
= 0; i
< 2; i
++) {
3727 fs_reg temp
= vgrf(glsl_type::float_type
);
3728 bld
.MUL(temp
, offset(offset_src
, bld
, i
), brw_imm_f(16.0f
));
3729 fs_reg itemp
= vgrf(glsl_type::int_type
);
3731 bld
.MOV(itemp
, temp
);
3733 /* Clamp the upper end of the range to +7/16.
3734 * ARB_gpu_shader5 requires that we support a maximum offset
3735 * of +0.5, which isn't representable in a S0.4 value -- if
3736 * we didn't clamp it, we'd end up with -8/16, which is the
3737 * opposite of what the shader author wanted.
3739 * This is legal due to ARB_gpu_shader5's quantization
3742 * "Not all values of <offset> may be supported; x and y
3743 * offsets may be rounded to fixed-point values with the
3744 * number of fraction bits given by the
3745 * implementation-dependent constant
3746 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3748 set_condmod(BRW_CONDITIONAL_L
,
3749 bld
.SEL(offset(src
, bld
, i
), itemp
, brw_imm_d(7)));
3752 const enum opcode opcode
= FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
;
3753 emit_pixel_interpolater_send(bld
,
3763 case nir_intrinsic_load_frag_coord
:
3764 emit_fragcoord_interpolation(dest
);
3767 case nir_intrinsic_load_interpolated_input
: {
3768 assert(instr
->src
[0].ssa
&&
3769 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_intrinsic
);
3770 nir_intrinsic_instr
*bary_intrinsic
=
3771 nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3772 nir_intrinsic_op bary_intrin
= bary_intrinsic
->intrinsic
;
3773 enum glsl_interp_mode interp_mode
=
3774 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(bary_intrinsic
);
3777 if (bary_intrin
== nir_intrinsic_load_barycentric_at_offset
||
3778 bary_intrin
== nir_intrinsic_load_barycentric_at_sample
) {
3779 /* Use the result of the PI message. */
3780 dst_xy
= retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_F
);
3782 /* Use the delta_xy values computed from the payload */
3783 enum brw_barycentric_mode bary
=
3784 brw_barycentric_mode(interp_mode
, bary_intrin
);
3785 dst_xy
= this->delta_xy
[bary
];
3788 for (unsigned int i
= 0; i
< instr
->num_components
; i
++) {
3790 component(interp_reg(nir_intrinsic_base(instr
),
3791 nir_intrinsic_component(instr
) + i
), 0);
3792 interp
.type
= BRW_REGISTER_TYPE_F
;
3793 dest
.type
= BRW_REGISTER_TYPE_F
;
3795 if (devinfo
->gen
< 6 && interp_mode
== INTERP_MODE_SMOOTH
) {
3796 fs_reg tmp
= vgrf(glsl_type::float_type
);
3797 bld
.emit(FS_OPCODE_LINTERP
, tmp
, dst_xy
, interp
);
3798 bld
.MUL(offset(dest
, bld
, i
), tmp
, this->pixel_w
);
3800 bld
.emit(FS_OPCODE_LINTERP
, offset(dest
, bld
, i
), dst_xy
, interp
);
3807 nir_emit_intrinsic(bld
, instr
);
3813 fs_visitor::nir_emit_cs_intrinsic(const fs_builder
&bld
,
3814 nir_intrinsic_instr
*instr
)
3816 assert(stage
== MESA_SHADER_COMPUTE
);
3817 struct brw_cs_prog_data
*cs_prog_data
= brw_cs_prog_data(prog_data
);
3820 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3821 dest
= get_nir_dest(instr
->dest
);
3823 switch (instr
->intrinsic
) {
3824 case nir_intrinsic_control_barrier
:
3825 /* The whole workgroup fits in a single HW thread, so all the
3826 * invocations are already executed lock-step. Instead of an actual
3827 * barrier just emit a scheduling fence, that will generate no code.
3829 if (!cs_prog_data
->uses_variable_group_size
&&
3830 workgroup_size() <= dispatch_width
) {
3831 bld
.exec_all().group(1, 0).emit(FS_OPCODE_SCHEDULING_FENCE
);
3836 cs_prog_data
->uses_barrier
= true;
3839 case nir_intrinsic_load_subgroup_id
:
3840 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
), subgroup_id
);
3843 case nir_intrinsic_load_local_invocation_id
:
3844 case nir_intrinsic_load_work_group_id
: {
3845 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
3846 fs_reg val
= nir_system_values
[sv
];
3847 assert(val
.file
!= BAD_FILE
);
3848 dest
.type
= val
.type
;
3849 for (unsigned i
= 0; i
< 3; i
++)
3850 bld
.MOV(offset(dest
, bld
, i
), offset(val
, bld
, i
));
3854 case nir_intrinsic_load_num_work_groups
: {
3855 const unsigned surface
=
3856 cs_prog_data
->binding_table
.work_groups_start
;
3858 cs_prog_data
->uses_num_work_groups
= true;
3860 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
3861 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(surface
);
3862 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
3863 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(1); /* num components */
3865 /* Read the 3 GLuint components of gl_NumWorkGroups */
3866 for (unsigned i
= 0; i
< 3; i
++) {
3867 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = brw_imm_ud(i
<< 2);
3868 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
,
3869 offset(dest
, bld
, i
), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
3874 case nir_intrinsic_shared_atomic_add
:
3875 case nir_intrinsic_shared_atomic_imin
:
3876 case nir_intrinsic_shared_atomic_umin
:
3877 case nir_intrinsic_shared_atomic_imax
:
3878 case nir_intrinsic_shared_atomic_umax
:
3879 case nir_intrinsic_shared_atomic_and
:
3880 case nir_intrinsic_shared_atomic_or
:
3881 case nir_intrinsic_shared_atomic_xor
:
3882 case nir_intrinsic_shared_atomic_exchange
:
3883 case nir_intrinsic_shared_atomic_comp_swap
:
3884 nir_emit_shared_atomic(bld
, brw_aop_for_nir_intrinsic(instr
), instr
);
3886 case nir_intrinsic_shared_atomic_fmin
:
3887 case nir_intrinsic_shared_atomic_fmax
:
3888 case nir_intrinsic_shared_atomic_fcomp_swap
:
3889 nir_emit_shared_atomic_float(bld
, brw_aop_for_nir_intrinsic(instr
), instr
);
3892 case nir_intrinsic_load_shared
: {
3893 assert(devinfo
->gen
>= 7);
3894 assert(stage
== MESA_SHADER_COMPUTE
);
3896 const unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
3897 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
3898 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(GEN7_BTI_SLM
);
3899 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[0]);
3900 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
3902 /* Make dest unsigned because that's what the temporary will be */
3903 dest
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
3905 /* Read the vector */
3906 assert(nir_dest_bit_size(instr
->dest
) <= 32);
3907 assert(nir_intrinsic_align(instr
) > 0);
3908 if (nir_dest_bit_size(instr
->dest
) == 32 &&
3909 nir_intrinsic_align(instr
) >= 4) {
3910 assert(nir_dest_num_components(instr
->dest
) <= 4);
3911 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
3913 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
,
3914 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
3915 inst
->size_written
= instr
->num_components
* dispatch_width
* 4;
3917 assert(nir_dest_num_components(instr
->dest
) == 1);
3918 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(bit_size
);
3920 fs_reg read_result
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3921 bld
.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
,
3922 read_result
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
3923 bld
.MOV(dest
, subscript(read_result
, dest
.type
, 0));
3928 case nir_intrinsic_store_shared
: {
3929 assert(devinfo
->gen
>= 7);
3930 assert(stage
== MESA_SHADER_COMPUTE
);
3932 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
3933 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
3934 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(GEN7_BTI_SLM
);
3935 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
3936 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
3938 fs_reg data
= get_nir_src(instr
->src
[0]);
3939 data
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
3941 assert(nir_src_bit_size(instr
->src
[0]) <= 32);
3942 assert(nir_intrinsic_write_mask(instr
) ==
3943 (1u << instr
->num_components
) - 1);
3944 assert(nir_intrinsic_align(instr
) > 0);
3945 if (nir_src_bit_size(instr
->src
[0]) == 32 &&
3946 nir_intrinsic_align(instr
) >= 4) {
3947 assert(nir_src_num_components(instr
->src
[0]) <= 4);
3948 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
3949 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
3950 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
,
3951 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
3953 assert(nir_src_num_components(instr
->src
[0]) == 1);
3954 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(bit_size
);
3956 srcs
[SURFACE_LOGICAL_SRC_DATA
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3957 bld
.MOV(srcs
[SURFACE_LOGICAL_SRC_DATA
], data
);
3959 bld
.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
,
3960 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
3965 case nir_intrinsic_load_local_group_size
: {
3966 for (unsigned i
= 0; i
< 3; i
++) {
3967 bld
.MOV(retype(offset(dest
, bld
, i
), BRW_REGISTER_TYPE_UD
),
3974 nir_emit_intrinsic(bld
, instr
);
3980 brw_nir_reduction_op_identity(const fs_builder
&bld
,
3981 nir_op op
, brw_reg_type type
)
3983 nir_const_value value
= nir_alu_binop_identity(op
, type_sz(type
) * 8);
3984 switch (type_sz(type
)) {
3986 if (type
== BRW_REGISTER_TYPE_UB
) {
3987 return brw_imm_uw(value
.u8
);
3989 assert(type
== BRW_REGISTER_TYPE_B
);
3990 return brw_imm_w(value
.i8
);
3993 return retype(brw_imm_uw(value
.u16
), type
);
3995 return retype(brw_imm_ud(value
.u32
), type
);
3997 if (type
== BRW_REGISTER_TYPE_DF
)
3998 return setup_imm_df(bld
, value
.f64
);
4000 return retype(brw_imm_u64(value
.u64
), type
);
4002 unreachable("Invalid type size");
4007 brw_op_for_nir_reduction_op(nir_op op
)
4010 case nir_op_iadd
: return BRW_OPCODE_ADD
;
4011 case nir_op_fadd
: return BRW_OPCODE_ADD
;
4012 case nir_op_imul
: return BRW_OPCODE_MUL
;
4013 case nir_op_fmul
: return BRW_OPCODE_MUL
;
4014 case nir_op_imin
: return BRW_OPCODE_SEL
;
4015 case nir_op_umin
: return BRW_OPCODE_SEL
;
4016 case nir_op_fmin
: return BRW_OPCODE_SEL
;
4017 case nir_op_imax
: return BRW_OPCODE_SEL
;
4018 case nir_op_umax
: return BRW_OPCODE_SEL
;
4019 case nir_op_fmax
: return BRW_OPCODE_SEL
;
4020 case nir_op_iand
: return BRW_OPCODE_AND
;
4021 case nir_op_ior
: return BRW_OPCODE_OR
;
4022 case nir_op_ixor
: return BRW_OPCODE_XOR
;
4024 unreachable("Invalid reduction operation");
4028 static brw_conditional_mod
4029 brw_cond_mod_for_nir_reduction_op(nir_op op
)
4032 case nir_op_iadd
: return BRW_CONDITIONAL_NONE
;
4033 case nir_op_fadd
: return BRW_CONDITIONAL_NONE
;
4034 case nir_op_imul
: return BRW_CONDITIONAL_NONE
;
4035 case nir_op_fmul
: return BRW_CONDITIONAL_NONE
;
4036 case nir_op_imin
: return BRW_CONDITIONAL_L
;
4037 case nir_op_umin
: return BRW_CONDITIONAL_L
;
4038 case nir_op_fmin
: return BRW_CONDITIONAL_L
;
4039 case nir_op_imax
: return BRW_CONDITIONAL_GE
;
4040 case nir_op_umax
: return BRW_CONDITIONAL_GE
;
4041 case nir_op_fmax
: return BRW_CONDITIONAL_GE
;
4042 case nir_op_iand
: return BRW_CONDITIONAL_NONE
;
4043 case nir_op_ior
: return BRW_CONDITIONAL_NONE
;
4044 case nir_op_ixor
: return BRW_CONDITIONAL_NONE
;
4046 unreachable("Invalid reduction operation");
4051 fs_visitor::get_nir_image_intrinsic_image(const brw::fs_builder
&bld
,
4052 nir_intrinsic_instr
*instr
)
4054 fs_reg image
= retype(get_nir_src_imm(instr
->src
[0]), BRW_REGISTER_TYPE_UD
);
4055 fs_reg surf_index
= image
;
4057 if (stage_prog_data
->binding_table
.image_start
> 0) {
4058 if (image
.file
== BRW_IMMEDIATE_VALUE
) {
4060 brw_imm_ud(image
.d
+ stage_prog_data
->binding_table
.image_start
);
4062 surf_index
= vgrf(glsl_type::uint_type
);
4063 bld
.ADD(surf_index
, image
,
4064 brw_imm_d(stage_prog_data
->binding_table
.image_start
));
4068 return bld
.emit_uniformize(surf_index
);
4072 fs_visitor::get_nir_ssbo_intrinsic_index(const brw::fs_builder
&bld
,
4073 nir_intrinsic_instr
*instr
)
4075 /* SSBO stores are weird in that their index is in src[1] */
4076 const unsigned src
= instr
->intrinsic
== nir_intrinsic_store_ssbo
? 1 : 0;
4079 if (nir_src_is_const(instr
->src
[src
])) {
4080 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
4081 nir_src_as_uint(instr
->src
[src
]);
4082 surf_index
= brw_imm_ud(index
);
4084 surf_index
= vgrf(glsl_type::uint_type
);
4085 bld
.ADD(surf_index
, get_nir_src(instr
->src
[src
]),
4086 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
4089 return bld
.emit_uniformize(surf_index
);
4093 * The offsets we get from NIR act as if each SIMD channel has it's own blob
4094 * of contiguous space. However, if we actually place each SIMD channel in
4095 * it's own space, we end up with terrible cache performance because each SIMD
4096 * channel accesses a different cache line even when they're all accessing the
4097 * same byte offset. To deal with this problem, we swizzle the address using
4098 * a simple algorithm which ensures that any time a SIMD message reads or
4099 * writes the same address, it's all in the same cache line. We have to keep
4100 * the bottom two bits fixed so that we can read/write up to a dword at a time
4101 * and the individual element is contiguous. We do this by splitting the
4102 * address as follows:
4105 * +-------------------------------+------------+----------+
4106 * | Hi address bits | chan index | addr low |
4107 * +-------------------------------+------------+----------+
4109 * In other words, the bottom two address bits stay, and the top 30 get
4110 * shifted up so that we can stick the SIMD channel index in the middle. This
4111 * way, we can access 8, 16, or 32-bit elements and, when accessing a 32-bit
4112 * at the same logical offset, the scratch read/write instruction acts on
4113 * continuous elements and we get good cache locality.
4116 fs_visitor::swizzle_nir_scratch_addr(const brw::fs_builder
&bld
,
4117 const fs_reg
&nir_addr
,
4120 const fs_reg
&chan_index
=
4121 nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
];
4122 const unsigned chan_index_bits
= ffs(dispatch_width
) - 1;
4124 fs_reg addr
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4126 /* In this case, we know the address is aligned to a DWORD and we want
4127 * the final address in DWORDs.
4129 bld
.SHL(addr
, nir_addr
, brw_imm_ud(chan_index_bits
- 2));
4130 bld
.OR(addr
, addr
, chan_index
);
4132 /* This case substantially more annoying because we have to pay
4133 * attention to those pesky two bottom bits.
4135 fs_reg addr_hi
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4136 bld
.AND(addr_hi
, nir_addr
, brw_imm_ud(~0x3u
));
4137 bld
.SHL(addr_hi
, addr_hi
, brw_imm_ud(chan_index_bits
));
4138 fs_reg chan_addr
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4139 bld
.SHL(chan_addr
, chan_index
, brw_imm_ud(2));
4140 bld
.AND(addr
, nir_addr
, brw_imm_ud(0x3u
));
4141 bld
.OR(addr
, addr
, addr_hi
);
4142 bld
.OR(addr
, addr
, chan_addr
);
4148 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
4151 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4152 dest
= get_nir_dest(instr
->dest
);
4154 switch (instr
->intrinsic
) {
4155 case nir_intrinsic_image_load
:
4156 case nir_intrinsic_image_store
:
4157 case nir_intrinsic_image_atomic_add
:
4158 case nir_intrinsic_image_atomic_imin
:
4159 case nir_intrinsic_image_atomic_umin
:
4160 case nir_intrinsic_image_atomic_imax
:
4161 case nir_intrinsic_image_atomic_umax
:
4162 case nir_intrinsic_image_atomic_and
:
4163 case nir_intrinsic_image_atomic_or
:
4164 case nir_intrinsic_image_atomic_xor
:
4165 case nir_intrinsic_image_atomic_exchange
:
4166 case nir_intrinsic_image_atomic_comp_swap
:
4167 case nir_intrinsic_bindless_image_load
:
4168 case nir_intrinsic_bindless_image_store
:
4169 case nir_intrinsic_bindless_image_atomic_add
:
4170 case nir_intrinsic_bindless_image_atomic_imin
:
4171 case nir_intrinsic_bindless_image_atomic_umin
:
4172 case nir_intrinsic_bindless_image_atomic_imax
:
4173 case nir_intrinsic_bindless_image_atomic_umax
:
4174 case nir_intrinsic_bindless_image_atomic_and
:
4175 case nir_intrinsic_bindless_image_atomic_or
:
4176 case nir_intrinsic_bindless_image_atomic_xor
:
4177 case nir_intrinsic_bindless_image_atomic_exchange
:
4178 case nir_intrinsic_bindless_image_atomic_comp_swap
: {
4179 if (stage
== MESA_SHADER_FRAGMENT
&&
4180 instr
->intrinsic
!= nir_intrinsic_image_load
)
4181 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4183 /* Get some metadata from the image intrinsic. */
4184 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
4186 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4188 switch (instr
->intrinsic
) {
4189 case nir_intrinsic_image_load
:
4190 case nir_intrinsic_image_store
:
4191 case nir_intrinsic_image_atomic_add
:
4192 case nir_intrinsic_image_atomic_imin
:
4193 case nir_intrinsic_image_atomic_umin
:
4194 case nir_intrinsic_image_atomic_imax
:
4195 case nir_intrinsic_image_atomic_umax
:
4196 case nir_intrinsic_image_atomic_and
:
4197 case nir_intrinsic_image_atomic_or
:
4198 case nir_intrinsic_image_atomic_xor
:
4199 case nir_intrinsic_image_atomic_exchange
:
4200 case nir_intrinsic_image_atomic_comp_swap
:
4201 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4202 get_nir_image_intrinsic_image(bld
, instr
);
4207 srcs
[SURFACE_LOGICAL_SRC_SURFACE_HANDLE
] =
4208 bld
.emit_uniformize(get_nir_src(instr
->src
[0]));
4212 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
4213 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] =
4214 brw_imm_ud(nir_image_intrinsic_coord_components(instr
));
4216 /* Emit an image load, store or atomic op. */
4217 if (instr
->intrinsic
== nir_intrinsic_image_load
||
4218 instr
->intrinsic
== nir_intrinsic_bindless_image_load
) {
4219 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
4221 bld
.emit(SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL
,
4222 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4223 inst
->size_written
= instr
->num_components
* dispatch_width
* 4;
4224 } else if (instr
->intrinsic
== nir_intrinsic_image_store
||
4225 instr
->intrinsic
== nir_intrinsic_bindless_image_store
) {
4226 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
4227 srcs
[SURFACE_LOGICAL_SRC_DATA
] = get_nir_src(instr
->src
[3]);
4228 bld
.emit(SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL
,
4229 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4231 unsigned num_srcs
= info
->num_srcs
;
4232 int op
= brw_aop_for_nir_intrinsic(instr
);
4233 if (op
== BRW_AOP_INC
|| op
== BRW_AOP_DEC
) {
4234 assert(num_srcs
== 4);
4238 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(op
);
4242 data
= get_nir_src(instr
->src
[3]);
4243 if (num_srcs
>= 5) {
4244 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
4245 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[4]) };
4246 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
4249 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
4251 bld
.emit(SHADER_OPCODE_TYPED_ATOMIC_LOGICAL
,
4252 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4257 case nir_intrinsic_image_size
:
4258 case nir_intrinsic_bindless_image_size
: {
4259 /* Unlike the [un]typed load and store opcodes, the TXS that this turns
4260 * into will handle the binding table index for us in the geneerator.
4261 * Incidentally, this means that we can handle bindless with exactly the
4264 fs_reg image
= retype(get_nir_src_imm(instr
->src
[0]),
4265 BRW_REGISTER_TYPE_UD
);
4266 image
= bld
.emit_uniformize(image
);
4268 fs_reg srcs
[TEX_LOGICAL_NUM_SRCS
];
4269 if (instr
->intrinsic
== nir_intrinsic_image_size
)
4270 srcs
[TEX_LOGICAL_SRC_SURFACE
] = image
;
4272 srcs
[TEX_LOGICAL_SRC_SURFACE_HANDLE
] = image
;
4273 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = brw_imm_d(0);
4274 srcs
[TEX_LOGICAL_SRC_COORD_COMPONENTS
] = brw_imm_d(0);
4275 srcs
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
] = brw_imm_d(0);
4277 /* Since the image size is always uniform, we can just emit a SIMD8
4278 * query instruction and splat the result out.
4280 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4282 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 4);
4283 fs_inst
*inst
= ubld
.emit(SHADER_OPCODE_IMAGE_SIZE_LOGICAL
,
4284 tmp
, srcs
, ARRAY_SIZE(srcs
));
4285 inst
->size_written
= 4 * REG_SIZE
;
4287 for (unsigned c
= 0; c
< instr
->dest
.ssa
.num_components
; ++c
) {
4288 if (c
== 2 && nir_intrinsic_image_dim(instr
) == GLSL_SAMPLER_DIM_CUBE
) {
4289 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
,
4290 offset(retype(dest
, tmp
.type
), bld
, c
),
4291 component(offset(tmp
, ubld
, c
), 0), brw_imm_ud(6));
4293 bld
.MOV(offset(retype(dest
, tmp
.type
), bld
, c
),
4294 component(offset(tmp
, ubld
, c
), 0));
4300 case nir_intrinsic_image_load_raw_intel
: {
4301 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4302 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4303 get_nir_image_intrinsic_image(bld
, instr
);
4304 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
4305 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
4306 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
4309 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
,
4310 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4311 inst
->size_written
= instr
->num_components
* dispatch_width
* 4;
4315 case nir_intrinsic_image_store_raw_intel
: {
4316 if (stage
== MESA_SHADER_FRAGMENT
)
4317 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4319 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4320 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4321 get_nir_image_intrinsic_image(bld
, instr
);
4322 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
4323 srcs
[SURFACE_LOGICAL_SRC_DATA
] = get_nir_src(instr
->src
[2]);
4324 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
4325 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
4327 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
,
4328 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4332 case nir_intrinsic_scoped_memory_barrier
:
4333 case nir_intrinsic_group_memory_barrier
:
4334 case nir_intrinsic_memory_barrier_shared
:
4335 case nir_intrinsic_memory_barrier_buffer
:
4336 case nir_intrinsic_memory_barrier_image
:
4337 case nir_intrinsic_memory_barrier
: {
4338 bool l3_fence
, slm_fence
;
4339 if (instr
->intrinsic
== nir_intrinsic_scoped_memory_barrier
) {
4340 nir_variable_mode modes
= nir_intrinsic_memory_modes(instr
);
4341 l3_fence
= modes
& (nir_var_shader_out
|
4343 nir_var_mem_global
);
4344 slm_fence
= modes
& nir_var_mem_shared
;
4346 l3_fence
= instr
->intrinsic
!= nir_intrinsic_memory_barrier_shared
;
4347 slm_fence
= instr
->intrinsic
== nir_intrinsic_group_memory_barrier
||
4348 instr
->intrinsic
== nir_intrinsic_memory_barrier
||
4349 instr
->intrinsic
== nir_intrinsic_memory_barrier_shared
;
4352 if (stage
!= MESA_SHADER_COMPUTE
)
4355 /* If the workgroup fits in a single HW thread, the messages for SLM are
4356 * processed in-order and the shader itself is already synchronized so
4357 * the memory fence is not necessary.
4359 * TODO: Check if applies for many HW threads sharing same Data Port.
4361 if (!brw_cs_prog_data(prog_data
)->uses_variable_group_size
&&
4362 slm_fence
&& workgroup_size() <= dispatch_width
)
4365 /* Prior to Gen11, there's only L3 fence, so emit that instead. */
4366 if (slm_fence
&& devinfo
->gen
< 11) {
4371 /* Be conservative in Gen11+ and always stall in a fence. Since there
4372 * are two different fences, and shader might want to synchronize
4375 * TODO: Improve NIR so that scope and visibility information for the
4376 * barriers is available here to make a better decision.
4378 * TODO: When emitting more than one fence, it might help emit all
4379 * the fences first and then generate the stall moves.
4381 const bool stall
= devinfo
->gen
>= 11;
4383 const fs_builder ubld
= bld
.group(8, 0);
4384 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
4387 ubld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
,
4388 brw_vec8_grf(0, 0), brw_imm_ud(stall
),
4389 /* bti */ brw_imm_ud(0))
4390 ->size_written
= 2 * REG_SIZE
;
4394 ubld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
,
4395 brw_vec8_grf(0, 0), brw_imm_ud(stall
),
4396 brw_imm_ud(GEN7_BTI_SLM
))
4397 ->size_written
= 2 * REG_SIZE
;
4400 if (!l3_fence
&& !slm_fence
)
4401 ubld
.emit(FS_OPCODE_SCHEDULING_FENCE
);
4406 case nir_intrinsic_memory_barrier_tcs_patch
:
4409 case nir_intrinsic_shader_clock
: {
4410 /* We cannot do anything if there is an event, so ignore it for now */
4411 const fs_reg shader_clock
= get_timestamp(bld
);
4412 const fs_reg srcs
[] = { component(shader_clock
, 0),
4413 component(shader_clock
, 1) };
4414 bld
.LOAD_PAYLOAD(dest
, srcs
, ARRAY_SIZE(srcs
), 0);
4418 case nir_intrinsic_image_samples
:
4419 /* The driver does not support multi-sampled images. */
4420 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(1));
4423 case nir_intrinsic_load_uniform
: {
4424 /* Offsets are in bytes but they should always aligned to
4427 assert(instr
->const_index
[0] % 4 == 0 ||
4428 instr
->const_index
[0] % type_sz(dest
.type
) == 0);
4430 fs_reg
src(UNIFORM
, instr
->const_index
[0] / 4, dest
.type
);
4432 if (nir_src_is_const(instr
->src
[0])) {
4433 unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
4434 assert(load_offset
% type_sz(dest
.type
) == 0);
4435 /* For 16-bit types we add the module of the const_index[0]
4436 * offset to access to not 32-bit aligned element
4438 src
.offset
= load_offset
+ instr
->const_index
[0] % 4;
4440 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
4441 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
4444 fs_reg indirect
= retype(get_nir_src(instr
->src
[0]),
4445 BRW_REGISTER_TYPE_UD
);
4447 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
4448 * go past the end of the uniform. In order to keep the n'th
4449 * component from running past, we subtract off the size of all but
4450 * one component of the vector.
4452 assert(instr
->const_index
[1] >=
4453 instr
->num_components
* (int) type_sz(dest
.type
));
4454 unsigned read_size
= instr
->const_index
[1] -
4455 (instr
->num_components
- 1) * type_sz(dest
.type
);
4457 bool supports_64bit_indirects
=
4458 !devinfo
->is_cherryview
&& !gen_device_info_is_9lp(devinfo
);
4460 if (type_sz(dest
.type
) != 8 || supports_64bit_indirects
) {
4461 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
4462 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
4463 offset(dest
, bld
, j
), offset(src
, bld
, j
),
4464 indirect
, brw_imm_ud(read_size
));
4467 const unsigned num_mov_indirects
=
4468 type_sz(dest
.type
) / type_sz(BRW_REGISTER_TYPE_UD
);
4469 /* We read a little bit less per MOV INDIRECT, as they are now
4470 * 32-bits ones instead of 64-bit. Fix read_size then.
4472 const unsigned read_size_32bit
= read_size
-
4473 (num_mov_indirects
- 1) * type_sz(BRW_REGISTER_TYPE_UD
);
4474 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
4475 for (unsigned i
= 0; i
< num_mov_indirects
; i
++) {
4476 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
4477 subscript(offset(dest
, bld
, j
), BRW_REGISTER_TYPE_UD
, i
),
4478 subscript(offset(src
, bld
, j
), BRW_REGISTER_TYPE_UD
, i
),
4479 indirect
, brw_imm_ud(read_size_32bit
));
4487 case nir_intrinsic_load_ubo
: {
4489 if (nir_src_is_const(instr
->src
[0])) {
4490 const unsigned index
= stage_prog_data
->binding_table
.ubo_start
+
4491 nir_src_as_uint(instr
->src
[0]);
4492 surf_index
= brw_imm_ud(index
);
4494 /* The block index is not a constant. Evaluate the index expression
4495 * per-channel and add the base UBO index; we have to select a value
4496 * from any live channel.
4498 surf_index
= vgrf(glsl_type::uint_type
);
4499 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
4500 brw_imm_ud(stage_prog_data
->binding_table
.ubo_start
));
4501 surf_index
= bld
.emit_uniformize(surf_index
);
4504 if (!nir_src_is_const(instr
->src
[1])) {
4505 fs_reg base_offset
= retype(get_nir_src(instr
->src
[1]),
4506 BRW_REGISTER_TYPE_UD
);
4508 for (int i
= 0; i
< instr
->num_components
; i
++)
4509 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
4510 base_offset
, i
* type_sz(dest
.type
));
4512 prog_data
->has_ubo_pull
= true;
4514 /* Even if we are loading doubles, a pull constant load will load
4515 * a 32-bit vec4, so should only reserve vgrf space for that. If we
4516 * need to load a full dvec4 we will have to emit 2 loads. This is
4517 * similar to demote_pull_constants(), except that in that case we
4518 * see individual accesses to each component of the vector and then
4519 * we let CSE deal with duplicate loads. Here we see a vector access
4520 * and we have to split it if necessary.
4522 const unsigned type_size
= type_sz(dest
.type
);
4523 const unsigned load_offset
= nir_src_as_uint(instr
->src
[1]);
4525 /* See if we've selected this as a push constant candidate */
4526 if (nir_src_is_const(instr
->src
[0])) {
4527 const unsigned ubo_block
= nir_src_as_uint(instr
->src
[0]);
4528 const unsigned offset_256b
= load_offset
/ 32;
4531 for (int i
= 0; i
< 4; i
++) {
4532 const struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
4533 if (range
->block
== ubo_block
&&
4534 offset_256b
>= range
->start
&&
4535 offset_256b
< range
->start
+ range
->length
) {
4537 push_reg
= fs_reg(UNIFORM
, UBO_START
+ i
, dest
.type
);
4538 push_reg
.offset
= load_offset
- 32 * range
->start
;
4543 if (push_reg
.file
!= BAD_FILE
) {
4544 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
4545 bld
.MOV(offset(dest
, bld
, i
),
4546 byte_offset(push_reg
, i
* type_size
));
4552 prog_data
->has_ubo_pull
= true;
4554 const unsigned block_sz
= 64; /* Fetch one cacheline at a time. */
4555 const fs_builder ubld
= bld
.exec_all().group(block_sz
/ 4, 0);
4556 const fs_reg packed_consts
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4558 for (unsigned c
= 0; c
< instr
->num_components
;) {
4559 const unsigned base
= load_offset
+ c
* type_size
;
4560 /* Number of usable components in the next block-aligned load. */
4561 const unsigned count
= MIN2(instr
->num_components
- c
,
4562 (block_sz
- base
% block_sz
) / type_size
);
4564 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
4565 packed_consts
, surf_index
,
4566 brw_imm_ud(base
& ~(block_sz
- 1)));
4568 const fs_reg consts
=
4569 retype(byte_offset(packed_consts
, base
& (block_sz
- 1)),
4572 for (unsigned d
= 0; d
< count
; d
++)
4573 bld
.MOV(offset(dest
, bld
, c
+ d
), component(consts
, d
));
4581 case nir_intrinsic_load_global
: {
4582 assert(devinfo
->gen
>= 8);
4584 assert(nir_dest_bit_size(instr
->dest
) <= 32);
4585 assert(nir_intrinsic_align(instr
) > 0);
4586 if (nir_dest_bit_size(instr
->dest
) == 32 &&
4587 nir_intrinsic_align(instr
) >= 4) {
4588 assert(nir_dest_num_components(instr
->dest
) <= 4);
4589 fs_inst
*inst
= bld
.emit(SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL
,
4591 get_nir_src(instr
->src
[0]), /* Address */
4592 fs_reg(), /* No source data */
4593 brw_imm_ud(instr
->num_components
));
4594 inst
->size_written
= instr
->num_components
*
4595 inst
->dst
.component_size(inst
->exec_size
);
4597 const unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
4598 assert(nir_dest_num_components(instr
->dest
) == 1);
4599 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4600 bld
.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL
,
4602 get_nir_src(instr
->src
[0]), /* Address */
4603 fs_reg(), /* No source data */
4604 brw_imm_ud(bit_size
));
4605 bld
.MOV(dest
, subscript(tmp
, dest
.type
, 0));
4610 case nir_intrinsic_store_global
:
4611 assert(devinfo
->gen
>= 8);
4613 if (stage
== MESA_SHADER_FRAGMENT
)
4614 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4616 assert(nir_src_bit_size(instr
->src
[0]) <= 32);
4617 assert(nir_intrinsic_write_mask(instr
) ==
4618 (1u << instr
->num_components
) - 1);
4619 assert(nir_intrinsic_align(instr
) > 0);
4620 if (nir_src_bit_size(instr
->src
[0]) == 32 &&
4621 nir_intrinsic_align(instr
) >= 4) {
4622 assert(nir_src_num_components(instr
->src
[0]) <= 4);
4623 bld
.emit(SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL
,
4625 get_nir_src(instr
->src
[1]), /* Address */
4626 get_nir_src(instr
->src
[0]), /* Data */
4627 brw_imm_ud(instr
->num_components
));
4629 assert(nir_src_num_components(instr
->src
[0]) == 1);
4630 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
4631 brw_reg_type data_type
=
4632 brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4633 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4634 bld
.MOV(tmp
, retype(get_nir_src(instr
->src
[0]), data_type
));
4635 bld
.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL
,
4637 get_nir_src(instr
->src
[1]), /* Address */
4639 brw_imm_ud(nir_src_bit_size(instr
->src
[0])));
4643 case nir_intrinsic_global_atomic_add
:
4644 case nir_intrinsic_global_atomic_imin
:
4645 case nir_intrinsic_global_atomic_umin
:
4646 case nir_intrinsic_global_atomic_imax
:
4647 case nir_intrinsic_global_atomic_umax
:
4648 case nir_intrinsic_global_atomic_and
:
4649 case nir_intrinsic_global_atomic_or
:
4650 case nir_intrinsic_global_atomic_xor
:
4651 case nir_intrinsic_global_atomic_exchange
:
4652 case nir_intrinsic_global_atomic_comp_swap
:
4653 nir_emit_global_atomic(bld
, brw_aop_for_nir_intrinsic(instr
), instr
);
4655 case nir_intrinsic_global_atomic_fmin
:
4656 case nir_intrinsic_global_atomic_fmax
:
4657 case nir_intrinsic_global_atomic_fcomp_swap
:
4658 nir_emit_global_atomic_float(bld
, brw_aop_for_nir_intrinsic(instr
), instr
);
4661 case nir_intrinsic_load_ssbo
: {
4662 assert(devinfo
->gen
>= 7);
4664 const unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
4665 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4666 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4667 get_nir_ssbo_intrinsic_index(bld
, instr
);
4668 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
4669 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
4671 /* Make dest unsigned because that's what the temporary will be */
4672 dest
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4674 /* Read the vector */
4675 assert(nir_dest_bit_size(instr
->dest
) <= 32);
4676 assert(nir_intrinsic_align(instr
) > 0);
4677 if (nir_dest_bit_size(instr
->dest
) == 32 &&
4678 nir_intrinsic_align(instr
) >= 4) {
4679 assert(nir_dest_num_components(instr
->dest
) <= 4);
4680 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
4682 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL
,
4683 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4684 inst
->size_written
= instr
->num_components
* dispatch_width
* 4;
4686 assert(nir_dest_num_components(instr
->dest
) == 1);
4687 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(bit_size
);
4689 fs_reg read_result
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4690 bld
.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
,
4691 read_result
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4692 bld
.MOV(dest
, subscript(read_result
, dest
.type
, 0));
4697 case nir_intrinsic_store_ssbo
: {
4698 assert(devinfo
->gen
>= 7);
4700 if (stage
== MESA_SHADER_FRAGMENT
)
4701 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4703 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
4704 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4705 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4706 get_nir_ssbo_intrinsic_index(bld
, instr
);
4707 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[2]);
4708 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
4710 fs_reg data
= get_nir_src(instr
->src
[0]);
4711 data
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4713 assert(nir_src_bit_size(instr
->src
[0]) <= 32);
4714 assert(nir_intrinsic_write_mask(instr
) ==
4715 (1u << instr
->num_components
) - 1);
4716 assert(nir_intrinsic_align(instr
) > 0);
4717 if (nir_src_bit_size(instr
->src
[0]) == 32 &&
4718 nir_intrinsic_align(instr
) >= 4) {
4719 assert(nir_src_num_components(instr
->src
[0]) <= 4);
4720 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
4721 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(instr
->num_components
);
4722 bld
.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL
,
4723 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4725 assert(nir_src_num_components(instr
->src
[0]) == 1);
4726 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(bit_size
);
4728 srcs
[SURFACE_LOGICAL_SRC_DATA
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4729 bld
.MOV(srcs
[SURFACE_LOGICAL_SRC_DATA
], data
);
4731 bld
.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
,
4732 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4737 case nir_intrinsic_store_output
: {
4738 assert(nir_src_bit_size(instr
->src
[0]) == 32);
4739 fs_reg src
= get_nir_src(instr
->src
[0]);
4741 unsigned store_offset
= nir_src_as_uint(instr
->src
[1]);
4742 unsigned num_components
= instr
->num_components
;
4743 unsigned first_component
= nir_intrinsic_component(instr
);
4745 fs_reg new_dest
= retype(offset(outputs
[instr
->const_index
[0]], bld
,
4746 4 * store_offset
), src
.type
);
4747 for (unsigned j
= 0; j
< num_components
; j
++) {
4748 bld
.MOV(offset(new_dest
, bld
, j
+ first_component
),
4749 offset(src
, bld
, j
));
4754 case nir_intrinsic_ssbo_atomic_add
:
4755 case nir_intrinsic_ssbo_atomic_imin
:
4756 case nir_intrinsic_ssbo_atomic_umin
:
4757 case nir_intrinsic_ssbo_atomic_imax
:
4758 case nir_intrinsic_ssbo_atomic_umax
:
4759 case nir_intrinsic_ssbo_atomic_and
:
4760 case nir_intrinsic_ssbo_atomic_or
:
4761 case nir_intrinsic_ssbo_atomic_xor
:
4762 case nir_intrinsic_ssbo_atomic_exchange
:
4763 case nir_intrinsic_ssbo_atomic_comp_swap
:
4764 nir_emit_ssbo_atomic(bld
, brw_aop_for_nir_intrinsic(instr
), instr
);
4766 case nir_intrinsic_ssbo_atomic_fmin
:
4767 case nir_intrinsic_ssbo_atomic_fmax
:
4768 case nir_intrinsic_ssbo_atomic_fcomp_swap
:
4769 nir_emit_ssbo_atomic_float(bld
, brw_aop_for_nir_intrinsic(instr
), instr
);
4772 case nir_intrinsic_get_buffer_size
: {
4773 assert(nir_src_num_components(instr
->src
[0]) == 1);
4774 unsigned ssbo_index
= nir_src_is_const(instr
->src
[0]) ?
4775 nir_src_as_uint(instr
->src
[0]) : 0;
4777 /* A resinfo's sampler message is used to get the buffer size. The
4778 * SIMD8's writeback message consists of four registers and SIMD16's
4779 * writeback message consists of 8 destination registers (two per each
4780 * component). Because we are only interested on the first channel of
4781 * the first returned component, where resinfo returns the buffer size
4782 * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
4783 * the dispatch width.
4785 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4786 fs_reg src_payload
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4787 fs_reg ret_payload
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 4);
4790 ubld
.MOV(src_payload
, brw_imm_d(0));
4792 const unsigned index
= prog_data
->binding_table
.ssbo_start
+ ssbo_index
;
4793 fs_inst
*inst
= ubld
.emit(SHADER_OPCODE_GET_BUFFER_SIZE
, ret_payload
,
4794 src_payload
, brw_imm_ud(index
));
4795 inst
->header_size
= 0;
4797 inst
->size_written
= 4 * REG_SIZE
;
4799 /* SKL PRM, vol07, 3D Media GPGPU Engine, Bounds Checking and Faulting:
4801 * "Out-of-bounds checking is always performed at a DWord granularity. If
4802 * any part of the DWord is out-of-bounds then the whole DWord is
4803 * considered out-of-bounds."
4805 * This implies that types with size smaller than 4-bytes need to be
4806 * padded if they don't complete the last dword of the buffer. But as we
4807 * need to maintain the original size we need to reverse the padding
4808 * calculation to return the correct size to know the number of elements
4809 * of an unsized array. As we stored in the last two bits of the surface
4810 * size the needed padding for the buffer, we calculate here the
4811 * original buffer_size reversing the surface_size calculation:
4813 * surface_size = isl_align(buffer_size, 4) +
4814 * (isl_align(buffer_size) - buffer_size)
4816 * buffer_size = surface_size & ~3 - surface_size & 3
4819 fs_reg size_aligned4
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4820 fs_reg size_padding
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4821 fs_reg buffer_size
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4823 ubld
.AND(size_padding
, ret_payload
, brw_imm_ud(3));
4824 ubld
.AND(size_aligned4
, ret_payload
, brw_imm_ud(~3));
4825 ubld
.ADD(buffer_size
, size_aligned4
, negate(size_padding
));
4827 bld
.MOV(retype(dest
, ret_payload
.type
), component(buffer_size
, 0));
4831 case nir_intrinsic_load_scratch
: {
4832 assert(devinfo
->gen
>= 7);
4834 assert(nir_dest_num_components(instr
->dest
) == 1);
4835 const unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
4836 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4838 if (devinfo
->gen
>= 8) {
4839 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4840 brw_imm_ud(GEN8_BTI_STATELESS_NON_COHERENT
);
4842 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(BRW_BTI_STATELESS
);
4845 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
4846 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(bit_size
);
4847 const fs_reg nir_addr
= get_nir_src(instr
->src
[0]);
4849 /* Make dest unsigned because that's what the temporary will be */
4850 dest
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4852 /* Read the vector */
4853 assert(nir_dest_num_components(instr
->dest
) == 1);
4854 assert(nir_dest_bit_size(instr
->dest
) <= 32);
4855 assert(nir_intrinsic_align(instr
) > 1);
4856 if (nir_dest_bit_size(instr
->dest
) >= 4 &&
4857 nir_intrinsic_align(instr
) >= 4) {
4858 /* The offset for a DWORD scattered message is in dwords. */
4859 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] =
4860 swizzle_nir_scratch_addr(bld
, nir_addr
, true);
4862 bld
.emit(SHADER_OPCODE_DWORD_SCATTERED_READ_LOGICAL
,
4863 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4865 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] =
4866 swizzle_nir_scratch_addr(bld
, nir_addr
, false);
4868 fs_reg read_result
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4869 bld
.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL
,
4870 read_result
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4871 bld
.MOV(dest
, read_result
);
4876 case nir_intrinsic_store_scratch
: {
4877 assert(devinfo
->gen
>= 7);
4879 assert(nir_src_num_components(instr
->src
[0]) == 1);
4880 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
4881 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
4883 if (devinfo
->gen
>= 8) {
4884 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] =
4885 brw_imm_ud(GEN8_BTI_STATELESS_NON_COHERENT
);
4887 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(BRW_BTI_STATELESS
);
4890 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
4891 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(bit_size
);
4892 const fs_reg nir_addr
= get_nir_src(instr
->src
[1]);
4894 fs_reg data
= get_nir_src(instr
->src
[0]);
4895 data
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4897 assert(nir_src_num_components(instr
->src
[0]) == 1);
4898 assert(nir_src_bit_size(instr
->src
[0]) <= 32);
4899 assert(nir_intrinsic_write_mask(instr
) == 1);
4900 assert(nir_intrinsic_align(instr
) > 1);
4901 if (nir_src_bit_size(instr
->src
[0]) == 32 &&
4902 nir_intrinsic_align(instr
) >= 4) {
4903 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
4905 /* The offset for a DWORD scattered message is in dwords. */
4906 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] =
4907 swizzle_nir_scratch_addr(bld
, nir_addr
, true);
4909 bld
.emit(SHADER_OPCODE_DWORD_SCATTERED_WRITE_LOGICAL
,
4910 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4912 srcs
[SURFACE_LOGICAL_SRC_DATA
] = bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4913 bld
.MOV(srcs
[SURFACE_LOGICAL_SRC_DATA
], data
);
4915 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] =
4916 swizzle_nir_scratch_addr(bld
, nir_addr
, false);
4918 bld
.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL
,
4919 fs_reg(), srcs
, SURFACE_LOGICAL_NUM_SRCS
);
4924 case nir_intrinsic_load_subgroup_size
:
4925 /* This should only happen for fragment shaders because every other case
4926 * is lowered in NIR so we can optimize on it.
4928 assert(stage
== MESA_SHADER_FRAGMENT
);
4929 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(dispatch_width
));
4932 case nir_intrinsic_load_subgroup_invocation
:
4933 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
4934 nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
]);
4937 case nir_intrinsic_load_subgroup_eq_mask
:
4938 case nir_intrinsic_load_subgroup_ge_mask
:
4939 case nir_intrinsic_load_subgroup_gt_mask
:
4940 case nir_intrinsic_load_subgroup_le_mask
:
4941 case nir_intrinsic_load_subgroup_lt_mask
:
4942 unreachable("not reached");
4944 case nir_intrinsic_vote_any
: {
4945 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4947 /* The any/all predicates do not consider channel enables. To prevent
4948 * dead channels from affecting the result, we initialize the flag with
4949 * with the identity value for the logical operation.
4951 if (dispatch_width
== 32) {
4952 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4953 ubld
.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD
),
4956 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0));
4958 bld
.CMP(bld
.null_reg_d(), get_nir_src(instr
->src
[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ
);
4960 /* For some reason, the any/all predicates don't work properly with
4961 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4962 * doesn't read the correct subset of the flag register and you end up
4963 * getting garbage in the second half. Work around this by using a pair
4964 * of 1-wide MOVs and scattering the result.
4966 fs_reg res1
= ubld
.vgrf(BRW_REGISTER_TYPE_D
);
4967 ubld
.MOV(res1
, brw_imm_d(0));
4968 set_predicate(dispatch_width
== 8 ? BRW_PREDICATE_ALIGN1_ANY8H
:
4969 dispatch_width
== 16 ? BRW_PREDICATE_ALIGN1_ANY16H
:
4970 BRW_PREDICATE_ALIGN1_ANY32H
,
4971 ubld
.MOV(res1
, brw_imm_d(-1)));
4973 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), component(res1
, 0));
4976 case nir_intrinsic_vote_all
: {
4977 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4979 /* The any/all predicates do not consider channel enables. To prevent
4980 * dead channels from affecting the result, we initialize the flag with
4981 * with the identity value for the logical operation.
4983 if (dispatch_width
== 32) {
4984 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4985 ubld
.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD
),
4986 brw_imm_ud(0xffffffff));
4988 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4990 bld
.CMP(bld
.null_reg_d(), get_nir_src(instr
->src
[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ
);
4992 /* For some reason, the any/all predicates don't work properly with
4993 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4994 * doesn't read the correct subset of the flag register and you end up
4995 * getting garbage in the second half. Work around this by using a pair
4996 * of 1-wide MOVs and scattering the result.
4998 fs_reg res1
= ubld
.vgrf(BRW_REGISTER_TYPE_D
);
4999 ubld
.MOV(res1
, brw_imm_d(0));
5000 set_predicate(dispatch_width
== 8 ? BRW_PREDICATE_ALIGN1_ALL8H
:
5001 dispatch_width
== 16 ? BRW_PREDICATE_ALIGN1_ALL16H
:
5002 BRW_PREDICATE_ALIGN1_ALL32H
,
5003 ubld
.MOV(res1
, brw_imm_d(-1)));
5005 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), component(res1
, 0));
5008 case nir_intrinsic_vote_feq
:
5009 case nir_intrinsic_vote_ieq
: {
5010 fs_reg value
= get_nir_src(instr
->src
[0]);
5011 if (instr
->intrinsic
== nir_intrinsic_vote_feq
) {
5012 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
5013 value
.type
= bit_size
== 8 ? BRW_REGISTER_TYPE_B
:
5014 brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_F
);
5017 fs_reg uniformized
= bld
.emit_uniformize(value
);
5018 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5020 /* The any/all predicates do not consider channel enables. To prevent
5021 * dead channels from affecting the result, we initialize the flag with
5022 * with the identity value for the logical operation.
5024 if (dispatch_width
== 32) {
5025 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
5026 ubld
.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD
),
5027 brw_imm_ud(0xffffffff));
5029 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
5031 bld
.CMP(bld
.null_reg_d(), value
, uniformized
, BRW_CONDITIONAL_Z
);
5033 /* For some reason, the any/all predicates don't work properly with
5034 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
5035 * doesn't read the correct subset of the flag register and you end up
5036 * getting garbage in the second half. Work around this by using a pair
5037 * of 1-wide MOVs and scattering the result.
5039 fs_reg res1
= ubld
.vgrf(BRW_REGISTER_TYPE_D
);
5040 ubld
.MOV(res1
, brw_imm_d(0));
5041 set_predicate(dispatch_width
== 8 ? BRW_PREDICATE_ALIGN1_ALL8H
:
5042 dispatch_width
== 16 ? BRW_PREDICATE_ALIGN1_ALL16H
:
5043 BRW_PREDICATE_ALIGN1_ALL32H
,
5044 ubld
.MOV(res1
, brw_imm_d(-1)));
5046 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), component(res1
, 0));
5050 case nir_intrinsic_ballot
: {
5051 const fs_reg value
= retype(get_nir_src(instr
->src
[0]),
5052 BRW_REGISTER_TYPE_UD
);
5053 struct brw_reg flag
= brw_flag_reg(0, 0);
5054 /* FIXME: For SIMD32 programs, this causes us to stomp on f0.1 as well
5055 * as f0.0. This is a problem for fragment programs as we currently use
5056 * f0.1 for discards. Fortunately, we don't support SIMD32 fragment
5057 * programs yet so this isn't a problem. When we do, something will
5060 if (dispatch_width
== 32)
5061 flag
.type
= BRW_REGISTER_TYPE_UD
;
5063 bld
.exec_all().group(1, 0).MOV(flag
, brw_imm_ud(0u));
5064 bld
.CMP(bld
.null_reg_ud(), value
, brw_imm_ud(0u), BRW_CONDITIONAL_NZ
);
5066 if (instr
->dest
.ssa
.bit_size
> 32) {
5067 dest
.type
= BRW_REGISTER_TYPE_UQ
;
5069 dest
.type
= BRW_REGISTER_TYPE_UD
;
5071 bld
.MOV(dest
, flag
);
5075 case nir_intrinsic_read_invocation
: {
5076 const fs_reg value
= get_nir_src(instr
->src
[0]);
5077 const fs_reg invocation
= get_nir_src(instr
->src
[1]);
5078 fs_reg tmp
= bld
.vgrf(value
.type
);
5080 bld
.exec_all().emit(SHADER_OPCODE_BROADCAST
, tmp
, value
,
5081 bld
.emit_uniformize(invocation
));
5083 bld
.MOV(retype(dest
, value
.type
), fs_reg(component(tmp
, 0)));
5087 case nir_intrinsic_read_first_invocation
: {
5088 const fs_reg value
= get_nir_src(instr
->src
[0]);
5089 bld
.MOV(retype(dest
, value
.type
), bld
.emit_uniformize(value
));
5093 case nir_intrinsic_shuffle
: {
5094 const fs_reg value
= get_nir_src(instr
->src
[0]);
5095 const fs_reg index
= get_nir_src(instr
->src
[1]);
5097 bld
.emit(SHADER_OPCODE_SHUFFLE
, retype(dest
, value
.type
), value
, index
);
5101 case nir_intrinsic_first_invocation
: {
5102 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
5103 bld
.exec_all().emit(SHADER_OPCODE_FIND_LIVE_CHANNEL
, tmp
);
5104 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
5105 fs_reg(component(tmp
, 0)));
5109 case nir_intrinsic_quad_broadcast
: {
5110 const fs_reg value
= get_nir_src(instr
->src
[0]);
5111 const unsigned index
= nir_src_as_uint(instr
->src
[1]);
5113 bld
.emit(SHADER_OPCODE_CLUSTER_BROADCAST
, retype(dest
, value
.type
),
5114 value
, brw_imm_ud(index
), brw_imm_ud(4));
5118 case nir_intrinsic_quad_swap_horizontal
: {
5119 const fs_reg value
= get_nir_src(instr
->src
[0]);
5120 const fs_reg tmp
= bld
.vgrf(value
.type
);
5121 if (devinfo
->gen
<= 7) {
5122 /* The hardware doesn't seem to support these crazy regions with
5123 * compressed instructions on gen7 and earlier so we fall back to
5124 * using quad swizzles. Fortunately, we don't support 64-bit
5125 * anything in Vulkan on gen7.
5127 assert(nir_src_bit_size(instr
->src
[0]) == 32);
5128 const fs_builder ubld
= bld
.exec_all();
5129 ubld
.emit(SHADER_OPCODE_QUAD_SWIZZLE
, tmp
, value
,
5130 brw_imm_ud(BRW_SWIZZLE4(1,0,3,2)));
5131 bld
.MOV(retype(dest
, value
.type
), tmp
);
5133 const fs_builder ubld
= bld
.exec_all().group(dispatch_width
/ 2, 0);
5135 const fs_reg src_left
= horiz_stride(value
, 2);
5136 const fs_reg src_right
= horiz_stride(horiz_offset(value
, 1), 2);
5137 const fs_reg tmp_left
= horiz_stride(tmp
, 2);
5138 const fs_reg tmp_right
= horiz_stride(horiz_offset(tmp
, 1), 2);
5140 ubld
.MOV(tmp_left
, src_right
);
5141 ubld
.MOV(tmp_right
, src_left
);
5144 bld
.MOV(retype(dest
, value
.type
), tmp
);
5148 case nir_intrinsic_quad_swap_vertical
: {
5149 const fs_reg value
= get_nir_src(instr
->src
[0]);
5150 if (nir_src_bit_size(instr
->src
[0]) == 32) {
5151 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
5152 const fs_reg tmp
= bld
.vgrf(value
.type
);
5153 const fs_builder ubld
= bld
.exec_all();
5154 ubld
.emit(SHADER_OPCODE_QUAD_SWIZZLE
, tmp
, value
,
5155 brw_imm_ud(BRW_SWIZZLE4(2,3,0,1)));
5156 bld
.MOV(retype(dest
, value
.type
), tmp
);
5158 /* For larger data types, we have to either emit dispatch_width many
5159 * MOVs or else fall back to doing indirects.
5161 fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
5162 bld
.XOR(idx
, nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
],
5164 bld
.emit(SHADER_OPCODE_SHUFFLE
, retype(dest
, value
.type
), value
, idx
);
5169 case nir_intrinsic_quad_swap_diagonal
: {
5170 const fs_reg value
= get_nir_src(instr
->src
[0]);
5171 if (nir_src_bit_size(instr
->src
[0]) == 32) {
5172 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
5173 const fs_reg tmp
= bld
.vgrf(value
.type
);
5174 const fs_builder ubld
= bld
.exec_all();
5175 ubld
.emit(SHADER_OPCODE_QUAD_SWIZZLE
, tmp
, value
,
5176 brw_imm_ud(BRW_SWIZZLE4(3,2,1,0)));
5177 bld
.MOV(retype(dest
, value
.type
), tmp
);
5179 /* For larger data types, we have to either emit dispatch_width many
5180 * MOVs or else fall back to doing indirects.
5182 fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
5183 bld
.XOR(idx
, nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
],
5185 bld
.emit(SHADER_OPCODE_SHUFFLE
, retype(dest
, value
.type
), value
, idx
);
5190 case nir_intrinsic_reduce
: {
5191 fs_reg src
= get_nir_src(instr
->src
[0]);
5192 nir_op redop
= (nir_op
)nir_intrinsic_reduction_op(instr
);
5193 unsigned cluster_size
= nir_intrinsic_cluster_size(instr
);
5194 if (cluster_size
== 0 || cluster_size
> dispatch_width
)
5195 cluster_size
= dispatch_width
;
5197 /* Figure out the source type */
5198 src
.type
= brw_type_for_nir_type(devinfo
,
5199 (nir_alu_type
)(nir_op_infos
[redop
].input_types
[0] |
5200 nir_src_bit_size(instr
->src
[0])));
5202 fs_reg identity
= brw_nir_reduction_op_identity(bld
, redop
, src
.type
);
5203 opcode brw_op
= brw_op_for_nir_reduction_op(redop
);
5204 brw_conditional_mod cond_mod
= brw_cond_mod_for_nir_reduction_op(redop
);
5206 /* There are a couple of register region issues that make things
5207 * complicated for 8-bit types:
5209 * 1. Only raw moves are allowed to write to a packed 8-bit
5211 * 2. If we use a strided destination, the efficient way to do scan
5212 * operations ends up using strides that are too big to encode in
5215 * To get around these issues, we just do all 8-bit scan operations in
5216 * 16 bits. It's actually fewer instructions than what we'd have to do
5217 * if we were trying to do it in native 8-bit types and the results are
5218 * the same once we truncate to 8 bits at the end.
5220 brw_reg_type scan_type
= src
.type
;
5221 if (type_sz(scan_type
) == 1)
5222 scan_type
= brw_reg_type_from_bit_size(16, src
.type
);
5224 /* Set up a register for all of our scratching around and initialize it
5225 * to reduction operation's identity value.
5227 fs_reg scan
= bld
.vgrf(scan_type
);
5228 bld
.exec_all().emit(SHADER_OPCODE_SEL_EXEC
, scan
, src
, identity
);
5230 bld
.emit_scan(brw_op
, scan
, cluster_size
, cond_mod
);
5232 dest
.type
= src
.type
;
5233 if (cluster_size
* type_sz(src
.type
) >= REG_SIZE
* 2) {
5234 /* In this case, CLUSTER_BROADCAST instruction isn't needed because
5235 * the distance between clusters is at least 2 GRFs. In this case,
5236 * we don't need the weird striding of the CLUSTER_BROADCAST
5237 * instruction and can just do regular MOVs.
5239 assert((cluster_size
* type_sz(src
.type
)) % (REG_SIZE
* 2) == 0);
5240 const unsigned groups
=
5241 (dispatch_width
* type_sz(src
.type
)) / (REG_SIZE
* 2);
5242 const unsigned group_size
= dispatch_width
/ groups
;
5243 for (unsigned i
= 0; i
< groups
; i
++) {
5244 const unsigned cluster
= (i
* group_size
) / cluster_size
;
5245 const unsigned comp
= cluster
* cluster_size
+ (cluster_size
- 1);
5246 bld
.group(group_size
, i
).MOV(horiz_offset(dest
, i
* group_size
),
5247 component(scan
, comp
));
5250 bld
.emit(SHADER_OPCODE_CLUSTER_BROADCAST
, dest
, scan
,
5251 brw_imm_ud(cluster_size
- 1), brw_imm_ud(cluster_size
));
5256 case nir_intrinsic_inclusive_scan
:
5257 case nir_intrinsic_exclusive_scan
: {
5258 fs_reg src
= get_nir_src(instr
->src
[0]);
5259 nir_op redop
= (nir_op
)nir_intrinsic_reduction_op(instr
);
5261 /* Figure out the source type */
5262 src
.type
= brw_type_for_nir_type(devinfo
,
5263 (nir_alu_type
)(nir_op_infos
[redop
].input_types
[0] |
5264 nir_src_bit_size(instr
->src
[0])));
5266 fs_reg identity
= brw_nir_reduction_op_identity(bld
, redop
, src
.type
);
5267 opcode brw_op
= brw_op_for_nir_reduction_op(redop
);
5268 brw_conditional_mod cond_mod
= brw_cond_mod_for_nir_reduction_op(redop
);
5270 /* There are a couple of register region issues that make things
5271 * complicated for 8-bit types:
5273 * 1. Only raw moves are allowed to write to a packed 8-bit
5275 * 2. If we use a strided destination, the efficient way to do scan
5276 * operations ends up using strides that are too big to encode in
5279 * To get around these issues, we just do all 8-bit scan operations in
5280 * 16 bits. It's actually fewer instructions than what we'd have to do
5281 * if we were trying to do it in native 8-bit types and the results are
5282 * the same once we truncate to 8 bits at the end.
5284 brw_reg_type scan_type
= src
.type
;
5285 if (type_sz(scan_type
) == 1)
5286 scan_type
= brw_reg_type_from_bit_size(16, src
.type
);
5288 /* Set up a register for all of our scratching around and initialize it
5289 * to reduction operation's identity value.
5291 fs_reg scan
= bld
.vgrf(scan_type
);
5292 const fs_builder allbld
= bld
.exec_all();
5293 allbld
.emit(SHADER_OPCODE_SEL_EXEC
, scan
, src
, identity
);
5295 if (instr
->intrinsic
== nir_intrinsic_exclusive_scan
) {
5296 /* Exclusive scan is a bit harder because we have to do an annoying
5297 * shift of the contents before we can begin. To make things worse,
5298 * we can't do this with a normal stride; we have to use indirects.
5300 fs_reg shifted
= bld
.vgrf(scan_type
);
5301 fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
5302 allbld
.ADD(idx
, nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
],
5304 allbld
.emit(SHADER_OPCODE_SHUFFLE
, shifted
, scan
, idx
);
5305 allbld
.group(1, 0).MOV(component(shifted
, 0), identity
);
5309 bld
.emit_scan(brw_op
, scan
, dispatch_width
, cond_mod
);
5311 bld
.MOV(retype(dest
, src
.type
), scan
);
5315 case nir_intrinsic_begin_invocation_interlock
: {
5316 const fs_builder ubld
= bld
.group(8, 0);
5317 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
5319 ubld
.emit(SHADER_OPCODE_INTERLOCK
, tmp
, brw_vec8_grf(0, 0))
5320 ->size_written
= 2 * REG_SIZE
;
5324 case nir_intrinsic_end_invocation_interlock
: {
5325 /* For endInvocationInterlock(), we need to insert a memory fence which
5326 * stalls in the shader until the memory transactions prior to that
5327 * fence are complete. This ensures that the shader does not end before
5328 * any writes from its critical section have landed. Otherwise, you can
5329 * end up with a case where the next invocation on that pixel properly
5330 * stalls for previous FS invocation on its pixel to complete but
5331 * doesn't actually wait for the dataport memory transactions from that
5332 * thread to land before submitting its own.
5334 const fs_builder ubld
= bld
.group(8, 0);
5335 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
5336 ubld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
,
5337 brw_vec8_grf(0, 0), brw_imm_ud(1), brw_imm_ud(0))
5338 ->size_written
= 2 * REG_SIZE
;
5343 unreachable("unknown intrinsic");
5348 fs_visitor::nir_emit_ssbo_atomic(const fs_builder
&bld
,
5349 int op
, nir_intrinsic_instr
*instr
)
5351 if (stage
== MESA_SHADER_FRAGMENT
)
5352 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
5354 /* The BTI untyped atomic messages only support 32-bit atomics. If you
5355 * just look at the big table of messages in the Vol 7 of the SKL PRM, they
5356 * appear to exist. However, if you look at Vol 2a, there are no message
5357 * descriptors provided for Qword atomic ops except for A64 messages.
5359 assert(nir_dest_bit_size(instr
->dest
) == 32);
5362 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
5363 dest
= get_nir_dest(instr
->dest
);
5365 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
5366 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = get_nir_ssbo_intrinsic_index(bld
, instr
);
5367 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
5368 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
5369 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(op
);
5372 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
5373 data
= get_nir_src(instr
->src
[2]);
5375 if (op
== BRW_AOP_CMPWR
) {
5376 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
5377 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[3]) };
5378 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
5381 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
5383 /* Emit the actual atomic operation */
5385 bld
.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
,
5386 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
5390 fs_visitor::nir_emit_ssbo_atomic_float(const fs_builder
&bld
,
5391 int op
, nir_intrinsic_instr
*instr
)
5393 if (stage
== MESA_SHADER_FRAGMENT
)
5394 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
5397 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
5398 dest
= get_nir_dest(instr
->dest
);
5400 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
5401 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = get_nir_ssbo_intrinsic_index(bld
, instr
);
5402 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = get_nir_src(instr
->src
[1]);
5403 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
5404 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(op
);
5406 fs_reg data
= get_nir_src(instr
->src
[2]);
5407 if (op
== BRW_AOP_FCMPWR
) {
5408 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
5409 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[3]) };
5410 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
5413 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
5415 /* Emit the actual atomic operation */
5417 bld
.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
,
5418 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
5422 fs_visitor::nir_emit_shared_atomic(const fs_builder
&bld
,
5423 int op
, nir_intrinsic_instr
*instr
)
5426 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
5427 dest
= get_nir_dest(instr
->dest
);
5429 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
5430 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(GEN7_BTI_SLM
);
5431 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
5432 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(op
);
5435 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
5436 data
= get_nir_src(instr
->src
[1]);
5437 if (op
== BRW_AOP_CMPWR
) {
5438 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
5439 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[2]) };
5440 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
5443 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
5445 /* Get the offset */
5446 if (nir_src_is_const(instr
->src
[0])) {
5447 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] =
5448 brw_imm_ud(instr
->const_index
[0] + nir_src_as_uint(instr
->src
[0]));
5450 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = vgrf(glsl_type::uint_type
);
5451 bld
.ADD(srcs
[SURFACE_LOGICAL_SRC_ADDRESS
],
5452 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
5453 brw_imm_ud(instr
->const_index
[0]));
5456 /* Emit the actual atomic operation operation */
5458 bld
.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL
,
5459 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
5463 fs_visitor::nir_emit_shared_atomic_float(const fs_builder
&bld
,
5464 int op
, nir_intrinsic_instr
*instr
)
5467 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
5468 dest
= get_nir_dest(instr
->dest
);
5470 fs_reg srcs
[SURFACE_LOGICAL_NUM_SRCS
];
5471 srcs
[SURFACE_LOGICAL_SRC_SURFACE
] = brw_imm_ud(GEN7_BTI_SLM
);
5472 srcs
[SURFACE_LOGICAL_SRC_IMM_DIMS
] = brw_imm_ud(1);
5473 srcs
[SURFACE_LOGICAL_SRC_IMM_ARG
] = brw_imm_ud(op
);
5475 fs_reg data
= get_nir_src(instr
->src
[1]);
5476 if (op
== BRW_AOP_FCMPWR
) {
5477 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
5478 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[2]) };
5479 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
5482 srcs
[SURFACE_LOGICAL_SRC_DATA
] = data
;
5484 /* Get the offset */
5485 if (nir_src_is_const(instr
->src
[0])) {
5486 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] =
5487 brw_imm_ud(instr
->const_index
[0] + nir_src_as_uint(instr
->src
[0]));
5489 srcs
[SURFACE_LOGICAL_SRC_ADDRESS
] = vgrf(glsl_type::uint_type
);
5490 bld
.ADD(srcs
[SURFACE_LOGICAL_SRC_ADDRESS
],
5491 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
5492 brw_imm_ud(instr
->const_index
[0]));
5495 /* Emit the actual atomic operation operation */
5497 bld
.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL
,
5498 dest
, srcs
, SURFACE_LOGICAL_NUM_SRCS
);
5502 fs_visitor::nir_emit_global_atomic(const fs_builder
&bld
,
5503 int op
, nir_intrinsic_instr
*instr
)
5505 if (stage
== MESA_SHADER_FRAGMENT
)
5506 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
5509 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
5510 dest
= get_nir_dest(instr
->dest
);
5512 fs_reg addr
= get_nir_src(instr
->src
[0]);
5515 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
5516 data
= get_nir_src(instr
->src
[1]);
5518 if (op
== BRW_AOP_CMPWR
) {
5519 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
5520 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[2]) };
5521 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
5525 if (nir_dest_bit_size(instr
->dest
) == 64) {
5526 bld
.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL
,
5527 dest
, addr
, data
, brw_imm_ud(op
));
5529 assert(nir_dest_bit_size(instr
->dest
) == 32);
5530 bld
.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
,
5531 dest
, addr
, data
, brw_imm_ud(op
));
5536 fs_visitor::nir_emit_global_atomic_float(const fs_builder
&bld
,
5537 int op
, nir_intrinsic_instr
*instr
)
5539 if (stage
== MESA_SHADER_FRAGMENT
)
5540 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
5542 assert(nir_intrinsic_infos
[instr
->intrinsic
].has_dest
);
5543 fs_reg dest
= get_nir_dest(instr
->dest
);
5545 fs_reg addr
= get_nir_src(instr
->src
[0]);
5547 assert(op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
);
5548 fs_reg data
= get_nir_src(instr
->src
[1]);
5550 if (op
== BRW_AOP_FCMPWR
) {
5551 fs_reg tmp
= bld
.vgrf(data
.type
, 2);
5552 fs_reg sources
[2] = { data
, get_nir_src(instr
->src
[2]) };
5553 bld
.LOAD_PAYLOAD(tmp
, sources
, 2, 0);
5557 bld
.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL
,
5558 dest
, addr
, data
, brw_imm_ud(op
));
5562 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
5564 unsigned texture
= instr
->texture_index
;
5565 unsigned sampler
= instr
->sampler_index
;
5567 fs_reg srcs
[TEX_LOGICAL_NUM_SRCS
];
5569 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(texture
);
5570 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = brw_imm_ud(sampler
);
5572 int lod_components
= 0;
5574 /* The hardware requires a LOD for buffer textures */
5575 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
5576 srcs
[TEX_LOGICAL_SRC_LOD
] = brw_imm_d(0);
5578 uint32_t header_bits
= 0;
5579 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
5580 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
5581 switch (instr
->src
[i
].src_type
) {
5582 case nir_tex_src_bias
:
5583 srcs
[TEX_LOGICAL_SRC_LOD
] =
5584 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
5586 case nir_tex_src_comparator
:
5587 srcs
[TEX_LOGICAL_SRC_SHADOW_C
] = retype(src
, BRW_REGISTER_TYPE_F
);
5589 case nir_tex_src_coord
:
5590 switch (instr
->op
) {
5592 case nir_texop_txf_ms
:
5593 case nir_texop_txf_ms_mcs
:
5594 case nir_texop_samples_identical
:
5595 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = retype(src
, BRW_REGISTER_TYPE_D
);
5598 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = retype(src
, BRW_REGISTER_TYPE_F
);
5602 case nir_tex_src_ddx
:
5603 srcs
[TEX_LOGICAL_SRC_LOD
] = retype(src
, BRW_REGISTER_TYPE_F
);
5604 lod_components
= nir_tex_instr_src_size(instr
, i
);
5606 case nir_tex_src_ddy
:
5607 srcs
[TEX_LOGICAL_SRC_LOD2
] = retype(src
, BRW_REGISTER_TYPE_F
);
5609 case nir_tex_src_lod
:
5610 switch (instr
->op
) {
5612 srcs
[TEX_LOGICAL_SRC_LOD
] =
5613 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_UD
);
5616 srcs
[TEX_LOGICAL_SRC_LOD
] =
5617 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_D
);
5620 srcs
[TEX_LOGICAL_SRC_LOD
] =
5621 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
5625 case nir_tex_src_min_lod
:
5626 srcs
[TEX_LOGICAL_SRC_MIN_LOD
] =
5627 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
5629 case nir_tex_src_ms_index
:
5630 srcs
[TEX_LOGICAL_SRC_SAMPLE_INDEX
] = retype(src
, BRW_REGISTER_TYPE_UD
);
5633 case nir_tex_src_offset
: {
5634 uint32_t offset_bits
= 0;
5635 if (brw_texture_offset(instr
, i
, &offset_bits
)) {
5636 header_bits
|= offset_bits
;
5638 srcs
[TEX_LOGICAL_SRC_TG4_OFFSET
] =
5639 retype(src
, BRW_REGISTER_TYPE_D
);
5644 case nir_tex_src_projector
:
5645 unreachable("should be lowered");
5647 case nir_tex_src_texture_offset
: {
5648 /* Emit code to evaluate the actual indexing expression */
5649 fs_reg tmp
= vgrf(glsl_type::uint_type
);
5650 bld
.ADD(tmp
, src
, brw_imm_ud(texture
));
5651 srcs
[TEX_LOGICAL_SRC_SURFACE
] = bld
.emit_uniformize(tmp
);
5655 case nir_tex_src_sampler_offset
: {
5656 /* Emit code to evaluate the actual indexing expression */
5657 fs_reg tmp
= vgrf(glsl_type::uint_type
);
5658 bld
.ADD(tmp
, src
, brw_imm_ud(sampler
));
5659 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = bld
.emit_uniformize(tmp
);
5663 case nir_tex_src_texture_handle
:
5664 assert(nir_tex_instr_src_index(instr
, nir_tex_src_texture_offset
) == -1);
5665 srcs
[TEX_LOGICAL_SRC_SURFACE
] = fs_reg();
5666 srcs
[TEX_LOGICAL_SRC_SURFACE_HANDLE
] = bld
.emit_uniformize(src
);
5669 case nir_tex_src_sampler_handle
:
5670 assert(nir_tex_instr_src_index(instr
, nir_tex_src_sampler_offset
) == -1);
5671 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = fs_reg();
5672 srcs
[TEX_LOGICAL_SRC_SAMPLER_HANDLE
] = bld
.emit_uniformize(src
);
5675 case nir_tex_src_ms_mcs
:
5676 assert(instr
->op
== nir_texop_txf_ms
);
5677 srcs
[TEX_LOGICAL_SRC_MCS
] = retype(src
, BRW_REGISTER_TYPE_D
);
5680 case nir_tex_src_plane
: {
5681 const uint32_t plane
= nir_src_as_uint(instr
->src
[i
].src
);
5682 const uint32_t texture_index
=
5683 instr
->texture_index
+
5684 stage_prog_data
->binding_table
.plane_start
[plane
] -
5685 stage_prog_data
->binding_table
.texture_start
;
5687 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(texture_index
);
5692 unreachable("unknown texture source");
5696 if (srcs
[TEX_LOGICAL_SRC_MCS
].file
== BAD_FILE
&&
5697 (instr
->op
== nir_texop_txf_ms
||
5698 instr
->op
== nir_texop_samples_identical
)) {
5699 if (devinfo
->gen
>= 7 &&
5700 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
5701 srcs
[TEX_LOGICAL_SRC_MCS
] =
5702 emit_mcs_fetch(srcs
[TEX_LOGICAL_SRC_COORDINATE
],
5703 instr
->coord_components
,
5704 srcs
[TEX_LOGICAL_SRC_SURFACE
],
5705 srcs
[TEX_LOGICAL_SRC_SURFACE_HANDLE
]);
5707 srcs
[TEX_LOGICAL_SRC_MCS
] = brw_imm_ud(0u);
5711 srcs
[TEX_LOGICAL_SRC_COORD_COMPONENTS
] = brw_imm_d(instr
->coord_components
);
5712 srcs
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
] = brw_imm_d(lod_components
);
5715 switch (instr
->op
) {
5717 opcode
= SHADER_OPCODE_TEX_LOGICAL
;
5720 opcode
= FS_OPCODE_TXB_LOGICAL
;
5723 opcode
= SHADER_OPCODE_TXL_LOGICAL
;
5726 opcode
= SHADER_OPCODE_TXD_LOGICAL
;
5729 opcode
= SHADER_OPCODE_TXF_LOGICAL
;
5731 case nir_texop_txf_ms
:
5732 if ((key_tex
->msaa_16
& (1 << sampler
)))
5733 opcode
= SHADER_OPCODE_TXF_CMS_W_LOGICAL
;
5735 opcode
= SHADER_OPCODE_TXF_CMS_LOGICAL
;
5737 case nir_texop_txf_ms_mcs
:
5738 opcode
= SHADER_OPCODE_TXF_MCS_LOGICAL
;
5740 case nir_texop_query_levels
:
5742 opcode
= SHADER_OPCODE_TXS_LOGICAL
;
5745 opcode
= SHADER_OPCODE_LOD_LOGICAL
;
5748 if (srcs
[TEX_LOGICAL_SRC_TG4_OFFSET
].file
!= BAD_FILE
)
5749 opcode
= SHADER_OPCODE_TG4_OFFSET_LOGICAL
;
5751 opcode
= SHADER_OPCODE_TG4_LOGICAL
;
5753 case nir_texop_texture_samples
:
5754 opcode
= SHADER_OPCODE_SAMPLEINFO_LOGICAL
;
5756 case nir_texop_samples_identical
: {
5757 fs_reg dst
= retype(get_nir_dest(instr
->dest
), BRW_REGISTER_TYPE_D
);
5759 /* If mcs is an immediate value, it means there is no MCS. In that case
5760 * just return false.
5762 if (srcs
[TEX_LOGICAL_SRC_MCS
].file
== BRW_IMMEDIATE_VALUE
) {
5763 bld
.MOV(dst
, brw_imm_ud(0u));
5764 } else if ((key_tex
->msaa_16
& (1 << sampler
))) {
5765 fs_reg tmp
= vgrf(glsl_type::uint_type
);
5766 bld
.OR(tmp
, srcs
[TEX_LOGICAL_SRC_MCS
],
5767 offset(srcs
[TEX_LOGICAL_SRC_MCS
], bld
, 1));
5768 bld
.CMP(dst
, tmp
, brw_imm_ud(0u), BRW_CONDITIONAL_EQ
);
5770 bld
.CMP(dst
, srcs
[TEX_LOGICAL_SRC_MCS
], brw_imm_ud(0u),
5771 BRW_CONDITIONAL_EQ
);
5776 unreachable("unknown texture opcode");
5779 if (instr
->op
== nir_texop_tg4
) {
5780 if (instr
->component
== 1 &&
5781 key_tex
->gather_channel_quirk_mask
& (1 << texture
)) {
5782 /* gather4 sampler is broken for green channel on RG32F --
5783 * we must ask for blue instead.
5785 header_bits
|= 2 << 16;
5787 header_bits
|= instr
->component
<< 16;
5791 fs_reg dst
= bld
.vgrf(brw_type_for_nir_type(devinfo
, instr
->dest_type
), 4);
5792 fs_inst
*inst
= bld
.emit(opcode
, dst
, srcs
, ARRAY_SIZE(srcs
));
5793 inst
->offset
= header_bits
;
5795 const unsigned dest_size
= nir_tex_instr_dest_size(instr
);
5796 if (devinfo
->gen
>= 9 &&
5797 instr
->op
!= nir_texop_tg4
&& instr
->op
!= nir_texop_query_levels
) {
5798 unsigned write_mask
= instr
->dest
.is_ssa
?
5799 nir_ssa_def_components_read(&instr
->dest
.ssa
):
5800 (1 << dest_size
) - 1;
5801 assert(write_mask
!= 0); /* dead code should have been eliminated */
5802 inst
->size_written
= util_last_bit(write_mask
) *
5803 inst
->dst
.component_size(inst
->exec_size
);
5805 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
5808 if (srcs
[TEX_LOGICAL_SRC_SHADOW_C
].file
!= BAD_FILE
)
5809 inst
->shadow_compare
= true;
5811 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
== 6)
5812 emit_gen6_gather_wa(key_tex
->gen6_gather_wa
[texture
], dst
);
5815 for (unsigned i
= 0; i
< dest_size
; i
++)
5816 nir_dest
[i
] = offset(dst
, bld
, i
);
5818 if (instr
->op
== nir_texop_query_levels
) {
5819 /* # levels is in .w */
5820 nir_dest
[0] = offset(dst
, bld
, 3);
5821 } else if (instr
->op
== nir_texop_txs
&&
5822 dest_size
>= 3 && devinfo
->gen
< 7) {
5823 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
5824 fs_reg depth
= offset(dst
, bld
, 2);
5825 nir_dest
[2] = vgrf(glsl_type::int_type
);
5826 bld
.emit_minmax(nir_dest
[2], depth
, brw_imm_d(1), BRW_CONDITIONAL_GE
);
5829 bld
.LOAD_PAYLOAD(get_nir_dest(instr
->dest
), nir_dest
, dest_size
, 0);
5833 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
5835 switch (instr
->type
) {
5836 case nir_jump_break
:
5837 bld
.emit(BRW_OPCODE_BREAK
);
5839 case nir_jump_continue
:
5840 bld
.emit(BRW_OPCODE_CONTINUE
);
5842 case nir_jump_return
:
5844 unreachable("unknown jump");
5849 * This helper takes a source register and un/shuffles it into the destination
5852 * If source type size is smaller than destination type size the operation
5853 * needed is a component shuffle. The opposite case would be an unshuffle. If
5854 * source/destination type size is equal a shuffle is done that would be
5855 * equivalent to a simple MOV.
5857 * For example, if source is a 16-bit type and destination is 32-bit. A 3
5858 * components .xyz 16-bit vector on SIMD8 would be.
5860 * |x1|x2|x3|x4|x5|x6|x7|x8|y1|y2|y3|y4|y5|y6|y7|y8|
5861 * |z1|z2|z3|z4|z5|z6|z7|z8| | | | | | | | |
5863 * This helper will return the following 2 32-bit components with the 16-bit
5866 * |x1 y1|x2 y2|x3 y3|x4 y4|x5 y5|x6 y6|x7 y7|x8 y8|
5867 * |z1 |z2 |z3 |z4 |z5 |z6 |z7 |z8 |
5869 * For unshuffle, the example would be the opposite, a 64-bit type source
5870 * and a 32-bit destination. A 2 component .xy 64-bit vector on SIMD8
5873 * | x1l x1h | x2l x2h | x3l x3h | x4l x4h |
5874 * | x5l x5h | x6l x6h | x7l x7h | x8l x8h |
5875 * | y1l y1h | y2l y2h | y3l y3h | y4l y4h |
5876 * | y5l y5h | y6l y6h | y7l y7h | y8l y8h |
5878 * The returned result would be the following 4 32-bit components unshuffled:
5880 * | x1l | x2l | x3l | x4l | x5l | x6l | x7l | x8l |
5881 * | x1h | x2h | x3h | x4h | x5h | x6h | x7h | x8h |
5882 * | y1l | y2l | y3l | y4l | y5l | y6l | y7l | y8l |
5883 * | y1h | y2h | y3h | y4h | y5h | y6h | y7h | y8h |
5885 * - Source and destination register must not be overlapped.
5886 * - components units are measured in terms of the smaller type between
5887 * source and destination because we are un/shuffling the smaller
5888 * components from/into the bigger ones.
5889 * - first_component parameter allows skipping source components.
5892 shuffle_src_to_dst(const fs_builder
&bld
,
5895 uint32_t first_component
,
5896 uint32_t components
)
5898 if (type_sz(src
.type
) == type_sz(dst
.type
)) {
5899 assert(!regions_overlap(dst
,
5900 type_sz(dst
.type
) * bld
.dispatch_width() * components
,
5901 offset(src
, bld
, first_component
),
5902 type_sz(src
.type
) * bld
.dispatch_width() * components
));
5903 for (unsigned i
= 0; i
< components
; i
++) {
5904 bld
.MOV(retype(offset(dst
, bld
, i
), src
.type
),
5905 offset(src
, bld
, i
+ first_component
));
5907 } else if (type_sz(src
.type
) < type_sz(dst
.type
)) {
5908 /* Source is shuffled into destination */
5909 unsigned size_ratio
= type_sz(dst
.type
) / type_sz(src
.type
);
5910 assert(!regions_overlap(dst
,
5911 type_sz(dst
.type
) * bld
.dispatch_width() *
5912 DIV_ROUND_UP(components
, size_ratio
),
5913 offset(src
, bld
, first_component
),
5914 type_sz(src
.type
) * bld
.dispatch_width() * components
));
5916 brw_reg_type shuffle_type
=
5917 brw_reg_type_from_bit_size(8 * type_sz(src
.type
),
5918 BRW_REGISTER_TYPE_D
);
5919 for (unsigned i
= 0; i
< components
; i
++) {
5920 fs_reg shuffle_component_i
=
5921 subscript(offset(dst
, bld
, i
/ size_ratio
),
5922 shuffle_type
, i
% size_ratio
);
5923 bld
.MOV(shuffle_component_i
,
5924 retype(offset(src
, bld
, i
+ first_component
), shuffle_type
));
5927 /* Source is unshuffled into destination */
5928 unsigned size_ratio
= type_sz(src
.type
) / type_sz(dst
.type
);
5929 assert(!regions_overlap(dst
,
5930 type_sz(dst
.type
) * bld
.dispatch_width() * components
,
5931 offset(src
, bld
, first_component
/ size_ratio
),
5932 type_sz(src
.type
) * bld
.dispatch_width() *
5933 DIV_ROUND_UP(components
+ (first_component
% size_ratio
),
5936 brw_reg_type shuffle_type
=
5937 brw_reg_type_from_bit_size(8 * type_sz(dst
.type
),
5938 BRW_REGISTER_TYPE_D
);
5939 for (unsigned i
= 0; i
< components
; i
++) {
5940 fs_reg shuffle_component_i
=
5941 subscript(offset(src
, bld
, (first_component
+ i
) / size_ratio
),
5942 shuffle_type
, (first_component
+ i
) % size_ratio
);
5943 bld
.MOV(retype(offset(dst
, bld
, i
), shuffle_type
),
5944 shuffle_component_i
);
5950 shuffle_from_32bit_read(const fs_builder
&bld
,
5953 uint32_t first_component
,
5954 uint32_t components
)
5956 assert(type_sz(src
.type
) == 4);
5958 /* This function takes components in units of the destination type while
5959 * shuffle_src_to_dst takes components in units of the smallest type
5961 if (type_sz(dst
.type
) > 4) {
5962 assert(type_sz(dst
.type
) == 8);
5963 first_component
*= 2;
5967 shuffle_src_to_dst(bld
, dst
, src
, first_component
, components
);
5971 setup_imm_df(const fs_builder
&bld
, double v
)
5973 const struct gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5974 assert(devinfo
->gen
>= 7);
5976 if (devinfo
->gen
>= 8)
5977 return brw_imm_df(v
);
5979 /* gen7.5 does not support DF immediates straighforward but the DIM
5980 * instruction allows to set the 64-bit immediate value.
5982 if (devinfo
->is_haswell
) {
5983 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5984 fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_DF
, 1);
5985 ubld
.DIM(dst
, brw_imm_df(v
));
5986 return component(dst
, 0);
5989 /* gen7 does not support DF immediates, so we generate a 64-bit constant by
5990 * writing the low 32-bit of the constant to suboffset 0 of a VGRF and
5991 * the high 32-bit to suboffset 4 and then applying a stride of 0.
5993 * Alternatively, we could also produce a normal VGRF (without stride 0)
5994 * by writing to all the channels in the VGRF, however, that would hit the
5995 * gen7 bug where we have to split writes that span more than 1 register
5996 * into instructions with a width of 4 (otherwise the write to the second
5997 * register written runs into an execmask hardware bug) which isn't very
6010 const fs_builder ubld
= bld
.exec_all().group(1, 0);
6011 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
6012 ubld
.MOV(tmp
, brw_imm_ud(di
.i1
));
6013 ubld
.MOV(horiz_offset(tmp
, 1), brw_imm_ud(di
.i2
));
6015 return component(retype(tmp
, BRW_REGISTER_TYPE_DF
), 0);
6019 setup_imm_b(const fs_builder
&bld
, int8_t v
)
6021 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_B
);
6022 bld
.MOV(tmp
, brw_imm_w(v
));
6027 setup_imm_ub(const fs_builder
&bld
, uint8_t v
)
6029 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UB
);
6030 bld
.MOV(tmp
, brw_imm_uw(v
));