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"
26 #include "brw_fs_surface_builder.h"
28 #include "util/u_math.h"
29 #include "util/bitscan.h"
32 using namespace brw::surface_access
;
35 fs_visitor::emit_nir_code()
37 /* emit the arrays used for inputs and outputs - load/store intrinsics will
38 * be converted to reads/writes of these arrays
42 nir_emit_system_values();
44 nir_emit_impl(nir_shader_get_entrypoint((nir_shader
*)nir
));
48 fs_visitor::nir_setup_outputs()
50 if (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_FRAGMENT
)
53 unsigned vec4s
[VARYING_SLOT_TESS_MAX
] = { 0, };
55 /* Calculate the size of output registers in a separate pass, before
56 * allocating them. With ARB_enhanced_layouts, multiple output variables
57 * may occupy the same slot, but have different type sizes.
59 nir_foreach_variable(var
, &nir
->outputs
) {
60 const int loc
= var
->data
.driver_location
;
61 const unsigned var_vec4s
=
62 var
->data
.compact
? DIV_ROUND_UP(glsl_get_length(var
->type
), 4)
63 : type_size_vec4(var
->type
);
64 vec4s
[loc
] = MAX2(vec4s
[loc
], var_vec4s
);
67 for (unsigned loc
= 0; loc
< ARRAY_SIZE(vec4s
);) {
68 if (vec4s
[loc
] == 0) {
73 unsigned reg_size
= vec4s
[loc
];
75 /* Check if there are any ranges that start within this range and extend
76 * past it. If so, include them in this allocation.
78 for (unsigned i
= 1; i
< reg_size
; i
++)
79 reg_size
= MAX2(vec4s
[i
+ loc
] + i
, reg_size
);
81 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4 * reg_size
);
82 for (unsigned i
= 0; i
< reg_size
; i
++)
83 outputs
[loc
+ i
] = offset(reg
, bld
, 4 * i
);
90 fs_visitor::nir_setup_uniforms()
92 /* Only the first compile gets to set up uniforms. */
93 if (push_constant_loc
) {
94 assert(pull_constant_loc
);
98 uniforms
= nir
->num_uniforms
/ 4;
100 if (stage
== MESA_SHADER_COMPUTE
) {
101 /* Add a uniform for the thread local id. It must be the last uniform
104 assert(uniforms
== prog_data
->nr_params
);
105 uint32_t *param
= brw_stage_prog_data_add_params(prog_data
, 1);
106 *param
= BRW_PARAM_BUILTIN_SUBGROUP_ID
;
107 subgroup_id
= fs_reg(UNIFORM
, uniforms
++, BRW_REGISTER_TYPE_UD
);
112 emit_system_values_block(nir_block
*block
, fs_visitor
*v
)
116 nir_foreach_instr(instr
, block
) {
117 if (instr
->type
!= nir_instr_type_intrinsic
)
120 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
121 switch (intrin
->intrinsic
) {
122 case nir_intrinsic_load_vertex_id
:
123 case nir_intrinsic_load_base_vertex
:
124 unreachable("should be lowered by nir_lower_system_values().");
126 case nir_intrinsic_load_vertex_id_zero_base
:
127 case nir_intrinsic_load_is_indexed_draw
:
128 case nir_intrinsic_load_first_vertex
:
129 case nir_intrinsic_load_instance_id
:
130 case nir_intrinsic_load_base_instance
:
131 case nir_intrinsic_load_draw_id
:
132 unreachable("should be lowered by brw_nir_lower_vs_inputs().");
134 case nir_intrinsic_load_invocation_id
:
135 if (v
->stage
== MESA_SHADER_TESS_CTRL
)
137 assert(v
->stage
== MESA_SHADER_GEOMETRY
);
138 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
139 if (reg
->file
== BAD_FILE
) {
140 const fs_builder abld
= v
->bld
.annotate("gl_InvocationID", NULL
);
141 fs_reg
g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
142 fs_reg iid
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
143 abld
.SHR(iid
, g1
, brw_imm_ud(27u));
148 case nir_intrinsic_load_sample_pos
:
149 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
150 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
151 if (reg
->file
== BAD_FILE
)
152 *reg
= *v
->emit_samplepos_setup();
155 case nir_intrinsic_load_sample_id
:
156 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
157 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
158 if (reg
->file
== BAD_FILE
)
159 *reg
= *v
->emit_sampleid_setup();
162 case nir_intrinsic_load_sample_mask_in
:
163 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
164 assert(v
->devinfo
->gen
>= 7);
165 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
166 if (reg
->file
== BAD_FILE
)
167 *reg
= *v
->emit_samplemaskin_setup();
170 case nir_intrinsic_load_work_group_id
:
171 assert(v
->stage
== MESA_SHADER_COMPUTE
);
172 reg
= &v
->nir_system_values
[SYSTEM_VALUE_WORK_GROUP_ID
];
173 if (reg
->file
== BAD_FILE
)
174 *reg
= *v
->emit_cs_work_group_id_setup();
177 case nir_intrinsic_load_helper_invocation
:
178 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
179 reg
= &v
->nir_system_values
[SYSTEM_VALUE_HELPER_INVOCATION
];
180 if (reg
->file
== BAD_FILE
) {
181 const fs_builder abld
=
182 v
->bld
.annotate("gl_HelperInvocation", NULL
);
184 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
185 * pixel mask is in g1.7 of the thread payload.
187 * We move the per-channel pixel enable bit to the low bit of each
188 * channel by shifting the byte containing the pixel mask by the
189 * vector immediate 0x76543210UV.
191 * The region of <1,8,0> reads only 1 byte (the pixel masks for
192 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
193 * masks for 2 and 3) in SIMD16.
195 fs_reg shifted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
197 for (unsigned i
= 0; i
< DIV_ROUND_UP(v
->dispatch_width
, 16); i
++) {
198 const fs_builder hbld
= abld
.group(MIN2(16, v
->dispatch_width
), i
);
199 hbld
.SHR(offset(shifted
, hbld
, i
),
200 stride(retype(brw_vec1_grf(1 + i
, 7),
201 BRW_REGISTER_TYPE_UB
),
203 brw_imm_v(0x76543210));
206 /* A set bit in the pixel mask means the channel is enabled, but
207 * that is the opposite of gl_HelperInvocation so we need to invert
210 * The negate source-modifier bit of logical instructions on Gen8+
211 * performs 1's complement negation, so we can use that instead of
214 fs_reg inverted
= negate(shifted
);
215 if (v
->devinfo
->gen
< 8) {
216 inverted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
217 abld
.NOT(inverted
, shifted
);
220 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
221 * with 1 and negating.
223 fs_reg anded
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
224 abld
.AND(anded
, inverted
, brw_imm_uw(1));
226 fs_reg dst
= abld
.vgrf(BRW_REGISTER_TYPE_D
, 1);
227 abld
.MOV(dst
, negate(retype(anded
, BRW_REGISTER_TYPE_D
)));
241 fs_visitor::nir_emit_system_values()
243 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
244 for (unsigned i
= 0; i
< SYSTEM_VALUE_MAX
; i
++) {
245 nir_system_values
[i
] = fs_reg();
248 /* Always emit SUBGROUP_INVOCATION. Dead code will clean it up if we
249 * never end up using it.
252 const fs_builder abld
= bld
.annotate("gl_SubgroupInvocation", NULL
);
253 fs_reg
®
= nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
];
254 reg
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
256 const fs_builder allbld8
= abld
.group(8, 0).exec_all();
257 allbld8
.MOV(reg
, brw_imm_v(0x76543210));
258 if (dispatch_width
> 8)
259 allbld8
.ADD(byte_offset(reg
, 16), reg
, brw_imm_uw(8u));
260 if (dispatch_width
> 16) {
261 const fs_builder allbld16
= abld
.group(16, 0).exec_all();
262 allbld16
.ADD(byte_offset(reg
, 32), reg
, brw_imm_uw(16u));
266 nir_function_impl
*impl
= nir_shader_get_entrypoint((nir_shader
*)nir
);
267 nir_foreach_block(block
, impl
)
268 emit_system_values_block(block
, this);
272 * Returns a type based on a reference_type (word, float, half-float) and a
275 * Reference BRW_REGISTER_TYPE are HF,F,DF,W,D,UW,UD.
277 * @FIXME: 64-bit return types are always DF on integer types to maintain
278 * compability with uses of DF previously to the introduction of int64
282 brw_reg_type_from_bit_size(const unsigned bit_size
,
283 const brw_reg_type reference_type
)
285 switch(reference_type
) {
286 case BRW_REGISTER_TYPE_HF
:
287 case BRW_REGISTER_TYPE_F
:
288 case BRW_REGISTER_TYPE_DF
:
291 return BRW_REGISTER_TYPE_HF
;
293 return BRW_REGISTER_TYPE_F
;
295 return BRW_REGISTER_TYPE_DF
;
297 unreachable("Invalid bit size");
299 case BRW_REGISTER_TYPE_B
:
300 case BRW_REGISTER_TYPE_W
:
301 case BRW_REGISTER_TYPE_D
:
302 case BRW_REGISTER_TYPE_Q
:
305 return BRW_REGISTER_TYPE_B
;
307 return BRW_REGISTER_TYPE_W
;
309 return BRW_REGISTER_TYPE_D
;
311 return BRW_REGISTER_TYPE_Q
;
313 unreachable("Invalid bit size");
315 case BRW_REGISTER_TYPE_UB
:
316 case BRW_REGISTER_TYPE_UW
:
317 case BRW_REGISTER_TYPE_UD
:
318 case BRW_REGISTER_TYPE_UQ
:
321 return BRW_REGISTER_TYPE_UB
;
323 return BRW_REGISTER_TYPE_UW
;
325 return BRW_REGISTER_TYPE_UD
;
327 return BRW_REGISTER_TYPE_UQ
;
329 unreachable("Invalid bit size");
332 unreachable("Unknown type");
337 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
339 nir_locals
= ralloc_array(mem_ctx
, fs_reg
, impl
->reg_alloc
);
340 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
341 nir_locals
[i
] = fs_reg();
344 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
345 unsigned array_elems
=
346 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
347 unsigned size
= array_elems
* reg
->num_components
;
348 const brw_reg_type reg_type
=
349 brw_reg_type_from_bit_size(reg
->bit_size
, BRW_REGISTER_TYPE_F
);
350 nir_locals
[reg
->index
] = bld
.vgrf(reg_type
, size
);
353 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
356 nir_emit_cf_list(&impl
->body
);
360 fs_visitor::nir_emit_cf_list(exec_list
*list
)
362 exec_list_validate(list
);
363 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
364 switch (node
->type
) {
366 nir_emit_if(nir_cf_node_as_if(node
));
369 case nir_cf_node_loop
:
370 nir_emit_loop(nir_cf_node_as_loop(node
));
373 case nir_cf_node_block
:
374 nir_emit_block(nir_cf_node_as_block(node
));
378 unreachable("Invalid CFG node block");
384 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
386 /* first, put the condition into f0 */
387 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
388 retype(get_nir_src(if_stmt
->condition
),
389 BRW_REGISTER_TYPE_D
));
390 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
392 bld
.IF(BRW_PREDICATE_NORMAL
);
394 nir_emit_cf_list(&if_stmt
->then_list
);
396 /* note: if the else is empty, dead CF elimination will remove it */
397 bld
.emit(BRW_OPCODE_ELSE
);
399 nir_emit_cf_list(&if_stmt
->else_list
);
401 bld
.emit(BRW_OPCODE_ENDIF
);
403 if (devinfo
->gen
< 7)
404 limit_dispatch_width(16, "Non-uniform control flow unsupported "
409 fs_visitor::nir_emit_loop(nir_loop
*loop
)
411 bld
.emit(BRW_OPCODE_DO
);
413 nir_emit_cf_list(&loop
->body
);
415 bld
.emit(BRW_OPCODE_WHILE
);
417 if (devinfo
->gen
< 7)
418 limit_dispatch_width(16, "Non-uniform control flow unsupported "
423 fs_visitor::nir_emit_block(nir_block
*block
)
425 nir_foreach_instr(instr
, block
) {
426 nir_emit_instr(instr
);
431 fs_visitor::nir_emit_instr(nir_instr
*instr
)
433 const fs_builder abld
= bld
.annotate(NULL
, instr
);
435 switch (instr
->type
) {
436 case nir_instr_type_alu
:
437 nir_emit_alu(abld
, nir_instr_as_alu(instr
));
440 case nir_instr_type_deref
:
441 /* Derefs can exist for images but they do nothing */
444 case nir_instr_type_intrinsic
:
446 case MESA_SHADER_VERTEX
:
447 nir_emit_vs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
449 case MESA_SHADER_TESS_CTRL
:
450 nir_emit_tcs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
452 case MESA_SHADER_TESS_EVAL
:
453 nir_emit_tes_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
455 case MESA_SHADER_GEOMETRY
:
456 nir_emit_gs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
458 case MESA_SHADER_FRAGMENT
:
459 nir_emit_fs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
461 case MESA_SHADER_COMPUTE
:
462 nir_emit_cs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
465 unreachable("unsupported shader stage");
469 case nir_instr_type_tex
:
470 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
473 case nir_instr_type_load_const
:
474 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
477 case nir_instr_type_ssa_undef
:
478 /* We create a new VGRF for undefs on every use (by handling
479 * them in get_nir_src()), rather than for each definition.
480 * This helps register coalescing eliminate MOVs from undef.
484 case nir_instr_type_jump
:
485 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
489 unreachable("unknown instruction type");
494 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
498 fs_visitor::optimize_extract_to_float(nir_alu_instr
*instr
,
499 const fs_reg
&result
)
501 if (!instr
->src
[0].src
.is_ssa
||
502 !instr
->src
[0].src
.ssa
->parent_instr
)
505 if (instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
508 nir_alu_instr
*src0
=
509 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
511 if (src0
->op
!= nir_op_extract_u8
&& src0
->op
!= nir_op_extract_u16
&&
512 src0
->op
!= nir_op_extract_i8
&& src0
->op
!= nir_op_extract_i16
)
515 unsigned element
= nir_src_as_uint(src0
->src
[1].src
);
517 /* Element type to extract.*/
518 const brw_reg_type type
= brw_int_type(
519 src0
->op
== nir_op_extract_u16
|| src0
->op
== nir_op_extract_i16
? 2 : 1,
520 src0
->op
== nir_op_extract_i16
|| src0
->op
== nir_op_extract_i8
);
522 fs_reg op0
= get_nir_src(src0
->src
[0].src
);
523 op0
.type
= brw_type_for_nir_type(devinfo
,
524 (nir_alu_type
)(nir_op_infos
[src0
->op
].input_types
[0] |
525 nir_src_bit_size(src0
->src
[0].src
)));
526 op0
= offset(op0
, bld
, src0
->src
[0].swizzle
[0]);
528 set_saturate(instr
->dest
.saturate
,
529 bld
.MOV(result
, subscript(op0
, type
, element
)));
534 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
535 const fs_reg
&result
)
537 if (!instr
->src
[0].src
.is_ssa
||
538 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_intrinsic
)
541 nir_intrinsic_instr
*src0
=
542 nir_instr_as_intrinsic(instr
->src
[0].src
.ssa
->parent_instr
);
544 if (src0
->intrinsic
!= nir_intrinsic_load_front_face
)
547 if (!nir_src_is_const(instr
->src
[1].src
) ||
548 !nir_src_is_const(instr
->src
[2].src
))
551 const float value1
= nir_src_as_float(instr
->src
[1].src
);
552 const float value2
= nir_src_as_float(instr
->src
[2].src
);
553 if (fabsf(value1
) != 1.0f
|| fabsf(value2
) != 1.0f
)
556 /* nir_opt_algebraic should have gotten rid of bcsel(b, a, a) */
557 assert(value1
== -value2
);
559 fs_reg tmp
= vgrf(glsl_type::int_type
);
561 if (devinfo
->gen
>= 6) {
562 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
563 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
565 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
567 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
568 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
570 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
572 * This negation looks like it's safe in practice, because bits 0:4 will
573 * surely be TRIANGLES
576 if (value1
== -1.0f
) {
580 bld
.OR(subscript(tmp
, BRW_REGISTER_TYPE_W
, 1),
581 g0
, brw_imm_uw(0x3f80));
583 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
584 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
586 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
588 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
589 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
591 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
593 * This negation looks like it's safe in practice, because bits 0:4 will
594 * surely be TRIANGLES
597 if (value1
== -1.0f
) {
601 bld
.OR(tmp
, g1_6
, brw_imm_d(0x3f800000));
603 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, brw_imm_d(0xbf800000));
609 emit_find_msb_using_lzd(const fs_builder
&bld
,
610 const fs_reg
&result
,
618 /* LZD of an absolute value source almost always does the right
619 * thing. There are two problem values:
621 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
622 * 0. However, findMSB(int(0x80000000)) == 30.
624 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
625 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
627 * For a value of zero or negative one, -1 will be returned.
629 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
630 * findMSB(-(1<<x)) should return x-1.
632 * For all negative number cases, including 0x80000000 and
633 * 0xffffffff, the correct value is obtained from LZD if instead of
634 * negating the (already negative) value the logical-not is used. A
635 * conditonal logical-not can be achieved in two instructions.
637 temp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
639 bld
.ASR(temp
, src
, brw_imm_d(31));
640 bld
.XOR(temp
, temp
, src
);
643 bld
.LZD(retype(result
, BRW_REGISTER_TYPE_UD
),
644 retype(temp
, BRW_REGISTER_TYPE_UD
));
646 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
647 * from the LSB side. Subtract the result from 31 to convert the MSB
648 * count into an LSB count. If no bits are set, LZD will return 32.
649 * 31-32 = -1, which is exactly what findMSB() is supposed to return.
651 inst
= bld
.ADD(result
, retype(result
, BRW_REGISTER_TYPE_D
), brw_imm_d(31));
652 inst
->src
[0].negate
= true;
656 brw_rnd_mode_from_nir_op (const nir_op op
) {
658 case nir_op_f2f16_rtz
:
659 return BRW_RND_MODE_RTZ
;
660 case nir_op_f2f16_rtne
:
661 return BRW_RND_MODE_RTNE
;
663 unreachable("Operation doesn't support rounding mode");
668 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
)
670 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
673 fs_reg result
= get_nir_dest(instr
->dest
.dest
);
674 result
.type
= brw_type_for_nir_type(devinfo
,
675 (nir_alu_type
)(nir_op_infos
[instr
->op
].output_type
|
676 nir_dest_bit_size(instr
->dest
.dest
)));
679 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
680 op
[i
] = get_nir_src(instr
->src
[i
].src
);
681 op
[i
].type
= brw_type_for_nir_type(devinfo
,
682 (nir_alu_type
)(nir_op_infos
[instr
->op
].input_types
[i
] |
683 nir_src_bit_size(instr
->src
[i
].src
)));
684 op
[i
].abs
= instr
->src
[i
].abs
;
685 op
[i
].negate
= instr
->src
[i
].negate
;
688 /* We get a bunch of mov's out of the from_ssa pass and they may still
689 * be vectorized. We'll handle them as a special-case. We'll also
690 * handle vecN here because it's basically the same thing.
698 fs_reg temp
= result
;
699 bool need_extra_copy
= false;
700 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
701 if (!instr
->src
[i
].src
.is_ssa
&&
702 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
703 need_extra_copy
= true;
704 temp
= bld
.vgrf(result
.type
, 4);
709 for (unsigned i
= 0; i
< 4; i
++) {
710 if (!(instr
->dest
.write_mask
& (1 << i
)))
713 if (instr
->op
== nir_op_imov
|| instr
->op
== nir_op_fmov
) {
714 inst
= bld
.MOV(offset(temp
, bld
, i
),
715 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
717 inst
= bld
.MOV(offset(temp
, bld
, i
),
718 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
720 inst
->saturate
= instr
->dest
.saturate
;
723 /* In this case the source and destination registers were the same,
724 * so we need to insert an extra set of moves in order to deal with
727 if (need_extra_copy
) {
728 for (unsigned i
= 0; i
< 4; i
++) {
729 if (!(instr
->dest
.write_mask
& (1 << i
)))
732 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
741 /* At this point, we have dealt with any instruction that operates on
742 * more than a single channel. Therefore, we can just adjust the source
743 * and destination registers for that channel and emit the instruction.
745 unsigned channel
= 0;
746 if (nir_op_infos
[instr
->op
].output_size
== 0) {
747 /* Since NIR is doing the scalarizing for us, we should only ever see
748 * vectorized operations with a single channel.
750 assert(util_bitcount(instr
->dest
.write_mask
) == 1);
751 channel
= ffs(instr
->dest
.write_mask
) - 1;
753 result
= offset(result
, bld
, channel
);
756 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
757 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
758 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
764 if (optimize_extract_to_float(instr
, result
))
766 inst
= bld
.MOV(result
, op
[0]);
767 inst
->saturate
= instr
->dest
.saturate
;
770 case nir_op_f2f16_rtne
:
771 case nir_op_f2f16_rtz
:
772 bld
.emit(SHADER_OPCODE_RND_MODE
, bld
.null_reg_ud(),
773 brw_imm_d(brw_rnd_mode_from_nir_op(instr
->op
)));
776 /* In theory, it would be better to use BRW_OPCODE_F32TO16. Depending
777 * on the HW gen, it is a special hw opcode or just a MOV, and
778 * brw_F32TO16 (at brw_eu_emit) would do the work to chose.
780 * But if we want to use that opcode, we need to provide support on
781 * different optimizations and lowerings. As right now HF support is
782 * only for gen8+, it will be better to use directly the MOV, and use
783 * BRW_OPCODE_F32TO16 when/if we work for HF support on gen7.
787 inst
= bld
.MOV(result
, op
[0]);
788 inst
->saturate
= instr
->dest
.saturate
;
798 op
[0].type
= BRW_REGISTER_TYPE_D
;
799 op
[0].negate
= !op
[0].negate
;
821 inst
= bld
.MOV(result
, op
[0]);
822 inst
->saturate
= instr
->dest
.saturate
;
826 assert(!instr
->dest
.saturate
);
828 /* Straightforward since the source can be assumed to be either
829 * strictly >= 0 or strictly <= 0 depending on the setting of the
832 set_condmod(BRW_CONDITIONAL_NZ
, bld
.MOV(result
, op
[0]));
834 inst
= (op
[0].negate
)
835 ? bld
.MOV(result
, brw_imm_f(-1.0f
))
836 : bld
.MOV(result
, brw_imm_f(1.0f
));
838 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
839 } else if (type_sz(op
[0].type
) < 8) {
840 /* AND(val, 0x80000000) gives the sign bit.
842 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
845 bld
.CMP(bld
.null_reg_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
847 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
848 op
[0].type
= BRW_REGISTER_TYPE_UD
;
849 result
.type
= BRW_REGISTER_TYPE_UD
;
850 bld
.AND(result_int
, op
[0], brw_imm_ud(0x80000000u
));
852 inst
= bld
.OR(result_int
, result_int
, brw_imm_ud(0x3f800000u
));
853 inst
->predicate
= BRW_PREDICATE_NORMAL
;
855 /* For doubles we do the same but we need to consider:
857 * - 2-src instructions can't operate with 64-bit immediates
858 * - The sign is encoded in the high 32-bit of each DF
859 * - We need to produce a DF result.
862 fs_reg zero
= vgrf(glsl_type::double_type
);
863 bld
.MOV(zero
, setup_imm_df(bld
, 0.0));
864 bld
.CMP(bld
.null_reg_df(), op
[0], zero
, BRW_CONDITIONAL_NZ
);
866 bld
.MOV(result
, zero
);
868 fs_reg r
= subscript(result
, BRW_REGISTER_TYPE_UD
, 1);
869 bld
.AND(r
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 1),
870 brw_imm_ud(0x80000000u
));
872 set_predicate(BRW_PREDICATE_NORMAL
,
873 bld
.OR(r
, r
, brw_imm_ud(0x3ff00000u
)));
879 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
880 * -> non-negative val generates 0x00000000.
881 * Predicated OR sets 1 if val is positive.
883 uint32_t bit_size
= nir_dest_bit_size(instr
->dest
.dest
);
884 assert(bit_size
== 32 || bit_size
== 16);
886 fs_reg zero
= bit_size
== 32 ? brw_imm_d(0) : brw_imm_w(0);
887 fs_reg one
= bit_size
== 32 ? brw_imm_d(1) : brw_imm_w(1);
888 fs_reg shift
= bit_size
== 32 ? brw_imm_d(31) : brw_imm_w(15);
890 bld
.CMP(bld
.null_reg_d(), op
[0], zero
, BRW_CONDITIONAL_G
);
891 bld
.ASR(result
, op
[0], shift
);
892 inst
= bld
.OR(result
, result
, one
);
893 inst
->predicate
= BRW_PREDICATE_NORMAL
;
898 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
899 inst
->saturate
= instr
->dest
.saturate
;
903 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
904 inst
->saturate
= instr
->dest
.saturate
;
908 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
909 inst
->saturate
= instr
->dest
.saturate
;
913 inst
= bld
.emit(SHADER_OPCODE_SIN
, result
, op
[0]);
914 inst
->saturate
= instr
->dest
.saturate
;
918 inst
= bld
.emit(SHADER_OPCODE_COS
, result
, op
[0]);
919 inst
->saturate
= instr
->dest
.saturate
;
923 if (fs_key
->high_quality_derivatives
) {
924 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
926 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
928 inst
->saturate
= instr
->dest
.saturate
;
930 case nir_op_fddx_fine
:
931 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
932 inst
->saturate
= instr
->dest
.saturate
;
934 case nir_op_fddx_coarse
:
935 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
936 inst
->saturate
= instr
->dest
.saturate
;
939 if (fs_key
->high_quality_derivatives
) {
940 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0]);
942 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0]);
944 inst
->saturate
= instr
->dest
.saturate
;
946 case nir_op_fddy_fine
:
947 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0]);
948 inst
->saturate
= instr
->dest
.saturate
;
950 case nir_op_fddy_coarse
:
951 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0]);
952 inst
->saturate
= instr
->dest
.saturate
;
957 inst
= bld
.ADD(result
, op
[0], op
[1]);
958 inst
->saturate
= instr
->dest
.saturate
;
961 case nir_op_uadd_sat
:
962 inst
= bld
.ADD(result
, op
[0], op
[1]);
963 inst
->saturate
= true;
967 inst
= bld
.MUL(result
, op
[0], op
[1]);
968 inst
->saturate
= instr
->dest
.saturate
;
972 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
973 bld
.MUL(result
, op
[0], op
[1]);
976 case nir_op_imul_high
:
977 case nir_op_umul_high
:
978 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
979 bld
.emit(SHADER_OPCODE_MULH
, result
, op
[0], op
[1]);
984 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
985 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
988 case nir_op_uadd_carry
:
989 unreachable("Should have been lowered by carry_to_arith().");
991 case nir_op_usub_borrow
:
992 unreachable("Should have been lowered by borrow_to_arith().");
996 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
997 * appears that our hardware just does the right thing for signed
1000 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1001 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
1005 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1006 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
1008 /* Math instructions don't support conditional mod */
1009 inst
= bld
.MOV(bld
.null_reg_d(), result
);
1010 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
1012 /* Now, we need to determine if signs of the sources are different.
1013 * When we XOR the sources, the top bit is 0 if they are the same and 1
1014 * if they are different. We can then use a conditional modifier to
1015 * turn that into a predicate. This leads us to an XOR.l instruction.
1017 * Technically, according to the PRM, you're not allowed to use .l on a
1018 * XOR instruction. However, emperical experiments and Curro's reading
1019 * of the simulator source both indicate that it's safe.
1021 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1022 inst
= bld
.XOR(tmp
, op
[0], op
[1]);
1023 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1024 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
1026 /* If the result of the initial remainder operation is non-zero and the
1027 * two sources have different signs, add in a copy of op[1] to get the
1028 * final integer modulus value.
1030 inst
= bld
.ADD(result
, result
, op
[1]);
1031 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1038 case nir_op_fne32
: {
1039 fs_reg dest
= result
;
1041 const uint32_t bit_size
= nir_src_bit_size(instr
->src
[0].src
);
1043 dest
= bld
.vgrf(op
[0].type
, 1);
1045 brw_conditional_mod cond
;
1046 switch (instr
->op
) {
1048 cond
= BRW_CONDITIONAL_L
;
1051 cond
= BRW_CONDITIONAL_GE
;
1054 cond
= BRW_CONDITIONAL_Z
;
1057 cond
= BRW_CONDITIONAL_NZ
;
1060 unreachable("bad opcode");
1063 bld
.CMP(dest
, op
[0], op
[1], cond
);
1065 if (bit_size
> 32) {
1066 bld
.MOV(result
, subscript(dest
, BRW_REGISTER_TYPE_UD
, 0));
1067 } else if(bit_size
< 32) {
1068 /* When we convert the result to 32-bit we need to be careful and do
1069 * it as a signed conversion to get sign extension (for 32-bit true)
1071 const brw_reg_type src_type
=
1072 brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_D
);
1074 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), retype(dest
, src_type
));
1084 case nir_op_ine32
: {
1085 fs_reg dest
= result
;
1087 const uint32_t bit_size
= nir_src_bit_size(instr
->src
[0].src
);
1089 dest
= bld
.vgrf(op
[0].type
, 1);
1091 brw_conditional_mod cond
;
1092 switch (instr
->op
) {
1095 cond
= BRW_CONDITIONAL_L
;
1099 cond
= BRW_CONDITIONAL_GE
;
1102 cond
= BRW_CONDITIONAL_Z
;
1105 cond
= BRW_CONDITIONAL_NZ
;
1108 unreachable("bad opcode");
1110 bld
.CMP(dest
, op
[0], op
[1], cond
);
1112 if (bit_size
> 32) {
1113 bld
.MOV(result
, subscript(dest
, BRW_REGISTER_TYPE_UD
, 0));
1114 } else if (bit_size
< 32) {
1115 /* When we convert the result to 32-bit we need to be careful and do
1116 * it as a signed conversion to get sign extension (for 32-bit true)
1118 const brw_reg_type src_type
=
1119 brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_D
);
1121 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), retype(dest
, src_type
));
1127 if (devinfo
->gen
>= 8) {
1128 op
[0] = resolve_source_modifiers(op
[0]);
1130 bld
.NOT(result
, op
[0]);
1133 if (devinfo
->gen
>= 8) {
1134 op
[0] = resolve_source_modifiers(op
[0]);
1135 op
[1] = resolve_source_modifiers(op
[1]);
1137 bld
.XOR(result
, op
[0], op
[1]);
1140 if (devinfo
->gen
>= 8) {
1141 op
[0] = resolve_source_modifiers(op
[0]);
1142 op
[1] = resolve_source_modifiers(op
[1]);
1144 bld
.OR(result
, op
[0], op
[1]);
1147 if (devinfo
->gen
>= 8) {
1148 op
[0] = resolve_source_modifiers(op
[0]);
1149 op
[1] = resolve_source_modifiers(op
[1]);
1151 bld
.AND(result
, op
[0], op
[1]);
1157 case nir_op_b32all_fequal2
:
1158 case nir_op_b32all_iequal2
:
1159 case nir_op_b32all_fequal3
:
1160 case nir_op_b32all_iequal3
:
1161 case nir_op_b32all_fequal4
:
1162 case nir_op_b32all_iequal4
:
1163 case nir_op_b32any_fnequal2
:
1164 case nir_op_b32any_inequal2
:
1165 case nir_op_b32any_fnequal3
:
1166 case nir_op_b32any_inequal3
:
1167 case nir_op_b32any_fnequal4
:
1168 case nir_op_b32any_inequal4
:
1169 unreachable("Lowered by nir_lower_alu_reductions");
1171 case nir_op_fnoise1_1
:
1172 case nir_op_fnoise1_2
:
1173 case nir_op_fnoise1_3
:
1174 case nir_op_fnoise1_4
:
1175 case nir_op_fnoise2_1
:
1176 case nir_op_fnoise2_2
:
1177 case nir_op_fnoise2_3
:
1178 case nir_op_fnoise2_4
:
1179 case nir_op_fnoise3_1
:
1180 case nir_op_fnoise3_2
:
1181 case nir_op_fnoise3_3
:
1182 case nir_op_fnoise3_4
:
1183 case nir_op_fnoise4_1
:
1184 case nir_op_fnoise4_2
:
1185 case nir_op_fnoise4_3
:
1186 case nir_op_fnoise4_4
:
1187 unreachable("not reached: should be handled by lower_noise");
1190 unreachable("not reached: should be handled by ldexp_to_arith()");
1193 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
1194 inst
->saturate
= instr
->dest
.saturate
;
1198 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
1199 inst
->saturate
= instr
->dest
.saturate
;
1203 case nir_op_f2b32
: {
1204 uint32_t bit_size
= nir_src_bit_size(instr
->src
[0].src
);
1205 if (bit_size
== 64) {
1206 /* two-argument instructions can't take 64-bit immediates */
1210 if (instr
->op
== nir_op_f2b32
) {
1211 zero
= vgrf(glsl_type::double_type
);
1212 tmp
= vgrf(glsl_type::double_type
);
1213 bld
.MOV(zero
, setup_imm_df(bld
, 0.0));
1215 zero
= vgrf(glsl_type::int64_t_type
);
1216 tmp
= vgrf(glsl_type::int64_t_type
);
1217 bld
.MOV(zero
, brw_imm_q(0));
1220 /* A SIMD16 execution needs to be split in two instructions, so use
1221 * a vgrf instead of the flag register as dst so instruction splitting
1224 bld
.CMP(tmp
, op
[0], zero
, BRW_CONDITIONAL_NZ
);
1225 bld
.MOV(result
, subscript(tmp
, BRW_REGISTER_TYPE_UD
, 0));
1228 if (bit_size
== 32) {
1229 zero
= instr
->op
== nir_op_f2b32
? brw_imm_f(0.0f
) : brw_imm_d(0);
1231 assert(bit_size
== 16);
1232 zero
= instr
->op
== nir_op_f2b32
?
1233 retype(brw_imm_w(0), BRW_REGISTER_TYPE_HF
) : brw_imm_w(0);
1235 bld
.CMP(result
, op
[0], zero
, BRW_CONDITIONAL_NZ
);
1241 inst
= bld
.RNDZ(result
, op
[0]);
1242 inst
->saturate
= instr
->dest
.saturate
;
1245 case nir_op_fceil
: {
1246 op
[0].negate
= !op
[0].negate
;
1247 fs_reg temp
= vgrf(glsl_type::float_type
);
1248 bld
.RNDD(temp
, op
[0]);
1250 inst
= bld
.MOV(result
, temp
);
1251 inst
->saturate
= instr
->dest
.saturate
;
1255 inst
= bld
.RNDD(result
, op
[0]);
1256 inst
->saturate
= instr
->dest
.saturate
;
1259 inst
= bld
.FRC(result
, op
[0]);
1260 inst
->saturate
= instr
->dest
.saturate
;
1262 case nir_op_fround_even
:
1263 inst
= bld
.RNDE(result
, op
[0]);
1264 inst
->saturate
= instr
->dest
.saturate
;
1267 case nir_op_fquantize2f16
: {
1268 fs_reg tmp16
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1269 fs_reg tmp32
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1270 fs_reg zero
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1272 /* The destination stride must be at least as big as the source stride. */
1273 tmp16
.type
= BRW_REGISTER_TYPE_W
;
1276 /* Check for denormal */
1277 fs_reg abs_src0
= op
[0];
1278 abs_src0
.abs
= true;
1279 bld
.CMP(bld
.null_reg_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1281 /* Get the appropriately signed zero */
1282 bld
.AND(retype(zero
, BRW_REGISTER_TYPE_UD
),
1283 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1284 brw_imm_ud(0x80000000));
1285 /* Do the actual F32 -> F16 -> F32 conversion */
1286 bld
.emit(BRW_OPCODE_F32TO16
, tmp16
, op
[0]);
1287 bld
.emit(BRW_OPCODE_F16TO32
, tmp32
, tmp16
);
1288 /* Select that or zero based on normal status */
1289 inst
= bld
.SEL(result
, zero
, tmp32
);
1290 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1291 inst
->saturate
= instr
->dest
.saturate
;
1298 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
1299 inst
->saturate
= instr
->dest
.saturate
;
1305 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
1306 inst
->saturate
= instr
->dest
.saturate
;
1309 case nir_op_pack_snorm_2x16
:
1310 case nir_op_pack_snorm_4x8
:
1311 case nir_op_pack_unorm_2x16
:
1312 case nir_op_pack_unorm_4x8
:
1313 case nir_op_unpack_snorm_2x16
:
1314 case nir_op_unpack_snorm_4x8
:
1315 case nir_op_unpack_unorm_2x16
:
1316 case nir_op_unpack_unorm_4x8
:
1317 case nir_op_unpack_half_2x16
:
1318 case nir_op_pack_half_2x16
:
1319 unreachable("not reached: should be handled by lower_packing_builtins");
1321 case nir_op_unpack_half_2x16_split_x
:
1322 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, result
, op
[0]);
1323 inst
->saturate
= instr
->dest
.saturate
;
1325 case nir_op_unpack_half_2x16_split_y
:
1326 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, result
, op
[0]);
1327 inst
->saturate
= instr
->dest
.saturate
;
1330 case nir_op_pack_64_2x32_split
:
1331 case nir_op_pack_32_2x16_split
:
1332 bld
.emit(FS_OPCODE_PACK
, result
, op
[0], op
[1]);
1335 case nir_op_unpack_64_2x32_split_x
:
1336 case nir_op_unpack_64_2x32_split_y
: {
1337 if (instr
->op
== nir_op_unpack_64_2x32_split_x
)
1338 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 0));
1340 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 1));
1344 case nir_op_unpack_32_2x16_split_x
:
1345 case nir_op_unpack_32_2x16_split_y
: {
1346 if (instr
->op
== nir_op_unpack_32_2x16_split_x
)
1347 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UW
, 0));
1349 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UW
, 1));
1354 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
1355 inst
->saturate
= instr
->dest
.saturate
;
1358 case nir_op_bitfield_reverse
:
1359 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1360 bld
.BFREV(result
, op
[0]);
1363 case nir_op_bit_count
:
1364 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1365 bld
.CBIT(result
, op
[0]);
1368 case nir_op_ufind_msb
: {
1369 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1370 emit_find_msb_using_lzd(bld
, result
, op
[0], false);
1374 case nir_op_ifind_msb
: {
1375 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1377 if (devinfo
->gen
< 7) {
1378 emit_find_msb_using_lzd(bld
, result
, op
[0], true);
1380 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1382 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1383 * count from the LSB side. If FBH didn't return an error
1384 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1385 * count into an LSB count.
1387 bld
.CMP(bld
.null_reg_d(), result
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1389 inst
= bld
.ADD(result
, result
, brw_imm_d(31));
1390 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1391 inst
->src
[0].negate
= true;
1396 case nir_op_find_lsb
:
1397 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1399 if (devinfo
->gen
< 7) {
1400 fs_reg temp
= vgrf(glsl_type::int_type
);
1402 /* (x & -x) generates a value that consists of only the LSB of x.
1403 * For all powers of 2, findMSB(y) == findLSB(y).
1405 fs_reg src
= retype(op
[0], BRW_REGISTER_TYPE_D
);
1406 fs_reg negated_src
= src
;
1408 /* One must be negated, and the other must be non-negated. It
1409 * doesn't matter which is which.
1411 negated_src
.negate
= true;
1414 bld
.AND(temp
, src
, negated_src
);
1415 emit_find_msb_using_lzd(bld
, result
, temp
, false);
1417 bld
.FBL(result
, op
[0]);
1421 case nir_op_ubitfield_extract
:
1422 case nir_op_ibitfield_extract
:
1423 unreachable("should have been lowered");
1426 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1427 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1430 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1431 bld
.BFI1(result
, op
[0], op
[1]);
1434 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1435 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1438 case nir_op_bitfield_insert
:
1439 unreachable("not reached: should have been lowered");
1442 bld
.SHL(result
, op
[0], op
[1]);
1445 bld
.ASR(result
, op
[0], op
[1]);
1448 bld
.SHR(result
, op
[0], op
[1]);
1451 case nir_op_pack_half_2x16_split
:
1452 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1456 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1457 inst
->saturate
= instr
->dest
.saturate
;
1461 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1462 inst
->saturate
= instr
->dest
.saturate
;
1465 case nir_op_b32csel
:
1466 if (optimize_frontfacing_ternary(instr
, result
))
1469 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1470 inst
= bld
.SEL(result
, op
[1], op
[2]);
1471 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1474 case nir_op_extract_u8
:
1475 case nir_op_extract_i8
: {
1476 unsigned byte
= nir_src_as_uint(instr
->src
[1].src
);
1481 * There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.
1482 * Use two instructions and a word or DWord intermediate integer type.
1484 if (nir_dest_bit_size(instr
->dest
.dest
) == 64) {
1485 const brw_reg_type type
= brw_int_type(2, instr
->op
== nir_op_extract_i8
);
1487 if (instr
->op
== nir_op_extract_i8
) {
1488 /* If we need to sign extend, extract to a word first */
1489 fs_reg w_temp
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
1490 bld
.MOV(w_temp
, subscript(op
[0], type
, byte
));
1491 bld
.MOV(result
, w_temp
);
1493 /* Otherwise use an AND with 0xff and a word type */
1494 bld
.AND(result
, subscript(op
[0], type
, byte
/ 2), brw_imm_uw(0xff));
1497 const brw_reg_type type
= brw_int_type(1, instr
->op
== nir_op_extract_i8
);
1498 bld
.MOV(result
, subscript(op
[0], type
, byte
));
1503 case nir_op_extract_u16
:
1504 case nir_op_extract_i16
: {
1505 const brw_reg_type type
= brw_int_type(2, instr
->op
== nir_op_extract_i16
);
1506 unsigned word
= nir_src_as_uint(instr
->src
[1].src
);
1507 bld
.MOV(result
, subscript(op
[0], type
, word
));
1512 unreachable("unhandled instruction");
1515 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1516 * to sign extend the low bit to 0/~0
1518 if (devinfo
->gen
<= 5 &&
1519 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1520 fs_reg masked
= vgrf(glsl_type::int_type
);
1521 bld
.AND(masked
, result
, brw_imm_d(1));
1522 masked
.negate
= true;
1523 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
1528 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
1529 nir_load_const_instr
*instr
)
1531 const brw_reg_type reg_type
=
1532 brw_reg_type_from_bit_size(instr
->def
.bit_size
, BRW_REGISTER_TYPE_D
);
1533 fs_reg reg
= bld
.vgrf(reg_type
, instr
->def
.num_components
);
1535 switch (instr
->def
.bit_size
) {
1537 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1538 bld
.MOV(offset(reg
, bld
, i
), setup_imm_b(bld
, instr
->value
.i8
[i
]));
1542 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1543 bld
.MOV(offset(reg
, bld
, i
), brw_imm_w(instr
->value
.i16
[i
]));
1547 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1548 bld
.MOV(offset(reg
, bld
, i
), brw_imm_d(instr
->value
.i32
[i
]));
1552 assert(devinfo
->gen
>= 7);
1553 if (devinfo
->gen
== 7) {
1554 /* We don't get 64-bit integer types until gen8 */
1555 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++) {
1556 bld
.MOV(retype(offset(reg
, bld
, i
), BRW_REGISTER_TYPE_DF
),
1557 setup_imm_df(bld
, instr
->value
.f64
[i
]));
1560 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1561 bld
.MOV(offset(reg
, bld
, i
), brw_imm_q(instr
->value
.i64
[i
]));
1566 unreachable("Invalid bit size");
1569 nir_ssa_values
[instr
->def
.index
] = reg
;
1573 fs_visitor::get_nir_src(const nir_src
&src
)
1577 if (src
.ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
1578 const brw_reg_type reg_type
=
1579 brw_reg_type_from_bit_size(src
.ssa
->bit_size
, BRW_REGISTER_TYPE_D
);
1580 reg
= bld
.vgrf(reg_type
, src
.ssa
->num_components
);
1582 reg
= nir_ssa_values
[src
.ssa
->index
];
1585 /* We don't handle indirects on locals */
1586 assert(src
.reg
.indirect
== NULL
);
1587 reg
= offset(nir_locals
[src
.reg
.reg
->index
], bld
,
1588 src
.reg
.base_offset
* src
.reg
.reg
->num_components
);
1591 if (nir_src_bit_size(src
) == 64 && devinfo
->gen
== 7) {
1592 /* The only 64-bit type available on gen7 is DF, so use that. */
1593 reg
.type
= BRW_REGISTER_TYPE_DF
;
1595 /* To avoid floating-point denorm flushing problems, set the type by
1596 * default to an integer type - instructions that need floating point
1597 * semantics will set this to F if they need to
1599 reg
.type
= brw_reg_type_from_bit_size(nir_src_bit_size(src
),
1600 BRW_REGISTER_TYPE_D
);
1607 * Return an IMM for constants; otherwise call get_nir_src() as normal.
1609 * This function should not be called on any value which may be 64 bits.
1610 * We could theoretically support 64-bit on gen8+ but we choose not to
1611 * because it wouldn't work in general (no gen7 support) and there are
1612 * enough restrictions in 64-bit immediates that you can't take the return
1613 * value and treat it the same as the result of get_nir_src().
1616 fs_visitor::get_nir_src_imm(const nir_src
&src
)
1618 assert(nir_src_bit_size(src
) == 32);
1619 return nir_src_is_const(src
) ?
1620 fs_reg(brw_imm_d(nir_src_as_int(src
))) : get_nir_src(src
);
1624 fs_visitor::get_nir_dest(const nir_dest
&dest
)
1627 const brw_reg_type reg_type
=
1628 brw_reg_type_from_bit_size(dest
.ssa
.bit_size
,
1629 dest
.ssa
.bit_size
== 8 ?
1630 BRW_REGISTER_TYPE_D
:
1631 BRW_REGISTER_TYPE_F
);
1632 nir_ssa_values
[dest
.ssa
.index
] =
1633 bld
.vgrf(reg_type
, dest
.ssa
.num_components
);
1634 return nir_ssa_values
[dest
.ssa
.index
];
1636 /* We don't handle indirects on locals */
1637 assert(dest
.reg
.indirect
== NULL
);
1638 return offset(nir_locals
[dest
.reg
.reg
->index
], bld
,
1639 dest
.reg
.base_offset
* dest
.reg
.reg
->num_components
);
1644 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
1647 for (unsigned i
= 0; i
< 4; i
++) {
1648 if (!((wr_mask
>> i
) & 1))
1651 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
1652 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
1653 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
1654 if (new_inst
->src
[j
].file
== VGRF
)
1655 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
1662 emit_pixel_interpolater_send(const fs_builder
&bld
,
1667 glsl_interp_mode interpolation
)
1669 struct brw_wm_prog_data
*wm_prog_data
=
1670 brw_wm_prog_data(bld
.shader
->stage_prog_data
);
1672 fs_inst
*inst
= bld
.emit(opcode
, dst
, src
, desc
);
1673 /* 2 floats per slot returned */
1674 inst
->size_written
= 2 * dst
.component_size(inst
->exec_size
);
1675 inst
->pi_noperspective
= interpolation
== INTERP_MODE_NOPERSPECTIVE
;
1677 wm_prog_data
->pulls_bary
= true;
1683 * Computes 1 << x, given a D/UD register containing some value x.
1686 intexp2(const fs_builder
&bld
, const fs_reg
&x
)
1688 assert(x
.type
== BRW_REGISTER_TYPE_UD
|| x
.type
== BRW_REGISTER_TYPE_D
);
1690 fs_reg result
= bld
.vgrf(x
.type
, 1);
1691 fs_reg one
= bld
.vgrf(x
.type
, 1);
1693 bld
.MOV(one
, retype(brw_imm_d(1), one
.type
));
1694 bld
.SHL(result
, one
, x
);
1699 fs_visitor::emit_gs_end_primitive(const nir_src
&vertex_count_nir_src
)
1701 assert(stage
== MESA_SHADER_GEOMETRY
);
1703 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1705 if (gs_compile
->control_data_header_size_bits
== 0)
1708 /* We can only do EndPrimitive() functionality when the control data
1709 * consists of cut bits. Fortunately, the only time it isn't is when the
1710 * output type is points, in which case EndPrimitive() is a no-op.
1712 if (gs_prog_data
->control_data_format
!=
1713 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
1717 /* Cut bits use one bit per vertex. */
1718 assert(gs_compile
->control_data_bits_per_vertex
== 1);
1720 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1721 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1723 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1724 * vertex n, 0 otherwise. So all we need to do here is mark bit
1725 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1726 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1727 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1729 * Note that if EndPrimitive() is called before emitting any vertices, this
1730 * will cause us to set bit 31 of the control_data_bits register to 1.
1731 * That's fine because:
1733 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1734 * output, so the hardware will ignore cut bit 31.
1736 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1737 * last vertex, so setting cut bit 31 has no effect (since the primitive
1738 * is automatically ended when the GS terminates).
1740 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1741 * control_data_bits register to 0 when the first vertex is emitted.
1744 const fs_builder abld
= bld
.annotate("end primitive");
1746 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1747 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1748 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1749 fs_reg mask
= intexp2(abld
, prev_count
);
1750 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1751 * attention to the lower 5 bits of its second source argument, so on this
1752 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1753 * ((vertex_count - 1) % 32).
1755 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1759 fs_visitor::emit_gs_control_data_bits(const fs_reg
&vertex_count
)
1761 assert(stage
== MESA_SHADER_GEOMETRY
);
1762 assert(gs_compile
->control_data_bits_per_vertex
!= 0);
1764 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1766 const fs_builder abld
= bld
.annotate("emit control data bits");
1767 const fs_builder fwa_bld
= bld
.exec_all();
1769 /* We use a single UD register to accumulate control data bits (32 bits
1770 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1773 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1774 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1775 * use the Channel Mask phase to enable/disable which DWord within that
1776 * group to write. (Remember, different SIMD8 channels may have emitted
1777 * different numbers of vertices, so we may need per-slot offsets.)
1779 * Channel masking presents an annoying problem: we may have to replicate
1780 * the data up to 4 times:
1782 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1784 * To avoid penalizing shaders that emit a small number of vertices, we
1785 * can avoid these sometimes: if the size of the control data header is
1786 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1787 * land in the same 128-bit group, so we can skip per-slot offsets.
1789 * Similarly, if the control data header is <= 32 bits, there is only one
1790 * DWord, so we can skip channel masks.
1792 enum opcode opcode
= SHADER_OPCODE_URB_WRITE_SIMD8
;
1794 fs_reg channel_mask
, per_slot_offset
;
1796 if (gs_compile
->control_data_header_size_bits
> 32) {
1797 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
1798 channel_mask
= vgrf(glsl_type::uint_type
);
1801 if (gs_compile
->control_data_header_size_bits
> 128) {
1802 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
;
1803 per_slot_offset
= vgrf(glsl_type::uint_type
);
1806 /* Figure out which DWord we're trying to write to using the formula:
1808 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1810 * Since bits_per_vertex is a power of two, and is known at compile
1811 * time, this can be optimized to:
1813 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1815 if (opcode
!= SHADER_OPCODE_URB_WRITE_SIMD8
) {
1816 fs_reg dword_index
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1817 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1818 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1819 unsigned log2_bits_per_vertex
=
1820 util_last_bit(gs_compile
->control_data_bits_per_vertex
);
1821 abld
.SHR(dword_index
, prev_count
, brw_imm_ud(6u - log2_bits_per_vertex
));
1823 if (per_slot_offset
.file
!= BAD_FILE
) {
1824 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1825 * the appropriate OWord within the control data header.
1827 abld
.SHR(per_slot_offset
, dword_index
, brw_imm_ud(2u));
1830 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1831 * write to the appropriate DWORD within the OWORD.
1833 fs_reg channel
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1834 fwa_bld
.AND(channel
, dword_index
, brw_imm_ud(3u));
1835 channel_mask
= intexp2(fwa_bld
, channel
);
1836 /* Then the channel masks need to be in bits 23:16. */
1837 fwa_bld
.SHL(channel_mask
, channel_mask
, brw_imm_ud(16u));
1840 /* Store the control data bits in the message payload and send it. */
1842 if (channel_mask
.file
!= BAD_FILE
)
1843 mlen
+= 4; /* channel masks, plus 3 extra copies of the data */
1844 if (per_slot_offset
.file
!= BAD_FILE
)
1847 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
1848 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, mlen
);
1850 sources
[i
++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1851 if (per_slot_offset
.file
!= BAD_FILE
)
1852 sources
[i
++] = per_slot_offset
;
1853 if (channel_mask
.file
!= BAD_FILE
)
1854 sources
[i
++] = channel_mask
;
1856 sources
[i
++] = this->control_data_bits
;
1859 abld
.LOAD_PAYLOAD(payload
, sources
, mlen
, mlen
);
1860 fs_inst
*inst
= abld
.emit(opcode
, reg_undef
, payload
);
1862 /* We need to increment Global Offset by 256-bits to make room for
1863 * Broadwell's extra "Vertex Count" payload at the beginning of the
1864 * URB entry. Since this is an OWord message, Global Offset is counted
1865 * in 128-bit units, so we must set it to 2.
1867 if (gs_prog_data
->static_vertex_count
== -1)
1872 fs_visitor::set_gs_stream_control_data_bits(const fs_reg
&vertex_count
,
1875 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1877 /* Note: we are calling this *before* increasing vertex_count, so
1878 * this->vertex_count == vertex_count - 1 in the formula above.
1881 /* Stream mode uses 2 bits per vertex */
1882 assert(gs_compile
->control_data_bits_per_vertex
== 2);
1884 /* Must be a valid stream */
1885 assert(stream_id
< MAX_VERTEX_STREAMS
);
1887 /* Control data bits are initialized to 0 so we don't have to set any
1888 * bits when sending vertices to stream 0.
1893 const fs_builder abld
= bld
.annotate("set stream control data bits", NULL
);
1895 /* reg::sid = stream_id */
1896 fs_reg sid
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1897 abld
.MOV(sid
, brw_imm_ud(stream_id
));
1899 /* reg:shift_count = 2 * (vertex_count - 1) */
1900 fs_reg shift_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1901 abld
.SHL(shift_count
, vertex_count
, brw_imm_ud(1u));
1903 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1904 * attention to the lower 5 bits of its second source argument, so on this
1905 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1906 * stream_id << ((2 * (vertex_count - 1)) % 32).
1908 fs_reg mask
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1909 abld
.SHL(mask
, sid
, shift_count
);
1910 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1914 fs_visitor::emit_gs_vertex(const nir_src
&vertex_count_nir_src
,
1917 assert(stage
== MESA_SHADER_GEOMETRY
);
1919 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1921 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1922 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1924 /* Haswell and later hardware ignores the "Render Stream Select" bits
1925 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1926 * and instead sends all primitives down the pipeline for rasterization.
1927 * If the SOL stage is enabled, "Render Stream Select" is honored and
1928 * primitives bound to non-zero streams are discarded after stream output.
1930 * Since the only purpose of primives sent to non-zero streams is to
1931 * be recorded by transform feedback, we can simply discard all geometry
1932 * bound to these streams when transform feedback is disabled.
1934 if (stream_id
> 0 && !nir
->info
.has_transform_feedback_varyings
)
1937 /* If we're outputting 32 control data bits or less, then we can wait
1938 * until the shader is over to output them all. Otherwise we need to
1939 * output them as we go. Now is the time to do it, since we're about to
1940 * output the vertex_count'th vertex, so it's guaranteed that the
1941 * control data bits associated with the (vertex_count - 1)th vertex are
1944 if (gs_compile
->control_data_header_size_bits
> 32) {
1945 const fs_builder abld
=
1946 bld
.annotate("emit vertex: emit control data bits");
1948 /* Only emit control data bits if we've finished accumulating a batch
1949 * of 32 bits. This is the case when:
1951 * (vertex_count * bits_per_vertex) % 32 == 0
1953 * (in other words, when the last 5 bits of vertex_count *
1954 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1955 * integer n (which is always the case, since bits_per_vertex is
1956 * always 1 or 2), this is equivalent to requiring that the last 5-n
1957 * bits of vertex_count are 0:
1959 * vertex_count & (2^(5-n) - 1) == 0
1961 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1964 * vertex_count & (32 / bits_per_vertex - 1) == 0
1966 * TODO: If vertex_count is an immediate, we could do some of this math
1967 * at compile time...
1970 abld
.AND(bld
.null_reg_d(), vertex_count
,
1971 brw_imm_ud(32u / gs_compile
->control_data_bits_per_vertex
- 1u));
1972 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1974 abld
.IF(BRW_PREDICATE_NORMAL
);
1975 /* If vertex_count is 0, then no control data bits have been
1976 * accumulated yet, so we can skip emitting them.
1978 abld
.CMP(bld
.null_reg_d(), vertex_count
, brw_imm_ud(0u),
1979 BRW_CONDITIONAL_NEQ
);
1980 abld
.IF(BRW_PREDICATE_NORMAL
);
1981 emit_gs_control_data_bits(vertex_count
);
1982 abld
.emit(BRW_OPCODE_ENDIF
);
1984 /* Reset control_data_bits to 0 so we can start accumulating a new
1987 * Note: in the case where vertex_count == 0, this neutralizes the
1988 * effect of any call to EndPrimitive() that the shader may have
1989 * made before outputting its first vertex.
1991 inst
= abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
1992 inst
->force_writemask_all
= true;
1993 abld
.emit(BRW_OPCODE_ENDIF
);
1996 emit_urb_writes(vertex_count
);
1998 /* In stream mode we have to set control data bits for all vertices
1999 * unless we have disabled control data bits completely (which we do
2000 * do for GL_POINTS outputs that don't use streams).
2002 if (gs_compile
->control_data_header_size_bits
> 0 &&
2003 gs_prog_data
->control_data_format
==
2004 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
2005 set_gs_stream_control_data_bits(vertex_count
, stream_id
);
2010 fs_visitor::emit_gs_input_load(const fs_reg
&dst
,
2011 const nir_src
&vertex_src
,
2012 unsigned base_offset
,
2013 const nir_src
&offset_src
,
2014 unsigned num_components
,
2015 unsigned first_component
)
2017 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
2018 const unsigned push_reg_count
= gs_prog_data
->base
.urb_read_length
* 8;
2020 /* TODO: figure out push input layout for invocations == 1 */
2021 /* TODO: make this work with 64-bit inputs */
2022 if (gs_prog_data
->invocations
== 1 &&
2023 type_sz(dst
.type
) <= 4 &&
2024 nir_src_is_const(offset_src
) && nir_src_is_const(vertex_src
) &&
2025 4 * (base_offset
+ nir_src_as_uint(offset_src
)) < push_reg_count
) {
2026 int imm_offset
= (base_offset
+ nir_src_as_uint(offset_src
)) * 4 +
2027 nir_src_as_uint(vertex_src
) * push_reg_count
;
2028 for (unsigned i
= 0; i
< num_components
; i
++) {
2029 bld
.MOV(offset(dst
, bld
, i
),
2030 fs_reg(ATTR
, imm_offset
+ i
+ first_component
, dst
.type
));
2035 /* Resort to the pull model. Ensure the VUE handles are provided. */
2036 assert(gs_prog_data
->base
.include_vue_handles
);
2038 unsigned first_icp_handle
= gs_prog_data
->include_primitive_id
? 3 : 2;
2039 fs_reg icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2041 if (gs_prog_data
->invocations
== 1) {
2042 if (nir_src_is_const(vertex_src
)) {
2043 /* The vertex index is constant; just select the proper URB handle. */
2045 retype(brw_vec8_grf(first_icp_handle
+ nir_src_as_uint(vertex_src
), 0),
2046 BRW_REGISTER_TYPE_UD
);
2048 /* The vertex index is non-constant. We need to use indirect
2049 * addressing to fetch the proper URB handle.
2051 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2052 * indicating that channel <n> should read the handle from
2053 * DWord <n>. We convert that to bytes by multiplying by 4.
2055 * Next, we convert the vertex index to bytes by multiplying
2056 * by 32 (shifting by 5), and add the two together. This is
2057 * the final indirect byte offset.
2059 fs_reg sequence
= bld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
2060 fs_reg channel_offsets
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2061 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2062 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2064 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2065 bld
.MOV(sequence
, fs_reg(brw_imm_v(0x76543210)));
2066 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2067 bld
.SHL(channel_offsets
, sequence
, brw_imm_ud(2u));
2068 /* Convert vertex_index to bytes (multiply by 32) */
2069 bld
.SHL(vertex_offset_bytes
,
2070 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2072 bld
.ADD(icp_offset_bytes
, vertex_offset_bytes
, channel_offsets
);
2074 /* Use first_icp_handle as the base offset. There is one register
2075 * of URB handles per vertex, so inform the register allocator that
2076 * we might read up to nir->info.gs.vertices_in registers.
2078 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2079 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
2080 fs_reg(icp_offset_bytes
),
2081 brw_imm_ud(nir
->info
.gs
.vertices_in
* REG_SIZE
));
2084 assert(gs_prog_data
->invocations
> 1);
2086 if (nir_src_is_const(vertex_src
)) {
2087 unsigned vertex
= nir_src_as_uint(vertex_src
);
2088 assert(devinfo
->gen
>= 9 || vertex
<= 5);
2090 retype(brw_vec1_grf(first_icp_handle
+ vertex
/ 8, vertex
% 8),
2091 BRW_REGISTER_TYPE_UD
));
2093 /* The vertex index is non-constant. We need to use indirect
2094 * addressing to fetch the proper URB handle.
2097 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2099 /* Convert vertex_index to bytes (multiply by 4) */
2100 bld
.SHL(icp_offset_bytes
,
2101 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2104 /* Use first_icp_handle as the base offset. There is one DWord
2105 * of URB handles per vertex, so inform the register allocator that
2106 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2108 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2109 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
2110 fs_reg(icp_offset_bytes
),
2111 brw_imm_ud(DIV_ROUND_UP(nir
->info
.gs
.vertices_in
, 8) *
2118 fs_reg tmp_dst
= dst
;
2119 fs_reg indirect_offset
= get_nir_src(offset_src
);
2120 unsigned num_iterations
= 1;
2121 unsigned orig_num_components
= num_components
;
2123 if (type_sz(dst
.type
) == 8) {
2124 if (num_components
> 2) {
2128 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(4), dst
.type
);
2130 first_component
= first_component
/ 2;
2133 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2134 if (nir_src_is_const(offset_src
)) {
2135 /* Constant indexing - use global offset. */
2136 if (first_component
!= 0) {
2137 unsigned read_components
= num_components
+ first_component
;
2138 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2139 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
, icp_handle
);
2140 inst
->size_written
= read_components
*
2141 tmp
.component_size(inst
->exec_size
);
2142 for (unsigned i
= 0; i
< num_components
; i
++) {
2143 bld
.MOV(offset(tmp_dst
, bld
, i
),
2144 offset(tmp
, bld
, i
+ first_component
));
2147 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp_dst
,
2149 inst
->size_written
= num_components
*
2150 tmp_dst
.component_size(inst
->exec_size
);
2152 inst
->offset
= base_offset
+ nir_src_as_uint(offset_src
);
2155 /* Indirect indexing - use per-slot offsets as well. */
2156 const fs_reg srcs
[] = { icp_handle
, indirect_offset
};
2157 unsigned read_components
= num_components
+ first_component
;
2158 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2159 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2160 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2161 if (first_component
!= 0) {
2162 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2164 inst
->size_written
= read_components
*
2165 tmp
.component_size(inst
->exec_size
);
2166 for (unsigned i
= 0; i
< num_components
; i
++) {
2167 bld
.MOV(offset(tmp_dst
, bld
, i
),
2168 offset(tmp
, bld
, i
+ first_component
));
2171 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp_dst
,
2173 inst
->size_written
= num_components
*
2174 tmp_dst
.component_size(inst
->exec_size
);
2176 inst
->offset
= base_offset
;
2180 if (type_sz(dst
.type
) == 8) {
2181 shuffle_from_32bit_read(bld
,
2182 offset(dst
, bld
, iter
* 2),
2183 retype(tmp_dst
, BRW_REGISTER_TYPE_D
),
2188 if (num_iterations
> 1) {
2189 num_components
= orig_num_components
- 2;
2190 if(nir_src_is_const(offset_src
)) {
2193 fs_reg new_indirect
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2194 bld
.ADD(new_indirect
, indirect_offset
, brw_imm_ud(1u));
2195 indirect_offset
= new_indirect
;
2202 fs_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
2204 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
2206 if (nir_src_is_const(*offset_src
)) {
2207 /* The only constant offset we should find is 0. brw_nir.c's
2208 * add_const_offset_to_base() will fold other constant offsets
2209 * into instr->const_index[0].
2211 assert(nir_src_as_uint(*offset_src
) == 0);
2215 return get_nir_src(*offset_src
);
2219 fs_visitor::nir_emit_vs_intrinsic(const fs_builder
&bld
,
2220 nir_intrinsic_instr
*instr
)
2222 assert(stage
== MESA_SHADER_VERTEX
);
2225 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2226 dest
= get_nir_dest(instr
->dest
);
2228 switch (instr
->intrinsic
) {
2229 case nir_intrinsic_load_vertex_id
:
2230 case nir_intrinsic_load_base_vertex
:
2231 unreachable("should be lowered by nir_lower_system_values()");
2233 case nir_intrinsic_load_input
: {
2234 fs_reg src
= fs_reg(ATTR
, nir_intrinsic_base(instr
) * 4, dest
.type
);
2235 unsigned first_component
= nir_intrinsic_component(instr
);
2236 unsigned num_components
= instr
->num_components
;
2238 src
= offset(src
, bld
, nir_src_as_uint(instr
->src
[0]));
2240 if (type_sz(dest
.type
) == 8)
2241 first_component
/= 2;
2243 /* For 16-bit support maybe a temporary will be needed to copy from
2246 shuffle_from_32bit_read(bld
, dest
, retype(src
, BRW_REGISTER_TYPE_D
),
2247 first_component
, num_components
);
2251 case nir_intrinsic_load_vertex_id_zero_base
:
2252 case nir_intrinsic_load_instance_id
:
2253 case nir_intrinsic_load_base_instance
:
2254 case nir_intrinsic_load_draw_id
:
2255 case nir_intrinsic_load_first_vertex
:
2256 case nir_intrinsic_load_is_indexed_draw
:
2257 unreachable("lowered by brw_nir_lower_vs_inputs");
2260 nir_emit_intrinsic(bld
, instr
);
2266 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder
&bld
,
2267 nir_intrinsic_instr
*instr
)
2269 assert(stage
== MESA_SHADER_TESS_CTRL
);
2270 struct brw_tcs_prog_key
*tcs_key
= (struct brw_tcs_prog_key
*) key
;
2271 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
2274 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2275 dst
= get_nir_dest(instr
->dest
);
2277 switch (instr
->intrinsic
) {
2278 case nir_intrinsic_load_primitive_id
:
2279 bld
.MOV(dst
, fs_reg(brw_vec1_grf(0, 1)));
2281 case nir_intrinsic_load_invocation_id
:
2282 bld
.MOV(retype(dst
, invocation_id
.type
), invocation_id
);
2284 case nir_intrinsic_load_patch_vertices_in
:
2285 bld
.MOV(retype(dst
, BRW_REGISTER_TYPE_D
),
2286 brw_imm_d(tcs_key
->input_vertices
));
2289 case nir_intrinsic_barrier
: {
2290 if (tcs_prog_data
->instances
== 1)
2293 fs_reg m0
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2294 fs_reg m0_2
= component(m0
, 2);
2296 const fs_builder chanbld
= bld
.exec_all().group(1, 0);
2298 /* Zero the message header */
2299 bld
.exec_all().MOV(m0
, brw_imm_ud(0u));
2301 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2302 chanbld
.AND(m0_2
, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
),
2303 brw_imm_ud(INTEL_MASK(16, 13)));
2305 /* Shift it up to bits 27:24. */
2306 chanbld
.SHL(m0_2
, m0_2
, brw_imm_ud(11));
2308 /* Set the Barrier Count and the enable bit */
2309 chanbld
.OR(m0_2
, m0_2
,
2310 brw_imm_ud(tcs_prog_data
->instances
<< 9 | (1 << 15)));
2312 bld
.emit(SHADER_OPCODE_BARRIER
, bld
.null_reg_ud(), m0
);
2316 case nir_intrinsic_load_input
:
2317 unreachable("nir_lower_io should never give us these.");
2320 case nir_intrinsic_load_per_vertex_input
: {
2321 fs_reg indirect_offset
= get_indirect_offset(instr
);
2322 unsigned imm_offset
= instr
->const_index
[0];
2324 const nir_src
&vertex_src
= instr
->src
[0];
2330 if (nir_src_is_const(vertex_src
)) {
2331 /* Emit a MOV to resolve <0,1,0> regioning. */
2332 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2333 unsigned vertex
= nir_src_as_uint(vertex_src
);
2335 retype(brw_vec1_grf(1 + (vertex
>> 3), vertex
& 7),
2336 BRW_REGISTER_TYPE_UD
));
2337 } else if (tcs_prog_data
->instances
== 1 &&
2338 vertex_src
.is_ssa
&&
2339 vertex_src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
&&
2340 nir_instr_as_intrinsic(vertex_src
.ssa
->parent_instr
)->intrinsic
== nir_intrinsic_load_invocation_id
) {
2341 /* For the common case of only 1 instance, an array index of
2342 * gl_InvocationID means reading g1. Skip all the indirect work.
2344 icp_handle
= retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
);
2346 /* The vertex index is non-constant. We need to use indirect
2347 * addressing to fetch the proper URB handle.
2349 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2351 /* Each ICP handle is a single DWord (4 bytes) */
2352 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2353 bld
.SHL(vertex_offset_bytes
,
2354 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2357 /* Start at g1. We might read up to 4 registers. */
2358 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2359 retype(brw_vec8_grf(1, 0), icp_handle
.type
), vertex_offset_bytes
,
2360 brw_imm_ud(4 * REG_SIZE
));
2363 /* We can only read two double components with each URB read, so
2364 * we send two read messages in that case, each one loading up to
2365 * two double components.
2367 unsigned num_iterations
= 1;
2368 unsigned num_components
= instr
->num_components
;
2369 unsigned first_component
= nir_intrinsic_component(instr
);
2370 fs_reg orig_dst
= dst
;
2371 if (type_sz(dst
.type
) == 8) {
2372 first_component
= first_component
/ 2;
2373 if (instr
->num_components
> 2) {
2378 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(4), dst
.type
);
2382 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2383 if (indirect_offset
.file
== BAD_FILE
) {
2384 /* Constant indexing - use global offset. */
2385 if (first_component
!= 0) {
2386 unsigned read_components
= num_components
+ first_component
;
2387 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2388 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
, icp_handle
);
2389 for (unsigned i
= 0; i
< num_components
; i
++) {
2390 bld
.MOV(offset(dst
, bld
, i
),
2391 offset(tmp
, bld
, i
+ first_component
));
2394 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
, icp_handle
);
2396 inst
->offset
= imm_offset
;
2399 /* Indirect indexing - use per-slot offsets as well. */
2400 const fs_reg srcs
[] = { icp_handle
, indirect_offset
};
2401 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2402 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2403 if (first_component
!= 0) {
2404 unsigned read_components
= num_components
+ first_component
;
2405 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2406 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2408 for (unsigned i
= 0; i
< num_components
; i
++) {
2409 bld
.MOV(offset(dst
, bld
, i
),
2410 offset(tmp
, bld
, i
+ first_component
));
2413 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
,
2416 inst
->offset
= imm_offset
;
2419 inst
->size_written
= (num_components
+ first_component
) *
2420 inst
->dst
.component_size(inst
->exec_size
);
2422 /* If we are reading 64-bit data using 32-bit read messages we need
2423 * build proper 64-bit data elements by shuffling the low and high
2424 * 32-bit components around like we do for other things like UBOs
2427 if (type_sz(dst
.type
) == 8) {
2428 shuffle_from_32bit_read(bld
,
2429 offset(orig_dst
, bld
, iter
* 2),
2430 retype(dst
, BRW_REGISTER_TYPE_D
),
2434 /* Copy the temporary to the destination to deal with writemasking.
2436 * Also attempt to deal with gl_PointSize being in the .w component.
2438 if (inst
->offset
== 0 && indirect_offset
.file
== BAD_FILE
) {
2439 assert(type_sz(dst
.type
) < 8);
2440 inst
->dst
= bld
.vgrf(dst
.type
, 4);
2441 inst
->size_written
= 4 * REG_SIZE
;
2442 bld
.MOV(dst
, offset(inst
->dst
, bld
, 3));
2445 /* If we are loading double data and we need a second read message
2446 * adjust the write offset
2448 if (num_iterations
> 1) {
2449 num_components
= instr
->num_components
- 2;
2456 case nir_intrinsic_load_output
:
2457 case nir_intrinsic_load_per_vertex_output
: {
2458 fs_reg indirect_offset
= get_indirect_offset(instr
);
2459 unsigned imm_offset
= instr
->const_index
[0];
2460 unsigned first_component
= nir_intrinsic_component(instr
);
2463 if (indirect_offset
.file
== BAD_FILE
) {
2464 /* Replicate the patch handle to all enabled channels */
2465 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2466 bld
.MOV(patch_handle
,
2467 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
));
2470 if (first_component
!= 0) {
2471 unsigned read_components
=
2472 instr
->num_components
+ first_component
;
2473 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2474 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
,
2476 inst
->size_written
= read_components
* REG_SIZE
;
2477 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2478 bld
.MOV(offset(dst
, bld
, i
),
2479 offset(tmp
, bld
, i
+ first_component
));
2482 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
,
2484 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2486 inst
->offset
= imm_offset
;
2490 /* Indirect indexing - use per-slot offsets as well. */
2491 const fs_reg srcs
[] = {
2492 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
2495 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2496 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2497 if (first_component
!= 0) {
2498 unsigned read_components
=
2499 instr
->num_components
+ first_component
;
2500 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2501 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2503 inst
->size_written
= read_components
* REG_SIZE
;
2504 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2505 bld
.MOV(offset(dst
, bld
, i
),
2506 offset(tmp
, bld
, i
+ first_component
));
2509 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
,
2511 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2513 inst
->offset
= imm_offset
;
2519 case nir_intrinsic_store_output
:
2520 case nir_intrinsic_store_per_vertex_output
: {
2521 fs_reg value
= get_nir_src(instr
->src
[0]);
2522 bool is_64bit
= (instr
->src
[0].is_ssa
?
2523 instr
->src
[0].ssa
->bit_size
: instr
->src
[0].reg
.reg
->bit_size
) == 64;
2524 fs_reg indirect_offset
= get_indirect_offset(instr
);
2525 unsigned imm_offset
= instr
->const_index
[0];
2526 unsigned mask
= instr
->const_index
[1];
2527 unsigned header_regs
= 0;
2529 srcs
[header_regs
++] = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
);
2531 if (indirect_offset
.file
!= BAD_FILE
) {
2532 srcs
[header_regs
++] = indirect_offset
;
2538 unsigned num_components
= util_last_bit(mask
);
2541 /* We can only pack two 64-bit components in a single message, so send
2542 * 2 messages if we have more components
2544 unsigned num_iterations
= 1;
2545 unsigned iter_components
= num_components
;
2546 unsigned first_component
= nir_intrinsic_component(instr
);
2548 first_component
= first_component
/ 2;
2549 if (instr
->num_components
> 2) {
2551 iter_components
= 2;
2555 mask
= mask
<< first_component
;
2557 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2558 if (!is_64bit
&& mask
!= WRITEMASK_XYZW
) {
2559 srcs
[header_regs
++] = brw_imm_ud(mask
<< 16);
2560 opcode
= indirect_offset
.file
!= BAD_FILE
?
2561 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
2562 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
2563 } else if (is_64bit
&& ((mask
& WRITEMASK_XY
) != WRITEMASK_XY
)) {
2564 /* Expand the 64-bit mask to 32-bit channels. We only handle
2565 * two channels in each iteration, so we only care about X/Y.
2567 unsigned mask32
= 0;
2568 if (mask
& WRITEMASK_X
)
2569 mask32
|= WRITEMASK_XY
;
2570 if (mask
& WRITEMASK_Y
)
2571 mask32
|= WRITEMASK_ZW
;
2573 /* If the mask does not include any of the channels X or Y there
2574 * is nothing to do in this iteration. Move on to the next couple
2575 * of 64-bit channels.
2583 srcs
[header_regs
++] = brw_imm_ud(mask32
<< 16);
2584 opcode
= indirect_offset
.file
!= BAD_FILE
?
2585 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
2586 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
2588 opcode
= indirect_offset
.file
!= BAD_FILE
?
2589 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
2590 SHADER_OPCODE_URB_WRITE_SIMD8
;
2593 for (unsigned i
= 0; i
< iter_components
; i
++) {
2594 if (!(mask
& (1 << (i
+ first_component
))))
2598 srcs
[header_regs
+ i
+ first_component
] = offset(value
, bld
, i
);
2600 /* We need to shuffle the 64-bit data to match the layout
2601 * expected by our 32-bit URB write messages. We use a temporary
2604 unsigned channel
= iter
* 2 + i
;
2605 fs_reg dest
= shuffle_for_32bit_write(bld
, value
, channel
, 1);
2607 srcs
[header_regs
+ (i
+ first_component
) * 2] = dest
;
2608 srcs
[header_regs
+ (i
+ first_component
) * 2 + 1] =
2609 offset(dest
, bld
, 1);
2614 header_regs
+ (is_64bit
? 2 * iter_components
: iter_components
) +
2615 (is_64bit
? 2 * first_component
: first_component
);
2617 bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
2618 bld
.LOAD_PAYLOAD(payload
, srcs
, mlen
, header_regs
);
2620 fs_inst
*inst
= bld
.emit(opcode
, bld
.null_reg_ud(), payload
);
2621 inst
->offset
= imm_offset
;
2624 /* If this is a 64-bit attribute, select the next two 64-bit channels
2625 * to be handled in the next iteration.
2636 nir_emit_intrinsic(bld
, instr
);
2642 fs_visitor::nir_emit_tes_intrinsic(const fs_builder
&bld
,
2643 nir_intrinsic_instr
*instr
)
2645 assert(stage
== MESA_SHADER_TESS_EVAL
);
2646 struct brw_tes_prog_data
*tes_prog_data
= brw_tes_prog_data(prog_data
);
2649 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2650 dest
= get_nir_dest(instr
->dest
);
2652 switch (instr
->intrinsic
) {
2653 case nir_intrinsic_load_primitive_id
:
2654 bld
.MOV(dest
, fs_reg(brw_vec1_grf(0, 1)));
2656 case nir_intrinsic_load_tess_coord
:
2657 /* gl_TessCoord is part of the payload in g1-3 */
2658 for (unsigned i
= 0; i
< 3; i
++) {
2659 bld
.MOV(offset(dest
, bld
, i
), fs_reg(brw_vec8_grf(1 + i
, 0)));
2663 case nir_intrinsic_load_input
:
2664 case nir_intrinsic_load_per_vertex_input
: {
2665 fs_reg indirect_offset
= get_indirect_offset(instr
);
2666 unsigned imm_offset
= instr
->const_index
[0];
2667 unsigned first_component
= nir_intrinsic_component(instr
);
2669 if (type_sz(dest
.type
) == 8) {
2670 first_component
= first_component
/ 2;
2674 if (indirect_offset
.file
== BAD_FILE
) {
2675 /* Arbitrarily only push up to 32 vec4 slots worth of data,
2676 * which is 16 registers (since each holds 2 vec4 slots).
2678 unsigned slot_count
= 1;
2679 if (type_sz(dest
.type
) == 8 && instr
->num_components
> 2)
2682 const unsigned max_push_slots
= 32;
2683 if (imm_offset
+ slot_count
<= max_push_slots
) {
2684 fs_reg src
= fs_reg(ATTR
, imm_offset
/ 2, dest
.type
);
2685 for (int i
= 0; i
< instr
->num_components
; i
++) {
2686 unsigned comp
= 16 / type_sz(dest
.type
) * (imm_offset
% 2) +
2687 i
+ first_component
;
2688 bld
.MOV(offset(dest
, bld
, i
), component(src
, comp
));
2691 tes_prog_data
->base
.urb_read_length
=
2692 MAX2(tes_prog_data
->base
.urb_read_length
,
2693 DIV_ROUND_UP(imm_offset
+ slot_count
, 2));
2695 /* Replicate the patch handle to all enabled channels */
2696 const fs_reg srcs
[] = {
2697 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)
2699 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2700 bld
.LOAD_PAYLOAD(patch_handle
, srcs
, ARRAY_SIZE(srcs
), 0);
2702 if (first_component
!= 0) {
2703 unsigned read_components
=
2704 instr
->num_components
+ first_component
;
2705 fs_reg tmp
= bld
.vgrf(dest
.type
, read_components
);
2706 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
,
2708 inst
->size_written
= read_components
* REG_SIZE
;
2709 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2710 bld
.MOV(offset(dest
, bld
, i
),
2711 offset(tmp
, bld
, i
+ first_component
));
2714 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dest
,
2716 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2719 inst
->offset
= imm_offset
;
2722 /* Indirect indexing - use per-slot offsets as well. */
2724 /* We can only read two double components with each URB read, so
2725 * we send two read messages in that case, each one loading up to
2726 * two double components.
2728 unsigned num_iterations
= 1;
2729 unsigned num_components
= instr
->num_components
;
2730 fs_reg orig_dest
= dest
;
2731 if (type_sz(dest
.type
) == 8) {
2732 if (instr
->num_components
> 2) {
2736 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(4), dest
.type
);
2740 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2741 const fs_reg srcs
[] = {
2742 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
2745 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2746 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2748 if (first_component
!= 0) {
2749 unsigned read_components
=
2750 num_components
+ first_component
;
2751 fs_reg tmp
= bld
.vgrf(dest
.type
, read_components
);
2752 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2754 for (unsigned i
= 0; i
< num_components
; i
++) {
2755 bld
.MOV(offset(dest
, bld
, i
),
2756 offset(tmp
, bld
, i
+ first_component
));
2759 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dest
,
2763 inst
->offset
= imm_offset
;
2764 inst
->size_written
= (num_components
+ first_component
) *
2765 inst
->dst
.component_size(inst
->exec_size
);
2767 /* If we are reading 64-bit data using 32-bit read messages we need
2768 * build proper 64-bit data elements by shuffling the low and high
2769 * 32-bit components around like we do for other things like UBOs
2772 if (type_sz(dest
.type
) == 8) {
2773 shuffle_from_32bit_read(bld
,
2774 offset(orig_dest
, bld
, iter
* 2),
2775 retype(dest
, BRW_REGISTER_TYPE_D
),
2779 /* If we are loading double data and we need a second read message
2782 if (num_iterations
> 1) {
2783 num_components
= instr
->num_components
- 2;
2791 nir_emit_intrinsic(bld
, instr
);
2797 fs_visitor::nir_emit_gs_intrinsic(const fs_builder
&bld
,
2798 nir_intrinsic_instr
*instr
)
2800 assert(stage
== MESA_SHADER_GEOMETRY
);
2801 fs_reg indirect_offset
;
2804 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2805 dest
= get_nir_dest(instr
->dest
);
2807 switch (instr
->intrinsic
) {
2808 case nir_intrinsic_load_primitive_id
:
2809 assert(stage
== MESA_SHADER_GEOMETRY
);
2810 assert(brw_gs_prog_data(prog_data
)->include_primitive_id
);
2811 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
2812 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD
));
2815 case nir_intrinsic_load_input
:
2816 unreachable("load_input intrinsics are invalid for the GS stage");
2818 case nir_intrinsic_load_per_vertex_input
:
2819 emit_gs_input_load(dest
, instr
->src
[0], instr
->const_index
[0],
2820 instr
->src
[1], instr
->num_components
,
2821 nir_intrinsic_component(instr
));
2824 case nir_intrinsic_emit_vertex_with_counter
:
2825 emit_gs_vertex(instr
->src
[0], instr
->const_index
[0]);
2828 case nir_intrinsic_end_primitive_with_counter
:
2829 emit_gs_end_primitive(instr
->src
[0]);
2832 case nir_intrinsic_set_vertex_count
:
2833 bld
.MOV(this->final_gs_vertex_count
, get_nir_src(instr
->src
[0]));
2836 case nir_intrinsic_load_invocation_id
: {
2837 fs_reg val
= nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
2838 assert(val
.file
!= BAD_FILE
);
2839 dest
.type
= val
.type
;
2845 nir_emit_intrinsic(bld
, instr
);
2851 * Fetch the current render target layer index.
2854 fetch_render_target_array_index(const fs_builder
&bld
)
2856 if (bld
.shader
->devinfo
->gen
>= 6) {
2857 /* The render target array index is provided in the thread payload as
2858 * bits 26:16 of r0.0.
2860 const fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
2861 bld
.AND(idx
, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE
, 0, 1),
2865 /* Pre-SNB we only ever render into the first layer of the framebuffer
2866 * since layered rendering is not implemented.
2868 return brw_imm_ud(0);
2873 * Fake non-coherent framebuffer read implemented using TXF to fetch from the
2874 * framebuffer at the current fragment coordinates and sample index.
2877 fs_visitor::emit_non_coherent_fb_read(const fs_builder
&bld
, const fs_reg
&dst
,
2880 const struct gen_device_info
*devinfo
= bld
.shader
->devinfo
;
2882 assert(bld
.shader
->stage
== MESA_SHADER_FRAGMENT
);
2883 const brw_wm_prog_key
*wm_key
=
2884 reinterpret_cast<const brw_wm_prog_key
*>(key
);
2885 assert(!wm_key
->coherent_fb_fetch
);
2886 const struct brw_wm_prog_data
*wm_prog_data
=
2887 brw_wm_prog_data(stage_prog_data
);
2889 /* Calculate the surface index relative to the start of the texture binding
2890 * table block, since that's what the texturing messages expect.
2892 const unsigned surface
= target
+
2893 wm_prog_data
->binding_table
.render_target_read_start
-
2894 wm_prog_data
->base
.binding_table
.texture_start
;
2896 brw_mark_surface_used(
2897 bld
.shader
->stage_prog_data
,
2898 wm_prog_data
->binding_table
.render_target_read_start
+ target
);
2900 /* Calculate the fragment coordinates. */
2901 const fs_reg coords
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 3);
2902 bld
.MOV(offset(coords
, bld
, 0), pixel_x
);
2903 bld
.MOV(offset(coords
, bld
, 1), pixel_y
);
2904 bld
.MOV(offset(coords
, bld
, 2), fetch_render_target_array_index(bld
));
2906 /* Calculate the sample index and MCS payload when multisampling. Luckily
2907 * the MCS fetch message behaves deterministically for UMS surfaces, so it
2908 * shouldn't be necessary to recompile based on whether the framebuffer is
2911 if (wm_key
->multisample_fbo
&&
2912 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
].file
== BAD_FILE
)
2913 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
] = *emit_sampleid_setup();
2915 const fs_reg sample
= nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
2916 const fs_reg mcs
= wm_key
->multisample_fbo
?
2917 emit_mcs_fetch(coords
, 3, brw_imm_ud(surface
)) : fs_reg();
2919 /* Use either a normal or a CMS texel fetch message depending on whether
2920 * the framebuffer is single or multisample. On SKL+ use the wide CMS
2921 * message just in case the framebuffer uses 16x multisampling, it should
2922 * be equivalent to the normal CMS fetch for lower multisampling modes.
2924 const opcode op
= !wm_key
->multisample_fbo
? SHADER_OPCODE_TXF_LOGICAL
:
2925 devinfo
->gen
>= 9 ? SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
2926 SHADER_OPCODE_TXF_CMS_LOGICAL
;
2928 /* Emit the instruction. */
2929 const fs_reg srcs
[] = { coords
, fs_reg(), brw_imm_ud(0), fs_reg(),
2930 fs_reg(), sample
, mcs
,
2931 brw_imm_ud(surface
), brw_imm_ud(0),
2932 fs_reg(), brw_imm_ud(3), brw_imm_ud(0) };
2933 STATIC_ASSERT(ARRAY_SIZE(srcs
) == TEX_LOGICAL_NUM_SRCS
);
2935 fs_inst
*inst
= bld
.emit(op
, dst
, srcs
, ARRAY_SIZE(srcs
));
2936 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
2942 * Actual coherent framebuffer read implemented using the native render target
2943 * read message. Requires SKL+.
2946 emit_coherent_fb_read(const fs_builder
&bld
, const fs_reg
&dst
, unsigned target
)
2948 assert(bld
.shader
->devinfo
->gen
>= 9);
2949 fs_inst
*inst
= bld
.emit(FS_OPCODE_FB_READ_LOGICAL
, dst
);
2950 inst
->target
= target
;
2951 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
2957 alloc_temporary(const fs_builder
&bld
, unsigned size
, fs_reg
*regs
, unsigned n
)
2959 if (n
&& regs
[0].file
!= BAD_FILE
) {
2963 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
2965 for (unsigned i
= 0; i
< n
; i
++)
2973 alloc_frag_output(fs_visitor
*v
, unsigned location
)
2975 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
2976 const brw_wm_prog_key
*const key
=
2977 reinterpret_cast<const brw_wm_prog_key
*>(v
->key
);
2978 const unsigned l
= GET_FIELD(location
, BRW_NIR_FRAG_OUTPUT_LOCATION
);
2979 const unsigned i
= GET_FIELD(location
, BRW_NIR_FRAG_OUTPUT_INDEX
);
2981 if (i
> 0 || (key
->force_dual_color_blend
&& l
== FRAG_RESULT_DATA1
))
2982 return alloc_temporary(v
->bld
, 4, &v
->dual_src_output
, 1);
2984 else if (l
== FRAG_RESULT_COLOR
)
2985 return alloc_temporary(v
->bld
, 4, v
->outputs
,
2986 MAX2(key
->nr_color_regions
, 1));
2988 else if (l
== FRAG_RESULT_DEPTH
)
2989 return alloc_temporary(v
->bld
, 1, &v
->frag_depth
, 1);
2991 else if (l
== FRAG_RESULT_STENCIL
)
2992 return alloc_temporary(v
->bld
, 1, &v
->frag_stencil
, 1);
2994 else if (l
== FRAG_RESULT_SAMPLE_MASK
)
2995 return alloc_temporary(v
->bld
, 1, &v
->sample_mask
, 1);
2997 else if (l
>= FRAG_RESULT_DATA0
&&
2998 l
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
)
2999 return alloc_temporary(v
->bld
, 4,
3000 &v
->outputs
[l
- FRAG_RESULT_DATA0
], 1);
3003 unreachable("Invalid location");
3007 fs_visitor::nir_emit_fs_intrinsic(const fs_builder
&bld
,
3008 nir_intrinsic_instr
*instr
)
3010 assert(stage
== MESA_SHADER_FRAGMENT
);
3013 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3014 dest
= get_nir_dest(instr
->dest
);
3016 switch (instr
->intrinsic
) {
3017 case nir_intrinsic_load_front_face
:
3018 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
3019 *emit_frontfacing_interpolation());
3022 case nir_intrinsic_load_sample_pos
: {
3023 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
3024 assert(sample_pos
.file
!= BAD_FILE
);
3025 dest
.type
= sample_pos
.type
;
3026 bld
.MOV(dest
, sample_pos
);
3027 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
3031 case nir_intrinsic_load_layer_id
:
3032 dest
.type
= BRW_REGISTER_TYPE_UD
;
3033 bld
.MOV(dest
, fetch_render_target_array_index(bld
));
3036 case nir_intrinsic_load_helper_invocation
:
3037 case nir_intrinsic_load_sample_mask_in
:
3038 case nir_intrinsic_load_sample_id
: {
3039 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
3040 fs_reg val
= nir_system_values
[sv
];
3041 assert(val
.file
!= BAD_FILE
);
3042 dest
.type
= val
.type
;
3047 case nir_intrinsic_store_output
: {
3048 const fs_reg src
= get_nir_src(instr
->src
[0]);
3049 const unsigned store_offset
= nir_src_as_uint(instr
->src
[1]);
3050 const unsigned location
= nir_intrinsic_base(instr
) +
3051 SET_FIELD(store_offset
, BRW_NIR_FRAG_OUTPUT_LOCATION
);
3052 const fs_reg new_dest
= retype(alloc_frag_output(this, location
),
3055 for (unsigned j
= 0; j
< instr
->num_components
; j
++)
3056 bld
.MOV(offset(new_dest
, bld
, nir_intrinsic_component(instr
) + j
),
3057 offset(src
, bld
, j
));
3062 case nir_intrinsic_load_output
: {
3063 const unsigned l
= GET_FIELD(nir_intrinsic_base(instr
),
3064 BRW_NIR_FRAG_OUTPUT_LOCATION
);
3065 assert(l
>= FRAG_RESULT_DATA0
);
3066 const unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
3067 const unsigned target
= l
- FRAG_RESULT_DATA0
+ load_offset
;
3068 const fs_reg tmp
= bld
.vgrf(dest
.type
, 4);
3070 if (reinterpret_cast<const brw_wm_prog_key
*>(key
)->coherent_fb_fetch
)
3071 emit_coherent_fb_read(bld
, tmp
, target
);
3073 emit_non_coherent_fb_read(bld
, tmp
, target
);
3075 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3076 bld
.MOV(offset(dest
, bld
, j
),
3077 offset(tmp
, bld
, nir_intrinsic_component(instr
) + j
));
3083 case nir_intrinsic_discard
:
3084 case nir_intrinsic_discard_if
: {
3085 /* We track our discarded pixels in f0.1. By predicating on it, we can
3086 * update just the flag bits that aren't yet discarded. If there's no
3087 * condition, we emit a CMP of g0 != g0, so all currently executing
3088 * channels will get turned off.
3091 if (instr
->intrinsic
== nir_intrinsic_discard_if
) {
3092 cmp
= bld
.CMP(bld
.null_reg_f(), get_nir_src(instr
->src
[0]),
3093 brw_imm_d(0), BRW_CONDITIONAL_Z
);
3095 fs_reg some_reg
= fs_reg(retype(brw_vec8_grf(0, 0),
3096 BRW_REGISTER_TYPE_UW
));
3097 cmp
= bld
.CMP(bld
.null_reg_f(), some_reg
, some_reg
, BRW_CONDITIONAL_NZ
);
3099 cmp
->predicate
= BRW_PREDICATE_NORMAL
;
3100 cmp
->flag_subreg
= 1;
3102 if (devinfo
->gen
>= 6) {
3103 emit_discard_jump();
3106 limit_dispatch_width(16, "Fragment discard not implemented in SIMD32 mode.");
3110 case nir_intrinsic_load_input
: {
3111 /* load_input is only used for flat inputs */
3112 unsigned base
= nir_intrinsic_base(instr
);
3113 unsigned comp
= nir_intrinsic_component(instr
);
3114 unsigned num_components
= instr
->num_components
;
3115 fs_reg orig_dest
= dest
;
3116 enum brw_reg_type type
= dest
.type
;
3118 /* Special case fields in the VUE header */
3119 if (base
== VARYING_SLOT_LAYER
)
3121 else if (base
== VARYING_SLOT_VIEWPORT
)
3124 if (nir_dest_bit_size(instr
->dest
) == 64) {
3125 /* const_index is in 32-bit type size units that could not be aligned
3126 * with DF. We need to read the double vector as if it was a float
3127 * vector of twice the number of components to fetch the right data.
3129 type
= BRW_REGISTER_TYPE_F
;
3130 num_components
*= 2;
3131 dest
= bld
.vgrf(type
, num_components
);
3134 for (unsigned int i
= 0; i
< num_components
; i
++) {
3135 bld
.MOV(offset(retype(dest
, type
), bld
, i
),
3136 retype(component(interp_reg(base
, comp
+ i
), 3), type
));
3139 if (nir_dest_bit_size(instr
->dest
) == 64) {
3140 shuffle_from_32bit_read(bld
, orig_dest
, dest
, 0,
3141 instr
->num_components
);
3146 case nir_intrinsic_load_barycentric_pixel
:
3147 case nir_intrinsic_load_barycentric_centroid
:
3148 case nir_intrinsic_load_barycentric_sample
:
3149 /* Do nothing - load_interpolated_input handling will handle it later. */
3152 case nir_intrinsic_load_barycentric_at_sample
: {
3153 const glsl_interp_mode interpolation
=
3154 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(instr
);
3156 if (nir_src_is_const(instr
->src
[0])) {
3157 unsigned msg_data
= nir_src_as_uint(instr
->src
[0]) << 4;
3159 emit_pixel_interpolater_send(bld
,
3160 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3163 brw_imm_ud(msg_data
),
3166 const fs_reg sample_src
= retype(get_nir_src(instr
->src
[0]),
3167 BRW_REGISTER_TYPE_UD
);
3169 if (nir_src_is_dynamically_uniform(instr
->src
[0])) {
3170 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
3171 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
3172 bld
.exec_all().group(1, 0)
3173 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
3174 emit_pixel_interpolater_send(bld
,
3175 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3181 /* Make a loop that sends a message to the pixel interpolater
3182 * for the sample number in each live channel. If there are
3183 * multiple channels with the same sample number then these
3184 * will be handled simultaneously with a single interation of
3187 bld
.emit(BRW_OPCODE_DO
);
3189 /* Get the next live sample number into sample_id_reg */
3190 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
3192 /* Set the flag register so that we can perform the send
3193 * message on all channels that have the same sample number
3195 bld
.CMP(bld
.null_reg_ud(),
3196 sample_src
, sample_id
,
3197 BRW_CONDITIONAL_EQ
);
3198 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
3199 bld
.exec_all().group(1, 0)
3200 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
3202 emit_pixel_interpolater_send(bld
,
3203 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3206 component(msg_data
, 0),
3208 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
3210 /* Continue the loop if there are any live channels left */
3211 set_predicate_inv(BRW_PREDICATE_NORMAL
,
3213 bld
.emit(BRW_OPCODE_WHILE
));
3219 case nir_intrinsic_load_barycentric_at_offset
: {
3220 const glsl_interp_mode interpolation
=
3221 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(instr
);
3223 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
3226 assert(nir_src_bit_size(instr
->src
[0]) == 32);
3227 unsigned off_x
= MIN2((int)(const_offset
->f32
[0] * 16), 7) & 0xf;
3228 unsigned off_y
= MIN2((int)(const_offset
->f32
[1] * 16), 7) & 0xf;
3230 emit_pixel_interpolater_send(bld
,
3231 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
,
3234 brw_imm_ud(off_x
| (off_y
<< 4)),
3237 fs_reg src
= vgrf(glsl_type::ivec2_type
);
3238 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
3239 BRW_REGISTER_TYPE_F
);
3240 for (int i
= 0; i
< 2; i
++) {
3241 fs_reg temp
= vgrf(glsl_type::float_type
);
3242 bld
.MUL(temp
, offset(offset_src
, bld
, i
), brw_imm_f(16.0f
));
3243 fs_reg itemp
= vgrf(glsl_type::int_type
);
3245 bld
.MOV(itemp
, temp
);
3247 /* Clamp the upper end of the range to +7/16.
3248 * ARB_gpu_shader5 requires that we support a maximum offset
3249 * of +0.5, which isn't representable in a S0.4 value -- if
3250 * we didn't clamp it, we'd end up with -8/16, which is the
3251 * opposite of what the shader author wanted.
3253 * This is legal due to ARB_gpu_shader5's quantization
3256 * "Not all values of <offset> may be supported; x and y
3257 * offsets may be rounded to fixed-point values with the
3258 * number of fraction bits given by the
3259 * implementation-dependent constant
3260 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3262 set_condmod(BRW_CONDITIONAL_L
,
3263 bld
.SEL(offset(src
, bld
, i
), itemp
, brw_imm_d(7)));
3266 const enum opcode opcode
= FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
;
3267 emit_pixel_interpolater_send(bld
,
3277 case nir_intrinsic_load_interpolated_input
: {
3278 if (nir_intrinsic_base(instr
) == VARYING_SLOT_POS
) {
3279 emit_fragcoord_interpolation(dest
);
3283 assert(instr
->src
[0].ssa
&&
3284 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_intrinsic
);
3285 nir_intrinsic_instr
*bary_intrinsic
=
3286 nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3287 nir_intrinsic_op bary_intrin
= bary_intrinsic
->intrinsic
;
3288 enum glsl_interp_mode interp_mode
=
3289 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(bary_intrinsic
);
3292 if (bary_intrin
== nir_intrinsic_load_barycentric_at_offset
||
3293 bary_intrin
== nir_intrinsic_load_barycentric_at_sample
) {
3294 /* Use the result of the PI message */
3295 dst_xy
= retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_F
);
3297 /* Use the delta_xy values computed from the payload */
3298 enum brw_barycentric_mode bary
=
3299 brw_barycentric_mode(interp_mode
, bary_intrin
);
3301 dst_xy
= this->delta_xy
[bary
];
3304 for (unsigned int i
= 0; i
< instr
->num_components
; i
++) {
3306 component(interp_reg(nir_intrinsic_base(instr
),
3307 nir_intrinsic_component(instr
) + i
), 0);
3308 interp
.type
= BRW_REGISTER_TYPE_F
;
3309 dest
.type
= BRW_REGISTER_TYPE_F
;
3311 if (devinfo
->gen
< 6 && interp_mode
== INTERP_MODE_SMOOTH
) {
3312 fs_reg tmp
= vgrf(glsl_type::float_type
);
3313 bld
.emit(FS_OPCODE_LINTERP
, tmp
, dst_xy
, interp
);
3314 bld
.MUL(offset(dest
, bld
, i
), tmp
, this->pixel_w
);
3316 bld
.emit(FS_OPCODE_LINTERP
, offset(dest
, bld
, i
), dst_xy
, interp
);
3323 nir_emit_intrinsic(bld
, instr
);
3329 get_op_for_atomic_add(nir_intrinsic_instr
*instr
, unsigned src
)
3331 if (nir_src_is_const(instr
->src
[src
])) {
3332 int64_t add_val
= nir_src_as_int(instr
->src
[src
]);
3335 else if (add_val
== -1)
3343 fs_visitor::nir_emit_cs_intrinsic(const fs_builder
&bld
,
3344 nir_intrinsic_instr
*instr
)
3346 assert(stage
== MESA_SHADER_COMPUTE
);
3347 struct brw_cs_prog_data
*cs_prog_data
= brw_cs_prog_data(prog_data
);
3350 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3351 dest
= get_nir_dest(instr
->dest
);
3353 switch (instr
->intrinsic
) {
3354 case nir_intrinsic_barrier
:
3356 cs_prog_data
->uses_barrier
= true;
3359 case nir_intrinsic_load_subgroup_id
:
3360 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
), subgroup_id
);
3363 case nir_intrinsic_load_local_invocation_id
:
3364 case nir_intrinsic_load_work_group_id
: {
3365 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
3366 fs_reg val
= nir_system_values
[sv
];
3367 assert(val
.file
!= BAD_FILE
);
3368 dest
.type
= val
.type
;
3369 for (unsigned i
= 0; i
< 3; i
++)
3370 bld
.MOV(offset(dest
, bld
, i
), offset(val
, bld
, i
));
3374 case nir_intrinsic_load_num_work_groups
: {
3375 const unsigned surface
=
3376 cs_prog_data
->binding_table
.work_groups_start
;
3378 cs_prog_data
->uses_num_work_groups
= true;
3380 fs_reg surf_index
= brw_imm_ud(surface
);
3381 brw_mark_surface_used(prog_data
, surface
);
3383 /* Read the 3 GLuint components of gl_NumWorkGroups */
3384 for (unsigned i
= 0; i
< 3; i
++) {
3385 fs_reg read_result
=
3386 emit_untyped_read(bld
, surf_index
,
3388 1 /* dims */, 1 /* size */,
3389 BRW_PREDICATE_NONE
);
3390 read_result
.type
= dest
.type
;
3391 bld
.MOV(dest
, read_result
);
3392 dest
= offset(dest
, bld
, 1);
3397 case nir_intrinsic_shared_atomic_add
:
3398 nir_emit_shared_atomic(bld
, get_op_for_atomic_add(instr
, 1), instr
);
3400 case nir_intrinsic_shared_atomic_imin
:
3401 nir_emit_shared_atomic(bld
, BRW_AOP_IMIN
, instr
);
3403 case nir_intrinsic_shared_atomic_umin
:
3404 nir_emit_shared_atomic(bld
, BRW_AOP_UMIN
, instr
);
3406 case nir_intrinsic_shared_atomic_imax
:
3407 nir_emit_shared_atomic(bld
, BRW_AOP_IMAX
, instr
);
3409 case nir_intrinsic_shared_atomic_umax
:
3410 nir_emit_shared_atomic(bld
, BRW_AOP_UMAX
, instr
);
3412 case nir_intrinsic_shared_atomic_and
:
3413 nir_emit_shared_atomic(bld
, BRW_AOP_AND
, instr
);
3415 case nir_intrinsic_shared_atomic_or
:
3416 nir_emit_shared_atomic(bld
, BRW_AOP_OR
, instr
);
3418 case nir_intrinsic_shared_atomic_xor
:
3419 nir_emit_shared_atomic(bld
, BRW_AOP_XOR
, instr
);
3421 case nir_intrinsic_shared_atomic_exchange
:
3422 nir_emit_shared_atomic(bld
, BRW_AOP_MOV
, instr
);
3424 case nir_intrinsic_shared_atomic_comp_swap
:
3425 nir_emit_shared_atomic(bld
, BRW_AOP_CMPWR
, instr
);
3427 case nir_intrinsic_shared_atomic_fmin
:
3428 nir_emit_shared_atomic_float(bld
, BRW_AOP_FMIN
, instr
);
3430 case nir_intrinsic_shared_atomic_fmax
:
3431 nir_emit_shared_atomic_float(bld
, BRW_AOP_FMAX
, instr
);
3433 case nir_intrinsic_shared_atomic_fcomp_swap
:
3434 nir_emit_shared_atomic_float(bld
, BRW_AOP_FCMPWR
, instr
);
3437 case nir_intrinsic_load_shared
: {
3438 assert(devinfo
->gen
>= 7);
3439 assert(stage
== MESA_SHADER_COMPUTE
);
3441 const unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
3442 fs_reg offset_reg
= retype(get_nir_src(instr
->src
[0]),
3443 BRW_REGISTER_TYPE_UD
);
3445 /* Make dest unsigned because that's what the temporary will be */
3446 dest
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
3448 /* Read the vector */
3449 if (nir_intrinsic_align(instr
) >= 4) {
3450 assert(nir_dest_bit_size(instr
->dest
) == 32);
3451 fs_reg read_result
= emit_untyped_read(bld
, brw_imm_ud(GEN7_BTI_SLM
),
3452 offset_reg
, 1 /* dims */,
3453 instr
->num_components
,
3454 BRW_PREDICATE_NONE
);
3455 for (unsigned i
= 0; i
< instr
->num_components
; i
++)
3456 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
3458 assert(nir_dest_bit_size(instr
->dest
) <= 32);
3459 assert(nir_dest_num_components(instr
->dest
) == 1);
3460 fs_reg read_result
=
3461 emit_byte_scattered_read(bld
, brw_imm_ud(GEN7_BTI_SLM
), offset_reg
,
3462 1 /* dims */, 1, bit_size
,
3463 BRW_PREDICATE_NONE
);
3464 bld
.MOV(dest
, read_result
);
3469 case nir_intrinsic_store_shared
: {
3470 assert(devinfo
->gen
>= 7);
3471 assert(stage
== MESA_SHADER_COMPUTE
);
3473 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
3474 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
3475 fs_reg offset_reg
= retype(get_nir_src(instr
->src
[1]),
3476 BRW_REGISTER_TYPE_UD
);
3478 val_reg
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
3480 assert(nir_intrinsic_write_mask(instr
) ==
3481 (1u << instr
->num_components
) - 1);
3482 if (nir_intrinsic_align(instr
) >= 4) {
3483 assert(nir_src_bit_size(instr
->src
[0]) == 32);
3484 assert(nir_src_num_components(instr
->src
[0]) <= 4);
3485 emit_untyped_write(bld
, brw_imm_ud(GEN7_BTI_SLM
), offset_reg
, val_reg
,
3486 1 /* dims */, instr
->num_components
,
3487 BRW_PREDICATE_NONE
);
3489 assert(nir_src_bit_size(instr
->src
[0]) <= 32);
3490 assert(nir_src_num_components(instr
->src
[0]) == 1);
3491 fs_reg write_src
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
3492 bld
.MOV(write_src
, val_reg
);
3493 emit_byte_scattered_write(bld
, brw_imm_ud(GEN7_BTI_SLM
), offset_reg
,
3494 write_src
, 1 /* dims */, bit_size
,
3495 BRW_PREDICATE_NONE
);
3501 nir_emit_intrinsic(bld
, instr
);
3507 brw_nir_reduction_op_identity(const fs_builder
&bld
,
3508 nir_op op
, brw_reg_type type
)
3510 nir_const_value value
= nir_alu_binop_identity(op
, type_sz(type
) * 8);
3511 switch (type_sz(type
)) {
3513 assert(type
!= BRW_REGISTER_TYPE_HF
);
3514 return retype(brw_imm_uw(value
.u16
[0]), type
);
3516 return retype(brw_imm_ud(value
.u32
[0]), type
);
3518 if (type
== BRW_REGISTER_TYPE_DF
)
3519 return setup_imm_df(bld
, value
.f64
[0]);
3521 return retype(brw_imm_u64(value
.u64
[0]), type
);
3523 unreachable("Invalid type size");
3528 brw_op_for_nir_reduction_op(nir_op op
)
3531 case nir_op_iadd
: return BRW_OPCODE_ADD
;
3532 case nir_op_fadd
: return BRW_OPCODE_ADD
;
3533 case nir_op_imul
: return BRW_OPCODE_MUL
;
3534 case nir_op_fmul
: return BRW_OPCODE_MUL
;
3535 case nir_op_imin
: return BRW_OPCODE_SEL
;
3536 case nir_op_umin
: return BRW_OPCODE_SEL
;
3537 case nir_op_fmin
: return BRW_OPCODE_SEL
;
3538 case nir_op_imax
: return BRW_OPCODE_SEL
;
3539 case nir_op_umax
: return BRW_OPCODE_SEL
;
3540 case nir_op_fmax
: return BRW_OPCODE_SEL
;
3541 case nir_op_iand
: return BRW_OPCODE_AND
;
3542 case nir_op_ior
: return BRW_OPCODE_OR
;
3543 case nir_op_ixor
: return BRW_OPCODE_XOR
;
3545 unreachable("Invalid reduction operation");
3549 static brw_conditional_mod
3550 brw_cond_mod_for_nir_reduction_op(nir_op op
)
3553 case nir_op_iadd
: return BRW_CONDITIONAL_NONE
;
3554 case nir_op_fadd
: return BRW_CONDITIONAL_NONE
;
3555 case nir_op_imul
: return BRW_CONDITIONAL_NONE
;
3556 case nir_op_fmul
: return BRW_CONDITIONAL_NONE
;
3557 case nir_op_imin
: return BRW_CONDITIONAL_L
;
3558 case nir_op_umin
: return BRW_CONDITIONAL_L
;
3559 case nir_op_fmin
: return BRW_CONDITIONAL_L
;
3560 case nir_op_imax
: return BRW_CONDITIONAL_GE
;
3561 case nir_op_umax
: return BRW_CONDITIONAL_GE
;
3562 case nir_op_fmax
: return BRW_CONDITIONAL_GE
;
3563 case nir_op_iand
: return BRW_CONDITIONAL_NONE
;
3564 case nir_op_ior
: return BRW_CONDITIONAL_NONE
;
3565 case nir_op_ixor
: return BRW_CONDITIONAL_NONE
;
3567 unreachable("Invalid reduction operation");
3572 fs_visitor::get_nir_image_intrinsic_image(const brw::fs_builder
&bld
,
3573 nir_intrinsic_instr
*instr
)
3575 fs_reg image
= retype(get_nir_src_imm(instr
->src
[0]), BRW_REGISTER_TYPE_UD
);
3577 if (stage_prog_data
->binding_table
.image_start
> 0) {
3578 if (image
.file
== BRW_IMMEDIATE_VALUE
) {
3579 image
.d
+= stage_prog_data
->binding_table
.image_start
;
3581 bld
.ADD(image
, image
,
3582 brw_imm_d(stage_prog_data
->binding_table
.image_start
));
3586 return bld
.emit_uniformize(image
);
3590 fs_visitor::get_nir_ssbo_intrinsic_index(const brw::fs_builder
&bld
,
3591 nir_intrinsic_instr
*instr
)
3593 /* SSBO stores are weird in that their index is in src[1] */
3594 const unsigned src
= instr
->intrinsic
== nir_intrinsic_store_ssbo
? 1 : 0;
3597 if (nir_src_is_const(instr
->src
[src
])) {
3598 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
3599 nir_src_as_uint(instr
->src
[src
]);
3600 surf_index
= brw_imm_ud(index
);
3601 brw_mark_surface_used(prog_data
, index
);
3603 surf_index
= vgrf(glsl_type::uint_type
);
3604 bld
.ADD(surf_index
, get_nir_src(instr
->src
[src
]),
3605 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
3607 /* Assume this may touch any UBO. It would be nice to provide
3608 * a tighter bound, but the array information is already lowered away.
3610 brw_mark_surface_used(prog_data
,
3611 stage_prog_data
->binding_table
.ssbo_start
+
3612 nir
->info
.num_ssbos
- 1);
3619 image_intrinsic_coord_components(nir_intrinsic_instr
*instr
)
3621 switch (nir_intrinsic_image_dim(instr
)) {
3622 case GLSL_SAMPLER_DIM_1D
:
3623 return 1 + nir_intrinsic_image_array(instr
);
3624 case GLSL_SAMPLER_DIM_2D
:
3625 case GLSL_SAMPLER_DIM_RECT
:
3626 return 2 + nir_intrinsic_image_array(instr
);
3627 case GLSL_SAMPLER_DIM_3D
:
3628 case GLSL_SAMPLER_DIM_CUBE
:
3630 case GLSL_SAMPLER_DIM_BUF
:
3632 case GLSL_SAMPLER_DIM_MS
:
3633 return 2 + nir_intrinsic_image_array(instr
);
3635 unreachable("Invalid image dimension");
3640 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
3643 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3644 dest
= get_nir_dest(instr
->dest
);
3646 switch (instr
->intrinsic
) {
3647 case nir_intrinsic_image_load
:
3648 case nir_intrinsic_image_store
:
3649 case nir_intrinsic_image_atomic_add
:
3650 case nir_intrinsic_image_atomic_min
:
3651 case nir_intrinsic_image_atomic_max
:
3652 case nir_intrinsic_image_atomic_and
:
3653 case nir_intrinsic_image_atomic_or
:
3654 case nir_intrinsic_image_atomic_xor
:
3655 case nir_intrinsic_image_atomic_exchange
:
3656 case nir_intrinsic_image_atomic_comp_swap
: {
3657 if (stage
== MESA_SHADER_FRAGMENT
&&
3658 instr
->intrinsic
!= nir_intrinsic_image_load
)
3659 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
3661 /* Get some metadata from the image intrinsic. */
3662 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
3663 const unsigned dims
= image_intrinsic_coord_components(instr
);
3664 const GLenum format
= nir_intrinsic_format(instr
);
3665 const unsigned dest_components
= nir_intrinsic_dest_components(instr
);
3667 /* Get the arguments of the image intrinsic. */
3668 const fs_reg image
= get_nir_image_intrinsic_image(bld
, instr
);
3669 const fs_reg coords
= retype(get_nir_src(instr
->src
[1]),
3670 BRW_REGISTER_TYPE_UD
);
3673 /* Emit an image load, store or atomic op. */
3674 if (instr
->intrinsic
== nir_intrinsic_image_load
) {
3675 tmp
= emit_typed_read(bld
, image
, coords
, dims
,
3676 instr
->num_components
);
3677 } else if (instr
->intrinsic
== nir_intrinsic_image_store
) {
3678 const fs_reg src0
= get_nir_src(instr
->src
[3]);
3679 emit_typed_write(bld
, image
, coords
, src0
, dims
,
3680 instr
->num_components
);
3683 unsigned num_srcs
= info
->num_srcs
;
3685 switch (instr
->intrinsic
) {
3686 case nir_intrinsic_image_atomic_add
:
3687 assert(num_srcs
== 4);
3689 op
= get_op_for_atomic_add(instr
, 3);
3691 if (op
!= BRW_AOP_ADD
)
3694 case nir_intrinsic_image_atomic_min
:
3695 assert(format
== GL_R32UI
|| format
== GL_R32I
);
3696 op
= (format
== GL_R32I
) ? BRW_AOP_IMIN
: BRW_AOP_UMIN
;
3698 case nir_intrinsic_image_atomic_max
:
3699 assert(format
== GL_R32UI
|| format
== GL_R32I
);
3700 op
= (format
== GL_R32I
) ? BRW_AOP_IMAX
: BRW_AOP_UMAX
;
3702 case nir_intrinsic_image_atomic_and
:
3705 case nir_intrinsic_image_atomic_or
:
3708 case nir_intrinsic_image_atomic_xor
:
3711 case nir_intrinsic_image_atomic_exchange
:
3714 case nir_intrinsic_image_atomic_comp_swap
:
3718 unreachable("Not reachable.");
3721 const fs_reg src0
= (num_srcs
>= 4 ?
3722 get_nir_src(instr
->src
[3]) : fs_reg());
3723 const fs_reg src1
= (num_srcs
>= 5 ?
3724 get_nir_src(instr
->src
[4]) : fs_reg());
3726 tmp
= emit_typed_atomic(bld
, image
, coords
, src0
, src1
, dims
, 1, op
);
3729 /* Assign the result. */
3730 for (unsigned c
= 0; c
< dest_components
; ++c
) {
3731 bld
.MOV(offset(retype(dest
, tmp
.type
), bld
, c
),
3732 offset(tmp
, bld
, c
));
3737 case nir_intrinsic_image_size
: {
3738 /* Unlike the [un]typed load and store opcodes, the TXS that this turns
3739 * into will handle the binding table index for us in the geneerator.
3741 fs_reg image
= retype(get_nir_src_imm(instr
->src
[0]),
3742 BRW_REGISTER_TYPE_UD
);
3743 image
= bld
.emit_uniformize(image
);
3745 /* Since the image size is always uniform, we can just emit a SIMD8
3746 * query instruction and splat the result out.
3748 const fs_builder ubld
= bld
.exec_all().group(8, 0);
3750 /* The LOD also serves as the message payload */
3751 fs_reg lod
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
3752 ubld
.MOV(lod
, brw_imm_ud(0));
3754 fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 4);
3755 fs_inst
*inst
= ubld
.emit(SHADER_OPCODE_IMAGE_SIZE
, tmp
, lod
, image
);
3757 inst
->size_written
= 4 * REG_SIZE
;
3759 for (unsigned c
= 0; c
< instr
->dest
.ssa
.num_components
; ++c
) {
3760 if (c
== 2 && nir_intrinsic_image_dim(instr
) == GLSL_SAMPLER_DIM_CUBE
) {
3761 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
,
3762 offset(retype(dest
, tmp
.type
), bld
, c
),
3763 component(offset(tmp
, ubld
, c
), 0), brw_imm_ud(6));
3765 bld
.MOV(offset(retype(dest
, tmp
.type
), bld
, c
),
3766 component(offset(tmp
, ubld
, c
), 0));
3772 case nir_intrinsic_image_load_raw_intel
: {
3773 const fs_reg image
= get_nir_image_intrinsic_image(bld
, instr
);
3774 const fs_reg addr
= retype(get_nir_src(instr
->src
[1]),
3775 BRW_REGISTER_TYPE_UD
);
3777 fs_reg tmp
= emit_untyped_read(bld
, image
, addr
, 1,
3778 instr
->num_components
);
3780 for (unsigned c
= 0; c
< instr
->num_components
; ++c
) {
3781 bld
.MOV(offset(retype(dest
, tmp
.type
), bld
, c
),
3782 offset(tmp
, bld
, c
));
3787 case nir_intrinsic_image_store_raw_intel
: {
3788 const fs_reg image
= get_nir_image_intrinsic_image(bld
, instr
);
3789 const fs_reg addr
= retype(get_nir_src(instr
->src
[1]),
3790 BRW_REGISTER_TYPE_UD
);
3791 const fs_reg data
= retype(get_nir_src(instr
->src
[2]),
3792 BRW_REGISTER_TYPE_UD
);
3794 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
3796 emit_untyped_write(bld
, image
, addr
, data
, 1,
3797 instr
->num_components
);
3801 case nir_intrinsic_group_memory_barrier
:
3802 case nir_intrinsic_memory_barrier_shared
:
3803 case nir_intrinsic_memory_barrier_atomic_counter
:
3804 case nir_intrinsic_memory_barrier_buffer
:
3805 case nir_intrinsic_memory_barrier_image
:
3806 case nir_intrinsic_memory_barrier
: {
3807 const fs_builder ubld
= bld
.group(8, 0);
3808 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
3809 ubld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
3810 ->size_written
= 2 * REG_SIZE
;
3814 case nir_intrinsic_shader_clock
: {
3815 /* We cannot do anything if there is an event, so ignore it for now */
3816 const fs_reg shader_clock
= get_timestamp(bld
);
3817 const fs_reg srcs
[] = { component(shader_clock
, 0),
3818 component(shader_clock
, 1) };
3819 bld
.LOAD_PAYLOAD(dest
, srcs
, ARRAY_SIZE(srcs
), 0);
3823 case nir_intrinsic_image_samples
:
3824 /* The driver does not support multi-sampled images. */
3825 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(1));
3828 case nir_intrinsic_load_uniform
: {
3829 /* Offsets are in bytes but they should always aligned to
3832 assert(instr
->const_index
[0] % 4 == 0 ||
3833 instr
->const_index
[0] % type_sz(dest
.type
) == 0);
3835 fs_reg
src(UNIFORM
, instr
->const_index
[0] / 4, dest
.type
);
3837 if (nir_src_is_const(instr
->src
[0])) {
3838 unsigned load_offset
= nir_src_as_uint(instr
->src
[0]);
3839 assert(load_offset
% type_sz(dest
.type
) == 0);
3840 /* For 16-bit types we add the module of the const_index[0]
3841 * offset to access to not 32-bit aligned element
3843 src
.offset
= load_offset
+ instr
->const_index
[0] % 4;
3845 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3846 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
3849 fs_reg indirect
= retype(get_nir_src(instr
->src
[0]),
3850 BRW_REGISTER_TYPE_UD
);
3852 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
3853 * go past the end of the uniform. In order to keep the n'th
3854 * component from running past, we subtract off the size of all but
3855 * one component of the vector.
3857 assert(instr
->const_index
[1] >=
3858 instr
->num_components
* (int) type_sz(dest
.type
));
3859 unsigned read_size
= instr
->const_index
[1] -
3860 (instr
->num_components
- 1) * type_sz(dest
.type
);
3862 bool supports_64bit_indirects
=
3863 !devinfo
->is_cherryview
&& !gen_device_info_is_9lp(devinfo
);
3865 if (type_sz(dest
.type
) != 8 || supports_64bit_indirects
) {
3866 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3867 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
3868 offset(dest
, bld
, j
), offset(src
, bld
, j
),
3869 indirect
, brw_imm_ud(read_size
));
3872 const unsigned num_mov_indirects
=
3873 type_sz(dest
.type
) / type_sz(BRW_REGISTER_TYPE_UD
);
3874 /* We read a little bit less per MOV INDIRECT, as they are now
3875 * 32-bits ones instead of 64-bit. Fix read_size then.
3877 const unsigned read_size_32bit
= read_size
-
3878 (num_mov_indirects
- 1) * type_sz(BRW_REGISTER_TYPE_UD
);
3879 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3880 for (unsigned i
= 0; i
< num_mov_indirects
; i
++) {
3881 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
3882 subscript(offset(dest
, bld
, j
), BRW_REGISTER_TYPE_UD
, i
),
3883 subscript(offset(src
, bld
, j
), BRW_REGISTER_TYPE_UD
, i
),
3884 indirect
, brw_imm_ud(read_size_32bit
));
3892 case nir_intrinsic_load_ubo
: {
3894 if (nir_src_is_const(instr
->src
[0])) {
3895 const unsigned index
= stage_prog_data
->binding_table
.ubo_start
+
3896 nir_src_as_uint(instr
->src
[0]);
3897 surf_index
= brw_imm_ud(index
);
3898 brw_mark_surface_used(prog_data
, index
);
3900 /* The block index is not a constant. Evaluate the index expression
3901 * per-channel and add the base UBO index; we have to select a value
3902 * from any live channel.
3904 surf_index
= vgrf(glsl_type::uint_type
);
3905 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
3906 brw_imm_ud(stage_prog_data
->binding_table
.ubo_start
));
3907 surf_index
= bld
.emit_uniformize(surf_index
);
3909 /* Assume this may touch any UBO. It would be nice to provide
3910 * a tighter bound, but the array information is already lowered away.
3912 brw_mark_surface_used(prog_data
,
3913 stage_prog_data
->binding_table
.ubo_start
+
3914 nir
->info
.num_ubos
- 1);
3917 if (!nir_src_is_const(instr
->src
[1])) {
3918 fs_reg base_offset
= retype(get_nir_src(instr
->src
[1]),
3919 BRW_REGISTER_TYPE_UD
);
3921 for (int i
= 0; i
< instr
->num_components
; i
++)
3922 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
3923 base_offset
, i
* type_sz(dest
.type
));
3925 /* Even if we are loading doubles, a pull constant load will load
3926 * a 32-bit vec4, so should only reserve vgrf space for that. If we
3927 * need to load a full dvec4 we will have to emit 2 loads. This is
3928 * similar to demote_pull_constants(), except that in that case we
3929 * see individual accesses to each component of the vector and then
3930 * we let CSE deal with duplicate loads. Here we see a vector access
3931 * and we have to split it if necessary.
3933 const unsigned type_size
= type_sz(dest
.type
);
3934 const unsigned load_offset
= nir_src_as_uint(instr
->src
[1]);
3936 /* See if we've selected this as a push constant candidate */
3937 if (nir_src_is_const(instr
->src
[0])) {
3938 const unsigned ubo_block
= nir_src_as_uint(instr
->src
[0]);
3939 const unsigned offset_256b
= load_offset
/ 32;
3942 for (int i
= 0; i
< 4; i
++) {
3943 const struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
3944 if (range
->block
== ubo_block
&&
3945 offset_256b
>= range
->start
&&
3946 offset_256b
< range
->start
+ range
->length
) {
3948 push_reg
= fs_reg(UNIFORM
, UBO_START
+ i
, dest
.type
);
3949 push_reg
.offset
= load_offset
- 32 * range
->start
;
3954 if (push_reg
.file
!= BAD_FILE
) {
3955 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
3956 bld
.MOV(offset(dest
, bld
, i
),
3957 byte_offset(push_reg
, i
* type_size
));
3963 const unsigned block_sz
= 64; /* Fetch one cacheline at a time. */
3964 const fs_builder ubld
= bld
.exec_all().group(block_sz
/ 4, 0);
3965 const fs_reg packed_consts
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
3967 for (unsigned c
= 0; c
< instr
->num_components
;) {
3968 const unsigned base
= load_offset
+ c
* type_size
;
3969 /* Number of usable components in the next block-aligned load. */
3970 const unsigned count
= MIN2(instr
->num_components
- c
,
3971 (block_sz
- base
% block_sz
) / type_size
);
3973 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
3974 packed_consts
, surf_index
,
3975 brw_imm_ud(base
& ~(block_sz
- 1)));
3977 const fs_reg consts
=
3978 retype(byte_offset(packed_consts
, base
& (block_sz
- 1)),
3981 for (unsigned d
= 0; d
< count
; d
++)
3982 bld
.MOV(offset(dest
, bld
, c
+ d
), component(consts
, d
));
3990 case nir_intrinsic_load_ssbo
: {
3991 assert(devinfo
->gen
>= 7);
3993 const unsigned bit_size
= nir_dest_bit_size(instr
->dest
);
3994 fs_reg surf_index
= get_nir_ssbo_intrinsic_index(bld
, instr
);
3995 fs_reg offset_reg
= retype(get_nir_src(instr
->src
[1]),
3996 BRW_REGISTER_TYPE_UD
);
3998 /* Make dest unsigned because that's what the temporary will be */
3999 dest
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4001 /* Read the vector */
4002 if (nir_intrinsic_align(instr
) >= 4) {
4003 assert(nir_dest_bit_size(instr
->dest
) == 32);
4004 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
4006 instr
->num_components
,
4007 BRW_PREDICATE_NONE
);
4008 for (unsigned i
= 0; i
< instr
->num_components
; i
++)
4009 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
4011 assert(nir_dest_bit_size(instr
->dest
) <= 32);
4012 assert(nir_dest_num_components(instr
->dest
) == 1);
4013 fs_reg read_result
=
4014 emit_byte_scattered_read(bld
, surf_index
, offset_reg
,
4015 1 /* dims */, 1, bit_size
,
4016 BRW_PREDICATE_NONE
);
4017 bld
.MOV(dest
, read_result
);
4022 case nir_intrinsic_store_ssbo
: {
4023 assert(devinfo
->gen
>= 7);
4025 if (stage
== MESA_SHADER_FRAGMENT
)
4026 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4028 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
4029 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
4030 fs_reg surf_index
= get_nir_ssbo_intrinsic_index(bld
, instr
);
4031 fs_reg offset_reg
= retype(get_nir_src(instr
->src
[2]),
4032 BRW_REGISTER_TYPE_UD
);
4034 val_reg
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_UD
);
4036 assert(nir_intrinsic_write_mask(instr
) ==
4037 (1u << instr
->num_components
) - 1);
4038 if (nir_intrinsic_align(instr
) >= 4) {
4039 assert(nir_src_bit_size(instr
->src
[0]) == 32);
4040 assert(nir_src_num_components(instr
->src
[0]) <= 4);
4041 emit_untyped_write(bld
, surf_index
, offset_reg
, val_reg
,
4042 1 /* dims */, instr
->num_components
,
4043 BRW_PREDICATE_NONE
);
4045 assert(nir_src_bit_size(instr
->src
[0]) <= 32);
4046 assert(nir_src_num_components(instr
->src
[0]) == 1);
4047 fs_reg write_src
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4048 bld
.MOV(write_src
, val_reg
);
4049 emit_byte_scattered_write(bld
, surf_index
, offset_reg
,
4050 write_src
, 1 /* dims */, bit_size
,
4051 BRW_PREDICATE_NONE
);
4056 case nir_intrinsic_store_output
: {
4057 fs_reg src
= get_nir_src(instr
->src
[0]);
4059 unsigned store_offset
= nir_src_as_uint(instr
->src
[1]);
4060 unsigned num_components
= instr
->num_components
;
4061 unsigned first_component
= nir_intrinsic_component(instr
);
4062 if (nir_src_bit_size(instr
->src
[0]) == 64) {
4063 src
= shuffle_for_32bit_write(bld
, src
, 0, num_components
);
4064 num_components
*= 2;
4067 fs_reg new_dest
= retype(offset(outputs
[instr
->const_index
[0]], bld
,
4068 4 * store_offset
), src
.type
);
4069 for (unsigned j
= 0; j
< num_components
; j
++) {
4070 bld
.MOV(offset(new_dest
, bld
, j
+ first_component
),
4071 offset(src
, bld
, j
));
4076 case nir_intrinsic_ssbo_atomic_add
:
4077 nir_emit_ssbo_atomic(bld
, get_op_for_atomic_add(instr
, 2), instr
);
4079 case nir_intrinsic_ssbo_atomic_imin
:
4080 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMIN
, instr
);
4082 case nir_intrinsic_ssbo_atomic_umin
:
4083 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMIN
, instr
);
4085 case nir_intrinsic_ssbo_atomic_imax
:
4086 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMAX
, instr
);
4088 case nir_intrinsic_ssbo_atomic_umax
:
4089 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMAX
, instr
);
4091 case nir_intrinsic_ssbo_atomic_and
:
4092 nir_emit_ssbo_atomic(bld
, BRW_AOP_AND
, instr
);
4094 case nir_intrinsic_ssbo_atomic_or
:
4095 nir_emit_ssbo_atomic(bld
, BRW_AOP_OR
, instr
);
4097 case nir_intrinsic_ssbo_atomic_xor
:
4098 nir_emit_ssbo_atomic(bld
, BRW_AOP_XOR
, instr
);
4100 case nir_intrinsic_ssbo_atomic_exchange
:
4101 nir_emit_ssbo_atomic(bld
, BRW_AOP_MOV
, instr
);
4103 case nir_intrinsic_ssbo_atomic_comp_swap
:
4104 nir_emit_ssbo_atomic(bld
, BRW_AOP_CMPWR
, instr
);
4106 case nir_intrinsic_ssbo_atomic_fmin
:
4107 nir_emit_ssbo_atomic_float(bld
, BRW_AOP_FMIN
, instr
);
4109 case nir_intrinsic_ssbo_atomic_fmax
:
4110 nir_emit_ssbo_atomic_float(bld
, BRW_AOP_FMAX
, instr
);
4112 case nir_intrinsic_ssbo_atomic_fcomp_swap
:
4113 nir_emit_ssbo_atomic_float(bld
, BRW_AOP_FCMPWR
, instr
);
4116 case nir_intrinsic_get_buffer_size
: {
4117 unsigned ssbo_index
= nir_src_is_const(instr
->src
[0]) ?
4118 nir_src_as_uint(instr
->src
[0]) : 0;
4120 /* A resinfo's sampler message is used to get the buffer size. The
4121 * SIMD8's writeback message consists of four registers and SIMD16's
4122 * writeback message consists of 8 destination registers (two per each
4123 * component). Because we are only interested on the first channel of
4124 * the first returned component, where resinfo returns the buffer size
4125 * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
4126 * the dispatch width.
4128 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4129 fs_reg src_payload
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4130 fs_reg ret_payload
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 4);
4133 ubld
.MOV(src_payload
, brw_imm_d(0));
4135 const unsigned index
= prog_data
->binding_table
.ssbo_start
+ ssbo_index
;
4136 fs_inst
*inst
= ubld
.emit(SHADER_OPCODE_GET_BUFFER_SIZE
, ret_payload
,
4137 src_payload
, brw_imm_ud(index
));
4138 inst
->header_size
= 0;
4140 inst
->size_written
= 4 * REG_SIZE
;
4142 /* SKL PRM, vol07, 3D Media GPGPU Engine, Bounds Checking and Faulting:
4144 * "Out-of-bounds checking is always performed at a DWord granularity. If
4145 * any part of the DWord is out-of-bounds then the whole DWord is
4146 * considered out-of-bounds."
4148 * This implies that types with size smaller than 4-bytes need to be
4149 * padded if they don't complete the last dword of the buffer. But as we
4150 * need to maintain the original size we need to reverse the padding
4151 * calculation to return the correct size to know the number of elements
4152 * of an unsized array. As we stored in the last two bits of the surface
4153 * size the needed padding for the buffer, we calculate here the
4154 * original buffer_size reversing the surface_size calculation:
4156 * surface_size = isl_align(buffer_size, 4) +
4157 * (isl_align(buffer_size) - buffer_size)
4159 * buffer_size = surface_size & ~3 - surface_size & 3
4162 fs_reg size_aligned4
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4163 fs_reg size_padding
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4164 fs_reg buffer_size
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4166 ubld
.AND(size_padding
, ret_payload
, brw_imm_ud(3));
4167 ubld
.AND(size_aligned4
, ret_payload
, brw_imm_ud(~3));
4168 ubld
.ADD(buffer_size
, size_aligned4
, negate(size_padding
));
4170 bld
.MOV(retype(dest
, ret_payload
.type
), component(buffer_size
, 0));
4172 brw_mark_surface_used(prog_data
, index
);
4176 case nir_intrinsic_load_subgroup_invocation
:
4177 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
4178 nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
]);
4181 case nir_intrinsic_load_subgroup_eq_mask
:
4182 case nir_intrinsic_load_subgroup_ge_mask
:
4183 case nir_intrinsic_load_subgroup_gt_mask
:
4184 case nir_intrinsic_load_subgroup_le_mask
:
4185 case nir_intrinsic_load_subgroup_lt_mask
:
4186 unreachable("not reached");
4188 case nir_intrinsic_vote_any
: {
4189 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4191 /* The any/all predicates do not consider channel enables. To prevent
4192 * dead channels from affecting the result, we initialize the flag with
4193 * with the identity value for the logical operation.
4195 if (dispatch_width
== 32) {
4196 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4197 ubld
.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD
),
4200 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0));
4202 bld
.CMP(bld
.null_reg_d(), get_nir_src(instr
->src
[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ
);
4204 /* For some reason, the any/all predicates don't work properly with
4205 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4206 * doesn't read the correct subset of the flag register and you end up
4207 * getting garbage in the second half. Work around this by using a pair
4208 * of 1-wide MOVs and scattering the result.
4210 fs_reg res1
= ubld
.vgrf(BRW_REGISTER_TYPE_D
);
4211 ubld
.MOV(res1
, brw_imm_d(0));
4212 set_predicate(dispatch_width
== 8 ? BRW_PREDICATE_ALIGN1_ANY8H
:
4213 dispatch_width
== 16 ? BRW_PREDICATE_ALIGN1_ANY16H
:
4214 BRW_PREDICATE_ALIGN1_ANY32H
,
4215 ubld
.MOV(res1
, brw_imm_d(-1)));
4217 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), component(res1
, 0));
4220 case nir_intrinsic_vote_all
: {
4221 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4223 /* The any/all predicates do not consider channel enables. To prevent
4224 * dead channels from affecting the result, we initialize the flag with
4225 * with the identity value for the logical operation.
4227 if (dispatch_width
== 32) {
4228 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4229 ubld
.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD
),
4230 brw_imm_ud(0xffffffff));
4232 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4234 bld
.CMP(bld
.null_reg_d(), get_nir_src(instr
->src
[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ
);
4236 /* For some reason, the any/all predicates don't work properly with
4237 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4238 * doesn't read the correct subset of the flag register and you end up
4239 * getting garbage in the second half. Work around this by using a pair
4240 * of 1-wide MOVs and scattering the result.
4242 fs_reg res1
= ubld
.vgrf(BRW_REGISTER_TYPE_D
);
4243 ubld
.MOV(res1
, brw_imm_d(0));
4244 set_predicate(dispatch_width
== 8 ? BRW_PREDICATE_ALIGN1_ALL8H
:
4245 dispatch_width
== 16 ? BRW_PREDICATE_ALIGN1_ALL16H
:
4246 BRW_PREDICATE_ALIGN1_ALL32H
,
4247 ubld
.MOV(res1
, brw_imm_d(-1)));
4249 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), component(res1
, 0));
4252 case nir_intrinsic_vote_feq
:
4253 case nir_intrinsic_vote_ieq
: {
4254 fs_reg value
= get_nir_src(instr
->src
[0]);
4255 if (instr
->intrinsic
== nir_intrinsic_vote_feq
) {
4256 const unsigned bit_size
= nir_src_bit_size(instr
->src
[0]);
4257 value
.type
= brw_reg_type_from_bit_size(bit_size
, BRW_REGISTER_TYPE_F
);
4260 fs_reg uniformized
= bld
.emit_uniformize(value
);
4261 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4263 /* The any/all predicates do not consider channel enables. To prevent
4264 * dead channels from affecting the result, we initialize the flag with
4265 * with the identity value for the logical operation.
4267 if (dispatch_width
== 32) {
4268 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4269 ubld
.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD
),
4270 brw_imm_ud(0xffffffff));
4272 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4274 bld
.CMP(bld
.null_reg_d(), value
, uniformized
, BRW_CONDITIONAL_Z
);
4276 /* For some reason, the any/all predicates don't work properly with
4277 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4278 * doesn't read the correct subset of the flag register and you end up
4279 * getting garbage in the second half. Work around this by using a pair
4280 * of 1-wide MOVs and scattering the result.
4282 fs_reg res1
= ubld
.vgrf(BRW_REGISTER_TYPE_D
);
4283 ubld
.MOV(res1
, brw_imm_d(0));
4284 set_predicate(dispatch_width
== 8 ? BRW_PREDICATE_ALIGN1_ALL8H
:
4285 dispatch_width
== 16 ? BRW_PREDICATE_ALIGN1_ALL16H
:
4286 BRW_PREDICATE_ALIGN1_ALL32H
,
4287 ubld
.MOV(res1
, brw_imm_d(-1)));
4289 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), component(res1
, 0));
4293 case nir_intrinsic_ballot
: {
4294 const fs_reg value
= retype(get_nir_src(instr
->src
[0]),
4295 BRW_REGISTER_TYPE_UD
);
4296 struct brw_reg flag
= brw_flag_reg(0, 0);
4297 /* FIXME: For SIMD32 programs, this causes us to stomp on f0.1 as well
4298 * as f0.0. This is a problem for fragment programs as we currently use
4299 * f0.1 for discards. Fortunately, we don't support SIMD32 fragment
4300 * programs yet so this isn't a problem. When we do, something will
4303 if (dispatch_width
== 32)
4304 flag
.type
= BRW_REGISTER_TYPE_UD
;
4306 bld
.exec_all().group(1, 0).MOV(flag
, brw_imm_ud(0u));
4307 bld
.CMP(bld
.null_reg_ud(), value
, brw_imm_ud(0u), BRW_CONDITIONAL_NZ
);
4309 if (instr
->dest
.ssa
.bit_size
> 32) {
4310 dest
.type
= BRW_REGISTER_TYPE_UQ
;
4312 dest
.type
= BRW_REGISTER_TYPE_UD
;
4314 bld
.MOV(dest
, flag
);
4318 case nir_intrinsic_read_invocation
: {
4319 const fs_reg value
= get_nir_src(instr
->src
[0]);
4320 const fs_reg invocation
= get_nir_src(instr
->src
[1]);
4321 fs_reg tmp
= bld
.vgrf(value
.type
);
4323 bld
.exec_all().emit(SHADER_OPCODE_BROADCAST
, tmp
, value
,
4324 bld
.emit_uniformize(invocation
));
4326 bld
.MOV(retype(dest
, value
.type
), fs_reg(component(tmp
, 0)));
4330 case nir_intrinsic_read_first_invocation
: {
4331 const fs_reg value
= get_nir_src(instr
->src
[0]);
4332 bld
.MOV(retype(dest
, value
.type
), bld
.emit_uniformize(value
));
4336 case nir_intrinsic_shuffle
: {
4337 const fs_reg value
= get_nir_src(instr
->src
[0]);
4338 const fs_reg index
= get_nir_src(instr
->src
[1]);
4340 bld
.emit(SHADER_OPCODE_SHUFFLE
, retype(dest
, value
.type
), value
, index
);
4344 case nir_intrinsic_first_invocation
: {
4345 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
4346 bld
.exec_all().emit(SHADER_OPCODE_FIND_LIVE_CHANNEL
, tmp
);
4347 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
4348 fs_reg(component(tmp
, 0)));
4352 case nir_intrinsic_quad_broadcast
: {
4353 const fs_reg value
= get_nir_src(instr
->src
[0]);
4354 const unsigned index
= nir_src_as_uint(instr
->src
[1]);
4356 bld
.emit(SHADER_OPCODE_CLUSTER_BROADCAST
, retype(dest
, value
.type
),
4357 value
, brw_imm_ud(index
), brw_imm_ud(4));
4361 case nir_intrinsic_quad_swap_horizontal
: {
4362 const fs_reg value
= get_nir_src(instr
->src
[0]);
4363 const fs_reg tmp
= bld
.vgrf(value
.type
);
4364 const fs_builder ubld
= bld
.exec_all().group(dispatch_width
/ 2, 0);
4366 const fs_reg src_left
= horiz_stride(value
, 2);
4367 const fs_reg src_right
= horiz_stride(horiz_offset(value
, 1), 2);
4368 const fs_reg tmp_left
= horiz_stride(tmp
, 2);
4369 const fs_reg tmp_right
= horiz_stride(horiz_offset(tmp
, 1), 2);
4371 ubld
.MOV(tmp_left
, src_right
);
4372 ubld
.MOV(tmp_right
, src_left
);
4374 bld
.MOV(retype(dest
, value
.type
), tmp
);
4378 case nir_intrinsic_quad_swap_vertical
: {
4379 const fs_reg value
= get_nir_src(instr
->src
[0]);
4380 if (nir_src_bit_size(instr
->src
[0]) == 32) {
4381 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
4382 const fs_reg tmp
= bld
.vgrf(value
.type
);
4383 const fs_builder ubld
= bld
.exec_all();
4384 ubld
.emit(SHADER_OPCODE_QUAD_SWIZZLE
, tmp
, value
,
4385 brw_imm_ud(BRW_SWIZZLE4(2,3,0,1)));
4386 bld
.MOV(retype(dest
, value
.type
), tmp
);
4388 /* For larger data types, we have to either emit dispatch_width many
4389 * MOVs or else fall back to doing indirects.
4391 fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
4392 bld
.XOR(idx
, nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
],
4394 bld
.emit(SHADER_OPCODE_SHUFFLE
, retype(dest
, value
.type
), value
, idx
);
4399 case nir_intrinsic_quad_swap_diagonal
: {
4400 const fs_reg value
= get_nir_src(instr
->src
[0]);
4401 if (nir_src_bit_size(instr
->src
[0]) == 32) {
4402 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
4403 const fs_reg tmp
= bld
.vgrf(value
.type
);
4404 const fs_builder ubld
= bld
.exec_all();
4405 ubld
.emit(SHADER_OPCODE_QUAD_SWIZZLE
, tmp
, value
,
4406 brw_imm_ud(BRW_SWIZZLE4(3,2,1,0)));
4407 bld
.MOV(retype(dest
, value
.type
), tmp
);
4409 /* For larger data types, we have to either emit dispatch_width many
4410 * MOVs or else fall back to doing indirects.
4412 fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
4413 bld
.XOR(idx
, nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
],
4415 bld
.emit(SHADER_OPCODE_SHUFFLE
, retype(dest
, value
.type
), value
, idx
);
4420 case nir_intrinsic_reduce
: {
4421 fs_reg src
= get_nir_src(instr
->src
[0]);
4422 nir_op redop
= (nir_op
)nir_intrinsic_reduction_op(instr
);
4423 unsigned cluster_size
= nir_intrinsic_cluster_size(instr
);
4424 if (cluster_size
== 0 || cluster_size
> dispatch_width
)
4425 cluster_size
= dispatch_width
;
4427 /* Figure out the source type */
4428 src
.type
= brw_type_for_nir_type(devinfo
,
4429 (nir_alu_type
)(nir_op_infos
[redop
].input_types
[0] |
4430 nir_src_bit_size(instr
->src
[0])));
4432 fs_reg identity
= brw_nir_reduction_op_identity(bld
, redop
, src
.type
);
4433 opcode brw_op
= brw_op_for_nir_reduction_op(redop
);
4434 brw_conditional_mod cond_mod
= brw_cond_mod_for_nir_reduction_op(redop
);
4436 /* Set up a register for all of our scratching around and initialize it
4437 * to reduction operation's identity value.
4439 fs_reg scan
= bld
.vgrf(src
.type
);
4440 bld
.exec_all().emit(SHADER_OPCODE_SEL_EXEC
, scan
, src
, identity
);
4442 bld
.emit_scan(brw_op
, scan
, cluster_size
, cond_mod
);
4444 dest
.type
= src
.type
;
4445 if (cluster_size
* type_sz(src
.type
) >= REG_SIZE
* 2) {
4446 /* In this case, CLUSTER_BROADCAST instruction isn't needed because
4447 * the distance between clusters is at least 2 GRFs. In this case,
4448 * we don't need the weird striding of the CLUSTER_BROADCAST
4449 * instruction and can just do regular MOVs.
4451 assert((cluster_size
* type_sz(src
.type
)) % (REG_SIZE
* 2) == 0);
4452 const unsigned groups
=
4453 (dispatch_width
* type_sz(src
.type
)) / (REG_SIZE
* 2);
4454 const unsigned group_size
= dispatch_width
/ groups
;
4455 for (unsigned i
= 0; i
< groups
; i
++) {
4456 const unsigned cluster
= (i
* group_size
) / cluster_size
;
4457 const unsigned comp
= cluster
* cluster_size
+ (cluster_size
- 1);
4458 bld
.group(group_size
, i
).MOV(horiz_offset(dest
, i
* group_size
),
4459 component(scan
, comp
));
4462 bld
.emit(SHADER_OPCODE_CLUSTER_BROADCAST
, dest
, scan
,
4463 brw_imm_ud(cluster_size
- 1), brw_imm_ud(cluster_size
));
4468 case nir_intrinsic_inclusive_scan
:
4469 case nir_intrinsic_exclusive_scan
: {
4470 fs_reg src
= get_nir_src(instr
->src
[0]);
4471 nir_op redop
= (nir_op
)nir_intrinsic_reduction_op(instr
);
4473 /* Figure out the source type */
4474 src
.type
= brw_type_for_nir_type(devinfo
,
4475 (nir_alu_type
)(nir_op_infos
[redop
].input_types
[0] |
4476 nir_src_bit_size(instr
->src
[0])));
4478 fs_reg identity
= brw_nir_reduction_op_identity(bld
, redop
, src
.type
);
4479 opcode brw_op
= brw_op_for_nir_reduction_op(redop
);
4480 brw_conditional_mod cond_mod
= brw_cond_mod_for_nir_reduction_op(redop
);
4482 /* Set up a register for all of our scratching around and initialize it
4483 * to reduction operation's identity value.
4485 fs_reg scan
= bld
.vgrf(src
.type
);
4486 const fs_builder allbld
= bld
.exec_all();
4487 allbld
.emit(SHADER_OPCODE_SEL_EXEC
, scan
, src
, identity
);
4489 if (instr
->intrinsic
== nir_intrinsic_exclusive_scan
) {
4490 /* Exclusive scan is a bit harder because we have to do an annoying
4491 * shift of the contents before we can begin. To make things worse,
4492 * we can't do this with a normal stride; we have to use indirects.
4494 fs_reg shifted
= bld
.vgrf(src
.type
);
4495 fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_W
);
4496 allbld
.ADD(idx
, nir_system_values
[SYSTEM_VALUE_SUBGROUP_INVOCATION
],
4498 allbld
.emit(SHADER_OPCODE_SHUFFLE
, shifted
, scan
, idx
);
4499 allbld
.group(1, 0).MOV(component(shifted
, 0), identity
);
4503 bld
.emit_scan(brw_op
, scan
, dispatch_width
, cond_mod
);
4505 bld
.MOV(retype(dest
, src
.type
), scan
);
4509 case nir_intrinsic_begin_invocation_interlock
: {
4510 const fs_builder ubld
= bld
.group(8, 0);
4511 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
4513 ubld
.emit(SHADER_OPCODE_INTERLOCK
, tmp
)->size_written
= 2 *
4519 case nir_intrinsic_end_invocation_interlock
: {
4520 /* We don't need to do anything here */
4525 unreachable("unknown intrinsic");
4530 fs_visitor::nir_emit_ssbo_atomic(const fs_builder
&bld
,
4531 int op
, nir_intrinsic_instr
*instr
)
4533 if (stage
== MESA_SHADER_FRAGMENT
)
4534 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4537 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4538 dest
= get_nir_dest(instr
->dest
);
4540 fs_reg surface
= get_nir_ssbo_intrinsic_index(bld
, instr
);
4541 fs_reg offset
= get_nir_src(instr
->src
[1]);
4543 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
4544 data1
= get_nir_src(instr
->src
[2]);
4546 if (op
== BRW_AOP_CMPWR
)
4547 data2
= get_nir_src(instr
->src
[3]);
4549 /* Emit the actual atomic operation */
4551 fs_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
4553 1 /* dims */, 1 /* rsize */,
4555 BRW_PREDICATE_NONE
);
4556 dest
.type
= atomic_result
.type
;
4557 bld
.MOV(dest
, atomic_result
);
4561 fs_visitor::nir_emit_ssbo_atomic_float(const fs_builder
&bld
,
4562 int op
, nir_intrinsic_instr
*instr
)
4564 if (stage
== MESA_SHADER_FRAGMENT
)
4565 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4568 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4569 dest
= get_nir_dest(instr
->dest
);
4571 fs_reg surface
= get_nir_ssbo_intrinsic_index(bld
, instr
);
4572 fs_reg offset
= get_nir_src(instr
->src
[1]);
4573 fs_reg data1
= get_nir_src(instr
->src
[2]);
4575 if (op
== BRW_AOP_FCMPWR
)
4576 data2
= get_nir_src(instr
->src
[3]);
4578 /* Emit the actual atomic operation */
4580 fs_reg atomic_result
= emit_untyped_atomic_float(bld
, surface
, offset
,
4582 1 /* dims */, 1 /* rsize */,
4584 BRW_PREDICATE_NONE
);
4585 dest
.type
= atomic_result
.type
;
4586 bld
.MOV(dest
, atomic_result
);
4590 fs_visitor::nir_emit_shared_atomic(const fs_builder
&bld
,
4591 int op
, nir_intrinsic_instr
*instr
)
4594 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4595 dest
= get_nir_dest(instr
->dest
);
4597 fs_reg surface
= brw_imm_ud(GEN7_BTI_SLM
);
4600 if (op
!= BRW_AOP_INC
&& op
!= BRW_AOP_DEC
&& op
!= BRW_AOP_PREDEC
)
4601 data1
= get_nir_src(instr
->src
[1]);
4603 if (op
== BRW_AOP_CMPWR
)
4604 data2
= get_nir_src(instr
->src
[2]);
4606 /* Get the offset */
4607 if (nir_src_is_const(instr
->src
[0])) {
4608 offset
= brw_imm_ud(instr
->const_index
[0] +
4609 nir_src_as_uint(instr
->src
[0]));
4611 offset
= vgrf(glsl_type::uint_type
);
4613 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
4614 brw_imm_ud(instr
->const_index
[0]));
4617 /* Emit the actual atomic operation operation */
4619 fs_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
4621 1 /* dims */, 1 /* rsize */,
4623 BRW_PREDICATE_NONE
);
4624 dest
.type
= atomic_result
.type
;
4625 bld
.MOV(dest
, atomic_result
);
4629 fs_visitor::nir_emit_shared_atomic_float(const fs_builder
&bld
,
4630 int op
, nir_intrinsic_instr
*instr
)
4633 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4634 dest
= get_nir_dest(instr
->dest
);
4636 fs_reg surface
= brw_imm_ud(GEN7_BTI_SLM
);
4638 fs_reg data1
= get_nir_src(instr
->src
[1]);
4640 if (op
== BRW_AOP_FCMPWR
)
4641 data2
= get_nir_src(instr
->src
[2]);
4643 /* Get the offset */
4644 if (nir_src_is_const(instr
->src
[0])) {
4645 offset
= brw_imm_ud(instr
->const_index
[0] +
4646 nir_src_as_uint(instr
->src
[0]));
4648 offset
= vgrf(glsl_type::uint_type
);
4650 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
4651 brw_imm_ud(instr
->const_index
[0]));
4654 /* Emit the actual atomic operation operation */
4656 fs_reg atomic_result
= emit_untyped_atomic_float(bld
, surface
, offset
,
4658 1 /* dims */, 1 /* rsize */,
4660 BRW_PREDICATE_NONE
);
4661 dest
.type
= atomic_result
.type
;
4662 bld
.MOV(dest
, atomic_result
);
4666 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
4668 unsigned texture
= instr
->texture_index
;
4669 unsigned sampler
= instr
->sampler_index
;
4671 fs_reg srcs
[TEX_LOGICAL_NUM_SRCS
];
4673 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(texture
);
4674 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = brw_imm_ud(sampler
);
4676 int lod_components
= 0;
4678 /* The hardware requires a LOD for buffer textures */
4679 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4680 srcs
[TEX_LOGICAL_SRC_LOD
] = brw_imm_d(0);
4682 uint32_t header_bits
= 0;
4683 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4684 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
4685 switch (instr
->src
[i
].src_type
) {
4686 case nir_tex_src_bias
:
4687 srcs
[TEX_LOGICAL_SRC_LOD
] =
4688 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
4690 case nir_tex_src_comparator
:
4691 srcs
[TEX_LOGICAL_SRC_SHADOW_C
] = retype(src
, BRW_REGISTER_TYPE_F
);
4693 case nir_tex_src_coord
:
4694 switch (instr
->op
) {
4696 case nir_texop_txf_ms
:
4697 case nir_texop_txf_ms_mcs
:
4698 case nir_texop_samples_identical
:
4699 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = retype(src
, BRW_REGISTER_TYPE_D
);
4702 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = retype(src
, BRW_REGISTER_TYPE_F
);
4706 case nir_tex_src_ddx
:
4707 srcs
[TEX_LOGICAL_SRC_LOD
] = retype(src
, BRW_REGISTER_TYPE_F
);
4708 lod_components
= nir_tex_instr_src_size(instr
, i
);
4710 case nir_tex_src_ddy
:
4711 srcs
[TEX_LOGICAL_SRC_LOD2
] = retype(src
, BRW_REGISTER_TYPE_F
);
4713 case nir_tex_src_lod
:
4714 switch (instr
->op
) {
4716 srcs
[TEX_LOGICAL_SRC_LOD
] =
4717 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_UD
);
4720 srcs
[TEX_LOGICAL_SRC_LOD
] =
4721 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_D
);
4724 srcs
[TEX_LOGICAL_SRC_LOD
] =
4725 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
4729 case nir_tex_src_min_lod
:
4730 srcs
[TEX_LOGICAL_SRC_MIN_LOD
] =
4731 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
4733 case nir_tex_src_ms_index
:
4734 srcs
[TEX_LOGICAL_SRC_SAMPLE_INDEX
] = retype(src
, BRW_REGISTER_TYPE_UD
);
4737 case nir_tex_src_offset
: {
4738 nir_const_value
*const_offset
=
4739 nir_src_as_const_value(instr
->src
[i
].src
);
4740 assert(nir_src_bit_size(instr
->src
[i
].src
) == 32);
4741 unsigned offset_bits
= 0;
4743 brw_texture_offset(const_offset
->i32
,
4744 nir_tex_instr_src_size(instr
, i
),
4746 header_bits
|= offset_bits
;
4748 srcs
[TEX_LOGICAL_SRC_TG4_OFFSET
] =
4749 retype(src
, BRW_REGISTER_TYPE_D
);
4754 case nir_tex_src_projector
:
4755 unreachable("should be lowered");
4757 case nir_tex_src_texture_offset
: {
4758 /* Figure out the highest possible texture index and mark it as used */
4759 uint32_t max_used
= texture
+ instr
->texture_array_size
- 1;
4760 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
< 8) {
4761 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
4763 max_used
+= stage_prog_data
->binding_table
.texture_start
;
4765 brw_mark_surface_used(prog_data
, max_used
);
4767 /* Emit code to evaluate the actual indexing expression */
4768 fs_reg tmp
= vgrf(glsl_type::uint_type
);
4769 bld
.ADD(tmp
, src
, brw_imm_ud(texture
));
4770 srcs
[TEX_LOGICAL_SRC_SURFACE
] = bld
.emit_uniformize(tmp
);
4774 case nir_tex_src_sampler_offset
: {
4775 /* Emit code to evaluate the actual indexing expression */
4776 fs_reg tmp
= vgrf(glsl_type::uint_type
);
4777 bld
.ADD(tmp
, src
, brw_imm_ud(sampler
));
4778 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = bld
.emit_uniformize(tmp
);
4782 case nir_tex_src_ms_mcs
:
4783 assert(instr
->op
== nir_texop_txf_ms
);
4784 srcs
[TEX_LOGICAL_SRC_MCS
] = retype(src
, BRW_REGISTER_TYPE_D
);
4787 case nir_tex_src_plane
: {
4788 const uint32_t plane
= nir_src_as_uint(instr
->src
[i
].src
);
4789 const uint32_t texture_index
=
4790 instr
->texture_index
+
4791 stage_prog_data
->binding_table
.plane_start
[plane
] -
4792 stage_prog_data
->binding_table
.texture_start
;
4794 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(texture_index
);
4799 unreachable("unknown texture source");
4803 if (srcs
[TEX_LOGICAL_SRC_MCS
].file
== BAD_FILE
&&
4804 (instr
->op
== nir_texop_txf_ms
||
4805 instr
->op
== nir_texop_samples_identical
)) {
4806 if (devinfo
->gen
>= 7 &&
4807 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
4808 srcs
[TEX_LOGICAL_SRC_MCS
] =
4809 emit_mcs_fetch(srcs
[TEX_LOGICAL_SRC_COORDINATE
],
4810 instr
->coord_components
,
4811 srcs
[TEX_LOGICAL_SRC_SURFACE
]);
4813 srcs
[TEX_LOGICAL_SRC_MCS
] = brw_imm_ud(0u);
4817 srcs
[TEX_LOGICAL_SRC_COORD_COMPONENTS
] = brw_imm_d(instr
->coord_components
);
4818 srcs
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
] = brw_imm_d(lod_components
);
4821 switch (instr
->op
) {
4823 opcode
= (stage
== MESA_SHADER_FRAGMENT
? SHADER_OPCODE_TEX_LOGICAL
:
4824 SHADER_OPCODE_TXL_LOGICAL
);
4827 opcode
= FS_OPCODE_TXB_LOGICAL
;
4830 opcode
= SHADER_OPCODE_TXL_LOGICAL
;
4833 opcode
= SHADER_OPCODE_TXD_LOGICAL
;
4836 opcode
= SHADER_OPCODE_TXF_LOGICAL
;
4838 case nir_texop_txf_ms
:
4839 if ((key_tex
->msaa_16
& (1 << sampler
)))
4840 opcode
= SHADER_OPCODE_TXF_CMS_W_LOGICAL
;
4842 opcode
= SHADER_OPCODE_TXF_CMS_LOGICAL
;
4844 case nir_texop_txf_ms_mcs
:
4845 opcode
= SHADER_OPCODE_TXF_MCS_LOGICAL
;
4847 case nir_texop_query_levels
:
4849 opcode
= SHADER_OPCODE_TXS_LOGICAL
;
4852 opcode
= SHADER_OPCODE_LOD_LOGICAL
;
4855 if (srcs
[TEX_LOGICAL_SRC_TG4_OFFSET
].file
!= BAD_FILE
)
4856 opcode
= SHADER_OPCODE_TG4_OFFSET_LOGICAL
;
4858 opcode
= SHADER_OPCODE_TG4_LOGICAL
;
4860 case nir_texop_texture_samples
:
4861 opcode
= SHADER_OPCODE_SAMPLEINFO_LOGICAL
;
4863 case nir_texop_samples_identical
: {
4864 fs_reg dst
= retype(get_nir_dest(instr
->dest
), BRW_REGISTER_TYPE_D
);
4866 /* If mcs is an immediate value, it means there is no MCS. In that case
4867 * just return false.
4869 if (srcs
[TEX_LOGICAL_SRC_MCS
].file
== BRW_IMMEDIATE_VALUE
) {
4870 bld
.MOV(dst
, brw_imm_ud(0u));
4871 } else if ((key_tex
->msaa_16
& (1 << sampler
))) {
4872 fs_reg tmp
= vgrf(glsl_type::uint_type
);
4873 bld
.OR(tmp
, srcs
[TEX_LOGICAL_SRC_MCS
],
4874 offset(srcs
[TEX_LOGICAL_SRC_MCS
], bld
, 1));
4875 bld
.CMP(dst
, tmp
, brw_imm_ud(0u), BRW_CONDITIONAL_EQ
);
4877 bld
.CMP(dst
, srcs
[TEX_LOGICAL_SRC_MCS
], brw_imm_ud(0u),
4878 BRW_CONDITIONAL_EQ
);
4883 unreachable("unknown texture opcode");
4886 if (instr
->op
== nir_texop_tg4
) {
4887 if (instr
->component
== 1 &&
4888 key_tex
->gather_channel_quirk_mask
& (1 << texture
)) {
4889 /* gather4 sampler is broken for green channel on RG32F --
4890 * we must ask for blue instead.
4892 header_bits
|= 2 << 16;
4894 header_bits
|= instr
->component
<< 16;
4898 fs_reg dst
= bld
.vgrf(brw_type_for_nir_type(devinfo
, instr
->dest_type
), 4);
4899 fs_inst
*inst
= bld
.emit(opcode
, dst
, srcs
, ARRAY_SIZE(srcs
));
4900 inst
->offset
= header_bits
;
4902 const unsigned dest_size
= nir_tex_instr_dest_size(instr
);
4903 if (devinfo
->gen
>= 9 &&
4904 instr
->op
!= nir_texop_tg4
&& instr
->op
!= nir_texop_query_levels
) {
4905 unsigned write_mask
= instr
->dest
.is_ssa
?
4906 nir_ssa_def_components_read(&instr
->dest
.ssa
):
4907 (1 << dest_size
) - 1;
4908 assert(write_mask
!= 0); /* dead code should have been eliminated */
4909 inst
->size_written
= util_last_bit(write_mask
) *
4910 inst
->dst
.component_size(inst
->exec_size
);
4912 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
4915 if (srcs
[TEX_LOGICAL_SRC_SHADOW_C
].file
!= BAD_FILE
)
4916 inst
->shadow_compare
= true;
4918 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
== 6)
4919 emit_gen6_gather_wa(key_tex
->gen6_gather_wa
[texture
], dst
);
4922 for (unsigned i
= 0; i
< dest_size
; i
++)
4923 nir_dest
[i
] = offset(dst
, bld
, i
);
4925 if (instr
->op
== nir_texop_query_levels
) {
4926 /* # levels is in .w */
4927 nir_dest
[0] = offset(dst
, bld
, 3);
4928 } else if (instr
->op
== nir_texop_txs
&&
4929 dest_size
>= 3 && devinfo
->gen
< 7) {
4930 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
4931 fs_reg depth
= offset(dst
, bld
, 2);
4932 nir_dest
[2] = vgrf(glsl_type::int_type
);
4933 bld
.emit_minmax(nir_dest
[2], depth
, brw_imm_d(1), BRW_CONDITIONAL_GE
);
4936 bld
.LOAD_PAYLOAD(get_nir_dest(instr
->dest
), nir_dest
, dest_size
, 0);
4940 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
4942 switch (instr
->type
) {
4943 case nir_jump_break
:
4944 bld
.emit(BRW_OPCODE_BREAK
);
4946 case nir_jump_continue
:
4947 bld
.emit(BRW_OPCODE_CONTINUE
);
4949 case nir_jump_return
:
4951 unreachable("unknown jump");
4956 * This helper takes a source register and un/shuffles it into the destination
4959 * If source type size is smaller than destination type size the operation
4960 * needed is a component shuffle. The opposite case would be an unshuffle. If
4961 * source/destination type size is equal a shuffle is done that would be
4962 * equivalent to a simple MOV.
4964 * For example, if source is a 16-bit type and destination is 32-bit. A 3
4965 * components .xyz 16-bit vector on SIMD8 would be.
4967 * |x1|x2|x3|x4|x5|x6|x7|x8|y1|y2|y3|y4|y5|y6|y7|y8|
4968 * |z1|z2|z3|z4|z5|z6|z7|z8| | | | | | | | |
4970 * This helper will return the following 2 32-bit components with the 16-bit
4973 * |x1 y1|x2 y2|x3 y3|x4 y4|x5 y5|x6 y6|x7 y7|x8 y8|
4974 * |z1 |z2 |z3 |z4 |z5 |z6 |z7 |z8 |
4976 * For unshuffle, the example would be the opposite, a 64-bit type source
4977 * and a 32-bit destination. A 2 component .xy 64-bit vector on SIMD8
4980 * | x1l x1h | x2l x2h | x3l x3h | x4l x4h |
4981 * | x5l x5h | x6l x6h | x7l x7h | x8l x8h |
4982 * | y1l y1h | y2l y2h | y3l y3h | y4l y4h |
4983 * | y5l y5h | y6l y6h | y7l y7h | y8l y8h |
4985 * The returned result would be the following 4 32-bit components unshuffled:
4987 * | x1l | x2l | x3l | x4l | x5l | x6l | x7l | x8l |
4988 * | x1h | x2h | x3h | x4h | x5h | x6h | x7h | x8h |
4989 * | y1l | y2l | y3l | y4l | y5l | y6l | y7l | y8l |
4990 * | y1h | y2h | y3h | y4h | y5h | y6h | y7h | y8h |
4992 * - Source and destination register must not be overlapped.
4993 * - components units are measured in terms of the smaller type between
4994 * source and destination because we are un/shuffling the smaller
4995 * components from/into the bigger ones.
4996 * - first_component parameter allows skipping source components.
4999 shuffle_src_to_dst(const fs_builder
&bld
,
5002 uint32_t first_component
,
5003 uint32_t components
)
5005 if (type_sz(src
.type
) == type_sz(dst
.type
)) {
5006 assert(!regions_overlap(dst
,
5007 type_sz(dst
.type
) * bld
.dispatch_width() * components
,
5008 offset(src
, bld
, first_component
),
5009 type_sz(src
.type
) * bld
.dispatch_width() * components
));
5010 for (unsigned i
= 0; i
< components
; i
++) {
5011 bld
.MOV(retype(offset(dst
, bld
, i
), src
.type
),
5012 offset(src
, bld
, i
+ first_component
));
5014 } else if (type_sz(src
.type
) < type_sz(dst
.type
)) {
5015 /* Source is shuffled into destination */
5016 unsigned size_ratio
= type_sz(dst
.type
) / type_sz(src
.type
);
5017 assert(!regions_overlap(dst
,
5018 type_sz(dst
.type
) * bld
.dispatch_width() *
5019 DIV_ROUND_UP(components
, size_ratio
),
5020 offset(src
, bld
, first_component
),
5021 type_sz(src
.type
) * bld
.dispatch_width() * components
));
5023 brw_reg_type shuffle_type
=
5024 brw_reg_type_from_bit_size(8 * type_sz(src
.type
),
5025 BRW_REGISTER_TYPE_D
);
5026 for (unsigned i
= 0; i
< components
; i
++) {
5027 fs_reg shuffle_component_i
=
5028 subscript(offset(dst
, bld
, i
/ size_ratio
),
5029 shuffle_type
, i
% size_ratio
);
5030 bld
.MOV(shuffle_component_i
,
5031 retype(offset(src
, bld
, i
+ first_component
), shuffle_type
));
5034 /* Source is unshuffled into destination */
5035 unsigned size_ratio
= type_sz(src
.type
) / type_sz(dst
.type
);
5036 assert(!regions_overlap(dst
,
5037 type_sz(dst
.type
) * bld
.dispatch_width() * components
,
5038 offset(src
, bld
, first_component
/ size_ratio
),
5039 type_sz(src
.type
) * bld
.dispatch_width() *
5040 DIV_ROUND_UP(components
+ (first_component
% size_ratio
),
5043 brw_reg_type shuffle_type
=
5044 brw_reg_type_from_bit_size(8 * type_sz(dst
.type
),
5045 BRW_REGISTER_TYPE_D
);
5046 for (unsigned i
= 0; i
< components
; i
++) {
5047 fs_reg shuffle_component_i
=
5048 subscript(offset(src
, bld
, (first_component
+ i
) / size_ratio
),
5049 shuffle_type
, (first_component
+ i
) % size_ratio
);
5050 bld
.MOV(retype(offset(dst
, bld
, i
), shuffle_type
),
5051 shuffle_component_i
);
5057 shuffle_from_32bit_read(const fs_builder
&bld
,
5060 uint32_t first_component
,
5061 uint32_t components
)
5063 assert(type_sz(src
.type
) == 4);
5065 /* This function takes components in units of the destination type while
5066 * shuffle_src_to_dst takes components in units of the smallest type
5068 if (type_sz(dst
.type
) > 4) {
5069 assert(type_sz(dst
.type
) == 8);
5070 first_component
*= 2;
5074 shuffle_src_to_dst(bld
, dst
, src
, first_component
, components
);
5078 shuffle_for_32bit_write(const fs_builder
&bld
,
5080 uint32_t first_component
,
5081 uint32_t components
)
5083 fs_reg dst
= bld
.vgrf(BRW_REGISTER_TYPE_D
,
5084 DIV_ROUND_UP (components
* type_sz(src
.type
), 4));
5085 /* This function takes components in units of the source type while
5086 * shuffle_src_to_dst takes components in units of the smallest type
5088 if (type_sz(src
.type
) > 4) {
5089 assert(type_sz(src
.type
) == 8);
5090 first_component
*= 2;
5094 shuffle_src_to_dst(bld
, dst
, src
, first_component
, components
);
5100 setup_imm_df(const fs_builder
&bld
, double v
)
5102 const struct gen_device_info
*devinfo
= bld
.shader
->devinfo
;
5103 assert(devinfo
->gen
>= 7);
5105 if (devinfo
->gen
>= 8)
5106 return brw_imm_df(v
);
5108 /* gen7.5 does not support DF immediates straighforward but the DIM
5109 * instruction allows to set the 64-bit immediate value.
5111 if (devinfo
->is_haswell
) {
5112 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5113 fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_DF
, 1);
5114 ubld
.DIM(dst
, brw_imm_df(v
));
5115 return component(dst
, 0);
5118 /* gen7 does not support DF immediates, so we generate a 64-bit constant by
5119 * writing the low 32-bit of the constant to suboffset 0 of a VGRF and
5120 * the high 32-bit to suboffset 4 and then applying a stride of 0.
5122 * Alternatively, we could also produce a normal VGRF (without stride 0)
5123 * by writing to all the channels in the VGRF, however, that would hit the
5124 * gen7 bug where we have to split writes that span more than 1 register
5125 * into instructions with a width of 4 (otherwise the write to the second
5126 * register written runs into an execmask hardware bug) which isn't very
5139 const fs_builder ubld
= bld
.exec_all().group(1, 0);
5140 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
5141 ubld
.MOV(tmp
, brw_imm_ud(di
.i1
));
5142 ubld
.MOV(horiz_offset(tmp
, 1), brw_imm_ud(di
.i2
));
5144 return component(retype(tmp
, BRW_REGISTER_TYPE_DF
), 0);
5148 setup_imm_b(const fs_builder
&bld
, int8_t v
)
5150 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_B
);
5151 bld
.MOV(tmp
, brw_imm_w(v
));
5156 setup_imm_ub(const fs_builder
&bld
, uint8_t v
)
5158 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UB
);
5159 bld
.MOV(tmp
, brw_imm_uw(v
));