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"
30 using namespace brw::surface_access
;
33 fs_visitor::emit_nir_code()
35 /* emit the arrays used for inputs and outputs - load/store intrinsics will
36 * be converted to reads/writes of these arrays
40 nir_emit_system_values();
42 /* get the main function and emit it */
43 nir_foreach_function(function
, nir
) {
44 assert(strcmp(function
->name
, "main") == 0);
45 assert(function
->impl
);
46 nir_emit_impl(function
->impl
);
51 fs_visitor::nir_setup_outputs()
53 if (stage
== MESA_SHADER_TESS_CTRL
|| stage
== MESA_SHADER_FRAGMENT
)
56 nir_foreach_variable(var
, &nir
->outputs
) {
57 const unsigned vec4s
=
58 var
->data
.compact
? DIV_ROUND_UP(glsl_get_length(var
->type
), 4)
59 : type_size_vec4(var
->type
);
60 fs_reg reg
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 4 * vec4s
);
61 for (unsigned i
= 0; i
< vec4s
; i
++) {
62 if (outputs
[var
->data
.driver_location
+ i
].file
== BAD_FILE
)
63 outputs
[var
->data
.driver_location
+ i
] = offset(reg
, bld
, 4 * i
);
69 fs_visitor::nir_setup_uniforms()
71 if (dispatch_width
!= min_dispatch_width
)
74 uniforms
= nir
->num_uniforms
/ 4;
78 emit_system_values_block(nir_block
*block
, fs_visitor
*v
)
82 nir_foreach_instr(instr
, block
) {
83 if (instr
->type
!= nir_instr_type_intrinsic
)
86 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
87 switch (intrin
->intrinsic
) {
88 case nir_intrinsic_load_vertex_id
:
89 unreachable("should be lowered by lower_vertex_id().");
91 case nir_intrinsic_load_vertex_id_zero_base
:
92 case nir_intrinsic_load_base_vertex
:
93 case nir_intrinsic_load_instance_id
:
94 case nir_intrinsic_load_base_instance
:
95 case nir_intrinsic_load_draw_id
:
96 unreachable("should be lowered by brw_nir_lower_vs_inputs().");
98 case nir_intrinsic_load_invocation_id
:
99 if (v
->stage
== MESA_SHADER_TESS_CTRL
)
101 assert(v
->stage
== MESA_SHADER_GEOMETRY
);
102 reg
= &v
->nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
103 if (reg
->file
== BAD_FILE
) {
104 const fs_builder abld
= v
->bld
.annotate("gl_InvocationID", NULL
);
105 fs_reg
g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
106 fs_reg iid
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
107 abld
.SHR(iid
, g1
, brw_imm_ud(27u));
112 case nir_intrinsic_load_sample_pos
:
113 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
114 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
115 if (reg
->file
== BAD_FILE
)
116 *reg
= *v
->emit_samplepos_setup();
119 case nir_intrinsic_load_sample_id
:
120 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
121 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
122 if (reg
->file
== BAD_FILE
)
123 *reg
= *v
->emit_sampleid_setup();
126 case nir_intrinsic_load_sample_mask_in
:
127 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
128 assert(v
->devinfo
->gen
>= 7);
129 reg
= &v
->nir_system_values
[SYSTEM_VALUE_SAMPLE_MASK_IN
];
130 if (reg
->file
== BAD_FILE
)
131 *reg
= *v
->emit_samplemaskin_setup();
134 case nir_intrinsic_load_work_group_id
:
135 assert(v
->stage
== MESA_SHADER_COMPUTE
);
136 reg
= &v
->nir_system_values
[SYSTEM_VALUE_WORK_GROUP_ID
];
137 if (reg
->file
== BAD_FILE
)
138 *reg
= *v
->emit_cs_work_group_id_setup();
141 case nir_intrinsic_load_helper_invocation
:
142 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
143 reg
= &v
->nir_system_values
[SYSTEM_VALUE_HELPER_INVOCATION
];
144 if (reg
->file
== BAD_FILE
) {
145 const fs_builder abld
=
146 v
->bld
.annotate("gl_HelperInvocation", NULL
);
148 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
149 * pixel mask is in g1.7 of the thread payload.
151 * We move the per-channel pixel enable bit to the low bit of each
152 * channel by shifting the byte containing the pixel mask by the
153 * vector immediate 0x76543210UV.
155 * The region of <1,8,0> reads only 1 byte (the pixel masks for
156 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
157 * masks for 2 and 3) in SIMD16.
159 fs_reg shifted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
, 1);
161 stride(byte_offset(retype(brw_vec1_grf(1, 0),
162 BRW_REGISTER_TYPE_UB
), 28),
164 brw_imm_v(0x76543210));
166 /* A set bit in the pixel mask means the channel is enabled, but
167 * that is the opposite of gl_HelperInvocation so we need to invert
170 * The negate source-modifier bit of logical instructions on Gen8+
171 * performs 1's complement negation, so we can use that instead of
174 fs_reg inverted
= negate(shifted
);
175 if (v
->devinfo
->gen
< 8) {
176 inverted
= abld
.vgrf(BRW_REGISTER_TYPE_UW
);
177 abld
.NOT(inverted
, shifted
);
180 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
181 * with 1 and negating.
183 fs_reg anded
= abld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
184 abld
.AND(anded
, inverted
, brw_imm_uw(1));
186 fs_reg dst
= abld
.vgrf(BRW_REGISTER_TYPE_D
, 1);
187 abld
.MOV(dst
, negate(retype(anded
, BRW_REGISTER_TYPE_D
)));
201 fs_visitor::nir_emit_system_values()
203 nir_system_values
= ralloc_array(mem_ctx
, fs_reg
, SYSTEM_VALUE_MAX
);
204 for (unsigned i
= 0; i
< SYSTEM_VALUE_MAX
; i
++) {
205 nir_system_values
[i
] = fs_reg();
208 nir_foreach_function(function
, nir
) {
209 assert(strcmp(function
->name
, "main") == 0);
210 assert(function
->impl
);
211 nir_foreach_block(block
, function
->impl
) {
212 emit_system_values_block(block
, this);
218 fs_visitor::nir_emit_impl(nir_function_impl
*impl
)
220 nir_locals
= ralloc_array(mem_ctx
, fs_reg
, impl
->reg_alloc
);
221 for (unsigned i
= 0; i
< impl
->reg_alloc
; i
++) {
222 nir_locals
[i
] = fs_reg();
225 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
226 unsigned array_elems
=
227 reg
->num_array_elems
== 0 ? 1 : reg
->num_array_elems
;
228 unsigned size
= array_elems
* reg
->num_components
;
229 const brw_reg_type reg_type
=
230 reg
->bit_size
== 32 ? BRW_REGISTER_TYPE_F
: BRW_REGISTER_TYPE_DF
;
231 nir_locals
[reg
->index
] = bld
.vgrf(reg_type
, size
);
234 nir_ssa_values
= reralloc(mem_ctx
, nir_ssa_values
, fs_reg
,
237 nir_emit_cf_list(&impl
->body
);
241 fs_visitor::nir_emit_cf_list(exec_list
*list
)
243 exec_list_validate(list
);
244 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
245 switch (node
->type
) {
247 nir_emit_if(nir_cf_node_as_if(node
));
250 case nir_cf_node_loop
:
251 nir_emit_loop(nir_cf_node_as_loop(node
));
254 case nir_cf_node_block
:
255 nir_emit_block(nir_cf_node_as_block(node
));
259 unreachable("Invalid CFG node block");
265 fs_visitor::nir_emit_if(nir_if
*if_stmt
)
267 /* first, put the condition into f0 */
268 fs_inst
*inst
= bld
.MOV(bld
.null_reg_d(),
269 retype(get_nir_src(if_stmt
->condition
),
270 BRW_REGISTER_TYPE_D
));
271 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
273 bld
.IF(BRW_PREDICATE_NORMAL
);
275 nir_emit_cf_list(&if_stmt
->then_list
);
277 /* note: if the else is empty, dead CF elimination will remove it */
278 bld
.emit(BRW_OPCODE_ELSE
);
280 nir_emit_cf_list(&if_stmt
->else_list
);
282 bld
.emit(BRW_OPCODE_ENDIF
);
286 fs_visitor::nir_emit_loop(nir_loop
*loop
)
288 bld
.emit(BRW_OPCODE_DO
);
290 nir_emit_cf_list(&loop
->body
);
292 bld
.emit(BRW_OPCODE_WHILE
);
296 fs_visitor::nir_emit_block(nir_block
*block
)
298 nir_foreach_instr(instr
, block
) {
299 nir_emit_instr(instr
);
304 fs_visitor::nir_emit_instr(nir_instr
*instr
)
306 const fs_builder abld
= bld
.annotate(NULL
, instr
);
308 switch (instr
->type
) {
309 case nir_instr_type_alu
:
310 nir_emit_alu(abld
, nir_instr_as_alu(instr
));
313 case nir_instr_type_intrinsic
:
315 case MESA_SHADER_VERTEX
:
316 nir_emit_vs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
318 case MESA_SHADER_TESS_CTRL
:
319 nir_emit_tcs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
321 case MESA_SHADER_TESS_EVAL
:
322 nir_emit_tes_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
324 case MESA_SHADER_GEOMETRY
:
325 nir_emit_gs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
327 case MESA_SHADER_FRAGMENT
:
328 nir_emit_fs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
330 case MESA_SHADER_COMPUTE
:
331 nir_emit_cs_intrinsic(abld
, nir_instr_as_intrinsic(instr
));
334 unreachable("unsupported shader stage");
338 case nir_instr_type_tex
:
339 nir_emit_texture(abld
, nir_instr_as_tex(instr
));
342 case nir_instr_type_load_const
:
343 nir_emit_load_const(abld
, nir_instr_as_load_const(instr
));
346 case nir_instr_type_ssa_undef
:
347 /* We create a new VGRF for undefs on every use (by handling
348 * them in get_nir_src()), rather than for each definition.
349 * This helps register coalescing eliminate MOVs from undef.
353 case nir_instr_type_jump
:
354 nir_emit_jump(abld
, nir_instr_as_jump(instr
));
358 unreachable("unknown instruction type");
363 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
367 fs_visitor::optimize_extract_to_float(nir_alu_instr
*instr
,
368 const fs_reg
&result
)
370 if (!instr
->src
[0].src
.is_ssa
||
371 !instr
->src
[0].src
.ssa
->parent_instr
)
374 if (instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
377 nir_alu_instr
*src0
=
378 nir_instr_as_alu(instr
->src
[0].src
.ssa
->parent_instr
);
380 if (src0
->op
!= nir_op_extract_u8
&& src0
->op
!= nir_op_extract_u16
&&
381 src0
->op
!= nir_op_extract_i8
&& src0
->op
!= nir_op_extract_i16
)
384 nir_const_value
*element
= nir_src_as_const_value(src0
->src
[1].src
);
385 assert(element
!= NULL
);
387 /* Element type to extract.*/
388 const brw_reg_type type
= brw_int_type(
389 src0
->op
== nir_op_extract_u16
|| src0
->op
== nir_op_extract_i16
? 2 : 1,
390 src0
->op
== nir_op_extract_i16
|| src0
->op
== nir_op_extract_i8
);
392 fs_reg op0
= get_nir_src(src0
->src
[0].src
);
393 op0
.type
= brw_type_for_nir_type(devinfo
,
394 (nir_alu_type
)(nir_op_infos
[src0
->op
].input_types
[0] |
395 nir_src_bit_size(src0
->src
[0].src
)));
396 op0
= offset(op0
, bld
, src0
->src
[0].swizzle
[0]);
398 set_saturate(instr
->dest
.saturate
,
399 bld
.MOV(result
, subscript(op0
, type
, element
->u32
[0])));
404 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr
*instr
,
405 const fs_reg
&result
)
407 if (!instr
->src
[0].src
.is_ssa
||
408 instr
->src
[0].src
.ssa
->parent_instr
->type
!= nir_instr_type_intrinsic
)
411 nir_intrinsic_instr
*src0
=
412 nir_instr_as_intrinsic(instr
->src
[0].src
.ssa
->parent_instr
);
414 if (src0
->intrinsic
!= nir_intrinsic_load_front_face
)
417 nir_const_value
*value1
= nir_src_as_const_value(instr
->src
[1].src
);
418 if (!value1
|| fabsf(value1
->f32
[0]) != 1.0f
)
421 nir_const_value
*value2
= nir_src_as_const_value(instr
->src
[2].src
);
422 if (!value2
|| fabsf(value2
->f32
[0]) != 1.0f
)
425 fs_reg tmp
= vgrf(glsl_type::int_type
);
427 if (devinfo
->gen
>= 6) {
428 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
429 fs_reg g0
= fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W
));
431 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
433 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
434 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
436 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
438 * This negation looks like it's safe in practice, because bits 0:4 will
439 * surely be TRIANGLES
442 if (value1
->f32
[0] == -1.0f
) {
446 bld
.OR(subscript(tmp
, BRW_REGISTER_TYPE_W
, 1),
447 g0
, brw_imm_uw(0x3f80));
449 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
450 fs_reg g1_6
= fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D
));
452 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
454 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
455 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
457 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
459 * This negation looks like it's safe in practice, because bits 0:4 will
460 * surely be TRIANGLES
463 if (value1
->f32
[0] == -1.0f
) {
467 bld
.OR(tmp
, g1_6
, brw_imm_d(0x3f800000));
469 bld
.AND(retype(result
, BRW_REGISTER_TYPE_D
), tmp
, brw_imm_d(0xbf800000));
475 emit_find_msb_using_lzd(const fs_builder
&bld
,
476 const fs_reg
&result
,
484 /* LZD of an absolute value source almost always does the right
485 * thing. There are two problem values:
487 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
488 * 0. However, findMSB(int(0x80000000)) == 30.
490 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
491 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
493 * For a value of zero or negative one, -1 will be returned.
495 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
496 * findMSB(-(1<<x)) should return x-1.
498 * For all negative number cases, including 0x80000000 and
499 * 0xffffffff, the correct value is obtained from LZD if instead of
500 * negating the (already negative) value the logical-not is used. A
501 * conditonal logical-not can be achieved in two instructions.
503 temp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
505 bld
.ASR(temp
, src
, brw_imm_d(31));
506 bld
.XOR(temp
, temp
, src
);
509 bld
.LZD(retype(result
, BRW_REGISTER_TYPE_UD
),
510 retype(temp
, BRW_REGISTER_TYPE_UD
));
512 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
513 * from the LSB side. Subtract the result from 31 to convert the MSB
514 * count into an LSB count. If no bits are set, LZD will return 32.
515 * 31-32 = -1, which is exactly what findMSB() is supposed to return.
517 inst
= bld
.ADD(result
, retype(result
, BRW_REGISTER_TYPE_D
), brw_imm_d(31));
518 inst
->src
[0].negate
= true;
522 fs_visitor::nir_emit_alu(const fs_builder
&bld
, nir_alu_instr
*instr
)
524 struct brw_wm_prog_key
*fs_key
= (struct brw_wm_prog_key
*) this->key
;
527 fs_reg result
= get_nir_dest(instr
->dest
.dest
);
528 result
.type
= brw_type_for_nir_type(devinfo
,
529 (nir_alu_type
)(nir_op_infos
[instr
->op
].output_type
|
530 nir_dest_bit_size(instr
->dest
.dest
)));
533 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
534 op
[i
] = get_nir_src(instr
->src
[i
].src
);
535 op
[i
].type
= brw_type_for_nir_type(devinfo
,
536 (nir_alu_type
)(nir_op_infos
[instr
->op
].input_types
[i
] |
537 nir_src_bit_size(instr
->src
[i
].src
)));
538 op
[i
].abs
= instr
->src
[i
].abs
;
539 op
[i
].negate
= instr
->src
[i
].negate
;
542 /* We get a bunch of mov's out of the from_ssa pass and they may still
543 * be vectorized. We'll handle them as a special-case. We'll also
544 * handle vecN here because it's basically the same thing.
552 fs_reg temp
= result
;
553 bool need_extra_copy
= false;
554 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
555 if (!instr
->src
[i
].src
.is_ssa
&&
556 instr
->dest
.dest
.reg
.reg
== instr
->src
[i
].src
.reg
.reg
) {
557 need_extra_copy
= true;
558 temp
= bld
.vgrf(result
.type
, 4);
563 for (unsigned i
= 0; i
< 4; i
++) {
564 if (!(instr
->dest
.write_mask
& (1 << i
)))
567 if (instr
->op
== nir_op_imov
|| instr
->op
== nir_op_fmov
) {
568 inst
= bld
.MOV(offset(temp
, bld
, i
),
569 offset(op
[0], bld
, instr
->src
[0].swizzle
[i
]));
571 inst
= bld
.MOV(offset(temp
, bld
, i
),
572 offset(op
[i
], bld
, instr
->src
[i
].swizzle
[0]));
574 inst
->saturate
= instr
->dest
.saturate
;
577 /* In this case the source and destination registers were the same,
578 * so we need to insert an extra set of moves in order to deal with
581 if (need_extra_copy
) {
582 for (unsigned i
= 0; i
< 4; i
++) {
583 if (!(instr
->dest
.write_mask
& (1 << i
)))
586 bld
.MOV(offset(result
, bld
, i
), offset(temp
, bld
, i
));
595 /* At this point, we have dealt with any instruction that operates on
596 * more than a single channel. Therefore, we can just adjust the source
597 * and destination registers for that channel and emit the instruction.
599 unsigned channel
= 0;
600 if (nir_op_infos
[instr
->op
].output_size
== 0) {
601 /* Since NIR is doing the scalarizing for us, we should only ever see
602 * vectorized operations with a single channel.
604 assert(_mesa_bitcount(instr
->dest
.write_mask
) == 1);
605 channel
= ffs(instr
->dest
.write_mask
) - 1;
607 result
= offset(result
, bld
, channel
);
610 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++) {
611 assert(nir_op_infos
[instr
->op
].input_sizes
[i
] < 2);
612 op
[i
] = offset(op
[i
], bld
, instr
->src
[i
].swizzle
[channel
]);
618 if (optimize_extract_to_float(instr
, result
))
620 inst
= bld
.MOV(result
, op
[0]);
621 inst
->saturate
= instr
->dest
.saturate
;
627 /* CHV PRM, vol07, 3D Media GPGPU Engine, Register Region Restrictions:
629 * "When source or destination is 64b (...), regioning in Align1
630 * must follow these rules:
632 * 1. Source and destination horizontal stride must be aligned to
636 * This means that 32-bit to 64-bit conversions need to have the 32-bit
637 * data elements aligned to 64-bit. This restriction does not apply to
640 if (nir_dest_bit_size(instr
->dest
.dest
) == 64 &&
641 nir_src_bit_size(instr
->src
[0].src
) == 32 &&
642 (devinfo
->is_cherryview
|| gen_device_info_is_9lp(devinfo
))) {
643 fs_reg tmp
= bld
.vgrf(result
.type
, 1);
644 tmp
= subscript(tmp
, op
[0].type
, 0);
645 inst
= bld
.MOV(tmp
, op
[0]);
646 inst
= bld
.MOV(result
, tmp
);
647 inst
->saturate
= instr
->dest
.saturate
;
660 inst
= bld
.MOV(result
, op
[0]);
661 inst
->saturate
= instr
->dest
.saturate
;
666 /* Straightforward since the source can be assumed to be
669 set_condmod(BRW_CONDITIONAL_NZ
, bld
.MOV(result
, op
[0]));
670 set_predicate(BRW_PREDICATE_NORMAL
, bld
.MOV(result
, brw_imm_f(1.0f
)));
672 } else if (type_sz(op
[0].type
) < 8) {
673 /* AND(val, 0x80000000) gives the sign bit.
675 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
678 bld
.CMP(bld
.null_reg_f(), op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
680 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
681 op
[0].type
= BRW_REGISTER_TYPE_UD
;
682 result
.type
= BRW_REGISTER_TYPE_UD
;
683 bld
.AND(result_int
, op
[0], brw_imm_ud(0x80000000u
));
685 inst
= bld
.OR(result_int
, result_int
, brw_imm_ud(0x3f800000u
));
686 inst
->predicate
= BRW_PREDICATE_NORMAL
;
687 if (instr
->dest
.saturate
) {
688 inst
= bld
.MOV(result
, result
);
689 inst
->saturate
= true;
692 /* For doubles we do the same but we need to consider:
694 * - 2-src instructions can't operate with 64-bit immediates
695 * - The sign is encoded in the high 32-bit of each DF
696 * - CMP with DF requires special handling in SIMD16
697 * - We need to produce a DF result.
700 /* 2-src instructions can't have 64-bit immediates, so put 0.0 in
701 * a register and compare with that.
703 fs_reg tmp
= vgrf(glsl_type::double_type
);
704 bld
.MOV(tmp
, setup_imm_df(bld
, 0.0));
706 /* A direct DF CMP using the flag register (null dst) won't work in
707 * SIMD16 because the CMP will be split in two by lower_simd_width,
708 * resulting in two CMP instructions with the same dst (NULL),
709 * leading to dead code elimination of the first one. In SIMD8,
710 * however, there is no need to split the CMP and we can save some
713 fs_reg dst_tmp
= vgrf(glsl_type::double_type
);
714 bld
.CMP(dst_tmp
, op
[0], tmp
, BRW_CONDITIONAL_NZ
);
716 /* In SIMD16 we want to avoid using a NULL dst register with DF CMP,
717 * so we store the result of the comparison in a vgrf instead and
718 * then we generate a UD comparison from that that won't have to
719 * be split by lower_simd_width. This is what NIR does to handle
720 * double comparisons in the general case.
722 if (bld
.dispatch_width() == 16 ) {
723 fs_reg dst_tmp_ud
= retype(dst_tmp
, BRW_REGISTER_TYPE_UD
);
724 bld
.MOV(dst_tmp_ud
, subscript(dst_tmp
, BRW_REGISTER_TYPE_UD
, 0));
725 bld
.CMP(bld
.null_reg_ud(),
726 dst_tmp_ud
, brw_imm_ud(0), BRW_CONDITIONAL_NZ
);
729 /* Get the high 32-bit of each double component where the sign is */
730 fs_reg result_int
= retype(result
, BRW_REGISTER_TYPE_UD
);
731 bld
.MOV(result_int
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 1));
733 /* Get the sign bit */
734 bld
.AND(result_int
, result_int
, brw_imm_ud(0x80000000u
));
736 /* Add 1.0 to the sign, predicated to skip the case of op[0] == 0.0 */
737 inst
= bld
.OR(result_int
, result_int
, brw_imm_ud(0x3f800000u
));
738 inst
->predicate
= BRW_PREDICATE_NORMAL
;
740 /* Convert from 32-bit float to 64-bit double */
741 result
.type
= BRW_REGISTER_TYPE_DF
;
742 inst
= bld
.MOV(result
, retype(result_int
, BRW_REGISTER_TYPE_F
));
744 if (instr
->dest
.saturate
) {
745 inst
= bld
.MOV(result
, result
);
746 inst
->saturate
= true;
753 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
754 * -> non-negative val generates 0x00000000.
755 * Predicated OR sets 1 if val is positive.
757 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
758 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_G
);
759 bld
.ASR(result
, op
[0], brw_imm_d(31));
760 inst
= bld
.OR(result
, result
, brw_imm_d(1));
761 inst
->predicate
= BRW_PREDICATE_NORMAL
;
765 inst
= bld
.emit(SHADER_OPCODE_RCP
, result
, op
[0]);
766 inst
->saturate
= instr
->dest
.saturate
;
770 inst
= bld
.emit(SHADER_OPCODE_EXP2
, result
, op
[0]);
771 inst
->saturate
= instr
->dest
.saturate
;
775 inst
= bld
.emit(SHADER_OPCODE_LOG2
, result
, op
[0]);
776 inst
->saturate
= instr
->dest
.saturate
;
780 inst
= bld
.emit(SHADER_OPCODE_SIN
, result
, op
[0]);
781 inst
->saturate
= instr
->dest
.saturate
;
785 inst
= bld
.emit(SHADER_OPCODE_COS
, result
, op
[0]);
786 inst
->saturate
= instr
->dest
.saturate
;
790 if (fs_key
->high_quality_derivatives
) {
791 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
793 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
795 inst
->saturate
= instr
->dest
.saturate
;
797 case nir_op_fddx_fine
:
798 inst
= bld
.emit(FS_OPCODE_DDX_FINE
, result
, op
[0]);
799 inst
->saturate
= instr
->dest
.saturate
;
801 case nir_op_fddx_coarse
:
802 inst
= bld
.emit(FS_OPCODE_DDX_COARSE
, result
, op
[0]);
803 inst
->saturate
= instr
->dest
.saturate
;
806 if (fs_key
->high_quality_derivatives
) {
807 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0]);
809 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0]);
811 inst
->saturate
= instr
->dest
.saturate
;
813 case nir_op_fddy_fine
:
814 inst
= bld
.emit(FS_OPCODE_DDY_FINE
, result
, op
[0]);
815 inst
->saturate
= instr
->dest
.saturate
;
817 case nir_op_fddy_coarse
:
818 inst
= bld
.emit(FS_OPCODE_DDY_COARSE
, result
, op
[0]);
819 inst
->saturate
= instr
->dest
.saturate
;
824 inst
= bld
.ADD(result
, op
[0], op
[1]);
825 inst
->saturate
= instr
->dest
.saturate
;
829 inst
= bld
.MUL(result
, op
[0], op
[1]);
830 inst
->saturate
= instr
->dest
.saturate
;
834 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
835 bld
.MUL(result
, op
[0], op
[1]);
838 case nir_op_imul_high
:
839 case nir_op_umul_high
:
840 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
841 bld
.emit(SHADER_OPCODE_MULH
, result
, op
[0], op
[1]);
846 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
847 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
, result
, op
[0], op
[1]);
850 case nir_op_uadd_carry
:
851 unreachable("Should have been lowered by carry_to_arith().");
853 case nir_op_usub_borrow
:
854 unreachable("Should have been lowered by borrow_to_arith().");
858 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
859 * appears that our hardware just does the right thing for signed
862 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
863 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
867 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
868 bld
.emit(SHADER_OPCODE_INT_REMAINDER
, result
, op
[0], op
[1]);
870 /* Math instructions don't support conditional mod */
871 inst
= bld
.MOV(bld
.null_reg_d(), result
);
872 inst
->conditional_mod
= BRW_CONDITIONAL_NZ
;
874 /* Now, we need to determine if signs of the sources are different.
875 * When we XOR the sources, the top bit is 0 if they are the same and 1
876 * if they are different. We can then use a conditional modifier to
877 * turn that into a predicate. This leads us to an XOR.l instruction.
879 * Technically, according to the PRM, you're not allowed to use .l on a
880 * XOR instruction. However, emperical experiments and Curro's reading
881 * of the simulator source both indicate that it's safe.
883 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
884 inst
= bld
.XOR(tmp
, op
[0], op
[1]);
885 inst
->predicate
= BRW_PREDICATE_NORMAL
;
886 inst
->conditional_mod
= BRW_CONDITIONAL_L
;
888 /* If the result of the initial remainder operation is non-zero and the
889 * two sources have different signs, add in a copy of op[1] to get the
890 * final integer modulus value.
892 inst
= bld
.ADD(result
, result
, op
[1]);
893 inst
->predicate
= BRW_PREDICATE_NORMAL
;
901 fs_reg dest
= result
;
902 if (nir_src_bit_size(instr
->src
[0].src
) > 32) {
903 dest
= bld
.vgrf(BRW_REGISTER_TYPE_DF
, 1);
905 brw_conditional_mod cond
;
908 cond
= BRW_CONDITIONAL_L
;
911 cond
= BRW_CONDITIONAL_GE
;
914 cond
= BRW_CONDITIONAL_Z
;
917 cond
= BRW_CONDITIONAL_NZ
;
920 unreachable("bad opcode");
922 bld
.CMP(dest
, op
[0], op
[1], cond
);
923 if (nir_src_bit_size(instr
->src
[0].src
) > 32) {
924 bld
.MOV(result
, subscript(dest
, BRW_REGISTER_TYPE_UD
, 0));
935 fs_reg dest
= result
;
936 if (nir_src_bit_size(instr
->src
[0].src
) > 32) {
937 dest
= bld
.vgrf(BRW_REGISTER_TYPE_UQ
, 1);
940 brw_conditional_mod cond
;
944 cond
= BRW_CONDITIONAL_L
;
948 cond
= BRW_CONDITIONAL_GE
;
951 cond
= BRW_CONDITIONAL_Z
;
954 cond
= BRW_CONDITIONAL_NZ
;
957 unreachable("bad opcode");
959 bld
.CMP(dest
, op
[0], op
[1], cond
);
960 if (nir_src_bit_size(instr
->src
[0].src
) > 32) {
961 bld
.MOV(result
, subscript(dest
, BRW_REGISTER_TYPE_UD
, 0));
967 if (devinfo
->gen
>= 8) {
968 op
[0] = resolve_source_modifiers(op
[0]);
970 bld
.NOT(result
, op
[0]);
973 if (devinfo
->gen
>= 8) {
974 op
[0] = resolve_source_modifiers(op
[0]);
975 op
[1] = resolve_source_modifiers(op
[1]);
977 bld
.XOR(result
, op
[0], op
[1]);
980 if (devinfo
->gen
>= 8) {
981 op
[0] = resolve_source_modifiers(op
[0]);
982 op
[1] = resolve_source_modifiers(op
[1]);
984 bld
.OR(result
, op
[0], op
[1]);
987 if (devinfo
->gen
>= 8) {
988 op
[0] = resolve_source_modifiers(op
[0]);
989 op
[1] = resolve_source_modifiers(op
[1]);
991 bld
.AND(result
, op
[0], op
[1]);
997 case nir_op_ball_fequal2
:
998 case nir_op_ball_iequal2
:
999 case nir_op_ball_fequal3
:
1000 case nir_op_ball_iequal3
:
1001 case nir_op_ball_fequal4
:
1002 case nir_op_ball_iequal4
:
1003 case nir_op_bany_fnequal2
:
1004 case nir_op_bany_inequal2
:
1005 case nir_op_bany_fnequal3
:
1006 case nir_op_bany_inequal3
:
1007 case nir_op_bany_fnequal4
:
1008 case nir_op_bany_inequal4
:
1009 unreachable("Lowered by nir_lower_alu_reductions");
1011 case nir_op_fnoise1_1
:
1012 case nir_op_fnoise1_2
:
1013 case nir_op_fnoise1_3
:
1014 case nir_op_fnoise1_4
:
1015 case nir_op_fnoise2_1
:
1016 case nir_op_fnoise2_2
:
1017 case nir_op_fnoise2_3
:
1018 case nir_op_fnoise2_4
:
1019 case nir_op_fnoise3_1
:
1020 case nir_op_fnoise3_2
:
1021 case nir_op_fnoise3_3
:
1022 case nir_op_fnoise3_4
:
1023 case nir_op_fnoise4_1
:
1024 case nir_op_fnoise4_2
:
1025 case nir_op_fnoise4_3
:
1026 case nir_op_fnoise4_4
:
1027 unreachable("not reached: should be handled by lower_noise");
1030 unreachable("not reached: should be handled by ldexp_to_arith()");
1033 inst
= bld
.emit(SHADER_OPCODE_SQRT
, result
, op
[0]);
1034 inst
->saturate
= instr
->dest
.saturate
;
1038 inst
= bld
.emit(SHADER_OPCODE_RSQ
, result
, op
[0]);
1039 inst
->saturate
= instr
->dest
.saturate
;
1044 bld
.MOV(result
, negate(op
[0]));
1049 if (nir_src_bit_size(instr
->src
[0].src
) == 64) {
1050 /* two-argument instructions can't take 64-bit immediates */
1054 if (instr
->op
== nir_op_f2b
) {
1055 zero
= vgrf(glsl_type::double_type
);
1056 tmp
= vgrf(glsl_type::double_type
);
1058 zero
= vgrf(glsl_type::int64_t_type
);
1059 tmp
= vgrf(glsl_type::int64_t_type
);
1062 bld
.MOV(zero
, setup_imm_df(bld
, 0.0));
1063 /* A SIMD16 execution needs to be split in two instructions, so use
1064 * a vgrf instead of the flag register as dst so instruction splitting
1067 bld
.CMP(tmp
, op
[0], zero
, BRW_CONDITIONAL_NZ
);
1068 bld
.MOV(result
, subscript(tmp
, BRW_REGISTER_TYPE_UD
, 0));
1070 if (instr
->op
== nir_op_f2b
) {
1071 bld
.CMP(result
, op
[0], brw_imm_f(0.0f
), BRW_CONDITIONAL_NZ
);
1073 bld
.CMP(result
, op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1079 inst
= bld
.RNDZ(result
, op
[0]);
1080 inst
->saturate
= instr
->dest
.saturate
;
1083 case nir_op_fceil
: {
1084 op
[0].negate
= !op
[0].negate
;
1085 fs_reg temp
= vgrf(glsl_type::float_type
);
1086 bld
.RNDD(temp
, op
[0]);
1088 inst
= bld
.MOV(result
, temp
);
1089 inst
->saturate
= instr
->dest
.saturate
;
1093 inst
= bld
.RNDD(result
, op
[0]);
1094 inst
->saturate
= instr
->dest
.saturate
;
1097 inst
= bld
.FRC(result
, op
[0]);
1098 inst
->saturate
= instr
->dest
.saturate
;
1100 case nir_op_fround_even
:
1101 inst
= bld
.RNDE(result
, op
[0]);
1102 inst
->saturate
= instr
->dest
.saturate
;
1105 case nir_op_fquantize2f16
: {
1106 fs_reg tmp16
= bld
.vgrf(BRW_REGISTER_TYPE_D
);
1107 fs_reg tmp32
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1108 fs_reg zero
= bld
.vgrf(BRW_REGISTER_TYPE_F
);
1110 /* The destination stride must be at least as big as the source stride. */
1111 tmp16
.type
= BRW_REGISTER_TYPE_W
;
1114 /* Check for denormal */
1115 fs_reg abs_src0
= op
[0];
1116 abs_src0
.abs
= true;
1117 bld
.CMP(bld
.null_reg_f(), abs_src0
, brw_imm_f(ldexpf(1.0, -14)),
1119 /* Get the appropriately signed zero */
1120 bld
.AND(retype(zero
, BRW_REGISTER_TYPE_UD
),
1121 retype(op
[0], BRW_REGISTER_TYPE_UD
),
1122 brw_imm_ud(0x80000000));
1123 /* Do the actual F32 -> F16 -> F32 conversion */
1124 bld
.emit(BRW_OPCODE_F32TO16
, tmp16
, op
[0]);
1125 bld
.emit(BRW_OPCODE_F16TO32
, tmp32
, tmp16
);
1126 /* Select that or zero based on normal status */
1127 inst
= bld
.SEL(result
, zero
, tmp32
);
1128 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1129 inst
->saturate
= instr
->dest
.saturate
;
1136 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_L
);
1137 inst
->saturate
= instr
->dest
.saturate
;
1143 inst
= bld
.emit_minmax(result
, op
[0], op
[1], BRW_CONDITIONAL_GE
);
1144 inst
->saturate
= instr
->dest
.saturate
;
1147 case nir_op_pack_snorm_2x16
:
1148 case nir_op_pack_snorm_4x8
:
1149 case nir_op_pack_unorm_2x16
:
1150 case nir_op_pack_unorm_4x8
:
1151 case nir_op_unpack_snorm_2x16
:
1152 case nir_op_unpack_snorm_4x8
:
1153 case nir_op_unpack_unorm_2x16
:
1154 case nir_op_unpack_unorm_4x8
:
1155 case nir_op_unpack_half_2x16
:
1156 case nir_op_pack_half_2x16
:
1157 unreachable("not reached: should be handled by lower_packing_builtins");
1159 case nir_op_unpack_half_2x16_split_x
:
1160 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_X
, result
, op
[0]);
1161 inst
->saturate
= instr
->dest
.saturate
;
1163 case nir_op_unpack_half_2x16_split_y
:
1164 inst
= bld
.emit(FS_OPCODE_UNPACK_HALF_2x16_SPLIT_Y
, result
, op
[0]);
1165 inst
->saturate
= instr
->dest
.saturate
;
1168 case nir_op_pack_64_2x32_split
:
1169 bld
.emit(FS_OPCODE_PACK
, result
, op
[0], op
[1]);
1172 case nir_op_unpack_64_2x32_split_x
:
1173 case nir_op_unpack_64_2x32_split_y
: {
1174 if (instr
->op
== nir_op_unpack_64_2x32_split_x
)
1175 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 0));
1177 bld
.MOV(result
, subscript(op
[0], BRW_REGISTER_TYPE_UD
, 1));
1182 inst
= bld
.emit(SHADER_OPCODE_POW
, result
, op
[0], op
[1]);
1183 inst
->saturate
= instr
->dest
.saturate
;
1186 case nir_op_bitfield_reverse
:
1187 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1188 bld
.BFREV(result
, op
[0]);
1191 case nir_op_bit_count
:
1192 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1193 bld
.CBIT(result
, op
[0]);
1196 case nir_op_ufind_msb
: {
1197 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1198 emit_find_msb_using_lzd(bld
, result
, op
[0], false);
1202 case nir_op_ifind_msb
: {
1203 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1205 if (devinfo
->gen
< 7) {
1206 emit_find_msb_using_lzd(bld
, result
, op
[0], true);
1208 bld
.FBH(retype(result
, BRW_REGISTER_TYPE_UD
), op
[0]);
1210 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1211 * count from the LSB side. If FBH didn't return an error
1212 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1213 * count into an LSB count.
1215 bld
.CMP(bld
.null_reg_d(), result
, brw_imm_d(-1), BRW_CONDITIONAL_NZ
);
1217 inst
= bld
.ADD(result
, result
, brw_imm_d(31));
1218 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1219 inst
->src
[0].negate
= true;
1224 case nir_op_find_lsb
:
1225 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1227 if (devinfo
->gen
< 7) {
1228 fs_reg temp
= vgrf(glsl_type::int_type
);
1230 /* (x & -x) generates a value that consists of only the LSB of x.
1231 * For all powers of 2, findMSB(y) == findLSB(y).
1233 fs_reg src
= retype(op
[0], BRW_REGISTER_TYPE_D
);
1234 fs_reg negated_src
= src
;
1236 /* One must be negated, and the other must be non-negated. It
1237 * doesn't matter which is which.
1239 negated_src
.negate
= true;
1242 bld
.AND(temp
, src
, negated_src
);
1243 emit_find_msb_using_lzd(bld
, result
, temp
, false);
1245 bld
.FBL(result
, op
[0]);
1249 case nir_op_ubitfield_extract
:
1250 case nir_op_ibitfield_extract
:
1251 unreachable("should have been lowered");
1254 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1255 bld
.BFE(result
, op
[2], op
[1], op
[0]);
1258 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1259 bld
.BFI1(result
, op
[0], op
[1]);
1262 assert(nir_dest_bit_size(instr
->dest
.dest
) < 64);
1263 bld
.BFI2(result
, op
[0], op
[1], op
[2]);
1266 case nir_op_bitfield_insert
:
1267 unreachable("not reached: should have been lowered");
1270 bld
.SHL(result
, op
[0], op
[1]);
1273 bld
.ASR(result
, op
[0], op
[1]);
1276 bld
.SHR(result
, op
[0], op
[1]);
1279 case nir_op_pack_half_2x16_split
:
1280 bld
.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT
, result
, op
[0], op
[1]);
1284 inst
= bld
.MAD(result
, op
[2], op
[1], op
[0]);
1285 inst
->saturate
= instr
->dest
.saturate
;
1289 inst
= bld
.LRP(result
, op
[0], op
[1], op
[2]);
1290 inst
->saturate
= instr
->dest
.saturate
;
1294 if (optimize_frontfacing_ternary(instr
, result
))
1297 bld
.CMP(bld
.null_reg_d(), op
[0], brw_imm_d(0), BRW_CONDITIONAL_NZ
);
1298 inst
= bld
.SEL(result
, op
[1], op
[2]);
1299 inst
->predicate
= BRW_PREDICATE_NORMAL
;
1302 case nir_op_extract_u8
:
1303 case nir_op_extract_i8
: {
1304 const brw_reg_type type
= brw_int_type(1, instr
->op
== nir_op_extract_i8
);
1305 nir_const_value
*byte
= nir_src_as_const_value(instr
->src
[1].src
);
1306 assert(byte
!= NULL
);
1307 bld
.MOV(result
, subscript(op
[0], type
, byte
->u32
[0]));
1311 case nir_op_extract_u16
:
1312 case nir_op_extract_i16
: {
1313 const brw_reg_type type
= brw_int_type(2, instr
->op
== nir_op_extract_i16
);
1314 nir_const_value
*word
= nir_src_as_const_value(instr
->src
[1].src
);
1315 assert(word
!= NULL
);
1316 bld
.MOV(result
, subscript(op
[0], type
, word
->u32
[0]));
1321 unreachable("unhandled instruction");
1324 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1325 * to sign extend the low bit to 0/~0
1327 if (devinfo
->gen
<= 5 &&
1328 (instr
->instr
.pass_flags
& BRW_NIR_BOOLEAN_MASK
) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE
) {
1329 fs_reg masked
= vgrf(glsl_type::int_type
);
1330 bld
.AND(masked
, result
, brw_imm_d(1));
1331 masked
.negate
= true;
1332 bld
.MOV(retype(result
, BRW_REGISTER_TYPE_D
), masked
);
1337 fs_visitor::nir_emit_load_const(const fs_builder
&bld
,
1338 nir_load_const_instr
*instr
)
1340 const brw_reg_type reg_type
=
1341 instr
->def
.bit_size
== 32 ? BRW_REGISTER_TYPE_D
: BRW_REGISTER_TYPE_DF
;
1342 fs_reg reg
= bld
.vgrf(reg_type
, instr
->def
.num_components
);
1344 switch (instr
->def
.bit_size
) {
1346 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1347 bld
.MOV(offset(reg
, bld
, i
), brw_imm_d(instr
->value
.i32
[i
]));
1351 for (unsigned i
= 0; i
< instr
->def
.num_components
; i
++)
1352 bld
.MOV(offset(reg
, bld
, i
),
1353 setup_imm_df(bld
, instr
->value
.f64
[i
]));
1357 unreachable("Invalid bit size");
1360 nir_ssa_values
[instr
->def
.index
] = reg
;
1364 fs_visitor::get_nir_src(const nir_src
&src
)
1368 if (src
.ssa
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
1369 const brw_reg_type reg_type
= src
.ssa
->bit_size
== 32 ?
1370 BRW_REGISTER_TYPE_D
: BRW_REGISTER_TYPE_DF
;
1371 reg
= bld
.vgrf(reg_type
, src
.ssa
->num_components
);
1373 reg
= nir_ssa_values
[src
.ssa
->index
];
1376 /* We don't handle indirects on locals */
1377 assert(src
.reg
.indirect
== NULL
);
1378 reg
= offset(nir_locals
[src
.reg
.reg
->index
], bld
,
1379 src
.reg
.base_offset
* src
.reg
.reg
->num_components
);
1382 /* to avoid floating-point denorm flushing problems, set the type by
1383 * default to D - instructions that need floating point semantics will set
1384 * this to F if they need to
1386 return retype(reg
, BRW_REGISTER_TYPE_D
);
1390 * Return an IMM for constants; otherwise call get_nir_src() as normal.
1393 fs_visitor::get_nir_src_imm(const nir_src
&src
)
1395 nir_const_value
*val
= nir_src_as_const_value(src
);
1396 return val
? fs_reg(brw_imm_d(val
->i32
[0])) : get_nir_src(src
);
1400 fs_visitor::get_nir_dest(const nir_dest
&dest
)
1403 const brw_reg_type reg_type
=
1404 dest
.ssa
.bit_size
== 32 ? BRW_REGISTER_TYPE_F
: BRW_REGISTER_TYPE_DF
;
1405 nir_ssa_values
[dest
.ssa
.index
] =
1406 bld
.vgrf(reg_type
, dest
.ssa
.num_components
);
1407 return nir_ssa_values
[dest
.ssa
.index
];
1409 /* We don't handle indirects on locals */
1410 assert(dest
.reg
.indirect
== NULL
);
1411 return offset(nir_locals
[dest
.reg
.reg
->index
], bld
,
1412 dest
.reg
.base_offset
* dest
.reg
.reg
->num_components
);
1417 fs_visitor::get_nir_image_deref(const nir_deref_var
*deref
)
1419 fs_reg
image(UNIFORM
, deref
->var
->data
.driver_location
/ 4,
1420 BRW_REGISTER_TYPE_UD
);
1422 unsigned indirect_max
= 0;
1424 for (const nir_deref
*tail
= &deref
->deref
; tail
->child
;
1425 tail
= tail
->child
) {
1426 const nir_deref_array
*deref_array
= nir_deref_as_array(tail
->child
);
1427 assert(tail
->child
->deref_type
== nir_deref_type_array
);
1428 const unsigned size
= glsl_get_length(tail
->type
);
1429 const unsigned element_size
= type_size_scalar(deref_array
->deref
.type
);
1430 const unsigned base
= MIN2(deref_array
->base_offset
, size
- 1);
1431 image
= offset(image
, bld
, base
* element_size
);
1433 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
) {
1434 fs_reg tmp
= vgrf(glsl_type::uint_type
);
1436 /* Accessing an invalid surface index with the dataport can result
1437 * in a hang. According to the spec "if the index used to
1438 * select an individual element is negative or greater than or
1439 * equal to the size of the array, the results of the operation
1440 * are undefined but may not lead to termination" -- which is one
1441 * of the possible outcomes of the hang. Clamp the index to
1442 * prevent access outside of the array bounds.
1444 bld
.emit_minmax(tmp
, retype(get_nir_src(deref_array
->indirect
),
1445 BRW_REGISTER_TYPE_UD
),
1446 brw_imm_ud(size
- base
- 1), BRW_CONDITIONAL_L
);
1448 indirect_max
+= element_size
* (tail
->type
->length
- 1);
1450 bld
.MUL(tmp
, tmp
, brw_imm_ud(element_size
* 4));
1451 if (indirect
.file
== BAD_FILE
) {
1454 bld
.ADD(indirect
, indirect
, tmp
);
1459 if (indirect
.file
== BAD_FILE
) {
1462 /* Emit a pile of MOVs to load the uniform into a temporary. The
1463 * dead-code elimination pass will get rid of what we don't use.
1465 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, BRW_IMAGE_PARAM_SIZE
);
1466 for (unsigned j
= 0; j
< BRW_IMAGE_PARAM_SIZE
; j
++) {
1467 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
1468 offset(tmp
, bld
, j
), offset(image
, bld
, j
),
1469 indirect
, brw_imm_ud((indirect_max
+ 1) * 4));
1476 fs_visitor::emit_percomp(const fs_builder
&bld
, const fs_inst
&inst
,
1479 for (unsigned i
= 0; i
< 4; i
++) {
1480 if (!((wr_mask
>> i
) & 1))
1483 fs_inst
*new_inst
= new(mem_ctx
) fs_inst(inst
);
1484 new_inst
->dst
= offset(new_inst
->dst
, bld
, i
);
1485 for (unsigned j
= 0; j
< new_inst
->sources
; j
++)
1486 if (new_inst
->src
[j
].file
== VGRF
)
1487 new_inst
->src
[j
] = offset(new_inst
->src
[j
], bld
, i
);
1494 * Get the matching channel register datatype for an image intrinsic of the
1495 * specified GLSL image type.
1498 get_image_base_type(const glsl_type
*type
)
1500 switch ((glsl_base_type
)type
->sampled_type
) {
1501 case GLSL_TYPE_UINT
:
1502 return BRW_REGISTER_TYPE_UD
;
1504 return BRW_REGISTER_TYPE_D
;
1505 case GLSL_TYPE_FLOAT
:
1506 return BRW_REGISTER_TYPE_F
;
1508 unreachable("Not reached.");
1513 * Get the appropriate atomic op for an image atomic intrinsic.
1516 get_image_atomic_op(nir_intrinsic_op op
, const glsl_type
*type
)
1519 case nir_intrinsic_image_atomic_add
:
1521 case nir_intrinsic_image_atomic_min
:
1522 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1523 BRW_AOP_IMIN
: BRW_AOP_UMIN
);
1524 case nir_intrinsic_image_atomic_max
:
1525 return (get_image_base_type(type
) == BRW_REGISTER_TYPE_D
?
1526 BRW_AOP_IMAX
: BRW_AOP_UMAX
);
1527 case nir_intrinsic_image_atomic_and
:
1529 case nir_intrinsic_image_atomic_or
:
1531 case nir_intrinsic_image_atomic_xor
:
1533 case nir_intrinsic_image_atomic_exchange
:
1535 case nir_intrinsic_image_atomic_comp_swap
:
1536 return BRW_AOP_CMPWR
;
1538 unreachable("Not reachable.");
1543 emit_pixel_interpolater_send(const fs_builder
&bld
,
1548 glsl_interp_mode interpolation
)
1550 struct brw_wm_prog_data
*wm_prog_data
=
1551 brw_wm_prog_data(bld
.shader
->stage_prog_data
);
1556 if (src
.file
== BAD_FILE
) {
1558 payload
= bld
.vgrf(BRW_REGISTER_TYPE_F
, 1);
1562 mlen
= 2 * bld
.dispatch_width() / 8;
1565 inst
= bld
.emit(opcode
, dst
, payload
, desc
);
1567 /* 2 floats per slot returned */
1568 inst
->size_written
= 2 * dst
.component_size(inst
->exec_size
);
1569 inst
->pi_noperspective
= interpolation
== INTERP_MODE_NOPERSPECTIVE
;
1571 wm_prog_data
->pulls_bary
= true;
1577 * Computes 1 << x, given a D/UD register containing some value x.
1580 intexp2(const fs_builder
&bld
, const fs_reg
&x
)
1582 assert(x
.type
== BRW_REGISTER_TYPE_UD
|| x
.type
== BRW_REGISTER_TYPE_D
);
1584 fs_reg result
= bld
.vgrf(x
.type
, 1);
1585 fs_reg one
= bld
.vgrf(x
.type
, 1);
1587 bld
.MOV(one
, retype(brw_imm_d(1), one
.type
));
1588 bld
.SHL(result
, one
, x
);
1593 fs_visitor::emit_gs_end_primitive(const nir_src
&vertex_count_nir_src
)
1595 assert(stage
== MESA_SHADER_GEOMETRY
);
1597 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1599 if (gs_compile
->control_data_header_size_bits
== 0)
1602 /* We can only do EndPrimitive() functionality when the control data
1603 * consists of cut bits. Fortunately, the only time it isn't is when the
1604 * output type is points, in which case EndPrimitive() is a no-op.
1606 if (gs_prog_data
->control_data_format
!=
1607 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT
) {
1611 /* Cut bits use one bit per vertex. */
1612 assert(gs_compile
->control_data_bits_per_vertex
== 1);
1614 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1615 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1617 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
1618 * vertex n, 0 otherwise. So all we need to do here is mark bit
1619 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
1620 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
1621 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
1623 * Note that if EndPrimitive() is called before emitting any vertices, this
1624 * will cause us to set bit 31 of the control_data_bits register to 1.
1625 * That's fine because:
1627 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
1628 * output, so the hardware will ignore cut bit 31.
1630 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
1631 * last vertex, so setting cut bit 31 has no effect (since the primitive
1632 * is automatically ended when the GS terminates).
1634 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
1635 * control_data_bits register to 0 when the first vertex is emitted.
1638 const fs_builder abld
= bld
.annotate("end primitive");
1640 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
1641 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1642 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1643 fs_reg mask
= intexp2(abld
, prev_count
);
1644 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1645 * attention to the lower 5 bits of its second source argument, so on this
1646 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
1647 * ((vertex_count - 1) % 32).
1649 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1653 fs_visitor::emit_gs_control_data_bits(const fs_reg
&vertex_count
)
1655 assert(stage
== MESA_SHADER_GEOMETRY
);
1656 assert(gs_compile
->control_data_bits_per_vertex
!= 0);
1658 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1660 const fs_builder abld
= bld
.annotate("emit control data bits");
1661 const fs_builder fwa_bld
= bld
.exec_all();
1663 /* We use a single UD register to accumulate control data bits (32 bits
1664 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
1667 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
1668 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
1669 * use the Channel Mask phase to enable/disable which DWord within that
1670 * group to write. (Remember, different SIMD8 channels may have emitted
1671 * different numbers of vertices, so we may need per-slot offsets.)
1673 * Channel masking presents an annoying problem: we may have to replicate
1674 * the data up to 4 times:
1676 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
1678 * To avoid penalizing shaders that emit a small number of vertices, we
1679 * can avoid these sometimes: if the size of the control data header is
1680 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
1681 * land in the same 128-bit group, so we can skip per-slot offsets.
1683 * Similarly, if the control data header is <= 32 bits, there is only one
1684 * DWord, so we can skip channel masks.
1686 enum opcode opcode
= SHADER_OPCODE_URB_WRITE_SIMD8
;
1688 fs_reg channel_mask
, per_slot_offset
;
1690 if (gs_compile
->control_data_header_size_bits
> 32) {
1691 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
1692 channel_mask
= vgrf(glsl_type::uint_type
);
1695 if (gs_compile
->control_data_header_size_bits
> 128) {
1696 opcode
= SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
;
1697 per_slot_offset
= vgrf(glsl_type::uint_type
);
1700 /* Figure out which DWord we're trying to write to using the formula:
1702 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
1704 * Since bits_per_vertex is a power of two, and is known at compile
1705 * time, this can be optimized to:
1707 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
1709 if (opcode
!= SHADER_OPCODE_URB_WRITE_SIMD8
) {
1710 fs_reg dword_index
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1711 fs_reg prev_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1712 abld
.ADD(prev_count
, vertex_count
, brw_imm_ud(0xffffffffu
));
1713 unsigned log2_bits_per_vertex
=
1714 util_last_bit(gs_compile
->control_data_bits_per_vertex
);
1715 abld
.SHR(dword_index
, prev_count
, brw_imm_ud(6u - log2_bits_per_vertex
));
1717 if (per_slot_offset
.file
!= BAD_FILE
) {
1718 /* Set the per-slot offset to dword_index / 4, so that we'll write to
1719 * the appropriate OWord within the control data header.
1721 abld
.SHR(per_slot_offset
, dword_index
, brw_imm_ud(2u));
1724 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
1725 * write to the appropriate DWORD within the OWORD.
1727 fs_reg channel
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1728 fwa_bld
.AND(channel
, dword_index
, brw_imm_ud(3u));
1729 channel_mask
= intexp2(fwa_bld
, channel
);
1730 /* Then the channel masks need to be in bits 23:16. */
1731 fwa_bld
.SHL(channel_mask
, channel_mask
, brw_imm_ud(16u));
1734 /* Store the control data bits in the message payload and send it. */
1736 if (channel_mask
.file
!= BAD_FILE
)
1737 mlen
+= 4; /* channel masks, plus 3 extra copies of the data */
1738 if (per_slot_offset
.file
!= BAD_FILE
)
1741 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, mlen
);
1742 fs_reg
*sources
= ralloc_array(mem_ctx
, fs_reg
, mlen
);
1744 sources
[i
++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
));
1745 if (per_slot_offset
.file
!= BAD_FILE
)
1746 sources
[i
++] = per_slot_offset
;
1747 if (channel_mask
.file
!= BAD_FILE
)
1748 sources
[i
++] = channel_mask
;
1750 sources
[i
++] = this->control_data_bits
;
1753 abld
.LOAD_PAYLOAD(payload
, sources
, mlen
, mlen
);
1754 fs_inst
*inst
= abld
.emit(opcode
, reg_undef
, payload
);
1756 /* We need to increment Global Offset by 256-bits to make room for
1757 * Broadwell's extra "Vertex Count" payload at the beginning of the
1758 * URB entry. Since this is an OWord message, Global Offset is counted
1759 * in 128-bit units, so we must set it to 2.
1761 if (gs_prog_data
->static_vertex_count
== -1)
1766 fs_visitor::set_gs_stream_control_data_bits(const fs_reg
&vertex_count
,
1769 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
1771 /* Note: we are calling this *before* increasing vertex_count, so
1772 * this->vertex_count == vertex_count - 1 in the formula above.
1775 /* Stream mode uses 2 bits per vertex */
1776 assert(gs_compile
->control_data_bits_per_vertex
== 2);
1778 /* Must be a valid stream */
1779 assert(stream_id
< MAX_VERTEX_STREAMS
);
1781 /* Control data bits are initialized to 0 so we don't have to set any
1782 * bits when sending vertices to stream 0.
1787 const fs_builder abld
= bld
.annotate("set stream control data bits", NULL
);
1789 /* reg::sid = stream_id */
1790 fs_reg sid
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1791 abld
.MOV(sid
, brw_imm_ud(stream_id
));
1793 /* reg:shift_count = 2 * (vertex_count - 1) */
1794 fs_reg shift_count
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1795 abld
.SHL(shift_count
, vertex_count
, brw_imm_ud(1u));
1797 /* Note: we're relying on the fact that the GEN SHL instruction only pays
1798 * attention to the lower 5 bits of its second source argument, so on this
1799 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
1800 * stream_id << ((2 * (vertex_count - 1)) % 32).
1802 fs_reg mask
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1803 abld
.SHL(mask
, sid
, shift_count
);
1804 abld
.OR(this->control_data_bits
, this->control_data_bits
, mask
);
1808 fs_visitor::emit_gs_vertex(const nir_src
&vertex_count_nir_src
,
1811 assert(stage
== MESA_SHADER_GEOMETRY
);
1813 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1815 fs_reg vertex_count
= get_nir_src(vertex_count_nir_src
);
1816 vertex_count
.type
= BRW_REGISTER_TYPE_UD
;
1818 /* Haswell and later hardware ignores the "Render Stream Select" bits
1819 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
1820 * and instead sends all primitives down the pipeline for rasterization.
1821 * If the SOL stage is enabled, "Render Stream Select" is honored and
1822 * primitives bound to non-zero streams are discarded after stream output.
1824 * Since the only purpose of primives sent to non-zero streams is to
1825 * be recorded by transform feedback, we can simply discard all geometry
1826 * bound to these streams when transform feedback is disabled.
1828 if (stream_id
> 0 && !nir
->info
.has_transform_feedback_varyings
)
1831 /* If we're outputting 32 control data bits or less, then we can wait
1832 * until the shader is over to output them all. Otherwise we need to
1833 * output them as we go. Now is the time to do it, since we're about to
1834 * output the vertex_count'th vertex, so it's guaranteed that the
1835 * control data bits associated with the (vertex_count - 1)th vertex are
1838 if (gs_compile
->control_data_header_size_bits
> 32) {
1839 const fs_builder abld
=
1840 bld
.annotate("emit vertex: emit control data bits");
1842 /* Only emit control data bits if we've finished accumulating a batch
1843 * of 32 bits. This is the case when:
1845 * (vertex_count * bits_per_vertex) % 32 == 0
1847 * (in other words, when the last 5 bits of vertex_count *
1848 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
1849 * integer n (which is always the case, since bits_per_vertex is
1850 * always 1 or 2), this is equivalent to requiring that the last 5-n
1851 * bits of vertex_count are 0:
1853 * vertex_count & (2^(5-n) - 1) == 0
1855 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
1858 * vertex_count & (32 / bits_per_vertex - 1) == 0
1860 * TODO: If vertex_count is an immediate, we could do some of this math
1861 * at compile time...
1864 abld
.AND(bld
.null_reg_d(), vertex_count
,
1865 brw_imm_ud(32u / gs_compile
->control_data_bits_per_vertex
- 1u));
1866 inst
->conditional_mod
= BRW_CONDITIONAL_Z
;
1868 abld
.IF(BRW_PREDICATE_NORMAL
);
1869 /* If vertex_count is 0, then no control data bits have been
1870 * accumulated yet, so we can skip emitting them.
1872 abld
.CMP(bld
.null_reg_d(), vertex_count
, brw_imm_ud(0u),
1873 BRW_CONDITIONAL_NEQ
);
1874 abld
.IF(BRW_PREDICATE_NORMAL
);
1875 emit_gs_control_data_bits(vertex_count
);
1876 abld
.emit(BRW_OPCODE_ENDIF
);
1878 /* Reset control_data_bits to 0 so we can start accumulating a new
1881 * Note: in the case where vertex_count == 0, this neutralizes the
1882 * effect of any call to EndPrimitive() that the shader may have
1883 * made before outputting its first vertex.
1885 inst
= abld
.MOV(this->control_data_bits
, brw_imm_ud(0u));
1886 inst
->force_writemask_all
= true;
1887 abld
.emit(BRW_OPCODE_ENDIF
);
1890 emit_urb_writes(vertex_count
);
1892 /* In stream mode we have to set control data bits for all vertices
1893 * unless we have disabled control data bits completely (which we do
1894 * do for GL_POINTS outputs that don't use streams).
1896 if (gs_compile
->control_data_header_size_bits
> 0 &&
1897 gs_prog_data
->control_data_format
==
1898 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID
) {
1899 set_gs_stream_control_data_bits(vertex_count
, stream_id
);
1904 fs_visitor::emit_gs_input_load(const fs_reg
&dst
,
1905 const nir_src
&vertex_src
,
1906 unsigned base_offset
,
1907 const nir_src
&offset_src
,
1908 unsigned num_components
,
1909 unsigned first_component
)
1911 struct brw_gs_prog_data
*gs_prog_data
= brw_gs_prog_data(prog_data
);
1913 nir_const_value
*vertex_const
= nir_src_as_const_value(vertex_src
);
1914 nir_const_value
*offset_const
= nir_src_as_const_value(offset_src
);
1915 const unsigned push_reg_count
= gs_prog_data
->base
.urb_read_length
* 8;
1917 /* TODO: figure out push input layout for invocations == 1 */
1918 /* TODO: make this work with 64-bit inputs */
1919 if (gs_prog_data
->invocations
== 1 &&
1920 type_sz(dst
.type
) <= 4 &&
1921 offset_const
!= NULL
&& vertex_const
!= NULL
&&
1922 4 * (base_offset
+ offset_const
->u32
[0]) < push_reg_count
) {
1923 int imm_offset
= (base_offset
+ offset_const
->u32
[0]) * 4 +
1924 vertex_const
->u32
[0] * push_reg_count
;
1925 for (unsigned i
= 0; i
< num_components
; i
++) {
1926 bld
.MOV(offset(dst
, bld
, i
),
1927 fs_reg(ATTR
, imm_offset
+ i
+ first_component
, dst
.type
));
1932 /* Resort to the pull model. Ensure the VUE handles are provided. */
1933 assert(gs_prog_data
->base
.include_vue_handles
);
1935 unsigned first_icp_handle
= gs_prog_data
->include_primitive_id
? 3 : 2;
1936 fs_reg icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1938 if (gs_prog_data
->invocations
== 1) {
1940 /* The vertex index is constant; just select the proper URB handle. */
1942 retype(brw_vec8_grf(first_icp_handle
+ vertex_const
->i32
[0], 0),
1943 BRW_REGISTER_TYPE_UD
);
1945 /* The vertex index is non-constant. We need to use indirect
1946 * addressing to fetch the proper URB handle.
1948 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
1949 * indicating that channel <n> should read the handle from
1950 * DWord <n>. We convert that to bytes by multiplying by 4.
1952 * Next, we convert the vertex index to bytes by multiplying
1953 * by 32 (shifting by 5), and add the two together. This is
1954 * the final indirect byte offset.
1956 fs_reg sequence
= bld
.vgrf(BRW_REGISTER_TYPE_W
, 1);
1957 fs_reg channel_offsets
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1958 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1959 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1961 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
1962 bld
.MOV(sequence
, fs_reg(brw_imm_v(0x76543210)));
1963 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
1964 bld
.SHL(channel_offsets
, sequence
, brw_imm_ud(2u));
1965 /* Convert vertex_index to bytes (multiply by 32) */
1966 bld
.SHL(vertex_offset_bytes
,
1967 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
1969 bld
.ADD(icp_offset_bytes
, vertex_offset_bytes
, channel_offsets
);
1971 /* Use first_icp_handle as the base offset. There is one register
1972 * of URB handles per vertex, so inform the register allocator that
1973 * we might read up to nir->info.gs.vertices_in registers.
1975 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
1976 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
1977 fs_reg(icp_offset_bytes
),
1978 brw_imm_ud(nir
->info
.gs
.vertices_in
* REG_SIZE
));
1981 assert(gs_prog_data
->invocations
> 1);
1984 assert(devinfo
->gen
>= 9 || vertex_const
->i32
[0] <= 5);
1986 retype(brw_vec1_grf(first_icp_handle
+
1987 vertex_const
->i32
[0] / 8,
1988 vertex_const
->i32
[0] % 8),
1989 BRW_REGISTER_TYPE_UD
));
1991 /* The vertex index is non-constant. We need to use indirect
1992 * addressing to fetch the proper URB handle.
1995 fs_reg icp_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
1997 /* Convert vertex_index to bytes (multiply by 4) */
1998 bld
.SHL(icp_offset_bytes
,
1999 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2002 /* Use first_icp_handle as the base offset. There is one DWord
2003 * of URB handles per vertex, so inform the register allocator that
2004 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2006 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2007 retype(brw_vec8_grf(first_icp_handle
, 0), icp_handle
.type
),
2008 fs_reg(icp_offset_bytes
),
2009 brw_imm_ud(DIV_ROUND_UP(nir
->info
.gs
.vertices_in
, 8) *
2016 fs_reg tmp_dst
= dst
;
2017 fs_reg indirect_offset
= get_nir_src(offset_src
);
2018 unsigned num_iterations
= 1;
2019 unsigned orig_num_components
= num_components
;
2021 if (type_sz(dst
.type
) == 8) {
2022 if (num_components
> 2) {
2026 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(4), dst
.type
);
2028 first_component
= first_component
/ 2;
2031 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2033 /* Constant indexing - use global offset. */
2034 if (first_component
!= 0) {
2035 unsigned read_components
= num_components
+ first_component
;
2036 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2037 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
, icp_handle
);
2038 inst
->size_written
= read_components
*
2039 tmp
.component_size(inst
->exec_size
);
2040 for (unsigned i
= 0; i
< num_components
; i
++) {
2041 bld
.MOV(offset(tmp_dst
, bld
, i
),
2042 offset(tmp
, bld
, i
+ first_component
));
2045 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp_dst
,
2047 inst
->size_written
= num_components
*
2048 tmp_dst
.component_size(inst
->exec_size
);
2050 inst
->offset
= base_offset
+ offset_const
->u32
[0];
2053 /* Indirect indexing - use per-slot offsets as well. */
2054 const fs_reg srcs
[] = { icp_handle
, indirect_offset
};
2055 unsigned read_components
= num_components
+ first_component
;
2056 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2057 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2058 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2059 if (first_component
!= 0) {
2060 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2062 inst
->size_written
= read_components
*
2063 tmp
.component_size(inst
->exec_size
);
2064 for (unsigned i
= 0; i
< num_components
; i
++) {
2065 bld
.MOV(offset(tmp_dst
, bld
, i
),
2066 offset(tmp
, bld
, i
+ first_component
));
2069 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp_dst
,
2071 inst
->size_written
= num_components
*
2072 tmp_dst
.component_size(inst
->exec_size
);
2074 inst
->offset
= base_offset
;
2078 if (type_sz(dst
.type
) == 8) {
2079 shuffle_32bit_load_result_to_64bit_data(
2080 bld
, tmp_dst
, retype(tmp_dst
, BRW_REGISTER_TYPE_F
), num_components
);
2082 for (unsigned c
= 0; c
< num_components
; c
++)
2083 bld
.MOV(offset(dst
, bld
, iter
* 2 + c
), offset(tmp_dst
, bld
, c
));
2086 if (num_iterations
> 1) {
2087 num_components
= orig_num_components
- 2;
2091 fs_reg new_indirect
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2092 bld
.ADD(new_indirect
, indirect_offset
, brw_imm_ud(1u));
2093 indirect_offset
= new_indirect
;
2100 fs_visitor::get_indirect_offset(nir_intrinsic_instr
*instr
)
2102 nir_src
*offset_src
= nir_get_io_offset_src(instr
);
2103 nir_const_value
*const_value
= nir_src_as_const_value(*offset_src
);
2106 /* The only constant offset we should find is 0. brw_nir.c's
2107 * add_const_offset_to_base() will fold other constant offsets
2108 * into instr->const_index[0].
2110 assert(const_value
->u32
[0] == 0);
2114 return get_nir_src(*offset_src
);
2118 do_untyped_vector_read(const fs_builder
&bld
,
2120 const fs_reg surf_index
,
2121 const fs_reg offset_reg
,
2122 unsigned num_components
)
2124 if (type_sz(dest
.type
) == 4) {
2125 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, offset_reg
,
2128 BRW_PREDICATE_NONE
);
2129 read_result
.type
= dest
.type
;
2130 for (unsigned i
= 0; i
< num_components
; i
++)
2131 bld
.MOV(offset(dest
, bld
, i
), offset(read_result
, bld
, i
));
2132 } else if (type_sz(dest
.type
) == 8) {
2133 /* Reading a dvec, so we need to:
2135 * 1. Multiply num_components by 2, to account for the fact that we
2136 * need to read 64-bit components.
2137 * 2. Shuffle the result of the load to form valid 64-bit elements
2138 * 3. Emit a second load (for components z/w) if needed.
2140 fs_reg read_offset
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
2141 bld
.MOV(read_offset
, offset_reg
);
2143 int iters
= num_components
<= 2 ? 1 : 2;
2145 /* Load the dvec, the first iteration loads components x/y, the second
2146 * iteration, if needed, loads components z/w
2148 for (int it
= 0; it
< iters
; it
++) {
2149 /* Compute number of components to read in this iteration */
2150 int iter_components
= MIN2(2, num_components
);
2151 num_components
-= iter_components
;
2153 /* Read. Since this message reads 32-bit components, we need to
2154 * read twice as many components.
2156 fs_reg read_result
= emit_untyped_read(bld
, surf_index
, read_offset
,
2158 iter_components
* 2,
2159 BRW_PREDICATE_NONE
);
2161 /* Shuffle the 32-bit load result into valid 64-bit data */
2162 const fs_reg packed_result
= bld
.vgrf(dest
.type
, iter_components
);
2163 shuffle_32bit_load_result_to_64bit_data(
2164 bld
, packed_result
, read_result
, iter_components
);
2166 /* Move each component to its destination */
2167 read_result
= retype(read_result
, BRW_REGISTER_TYPE_DF
);
2168 for (int c
= 0; c
< iter_components
; c
++) {
2169 bld
.MOV(offset(dest
, bld
, it
* 2 + c
),
2170 offset(packed_result
, bld
, c
));
2173 bld
.ADD(read_offset
, read_offset
, brw_imm_ud(16));
2176 unreachable("Unsupported type");
2181 fs_visitor::nir_emit_vs_intrinsic(const fs_builder
&bld
,
2182 nir_intrinsic_instr
*instr
)
2184 assert(stage
== MESA_SHADER_VERTEX
);
2187 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2188 dest
= get_nir_dest(instr
->dest
);
2190 switch (instr
->intrinsic
) {
2191 case nir_intrinsic_load_vertex_id
:
2192 unreachable("should be lowered by lower_vertex_id()");
2194 case nir_intrinsic_load_vertex_id_zero_base
:
2195 case nir_intrinsic_load_base_vertex
:
2196 case nir_intrinsic_load_instance_id
:
2197 case nir_intrinsic_load_base_instance
:
2198 case nir_intrinsic_load_draw_id
: {
2199 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
2200 fs_reg val
= nir_system_values
[sv
];
2201 assert(val
.file
!= BAD_FILE
);
2202 dest
.type
= val
.type
;
2207 case nir_intrinsic_load_input
: {
2208 fs_reg src
= fs_reg(ATTR
, nir_intrinsic_base(instr
) * 4, dest
.type
);
2209 unsigned first_component
= nir_intrinsic_component(instr
);
2210 unsigned num_components
= instr
->num_components
;
2211 enum brw_reg_type type
= dest
.type
;
2213 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
2214 assert(const_offset
&& "Indirect input loads not allowed");
2215 src
= offset(src
, bld
, const_offset
->u32
[0]);
2217 for (unsigned j
= 0; j
< num_components
; j
++) {
2218 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
+ first_component
));
2221 if (type
== BRW_REGISTER_TYPE_DF
) {
2222 /* Once the double vector is read, set again its original register
2223 * type to continue with normal execution.
2225 src
= retype(src
, type
);
2226 dest
= retype(dest
, type
);
2229 if (type_sz(src
.type
) == 8) {
2230 shuffle_32bit_load_result_to_64bit_data(bld
,
2232 retype(dest
, BRW_REGISTER_TYPE_F
),
2233 instr
->num_components
);
2239 nir_emit_intrinsic(bld
, instr
);
2245 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder
&bld
,
2246 nir_intrinsic_instr
*instr
)
2248 assert(stage
== MESA_SHADER_TESS_CTRL
);
2249 struct brw_tcs_prog_key
*tcs_key
= (struct brw_tcs_prog_key
*) key
;
2250 struct brw_tcs_prog_data
*tcs_prog_data
= brw_tcs_prog_data(prog_data
);
2253 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2254 dst
= get_nir_dest(instr
->dest
);
2256 switch (instr
->intrinsic
) {
2257 case nir_intrinsic_load_primitive_id
:
2258 bld
.MOV(dst
, fs_reg(brw_vec1_grf(0, 1)));
2260 case nir_intrinsic_load_invocation_id
:
2261 bld
.MOV(retype(dst
, invocation_id
.type
), invocation_id
);
2263 case nir_intrinsic_load_patch_vertices_in
:
2264 bld
.MOV(retype(dst
, BRW_REGISTER_TYPE_D
),
2265 brw_imm_d(tcs_key
->input_vertices
));
2268 case nir_intrinsic_barrier
: {
2269 if (tcs_prog_data
->instances
== 1)
2272 fs_reg m0
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2273 fs_reg m0_2
= component(m0
, 2);
2275 const fs_builder chanbld
= bld
.exec_all().group(1, 0);
2277 /* Zero the message header */
2278 bld
.exec_all().MOV(m0
, brw_imm_ud(0u));
2280 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2281 chanbld
.AND(m0_2
, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD
),
2282 brw_imm_ud(INTEL_MASK(16, 13)));
2284 /* Shift it up to bits 27:24. */
2285 chanbld
.SHL(m0_2
, m0_2
, brw_imm_ud(11));
2287 /* Set the Barrier Count and the enable bit */
2288 chanbld
.OR(m0_2
, m0_2
,
2289 brw_imm_ud(tcs_prog_data
->instances
<< 9 | (1 << 15)));
2291 bld
.emit(SHADER_OPCODE_BARRIER
, bld
.null_reg_ud(), m0
);
2295 case nir_intrinsic_load_input
:
2296 unreachable("nir_lower_io should never give us these.");
2299 case nir_intrinsic_load_per_vertex_input
: {
2300 fs_reg indirect_offset
= get_indirect_offset(instr
);
2301 unsigned imm_offset
= instr
->const_index
[0];
2303 const nir_src
&vertex_src
= instr
->src
[0];
2304 nir_const_value
*vertex_const
= nir_src_as_const_value(vertex_src
);
2311 /* Emit a MOV to resolve <0,1,0> regioning. */
2312 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2314 retype(brw_vec1_grf(1 + (vertex_const
->i32
[0] >> 3),
2315 vertex_const
->i32
[0] & 7),
2316 BRW_REGISTER_TYPE_UD
));
2317 } else if (tcs_prog_data
->instances
== 1 &&
2318 vertex_src
.is_ssa
&&
2319 vertex_src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
&&
2320 nir_instr_as_intrinsic(vertex_src
.ssa
->parent_instr
)->intrinsic
== nir_intrinsic_load_invocation_id
) {
2321 /* For the common case of only 1 instance, an array index of
2322 * gl_InvocationID means reading g1. Skip all the indirect work.
2324 icp_handle
= retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD
);
2326 /* The vertex index is non-constant. We need to use indirect
2327 * addressing to fetch the proper URB handle.
2329 icp_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2331 /* Each ICP handle is a single DWord (4 bytes) */
2332 fs_reg vertex_offset_bytes
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2333 bld
.SHL(vertex_offset_bytes
,
2334 retype(get_nir_src(vertex_src
), BRW_REGISTER_TYPE_UD
),
2337 /* Start at g1. We might read up to 4 registers. */
2338 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
, icp_handle
,
2339 retype(brw_vec8_grf(1, 0), icp_handle
.type
), vertex_offset_bytes
,
2340 brw_imm_ud(4 * REG_SIZE
));
2343 /* We can only read two double components with each URB read, so
2344 * we send two read messages in that case, each one loading up to
2345 * two double components.
2347 unsigned num_iterations
= 1;
2348 unsigned num_components
= instr
->num_components
;
2349 unsigned first_component
= nir_intrinsic_component(instr
);
2350 fs_reg orig_dst
= dst
;
2351 if (type_sz(dst
.type
) == 8) {
2352 first_component
= first_component
/ 2;
2353 if (instr
->num_components
> 2) {
2358 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(4), dst
.type
);
2362 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2363 if (indirect_offset
.file
== BAD_FILE
) {
2364 /* Constant indexing - use global offset. */
2365 if (first_component
!= 0) {
2366 unsigned read_components
= num_components
+ first_component
;
2367 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2368 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
, icp_handle
);
2369 for (unsigned i
= 0; i
< num_components
; i
++) {
2370 bld
.MOV(offset(dst
, bld
, i
),
2371 offset(tmp
, bld
, i
+ first_component
));
2374 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
, icp_handle
);
2376 inst
->offset
= imm_offset
;
2379 /* Indirect indexing - use per-slot offsets as well. */
2380 const fs_reg srcs
[] = { icp_handle
, indirect_offset
};
2381 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2382 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2383 if (first_component
!= 0) {
2384 unsigned read_components
= num_components
+ first_component
;
2385 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2386 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2388 for (unsigned i
= 0; i
< num_components
; i
++) {
2389 bld
.MOV(offset(dst
, bld
, i
),
2390 offset(tmp
, bld
, i
+ first_component
));
2393 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
,
2396 inst
->offset
= imm_offset
;
2399 inst
->size_written
= (num_components
+ first_component
) *
2400 inst
->dst
.component_size(inst
->exec_size
);
2402 /* If we are reading 64-bit data using 32-bit read messages we need
2403 * build proper 64-bit data elements by shuffling the low and high
2404 * 32-bit components around like we do for other things like UBOs
2407 if (type_sz(dst
.type
) == 8) {
2408 shuffle_32bit_load_result_to_64bit_data(
2409 bld
, dst
, retype(dst
, BRW_REGISTER_TYPE_F
), num_components
);
2411 for (unsigned c
= 0; c
< num_components
; c
++) {
2412 bld
.MOV(offset(orig_dst
, bld
, iter
* 2 + c
),
2413 offset(dst
, bld
, c
));
2417 /* Copy the temporary to the destination to deal with writemasking.
2419 * Also attempt to deal with gl_PointSize being in the .w component.
2421 if (inst
->offset
== 0 && indirect_offset
.file
== BAD_FILE
) {
2422 assert(type_sz(dst
.type
) < 8);
2423 inst
->dst
= bld
.vgrf(dst
.type
, 4);
2424 inst
->size_written
= 4 * REG_SIZE
;
2425 bld
.MOV(dst
, offset(inst
->dst
, bld
, 3));
2428 /* If we are loading double data and we need a second read message
2429 * adjust the write offset
2431 if (num_iterations
> 1) {
2432 num_components
= instr
->num_components
- 2;
2439 case nir_intrinsic_load_output
:
2440 case nir_intrinsic_load_per_vertex_output
: {
2441 fs_reg indirect_offset
= get_indirect_offset(instr
);
2442 unsigned imm_offset
= instr
->const_index
[0];
2443 unsigned first_component
= nir_intrinsic_component(instr
);
2446 if (indirect_offset
.file
== BAD_FILE
) {
2447 /* Replicate the patch handle to all enabled channels */
2448 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2449 bld
.MOV(patch_handle
,
2450 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
));
2453 if (first_component
!= 0) {
2454 unsigned read_components
=
2455 instr
->num_components
+ first_component
;
2456 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2457 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
,
2459 inst
->size_written
= read_components
* REG_SIZE
;
2460 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2461 bld
.MOV(offset(dst
, bld
, i
),
2462 offset(tmp
, bld
, i
+ first_component
));
2465 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dst
,
2467 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2469 inst
->offset
= imm_offset
;
2473 /* Indirect indexing - use per-slot offsets as well. */
2474 const fs_reg srcs
[] = {
2475 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
2478 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2479 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2480 if (first_component
!= 0) {
2481 unsigned read_components
=
2482 instr
->num_components
+ first_component
;
2483 fs_reg tmp
= bld
.vgrf(dst
.type
, read_components
);
2484 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2486 inst
->size_written
= read_components
* REG_SIZE
;
2487 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2488 bld
.MOV(offset(dst
, bld
, i
),
2489 offset(tmp
, bld
, i
+ first_component
));
2492 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dst
,
2494 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2496 inst
->offset
= imm_offset
;
2502 case nir_intrinsic_store_output
:
2503 case nir_intrinsic_store_per_vertex_output
: {
2504 fs_reg value
= get_nir_src(instr
->src
[0]);
2505 bool is_64bit
= (instr
->src
[0].is_ssa
?
2506 instr
->src
[0].ssa
->bit_size
: instr
->src
[0].reg
.reg
->bit_size
) == 64;
2507 fs_reg indirect_offset
= get_indirect_offset(instr
);
2508 unsigned imm_offset
= instr
->const_index
[0];
2509 unsigned swiz
= BRW_SWIZZLE_XYZW
;
2510 unsigned mask
= instr
->const_index
[1];
2511 unsigned header_regs
= 0;
2513 srcs
[header_regs
++] = retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
);
2515 if (indirect_offset
.file
!= BAD_FILE
) {
2516 srcs
[header_regs
++] = indirect_offset
;
2522 unsigned num_components
= util_last_bit(mask
);
2525 /* We can only pack two 64-bit components in a single message, so send
2526 * 2 messages if we have more components
2528 unsigned num_iterations
= 1;
2529 unsigned iter_components
= num_components
;
2530 unsigned first_component
= nir_intrinsic_component(instr
);
2532 first_component
= first_component
/ 2;
2533 if (instr
->num_components
> 2) {
2535 iter_components
= 2;
2539 /* 64-bit data needs to me shuffled before we can write it to the URB.
2540 * We will use this temporary to shuffle the components in each
2544 fs_reg(VGRF
, alloc
.allocate(2 * iter_components
), value
.type
);
2546 mask
= mask
<< first_component
;
2548 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2549 if (!is_64bit
&& mask
!= WRITEMASK_XYZW
) {
2550 srcs
[header_regs
++] = brw_imm_ud(mask
<< 16);
2551 opcode
= indirect_offset
.file
!= BAD_FILE
?
2552 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
2553 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
2554 } else if (is_64bit
&& ((mask
& WRITEMASK_XY
) != WRITEMASK_XY
)) {
2555 /* Expand the 64-bit mask to 32-bit channels. We only handle
2556 * two channels in each iteration, so we only care about X/Y.
2558 unsigned mask32
= 0;
2559 if (mask
& WRITEMASK_X
)
2560 mask32
|= WRITEMASK_XY
;
2561 if (mask
& WRITEMASK_Y
)
2562 mask32
|= WRITEMASK_ZW
;
2564 /* If the mask does not include any of the channels X or Y there
2565 * is nothing to do in this iteration. Move on to the next couple
2566 * of 64-bit channels.
2574 srcs
[header_regs
++] = brw_imm_ud(mask32
<< 16);
2575 opcode
= indirect_offset
.file
!= BAD_FILE
?
2576 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT
:
2577 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED
;
2579 opcode
= indirect_offset
.file
!= BAD_FILE
?
2580 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT
:
2581 SHADER_OPCODE_URB_WRITE_SIMD8
;
2584 for (unsigned i
= 0; i
< iter_components
; i
++) {
2585 if (!(mask
& (1 << (i
+ first_component
))))
2589 srcs
[header_regs
+ i
+ first_component
] =
2590 offset(value
, bld
, BRW_GET_SWZ(swiz
, i
));
2592 /* We need to shuffle the 64-bit data to match the layout
2593 * expected by our 32-bit URB write messages. We use a temporary
2596 unsigned channel
= BRW_GET_SWZ(swiz
, iter
* 2 + i
);
2597 shuffle_64bit_data_for_32bit_write(bld
,
2598 retype(offset(tmp
, bld
, 2 * i
), BRW_REGISTER_TYPE_F
),
2599 retype(offset(value
, bld
, 2 * channel
), BRW_REGISTER_TYPE_DF
),
2602 /* Now copy the data to the destination */
2603 fs_reg dest
= fs_reg(VGRF
, alloc
.allocate(2), value
.type
);
2604 unsigned idx
= 2 * i
;
2605 bld
.MOV(dest
, offset(tmp
, bld
, idx
));
2606 bld
.MOV(offset(dest
, bld
, 1), offset(tmp
, bld
, idx
+ 1));
2607 srcs
[header_regs
+ idx
+ first_component
* 2] = dest
;
2608 srcs
[header_regs
+ idx
+ 1 + first_component
* 2] =
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 const unsigned max_push_slots
= 32;
2679 if (imm_offset
< max_push_slots
) {
2680 fs_reg src
= fs_reg(ATTR
, imm_offset
/ 2, dest
.type
);
2681 for (int i
= 0; i
< instr
->num_components
; i
++) {
2682 unsigned comp
= 16 / type_sz(dest
.type
) * (imm_offset
% 2) +
2683 i
+ first_component
;
2684 bld
.MOV(offset(dest
, bld
, i
), component(src
, comp
));
2686 tes_prog_data
->base
.urb_read_length
=
2687 MAX2(tes_prog_data
->base
.urb_read_length
,
2688 DIV_ROUND_UP(imm_offset
+ 1, 2));
2690 /* Replicate the patch handle to all enabled channels */
2691 const fs_reg srcs
[] = {
2692 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
)
2694 fs_reg patch_handle
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 1);
2695 bld
.LOAD_PAYLOAD(patch_handle
, srcs
, ARRAY_SIZE(srcs
), 0);
2697 if (first_component
!= 0) {
2698 unsigned read_components
=
2699 instr
->num_components
+ first_component
;
2700 fs_reg tmp
= bld
.vgrf(dest
.type
, read_components
);
2701 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, tmp
,
2703 inst
->size_written
= read_components
* REG_SIZE
;
2704 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
2705 bld
.MOV(offset(dest
, bld
, i
),
2706 offset(tmp
, bld
, i
+ first_component
));
2709 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8
, dest
,
2711 inst
->size_written
= instr
->num_components
* REG_SIZE
;
2714 inst
->offset
= imm_offset
;
2717 /* Indirect indexing - use per-slot offsets as well. */
2719 /* We can only read two double components with each URB read, so
2720 * we send two read messages in that case, each one loading up to
2721 * two double components.
2723 unsigned num_iterations
= 1;
2724 unsigned num_components
= instr
->num_components
;
2725 fs_reg orig_dest
= dest
;
2726 if (type_sz(dest
.type
) == 8) {
2727 if (instr
->num_components
> 2) {
2731 fs_reg tmp
= fs_reg(VGRF
, alloc
.allocate(4), dest
.type
);
2735 for (unsigned iter
= 0; iter
< num_iterations
; iter
++) {
2736 const fs_reg srcs
[] = {
2737 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD
),
2740 fs_reg payload
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
2741 bld
.LOAD_PAYLOAD(payload
, srcs
, ARRAY_SIZE(srcs
), 0);
2743 if (first_component
!= 0) {
2744 unsigned read_components
=
2745 num_components
+ first_component
;
2746 fs_reg tmp
= bld
.vgrf(dest
.type
, read_components
);
2747 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, tmp
,
2749 for (unsigned i
= 0; i
< num_components
; i
++) {
2750 bld
.MOV(offset(dest
, bld
, i
),
2751 offset(tmp
, bld
, i
+ first_component
));
2754 inst
= bld
.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT
, dest
,
2758 inst
->offset
= imm_offset
;
2759 inst
->size_written
= (num_components
+ first_component
) *
2760 inst
->dst
.component_size(inst
->exec_size
);
2762 /* If we are reading 64-bit data using 32-bit read messages we need
2763 * build proper 64-bit data elements by shuffling the low and high
2764 * 32-bit components around like we do for other things like UBOs
2767 if (type_sz(dest
.type
) == 8) {
2768 shuffle_32bit_load_result_to_64bit_data(
2769 bld
, dest
, retype(dest
, BRW_REGISTER_TYPE_F
), num_components
);
2771 for (unsigned c
= 0; c
< num_components
; c
++) {
2772 bld
.MOV(offset(orig_dest
, bld
, iter
* 2 + c
),
2773 offset(dest
, bld
, c
));
2777 /* If we are loading double data and we need a second read message
2780 if (num_iterations
> 1) {
2781 num_components
= instr
->num_components
- 2;
2789 nir_emit_intrinsic(bld
, instr
);
2795 fs_visitor::nir_emit_gs_intrinsic(const fs_builder
&bld
,
2796 nir_intrinsic_instr
*instr
)
2798 assert(stage
== MESA_SHADER_GEOMETRY
);
2799 fs_reg indirect_offset
;
2802 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
2803 dest
= get_nir_dest(instr
->dest
);
2805 switch (instr
->intrinsic
) {
2806 case nir_intrinsic_load_primitive_id
:
2807 assert(stage
== MESA_SHADER_GEOMETRY
);
2808 assert(brw_gs_prog_data(prog_data
)->include_primitive_id
);
2809 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
),
2810 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD
));
2813 case nir_intrinsic_load_input
:
2814 unreachable("load_input intrinsics are invalid for the GS stage");
2816 case nir_intrinsic_load_per_vertex_input
:
2817 emit_gs_input_load(dest
, instr
->src
[0], instr
->const_index
[0],
2818 instr
->src
[1], instr
->num_components
,
2819 nir_intrinsic_component(instr
));
2822 case nir_intrinsic_emit_vertex_with_counter
:
2823 emit_gs_vertex(instr
->src
[0], instr
->const_index
[0]);
2826 case nir_intrinsic_end_primitive_with_counter
:
2827 emit_gs_end_primitive(instr
->src
[0]);
2830 case nir_intrinsic_set_vertex_count
:
2831 bld
.MOV(this->final_gs_vertex_count
, get_nir_src(instr
->src
[0]));
2834 case nir_intrinsic_load_invocation_id
: {
2835 fs_reg val
= nir_system_values
[SYSTEM_VALUE_INVOCATION_ID
];
2836 assert(val
.file
!= BAD_FILE
);
2837 dest
.type
= val
.type
;
2843 nir_emit_intrinsic(bld
, instr
);
2849 * Fetch the current render target layer index.
2852 fetch_render_target_array_index(const fs_builder
&bld
)
2854 if (bld
.shader
->devinfo
->gen
>= 6) {
2855 /* The render target array index is provided in the thread payload as
2856 * bits 26:16 of r0.0.
2858 const fs_reg idx
= bld
.vgrf(BRW_REGISTER_TYPE_UD
);
2859 bld
.AND(idx
, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE
, 0, 1),
2863 /* Pre-SNB we only ever render into the first layer of the framebuffer
2864 * since layered rendering is not implemented.
2866 return brw_imm_ud(0);
2871 * Fake non-coherent framebuffer read implemented using TXF to fetch from the
2872 * framebuffer at the current fragment coordinates and sample index.
2875 fs_visitor::emit_non_coherent_fb_read(const fs_builder
&bld
, const fs_reg
&dst
,
2878 const struct gen_device_info
*devinfo
= bld
.shader
->devinfo
;
2880 assert(bld
.shader
->stage
== MESA_SHADER_FRAGMENT
);
2881 const brw_wm_prog_key
*wm_key
=
2882 reinterpret_cast<const brw_wm_prog_key
*>(key
);
2883 assert(!wm_key
->coherent_fb_fetch
);
2884 const struct brw_wm_prog_data
*wm_prog_data
=
2885 brw_wm_prog_data(stage_prog_data
);
2887 /* Calculate the surface index relative to the start of the texture binding
2888 * table block, since that's what the texturing messages expect.
2890 const unsigned surface
= target
+
2891 wm_prog_data
->binding_table
.render_target_read_start
-
2892 wm_prog_data
->base
.binding_table
.texture_start
;
2894 brw_mark_surface_used(
2895 bld
.shader
->stage_prog_data
,
2896 wm_prog_data
->binding_table
.render_target_read_start
+ target
);
2898 /* Calculate the fragment coordinates. */
2899 const fs_reg coords
= bld
.vgrf(BRW_REGISTER_TYPE_UD
, 3);
2900 bld
.MOV(offset(coords
, bld
, 0), pixel_x
);
2901 bld
.MOV(offset(coords
, bld
, 1), pixel_y
);
2902 bld
.MOV(offset(coords
, bld
, 2), fetch_render_target_array_index(bld
));
2904 /* Calculate the sample index and MCS payload when multisampling. Luckily
2905 * the MCS fetch message behaves deterministically for UMS surfaces, so it
2906 * shouldn't be necessary to recompile based on whether the framebuffer is
2909 if (wm_key
->multisample_fbo
&&
2910 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
].file
== BAD_FILE
)
2911 nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
] = *emit_sampleid_setup();
2913 const fs_reg sample
= nir_system_values
[SYSTEM_VALUE_SAMPLE_ID
];
2914 const fs_reg mcs
= wm_key
->multisample_fbo
?
2915 emit_mcs_fetch(coords
, 3, brw_imm_ud(surface
)) : fs_reg();
2917 /* Use either a normal or a CMS texel fetch message depending on whether
2918 * the framebuffer is single or multisample. On SKL+ use the wide CMS
2919 * message just in case the framebuffer uses 16x multisampling, it should
2920 * be equivalent to the normal CMS fetch for lower multisampling modes.
2922 const opcode op
= !wm_key
->multisample_fbo
? SHADER_OPCODE_TXF_LOGICAL
:
2923 devinfo
->gen
>= 9 ? SHADER_OPCODE_TXF_CMS_W_LOGICAL
:
2924 SHADER_OPCODE_TXF_CMS_LOGICAL
;
2926 /* Emit the instruction. */
2927 const fs_reg srcs
[] = { coords
, fs_reg(), brw_imm_ud(0), fs_reg(),
2929 brw_imm_ud(surface
), brw_imm_ud(0),
2930 fs_reg(), brw_imm_ud(3), brw_imm_ud(0) };
2931 STATIC_ASSERT(ARRAY_SIZE(srcs
) == TEX_LOGICAL_NUM_SRCS
);
2933 fs_inst
*inst
= bld
.emit(op
, dst
, srcs
, ARRAY_SIZE(srcs
));
2934 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
2940 * Actual coherent framebuffer read implemented using the native render target
2941 * read message. Requires SKL+.
2944 emit_coherent_fb_read(const fs_builder
&bld
, const fs_reg
&dst
, unsigned target
)
2946 assert(bld
.shader
->devinfo
->gen
>= 9);
2947 fs_inst
*inst
= bld
.emit(FS_OPCODE_FB_READ_LOGICAL
, dst
);
2948 inst
->target
= target
;
2949 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
2955 alloc_temporary(const fs_builder
&bld
, unsigned size
, fs_reg
*regs
, unsigned n
)
2957 if (n
&& regs
[0].file
!= BAD_FILE
) {
2961 const fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_F
, size
);
2963 for (unsigned i
= 0; i
< n
; i
++)
2971 alloc_frag_output(fs_visitor
*v
, unsigned location
)
2973 assert(v
->stage
== MESA_SHADER_FRAGMENT
);
2974 const brw_wm_prog_key
*const key
=
2975 reinterpret_cast<const brw_wm_prog_key
*>(v
->key
);
2976 const unsigned l
= GET_FIELD(location
, BRW_NIR_FRAG_OUTPUT_LOCATION
);
2977 const unsigned i
= GET_FIELD(location
, BRW_NIR_FRAG_OUTPUT_INDEX
);
2979 if (i
> 0 || (key
->force_dual_color_blend
&& l
== FRAG_RESULT_DATA1
))
2980 return alloc_temporary(v
->bld
, 4, &v
->dual_src_output
, 1);
2982 else if (l
== FRAG_RESULT_COLOR
)
2983 return alloc_temporary(v
->bld
, 4, v
->outputs
,
2984 MAX2(key
->nr_color_regions
, 1));
2986 else if (l
== FRAG_RESULT_DEPTH
)
2987 return alloc_temporary(v
->bld
, 1, &v
->frag_depth
, 1);
2989 else if (l
== FRAG_RESULT_STENCIL
)
2990 return alloc_temporary(v
->bld
, 1, &v
->frag_stencil
, 1);
2992 else if (l
== FRAG_RESULT_SAMPLE_MASK
)
2993 return alloc_temporary(v
->bld
, 1, &v
->sample_mask
, 1);
2995 else if (l
>= FRAG_RESULT_DATA0
&&
2996 l
< FRAG_RESULT_DATA0
+ BRW_MAX_DRAW_BUFFERS
)
2997 return alloc_temporary(v
->bld
, 4,
2998 &v
->outputs
[l
- FRAG_RESULT_DATA0
], 1);
3001 unreachable("Invalid location");
3005 fs_visitor::nir_emit_fs_intrinsic(const fs_builder
&bld
,
3006 nir_intrinsic_instr
*instr
)
3008 assert(stage
== MESA_SHADER_FRAGMENT
);
3011 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3012 dest
= get_nir_dest(instr
->dest
);
3014 switch (instr
->intrinsic
) {
3015 case nir_intrinsic_load_front_face
:
3016 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
3017 *emit_frontfacing_interpolation());
3020 case nir_intrinsic_load_sample_pos
: {
3021 fs_reg sample_pos
= nir_system_values
[SYSTEM_VALUE_SAMPLE_POS
];
3022 assert(sample_pos
.file
!= BAD_FILE
);
3023 dest
.type
= sample_pos
.type
;
3024 bld
.MOV(dest
, sample_pos
);
3025 bld
.MOV(offset(dest
, bld
, 1), offset(sample_pos
, bld
, 1));
3029 case nir_intrinsic_load_layer_id
:
3030 dest
.type
= BRW_REGISTER_TYPE_UD
;
3031 bld
.MOV(dest
, fetch_render_target_array_index(bld
));
3034 case nir_intrinsic_load_helper_invocation
:
3035 case nir_intrinsic_load_sample_mask_in
:
3036 case nir_intrinsic_load_sample_id
: {
3037 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
3038 fs_reg val
= nir_system_values
[sv
];
3039 assert(val
.file
!= BAD_FILE
);
3040 dest
.type
= val
.type
;
3045 case nir_intrinsic_store_output
: {
3046 const fs_reg src
= get_nir_src(instr
->src
[0]);
3047 const nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
3048 assert(const_offset
&& "Indirect output stores not allowed");
3049 const unsigned location
= nir_intrinsic_base(instr
) +
3050 SET_FIELD(const_offset
->u32
[0], BRW_NIR_FRAG_OUTPUT_LOCATION
);
3051 const fs_reg new_dest
= retype(alloc_frag_output(this, location
),
3054 for (unsigned j
= 0; j
< instr
->num_components
; j
++)
3055 bld
.MOV(offset(new_dest
, bld
, nir_intrinsic_component(instr
) + j
),
3056 offset(src
, bld
, j
));
3061 case nir_intrinsic_load_output
: {
3062 const unsigned l
= GET_FIELD(nir_intrinsic_base(instr
),
3063 BRW_NIR_FRAG_OUTPUT_LOCATION
);
3064 assert(l
>= FRAG_RESULT_DATA0
);
3065 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
3066 assert(const_offset
&& "Indirect output loads not allowed");
3067 const unsigned target
= l
- FRAG_RESULT_DATA0
+ const_offset
->u32
[0];
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();
3108 case nir_intrinsic_load_input
: {
3109 /* load_input is only used for flat inputs */
3110 unsigned base
= nir_intrinsic_base(instr
);
3111 unsigned component
= nir_intrinsic_component(instr
);
3112 unsigned num_components
= instr
->num_components
;
3113 enum brw_reg_type type
= dest
.type
;
3115 /* Special case fields in the VUE header */
3116 if (base
== VARYING_SLOT_LAYER
)
3118 else if (base
== VARYING_SLOT_VIEWPORT
)
3121 if (nir_dest_bit_size(instr
->dest
) == 64) {
3122 /* const_index is in 32-bit type size units that could not be aligned
3123 * with DF. We need to read the double vector as if it was a float
3124 * vector of twice the number of components to fetch the right data.
3126 type
= BRW_REGISTER_TYPE_F
;
3127 num_components
*= 2;
3130 for (unsigned int i
= 0; i
< num_components
; i
++) {
3131 struct brw_reg interp
= interp_reg(base
, component
+ i
);
3132 interp
= suboffset(interp
, 3);
3133 bld
.emit(FS_OPCODE_CINTERP
, offset(retype(dest
, type
), bld
, i
),
3134 retype(fs_reg(interp
), type
));
3137 if (nir_dest_bit_size(instr
->dest
) == 64) {
3138 shuffle_32bit_load_result_to_64bit_data(bld
,
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 nir_const_value
*const_sample
= nir_src_as_const_value(instr
->src
[0]);
3159 unsigned msg_data
= const_sample
->i32
[0] << 4;
3161 emit_pixel_interpolater_send(bld
,
3162 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3165 brw_imm_ud(msg_data
),
3168 const fs_reg sample_src
= retype(get_nir_src(instr
->src
[0]),
3169 BRW_REGISTER_TYPE_UD
);
3171 if (nir_src_is_dynamically_uniform(instr
->src
[0])) {
3172 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
3173 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
3174 bld
.exec_all().group(1, 0)
3175 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
3176 emit_pixel_interpolater_send(bld
,
3177 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3183 /* Make a loop that sends a message to the pixel interpolater
3184 * for the sample number in each live channel. If there are
3185 * multiple channels with the same sample number then these
3186 * will be handled simultaneously with a single interation of
3189 bld
.emit(BRW_OPCODE_DO
);
3191 /* Get the next live sample number into sample_id_reg */
3192 const fs_reg sample_id
= bld
.emit_uniformize(sample_src
);
3194 /* Set the flag register so that we can perform the send
3195 * message on all channels that have the same sample number
3197 bld
.CMP(bld
.null_reg_ud(),
3198 sample_src
, sample_id
,
3199 BRW_CONDITIONAL_EQ
);
3200 const fs_reg msg_data
= vgrf(glsl_type::uint_type
);
3201 bld
.exec_all().group(1, 0)
3202 .SHL(msg_data
, sample_id
, brw_imm_ud(4u));
3204 emit_pixel_interpolater_send(bld
,
3205 FS_OPCODE_INTERPOLATE_AT_SAMPLE
,
3210 set_predicate(BRW_PREDICATE_NORMAL
, inst
);
3212 /* Continue the loop if there are any live channels left */
3213 set_predicate_inv(BRW_PREDICATE_NORMAL
,
3215 bld
.emit(BRW_OPCODE_WHILE
));
3221 case nir_intrinsic_load_barycentric_at_offset
: {
3222 const glsl_interp_mode interpolation
=
3223 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(instr
);
3225 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
3228 unsigned off_x
= MIN2((int)(const_offset
->f32
[0] * 16), 7) & 0xf;
3229 unsigned off_y
= MIN2((int)(const_offset
->f32
[1] * 16), 7) & 0xf;
3231 emit_pixel_interpolater_send(bld
,
3232 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET
,
3235 brw_imm_ud(off_x
| (off_y
<< 4)),
3238 fs_reg src
= vgrf(glsl_type::ivec2_type
);
3239 fs_reg offset_src
= retype(get_nir_src(instr
->src
[0]),
3240 BRW_REGISTER_TYPE_F
);
3241 for (int i
= 0; i
< 2; i
++) {
3242 fs_reg temp
= vgrf(glsl_type::float_type
);
3243 bld
.MUL(temp
, offset(offset_src
, bld
, i
), brw_imm_f(16.0f
));
3244 fs_reg itemp
= vgrf(glsl_type::int_type
);
3246 bld
.MOV(itemp
, temp
);
3248 /* Clamp the upper end of the range to +7/16.
3249 * ARB_gpu_shader5 requires that we support a maximum offset
3250 * of +0.5, which isn't representable in a S0.4 value -- if
3251 * we didn't clamp it, we'd end up with -8/16, which is the
3252 * opposite of what the shader author wanted.
3254 * This is legal due to ARB_gpu_shader5's quantization
3257 * "Not all values of <offset> may be supported; x and y
3258 * offsets may be rounded to fixed-point values with the
3259 * number of fraction bits given by the
3260 * implementation-dependent constant
3261 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3263 set_condmod(BRW_CONDITIONAL_L
,
3264 bld
.SEL(offset(src
, bld
, i
), itemp
, brw_imm_d(7)));
3267 const enum opcode opcode
= FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET
;
3268 emit_pixel_interpolater_send(bld
,
3278 case nir_intrinsic_load_interpolated_input
: {
3279 if (nir_intrinsic_base(instr
) == VARYING_SLOT_POS
) {
3280 emit_fragcoord_interpolation(dest
);
3284 assert(instr
->src
[0].ssa
&&
3285 instr
->src
[0].ssa
->parent_instr
->type
== nir_instr_type_intrinsic
);
3286 nir_intrinsic_instr
*bary_intrinsic
=
3287 nir_instr_as_intrinsic(instr
->src
[0].ssa
->parent_instr
);
3288 nir_intrinsic_op bary_intrin
= bary_intrinsic
->intrinsic
;
3289 enum glsl_interp_mode interp_mode
=
3290 (enum glsl_interp_mode
) nir_intrinsic_interp_mode(bary_intrinsic
);
3293 if (bary_intrin
== nir_intrinsic_load_barycentric_at_offset
||
3294 bary_intrin
== nir_intrinsic_load_barycentric_at_sample
) {
3295 /* Use the result of the PI message */
3296 dst_xy
= retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_F
);
3298 /* Use the delta_xy values computed from the payload */
3299 enum brw_barycentric_mode bary
=
3300 brw_barycentric_mode(interp_mode
, bary_intrin
);
3302 dst_xy
= this->delta_xy
[bary
];
3305 for (unsigned int i
= 0; i
< instr
->num_components
; i
++) {
3307 fs_reg(interp_reg(nir_intrinsic_base(instr
),
3308 nir_intrinsic_component(instr
) + i
));
3309 interp
.type
= BRW_REGISTER_TYPE_F
;
3310 dest
.type
= BRW_REGISTER_TYPE_F
;
3312 if (devinfo
->gen
< 6 && interp_mode
== INTERP_MODE_SMOOTH
) {
3313 fs_reg tmp
= vgrf(glsl_type::float_type
);
3314 bld
.emit(FS_OPCODE_LINTERP
, tmp
, dst_xy
, interp
);
3315 bld
.MUL(offset(dest
, bld
, i
), tmp
, this->pixel_w
);
3317 bld
.emit(FS_OPCODE_LINTERP
, offset(dest
, bld
, i
), dst_xy
, interp
);
3324 nir_emit_intrinsic(bld
, instr
);
3330 fs_visitor::nir_emit_cs_intrinsic(const fs_builder
&bld
,
3331 nir_intrinsic_instr
*instr
)
3333 assert(stage
== MESA_SHADER_COMPUTE
);
3334 struct brw_cs_prog_data
*cs_prog_data
= brw_cs_prog_data(prog_data
);
3337 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3338 dest
= get_nir_dest(instr
->dest
);
3340 switch (instr
->intrinsic
) {
3341 case nir_intrinsic_barrier
:
3343 cs_prog_data
->uses_barrier
= true;
3346 case nir_intrinsic_load_local_invocation_id
:
3347 case nir_intrinsic_load_work_group_id
: {
3348 gl_system_value sv
= nir_system_value_from_intrinsic(instr
->intrinsic
);
3349 fs_reg val
= nir_system_values
[sv
];
3350 assert(val
.file
!= BAD_FILE
);
3351 dest
.type
= val
.type
;
3352 for (unsigned i
= 0; i
< 3; i
++)
3353 bld
.MOV(offset(dest
, bld
, i
), offset(val
, bld
, i
));
3357 case nir_intrinsic_load_num_work_groups
: {
3358 const unsigned surface
=
3359 cs_prog_data
->binding_table
.work_groups_start
;
3361 cs_prog_data
->uses_num_work_groups
= true;
3363 fs_reg surf_index
= brw_imm_ud(surface
);
3364 brw_mark_surface_used(prog_data
, surface
);
3366 /* Read the 3 GLuint components of gl_NumWorkGroups */
3367 for (unsigned i
= 0; i
< 3; i
++) {
3368 fs_reg read_result
=
3369 emit_untyped_read(bld
, surf_index
,
3371 1 /* dims */, 1 /* size */,
3372 BRW_PREDICATE_NONE
);
3373 read_result
.type
= dest
.type
;
3374 bld
.MOV(dest
, read_result
);
3375 dest
= offset(dest
, bld
, 1);
3380 case nir_intrinsic_shared_atomic_add
:
3381 nir_emit_shared_atomic(bld
, BRW_AOP_ADD
, instr
);
3383 case nir_intrinsic_shared_atomic_imin
:
3384 nir_emit_shared_atomic(bld
, BRW_AOP_IMIN
, instr
);
3386 case nir_intrinsic_shared_atomic_umin
:
3387 nir_emit_shared_atomic(bld
, BRW_AOP_UMIN
, instr
);
3389 case nir_intrinsic_shared_atomic_imax
:
3390 nir_emit_shared_atomic(bld
, BRW_AOP_IMAX
, instr
);
3392 case nir_intrinsic_shared_atomic_umax
:
3393 nir_emit_shared_atomic(bld
, BRW_AOP_UMAX
, instr
);
3395 case nir_intrinsic_shared_atomic_and
:
3396 nir_emit_shared_atomic(bld
, BRW_AOP_AND
, instr
);
3398 case nir_intrinsic_shared_atomic_or
:
3399 nir_emit_shared_atomic(bld
, BRW_AOP_OR
, instr
);
3401 case nir_intrinsic_shared_atomic_xor
:
3402 nir_emit_shared_atomic(bld
, BRW_AOP_XOR
, instr
);
3404 case nir_intrinsic_shared_atomic_exchange
:
3405 nir_emit_shared_atomic(bld
, BRW_AOP_MOV
, instr
);
3407 case nir_intrinsic_shared_atomic_comp_swap
:
3408 nir_emit_shared_atomic(bld
, BRW_AOP_CMPWR
, instr
);
3411 case nir_intrinsic_load_shared
: {
3412 assert(devinfo
->gen
>= 7);
3414 fs_reg surf_index
= brw_imm_ud(GEN7_BTI_SLM
);
3416 /* Get the offset to read from */
3418 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
3420 offset_reg
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u32
[0]);
3422 offset_reg
= vgrf(glsl_type::uint_type
);
3424 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
3425 brw_imm_ud(instr
->const_index
[0]));
3428 /* Read the vector */
3429 do_untyped_vector_read(bld
, dest
, surf_index
, offset_reg
,
3430 instr
->num_components
);
3434 case nir_intrinsic_store_shared
: {
3435 assert(devinfo
->gen
>= 7);
3438 fs_reg surf_index
= brw_imm_ud(GEN7_BTI_SLM
);
3441 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
3444 unsigned writemask
= instr
->const_index
[1];
3446 /* get_nir_src() retypes to integer. Be wary of 64-bit types though
3447 * since the untyped writes below operate in units of 32-bits, which
3448 * means that we need to write twice as many components each time.
3449 * Also, we have to suffle 64-bit data to be in the appropriate layout
3450 * expected by our 32-bit write messages.
3452 unsigned type_size
= 4;
3453 unsigned bit_size
= instr
->src
[0].is_ssa
?
3454 instr
->src
[0].ssa
->bit_size
: instr
->src
[0].reg
.reg
->bit_size
;
3455 if (bit_size
== 64) {
3458 fs_reg(VGRF
, alloc
.allocate(alloc
.sizes
[val_reg
.nr
]), val_reg
.type
);
3459 shuffle_64bit_data_for_32bit_write(
3461 retype(tmp
, BRW_REGISTER_TYPE_F
),
3462 retype(val_reg
, BRW_REGISTER_TYPE_DF
),
3463 instr
->num_components
);
3467 unsigned type_slots
= type_size
/ 4;
3469 /* Combine groups of consecutive enabled channels in one write
3470 * message. We use ffs to find the first enabled channel and then ffs on
3471 * the bit-inverse, down-shifted writemask to determine the length of
3472 * the block of enabled bits.
3475 unsigned first_component
= ffs(writemask
) - 1;
3476 unsigned length
= ffs(~(writemask
>> first_component
)) - 1;
3478 /* We can't write more than 2 64-bit components at once. Limit the
3479 * length of the write to what we can do and let the next iteration
3483 length
= MIN2(2, length
);
3486 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
3488 offset_reg
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u32
[0] +
3489 type_size
* first_component
);
3491 offset_reg
= vgrf(glsl_type::uint_type
);
3493 retype(get_nir_src(instr
->src
[1]), BRW_REGISTER_TYPE_UD
),
3494 brw_imm_ud(instr
->const_index
[0] + type_size
* first_component
));
3497 emit_untyped_write(bld
, surf_index
, offset_reg
,
3498 offset(val_reg
, bld
, first_component
* type_slots
),
3499 1 /* dims */, length
* type_slots
,
3500 BRW_PREDICATE_NONE
);
3502 /* Clear the bits in the writemask that we just wrote, then try
3503 * again to see if more channels are left.
3505 writemask
&= (15 << (first_component
+ length
));
3512 nir_emit_intrinsic(bld
, instr
);
3518 fs_visitor::nir_emit_intrinsic(const fs_builder
&bld
, nir_intrinsic_instr
*instr
)
3521 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
3522 dest
= get_nir_dest(instr
->dest
);
3524 switch (instr
->intrinsic
) {
3525 case nir_intrinsic_atomic_counter_inc
:
3526 case nir_intrinsic_atomic_counter_dec
:
3527 case nir_intrinsic_atomic_counter_read
:
3528 case nir_intrinsic_atomic_counter_add
:
3529 case nir_intrinsic_atomic_counter_min
:
3530 case nir_intrinsic_atomic_counter_max
:
3531 case nir_intrinsic_atomic_counter_and
:
3532 case nir_intrinsic_atomic_counter_or
:
3533 case nir_intrinsic_atomic_counter_xor
:
3534 case nir_intrinsic_atomic_counter_exchange
:
3535 case nir_intrinsic_atomic_counter_comp_swap
: {
3536 if (stage
== MESA_SHADER_FRAGMENT
&&
3537 instr
->intrinsic
!= nir_intrinsic_atomic_counter_read
)
3538 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
3540 /* Get some metadata from the image intrinsic. */
3541 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
3543 /* Get the arguments of the atomic intrinsic. */
3544 const fs_reg offset
= get_nir_src(instr
->src
[0]);
3545 const unsigned surface
= (stage_prog_data
->binding_table
.abo_start
+
3546 instr
->const_index
[0]);
3547 const fs_reg src0
= (info
->num_srcs
>= 2
3548 ? get_nir_src(instr
->src
[1]) : fs_reg());
3549 const fs_reg src1
= (info
->num_srcs
>= 3
3550 ? get_nir_src(instr
->src
[2]) : fs_reg());
3553 assert(info
->num_srcs
<= 3);
3555 /* Emit a surface read or atomic op. */
3556 if (instr
->intrinsic
== nir_intrinsic_atomic_counter_read
) {
3557 tmp
= emit_untyped_read(bld
, brw_imm_ud(surface
), offset
, 1, 1);
3559 tmp
= emit_untyped_atomic(bld
, brw_imm_ud(surface
), offset
, src0
,
3561 get_atomic_counter_op(instr
->intrinsic
));
3564 /* Assign the result. */
3565 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_UD
), tmp
);
3567 /* Mark the surface as used. */
3568 brw_mark_surface_used(stage_prog_data
, surface
);
3572 case nir_intrinsic_image_load
:
3573 case nir_intrinsic_image_store
:
3574 case nir_intrinsic_image_atomic_add
:
3575 case nir_intrinsic_image_atomic_min
:
3576 case nir_intrinsic_image_atomic_max
:
3577 case nir_intrinsic_image_atomic_and
:
3578 case nir_intrinsic_image_atomic_or
:
3579 case nir_intrinsic_image_atomic_xor
:
3580 case nir_intrinsic_image_atomic_exchange
:
3581 case nir_intrinsic_image_atomic_comp_swap
: {
3582 using namespace image_access
;
3584 if (stage
== MESA_SHADER_FRAGMENT
&&
3585 instr
->intrinsic
!= nir_intrinsic_image_load
)
3586 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
3588 /* Get the referenced image variable and type. */
3589 const nir_variable
*var
= instr
->variables
[0]->var
;
3590 const glsl_type
*type
= var
->type
->without_array();
3591 const brw_reg_type base_type
= get_image_base_type(type
);
3593 /* Get some metadata from the image intrinsic. */
3594 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
3595 const unsigned arr_dims
= type
->sampler_array
? 1 : 0;
3596 const unsigned surf_dims
= type
->coordinate_components() - arr_dims
;
3597 const unsigned format
= var
->data
.image
.format
;
3599 /* Get the arguments of the image intrinsic. */
3600 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
3601 const fs_reg addr
= retype(get_nir_src(instr
->src
[0]),
3602 BRW_REGISTER_TYPE_UD
);
3603 const fs_reg src0
= (info
->num_srcs
>= 3 ?
3604 retype(get_nir_src(instr
->src
[2]), base_type
) :
3606 const fs_reg src1
= (info
->num_srcs
>= 4 ?
3607 retype(get_nir_src(instr
->src
[3]), base_type
) :
3611 /* Emit an image load, store or atomic op. */
3612 if (instr
->intrinsic
== nir_intrinsic_image_load
)
3613 tmp
= emit_image_load(bld
, image
, addr
, surf_dims
, arr_dims
, format
);
3615 else if (instr
->intrinsic
== nir_intrinsic_image_store
)
3616 emit_image_store(bld
, image
, addr
, src0
, surf_dims
, arr_dims
,
3617 var
->data
.image
.write_only
? GL_NONE
: format
);
3620 tmp
= emit_image_atomic(bld
, image
, addr
, src0
, src1
,
3621 surf_dims
, arr_dims
, info
->dest_components
,
3622 get_image_atomic_op(instr
->intrinsic
, type
));
3624 /* Assign the result. */
3625 for (unsigned c
= 0; c
< info
->dest_components
; ++c
)
3626 bld
.MOV(offset(retype(dest
, base_type
), bld
, c
),
3627 offset(tmp
, bld
, c
));
3631 case nir_intrinsic_memory_barrier_atomic_counter
:
3632 case nir_intrinsic_memory_barrier_buffer
:
3633 case nir_intrinsic_memory_barrier_image
:
3634 case nir_intrinsic_memory_barrier
: {
3635 const fs_builder ubld
= bld
.group(8, 0);
3636 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
3637 ubld
.emit(SHADER_OPCODE_MEMORY_FENCE
, tmp
)
3638 ->size_written
= 2 * REG_SIZE
;
3642 case nir_intrinsic_group_memory_barrier
:
3643 case nir_intrinsic_memory_barrier_shared
:
3644 /* We treat these workgroup-level barriers as no-ops. This should be
3645 * safe at present and as long as:
3647 * - Memory access instructions are not subsequently reordered by the
3648 * compiler back-end.
3650 * - All threads from a given compute shader workgroup fit within a
3651 * single subslice and therefore talk to the same HDC shared unit
3652 * what supposedly guarantees ordering and coherency between threads
3653 * from the same workgroup. This may change in the future when we
3654 * start splitting workgroups across multiple subslices.
3656 * - The context is not in fault-and-stream mode, which could cause
3657 * memory transactions (including to SLM) prior to the barrier to be
3658 * replayed after the barrier if a pagefault occurs. This shouldn't
3659 * be a problem up to and including SKL because fault-and-stream is
3660 * not usable due to hardware issues, but that's likely to change in
3665 case nir_intrinsic_shader_clock
: {
3666 /* We cannot do anything if there is an event, so ignore it for now */
3667 const fs_reg shader_clock
= get_timestamp(bld
);
3668 const fs_reg srcs
[] = { component(shader_clock
, 0),
3669 component(shader_clock
, 1) };
3670 bld
.LOAD_PAYLOAD(dest
, srcs
, ARRAY_SIZE(srcs
), 0);
3674 case nir_intrinsic_image_size
: {
3675 /* Get the referenced image variable and type. */
3676 const nir_variable
*var
= instr
->variables
[0]->var
;
3677 const glsl_type
*type
= var
->type
->without_array();
3679 /* Get the size of the image. */
3680 const fs_reg image
= get_nir_image_deref(instr
->variables
[0]);
3681 const fs_reg size
= offset(image
, bld
, BRW_IMAGE_PARAM_SIZE_OFFSET
);
3683 /* For 1DArray image types, the array index is stored in the Z component.
3684 * Fix this by swizzling the Z component to the Y component.
3686 const bool is_1d_array_image
=
3687 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_1D
&&
3688 type
->sampler_array
;
3690 /* For CubeArray images, we should count the number of cubes instead
3691 * of the number of faces. Fix it by dividing the (Z component) by 6.
3693 const bool is_cube_array_image
=
3694 type
->sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
&&
3695 type
->sampler_array
;
3697 /* Copy all the components. */
3698 for (unsigned c
= 0; c
< instr
->dest
.ssa
.num_components
; ++c
) {
3699 if ((int)c
>= type
->coordinate_components()) {
3700 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
3702 } else if (c
== 1 && is_1d_array_image
) {
3703 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
3704 offset(size
, bld
, 2));
3705 } else if (c
== 2 && is_cube_array_image
) {
3706 bld
.emit(SHADER_OPCODE_INT_QUOTIENT
,
3707 offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
3708 offset(size
, bld
, c
), brw_imm_d(6));
3710 bld
.MOV(offset(retype(dest
, BRW_REGISTER_TYPE_D
), bld
, c
),
3711 offset(size
, bld
, c
));
3718 case nir_intrinsic_image_samples
:
3719 /* The driver does not support multi-sampled images. */
3720 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(1));
3723 case nir_intrinsic_load_uniform
: {
3724 /* Offsets are in bytes but they should always be multiples of 4 */
3725 assert(instr
->const_index
[0] % 4 == 0);
3727 fs_reg
src(UNIFORM
, instr
->const_index
[0] / 4, dest
.type
);
3729 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
3731 /* Offsets are in bytes but they should always be multiples of 4 */
3732 assert(const_offset
->u32
[0] % 4 == 0);
3733 src
.offset
= const_offset
->u32
[0];
3735 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3736 bld
.MOV(offset(dest
, bld
, j
), offset(src
, bld
, j
));
3739 fs_reg indirect
= retype(get_nir_src(instr
->src
[0]),
3740 BRW_REGISTER_TYPE_UD
);
3742 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
3743 * go past the end of the uniform. In order to keep the n'th
3744 * component from running past, we subtract off the size of all but
3745 * one component of the vector.
3747 assert(instr
->const_index
[1] >=
3748 instr
->num_components
* (int) type_sz(dest
.type
));
3749 unsigned read_size
= instr
->const_index
[1] -
3750 (instr
->num_components
- 1) * type_sz(dest
.type
);
3752 bool supports_64bit_indirects
=
3753 !devinfo
->is_cherryview
&& !gen_device_info_is_9lp(devinfo
);
3755 if (type_sz(dest
.type
) != 8 || supports_64bit_indirects
) {
3756 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3757 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
3758 offset(dest
, bld
, j
), offset(src
, bld
, j
),
3759 indirect
, brw_imm_ud(read_size
));
3762 const unsigned num_mov_indirects
=
3763 type_sz(dest
.type
) / type_sz(BRW_REGISTER_TYPE_UD
);
3764 /* We read a little bit less per MOV INDIRECT, as they are now
3765 * 32-bits ones instead of 64-bit. Fix read_size then.
3767 const unsigned read_size_32bit
= read_size
-
3768 (num_mov_indirects
- 1) * type_sz(BRW_REGISTER_TYPE_UD
);
3769 for (unsigned j
= 0; j
< instr
->num_components
; j
++) {
3770 for (unsigned i
= 0; i
< num_mov_indirects
; i
++) {
3771 bld
.emit(SHADER_OPCODE_MOV_INDIRECT
,
3772 subscript(offset(dest
, bld
, j
), BRW_REGISTER_TYPE_UD
, i
),
3773 subscript(offset(src
, bld
, j
), BRW_REGISTER_TYPE_UD
, i
),
3774 indirect
, brw_imm_ud(read_size_32bit
));
3782 case nir_intrinsic_load_ubo
: {
3783 nir_const_value
*const_index
= nir_src_as_const_value(instr
->src
[0]);
3787 const unsigned index
= stage_prog_data
->binding_table
.ubo_start
+
3788 const_index
->u32
[0];
3789 surf_index
= brw_imm_ud(index
);
3790 brw_mark_surface_used(prog_data
, index
);
3792 /* The block index is not a constant. Evaluate the index expression
3793 * per-channel and add the base UBO index; we have to select a value
3794 * from any live channel.
3796 surf_index
= vgrf(glsl_type::uint_type
);
3797 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
3798 brw_imm_ud(stage_prog_data
->binding_table
.ubo_start
));
3799 surf_index
= bld
.emit_uniformize(surf_index
);
3801 /* Assume this may touch any UBO. It would be nice to provide
3802 * a tighter bound, but the array information is already lowered away.
3804 brw_mark_surface_used(prog_data
,
3805 stage_prog_data
->binding_table
.ubo_start
+
3806 nir
->info
.num_ubos
- 1);
3809 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
3810 if (const_offset
== NULL
) {
3811 fs_reg base_offset
= retype(get_nir_src(instr
->src
[1]),
3812 BRW_REGISTER_TYPE_UD
);
3814 for (int i
= 0; i
< instr
->num_components
; i
++)
3815 VARYING_PULL_CONSTANT_LOAD(bld
, offset(dest
, bld
, i
), surf_index
,
3816 base_offset
, i
* type_sz(dest
.type
));
3818 /* Even if we are loading doubles, a pull constant load will load
3819 * a 32-bit vec4, so should only reserve vgrf space for that. If we
3820 * need to load a full dvec4 we will have to emit 2 loads. This is
3821 * similar to demote_pull_constants(), except that in that case we
3822 * see individual accesses to each component of the vector and then
3823 * we let CSE deal with duplicate loads. Here we see a vector access
3824 * and we have to split it if necessary.
3826 const unsigned type_size
= type_sz(dest
.type
);
3828 /* See if we've selected this as a push constant candidate */
3830 const unsigned ubo_block
= const_index
->u32
[0];
3831 const unsigned offset_256b
= const_offset
->u32
[0] / 32;
3834 for (int i
= 0; i
< 4; i
++) {
3835 const struct brw_ubo_range
*range
= &prog_data
->ubo_ranges
[i
];
3836 if (range
->block
== ubo_block
&&
3837 offset_256b
>= range
->start
&&
3838 offset_256b
< range
->start
+ range
->length
) {
3840 push_reg
= fs_reg(UNIFORM
, UBO_START
+ i
, dest
.type
);
3841 push_reg
.offset
= const_offset
->u32
[0] - 32 * range
->start
;
3846 if (push_reg
.file
!= BAD_FILE
) {
3847 for (unsigned i
= 0; i
< instr
->num_components
; i
++) {
3848 bld
.MOV(offset(dest
, bld
, i
),
3849 byte_offset(push_reg
, i
* type_size
));
3855 const unsigned block_sz
= 64; /* Fetch one cacheline at a time. */
3856 const fs_builder ubld
= bld
.exec_all().group(block_sz
/ 4, 0);
3857 const fs_reg packed_consts
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
3859 for (unsigned c
= 0; c
< instr
->num_components
;) {
3860 const unsigned base
= const_offset
->u32
[0] + c
* type_size
;
3861 /* Number of usable components in the next block-aligned load. */
3862 const unsigned count
= MIN2(instr
->num_components
- c
,
3863 (block_sz
- base
% block_sz
) / type_size
);
3865 ubld
.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
,
3866 packed_consts
, surf_index
,
3867 brw_imm_ud(base
& ~(block_sz
- 1)));
3869 const fs_reg consts
=
3870 retype(byte_offset(packed_consts
, base
& (block_sz
- 1)),
3873 for (unsigned d
= 0; d
< count
; d
++)
3874 bld
.MOV(offset(dest
, bld
, c
+ d
), component(consts
, d
));
3882 case nir_intrinsic_load_ssbo
: {
3883 assert(devinfo
->gen
>= 7);
3885 nir_const_value
*const_uniform_block
=
3886 nir_src_as_const_value(instr
->src
[0]);
3889 if (const_uniform_block
) {
3890 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
3891 const_uniform_block
->u32
[0];
3892 surf_index
= brw_imm_ud(index
);
3893 brw_mark_surface_used(prog_data
, index
);
3895 surf_index
= vgrf(glsl_type::uint_type
);
3896 bld
.ADD(surf_index
, get_nir_src(instr
->src
[0]),
3897 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
3899 /* Assume this may touch any UBO. It would be nice to provide
3900 * a tighter bound, but the array information is already lowered away.
3902 brw_mark_surface_used(prog_data
,
3903 stage_prog_data
->binding_table
.ssbo_start
+
3904 nir
->info
.num_ssbos
- 1);
3908 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
3910 offset_reg
= brw_imm_ud(const_offset
->u32
[0]);
3912 offset_reg
= get_nir_src(instr
->src
[1]);
3915 /* Read the vector */
3916 do_untyped_vector_read(bld
, dest
, surf_index
, offset_reg
,
3917 instr
->num_components
);
3922 case nir_intrinsic_store_ssbo
: {
3923 assert(devinfo
->gen
>= 7);
3925 if (stage
== MESA_SHADER_FRAGMENT
)
3926 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
3930 nir_const_value
*const_uniform_block
=
3931 nir_src_as_const_value(instr
->src
[1]);
3932 if (const_uniform_block
) {
3933 unsigned index
= stage_prog_data
->binding_table
.ssbo_start
+
3934 const_uniform_block
->u32
[0];
3935 surf_index
= brw_imm_ud(index
);
3936 brw_mark_surface_used(prog_data
, index
);
3938 surf_index
= vgrf(glsl_type::uint_type
);
3939 bld
.ADD(surf_index
, get_nir_src(instr
->src
[1]),
3940 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
3942 brw_mark_surface_used(prog_data
,
3943 stage_prog_data
->binding_table
.ssbo_start
+
3944 nir
->info
.num_ssbos
- 1);
3948 fs_reg val_reg
= get_nir_src(instr
->src
[0]);
3951 unsigned writemask
= instr
->const_index
[0];
3953 /* get_nir_src() retypes to integer. Be wary of 64-bit types though
3954 * since the untyped writes below operate in units of 32-bits, which
3955 * means that we need to write twice as many components each time.
3956 * Also, we have to suffle 64-bit data to be in the appropriate layout
3957 * expected by our 32-bit write messages.
3959 unsigned type_size
= 4;
3960 unsigned bit_size
= instr
->src
[0].is_ssa
?
3961 instr
->src
[0].ssa
->bit_size
: instr
->src
[0].reg
.reg
->bit_size
;
3962 if (bit_size
== 64) {
3965 fs_reg(VGRF
, alloc
.allocate(alloc
.sizes
[val_reg
.nr
]), val_reg
.type
);
3966 shuffle_64bit_data_for_32bit_write(bld
,
3967 retype(tmp
, BRW_REGISTER_TYPE_F
),
3968 retype(val_reg
, BRW_REGISTER_TYPE_DF
),
3969 instr
->num_components
);
3973 unsigned type_slots
= type_size
/ 4;
3975 /* Combine groups of consecutive enabled channels in one write
3976 * message. We use ffs to find the first enabled channel and then ffs on
3977 * the bit-inverse, down-shifted writemask to determine the length of
3978 * the block of enabled bits.
3981 unsigned first_component
= ffs(writemask
) - 1;
3982 unsigned length
= ffs(~(writemask
>> first_component
)) - 1;
3984 /* We can't write more than 2 64-bit components at once. Limit the
3985 * length of the write to what we can do and let the next iteration
3989 length
= MIN2(2, length
);
3992 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[2]);
3994 offset_reg
= brw_imm_ud(const_offset
->u32
[0] +
3995 type_size
* first_component
);
3997 offset_reg
= vgrf(glsl_type::uint_type
);
3999 retype(get_nir_src(instr
->src
[2]), BRW_REGISTER_TYPE_UD
),
4000 brw_imm_ud(type_size
* first_component
));
4004 emit_untyped_write(bld
, surf_index
, offset_reg
,
4005 offset(val_reg
, bld
, first_component
* type_slots
),
4006 1 /* dims */, length
* type_slots
,
4007 BRW_PREDICATE_NONE
);
4009 /* Clear the bits in the writemask that we just wrote, then try
4010 * again to see if more channels are left.
4012 writemask
&= (15 << (first_component
+ length
));
4017 case nir_intrinsic_store_output
: {
4018 fs_reg src
= get_nir_src(instr
->src
[0]);
4020 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[1]);
4021 assert(const_offset
&& "Indirect output stores not allowed");
4022 fs_reg new_dest
= retype(offset(outputs
[instr
->const_index
[0]], bld
,
4023 4 * const_offset
->u32
[0]), src
.type
);
4025 unsigned num_components
= instr
->num_components
;
4026 unsigned first_component
= nir_intrinsic_component(instr
);
4027 unsigned bit_size
= instr
->src
[0].is_ssa
?
4028 instr
->src
[0].ssa
->bit_size
: instr
->src
[0].reg
.reg
->bit_size
;
4029 if (bit_size
== 64) {
4031 fs_reg(VGRF
, alloc
.allocate(2 * num_components
),
4032 BRW_REGISTER_TYPE_F
);
4033 shuffle_64bit_data_for_32bit_write(
4034 bld
, tmp
, retype(src
, BRW_REGISTER_TYPE_DF
), num_components
);
4035 src
= retype(tmp
, src
.type
);
4036 num_components
*= 2;
4039 for (unsigned j
= 0; j
< num_components
; j
++) {
4040 bld
.MOV(offset(new_dest
, bld
, j
+ first_component
),
4041 offset(src
, bld
, j
));
4046 case nir_intrinsic_ssbo_atomic_add
:
4047 nir_emit_ssbo_atomic(bld
, BRW_AOP_ADD
, instr
);
4049 case nir_intrinsic_ssbo_atomic_imin
:
4050 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMIN
, instr
);
4052 case nir_intrinsic_ssbo_atomic_umin
:
4053 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMIN
, instr
);
4055 case nir_intrinsic_ssbo_atomic_imax
:
4056 nir_emit_ssbo_atomic(bld
, BRW_AOP_IMAX
, instr
);
4058 case nir_intrinsic_ssbo_atomic_umax
:
4059 nir_emit_ssbo_atomic(bld
, BRW_AOP_UMAX
, instr
);
4061 case nir_intrinsic_ssbo_atomic_and
:
4062 nir_emit_ssbo_atomic(bld
, BRW_AOP_AND
, instr
);
4064 case nir_intrinsic_ssbo_atomic_or
:
4065 nir_emit_ssbo_atomic(bld
, BRW_AOP_OR
, instr
);
4067 case nir_intrinsic_ssbo_atomic_xor
:
4068 nir_emit_ssbo_atomic(bld
, BRW_AOP_XOR
, instr
);
4070 case nir_intrinsic_ssbo_atomic_exchange
:
4071 nir_emit_ssbo_atomic(bld
, BRW_AOP_MOV
, instr
);
4073 case nir_intrinsic_ssbo_atomic_comp_swap
:
4074 nir_emit_ssbo_atomic(bld
, BRW_AOP_CMPWR
, instr
);
4077 case nir_intrinsic_get_buffer_size
: {
4078 nir_const_value
*const_uniform_block
= nir_src_as_const_value(instr
->src
[0]);
4079 unsigned ssbo_index
= const_uniform_block
? const_uniform_block
->u32
[0] : 0;
4081 /* A resinfo's sampler message is used to get the buffer size. The
4082 * SIMD8's writeback message consists of four registers and SIMD16's
4083 * writeback message consists of 8 destination registers (two per each
4084 * component). Because we are only interested on the first channel of
4085 * the first returned component, where resinfo returns the buffer size
4086 * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
4087 * the dispatch width.
4089 const fs_builder ubld
= bld
.exec_all().group(8, 0);
4090 fs_reg src_payload
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
);
4091 fs_reg ret_payload
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 4);
4094 ubld
.MOV(src_payload
, brw_imm_d(0));
4096 const unsigned index
= prog_data
->binding_table
.ssbo_start
+ ssbo_index
;
4097 fs_inst
*inst
= ubld
.emit(FS_OPCODE_GET_BUFFER_SIZE
, ret_payload
,
4098 src_payload
, brw_imm_ud(index
));
4099 inst
->header_size
= 0;
4101 inst
->size_written
= 4 * REG_SIZE
;
4103 bld
.MOV(retype(dest
, ret_payload
.type
), component(ret_payload
, 0));
4104 brw_mark_surface_used(prog_data
, index
);
4108 case nir_intrinsic_load_subgroup_size
:
4109 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
), brw_imm_d(dispatch_width
));
4112 case nir_intrinsic_load_subgroup_invocation
: {
4113 fs_reg tmp
= bld
.vgrf(BRW_REGISTER_TYPE_UW
);
4114 dest
= retype(dest
, BRW_REGISTER_TYPE_UD
);
4115 const fs_builder allbld8
= bld
.group(8, 0).exec_all();
4116 allbld8
.MOV(tmp
, brw_imm_v(0x76543210));
4117 if (dispatch_width
> 8)
4118 allbld8
.ADD(byte_offset(tmp
, 16), tmp
, brw_imm_uw(8u));
4119 if (dispatch_width
> 16) {
4120 const fs_builder allbld16
= bld
.group(16, 0).exec_all();
4121 allbld16
.ADD(byte_offset(tmp
, 32), tmp
, brw_imm_uw(16u));
4127 case nir_intrinsic_load_subgroup_eq_mask
:
4128 case nir_intrinsic_load_subgroup_ge_mask
:
4129 case nir_intrinsic_load_subgroup_gt_mask
:
4130 case nir_intrinsic_load_subgroup_le_mask
:
4131 case nir_intrinsic_load_subgroup_lt_mask
:
4132 unreachable("not reached");
4134 case nir_intrinsic_vote_any
: {
4135 const fs_builder ubld
= bld
.exec_all();
4137 /* The any/all predicates do not consider channel enables. To prevent
4138 * dead channels from affecting the result, we initialize the flag with
4139 * with the identity value for the logical operation.
4141 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0));
4142 bld
.CMP(bld
.null_reg_d(), get_nir_src(instr
->src
[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ
);
4143 bld
.MOV(dest
, brw_imm_d(-1));
4144 set_predicate(dispatch_width
== 8 ?
4145 BRW_PREDICATE_ALIGN1_ANY8H
:
4146 BRW_PREDICATE_ALIGN1_ANY16H
,
4147 bld
.SEL(dest
, dest
, brw_imm_d(0)));
4150 case nir_intrinsic_vote_all
: {
4151 const fs_builder ubld
= bld
.exec_all();
4153 /* The any/all predicates do not consider channel enables. To prevent
4154 * dead channels from affecting the result, we initialize the flag with
4155 * with the identity value for the logical operation.
4157 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4158 bld
.CMP(bld
.null_reg_d(), get_nir_src(instr
->src
[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ
);
4159 bld
.MOV(dest
, brw_imm_d(-1));
4160 set_predicate(dispatch_width
== 8 ?
4161 BRW_PREDICATE_ALIGN1_ALL8H
:
4162 BRW_PREDICATE_ALIGN1_ALL16H
,
4163 bld
.SEL(dest
, dest
, brw_imm_d(0)));
4166 case nir_intrinsic_vote_eq
: {
4167 fs_reg value
= get_nir_src(instr
->src
[0]);
4168 fs_reg uniformized
= bld
.emit_uniformize(value
);
4169 const fs_builder ubld
= bld
.exec_all();
4171 /* The any/all predicates do not consider channel enables. To prevent
4172 * dead channels from affecting the result, we initialize the flag with
4173 * with the identity value for the logical operation.
4175 ubld
.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4176 bld
.CMP(bld
.null_reg_d(), value
, uniformized
, BRW_CONDITIONAL_Z
);
4177 bld
.MOV(dest
, brw_imm_d(-1));
4178 set_predicate(dispatch_width
== 8 ?
4179 BRW_PREDICATE_ALIGN1_ALL8H
:
4180 BRW_PREDICATE_ALIGN1_ALL16H
,
4181 bld
.SEL(dest
, dest
, brw_imm_d(0)));
4185 case nir_intrinsic_ballot
: {
4186 const fs_reg value
= retype(get_nir_src(instr
->src
[0]),
4187 BRW_REGISTER_TYPE_UD
);
4188 const struct brw_reg flag
= retype(brw_flag_reg(0, 0),
4189 BRW_REGISTER_TYPE_UD
);
4191 bld
.exec_all().MOV(flag
, brw_imm_ud(0u));
4192 bld
.CMP(bld
.null_reg_ud(), value
, brw_imm_ud(0u), BRW_CONDITIONAL_NZ
);
4194 if (instr
->dest
.ssa
.bit_size
> 32) {
4195 dest
.type
= BRW_REGISTER_TYPE_UQ
;
4197 dest
.type
= BRW_REGISTER_TYPE_UD
;
4199 bld
.MOV(dest
, flag
);
4203 case nir_intrinsic_read_invocation
: {
4204 const fs_reg value
= get_nir_src(instr
->src
[0]);
4205 const fs_reg invocation
= get_nir_src(instr
->src
[1]);
4206 fs_reg tmp
= bld
.vgrf(value
.type
);
4208 bld
.exec_all().emit(SHADER_OPCODE_BROADCAST
, tmp
, value
,
4209 component(invocation
, 0));
4211 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
4212 fs_reg(component(tmp
, 0)));
4216 case nir_intrinsic_read_first_invocation
: {
4217 const fs_reg value
= get_nir_src(instr
->src
[0]);
4218 bld
.MOV(retype(dest
, BRW_REGISTER_TYPE_D
),
4219 bld
.emit_uniformize(value
));
4224 unreachable("unknown intrinsic");
4229 fs_visitor::nir_emit_ssbo_atomic(const fs_builder
&bld
,
4230 int op
, nir_intrinsic_instr
*instr
)
4232 if (stage
== MESA_SHADER_FRAGMENT
)
4233 brw_wm_prog_data(prog_data
)->has_side_effects
= true;
4236 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4237 dest
= get_nir_dest(instr
->dest
);
4240 nir_const_value
*const_surface
= nir_src_as_const_value(instr
->src
[0]);
4241 if (const_surface
) {
4242 unsigned surf_index
= stage_prog_data
->binding_table
.ssbo_start
+
4243 const_surface
->u32
[0];
4244 surface
= brw_imm_ud(surf_index
);
4245 brw_mark_surface_used(prog_data
, surf_index
);
4247 surface
= vgrf(glsl_type::uint_type
);
4248 bld
.ADD(surface
, get_nir_src(instr
->src
[0]),
4249 brw_imm_ud(stage_prog_data
->binding_table
.ssbo_start
));
4251 /* Assume this may touch any SSBO. This is the same we do for other
4252 * UBO/SSBO accesses with non-constant surface.
4254 brw_mark_surface_used(prog_data
,
4255 stage_prog_data
->binding_table
.ssbo_start
+
4256 nir
->info
.num_ssbos
- 1);
4259 fs_reg offset
= get_nir_src(instr
->src
[1]);
4260 fs_reg data1
= get_nir_src(instr
->src
[2]);
4262 if (op
== BRW_AOP_CMPWR
)
4263 data2
= get_nir_src(instr
->src
[3]);
4265 /* Emit the actual atomic operation */
4267 fs_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
4269 1 /* dims */, 1 /* rsize */,
4271 BRW_PREDICATE_NONE
);
4272 dest
.type
= atomic_result
.type
;
4273 bld
.MOV(dest
, atomic_result
);
4277 fs_visitor::nir_emit_shared_atomic(const fs_builder
&bld
,
4278 int op
, nir_intrinsic_instr
*instr
)
4281 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
4282 dest
= get_nir_dest(instr
->dest
);
4284 fs_reg surface
= brw_imm_ud(GEN7_BTI_SLM
);
4286 fs_reg data1
= get_nir_src(instr
->src
[1]);
4288 if (op
== BRW_AOP_CMPWR
)
4289 data2
= get_nir_src(instr
->src
[2]);
4291 /* Get the offset */
4292 nir_const_value
*const_offset
= nir_src_as_const_value(instr
->src
[0]);
4294 offset
= brw_imm_ud(instr
->const_index
[0] + const_offset
->u32
[0]);
4296 offset
= vgrf(glsl_type::uint_type
);
4298 retype(get_nir_src(instr
->src
[0]), BRW_REGISTER_TYPE_UD
),
4299 brw_imm_ud(instr
->const_index
[0]));
4302 /* Emit the actual atomic operation operation */
4304 fs_reg atomic_result
= emit_untyped_atomic(bld
, surface
, offset
,
4306 1 /* dims */, 1 /* rsize */,
4308 BRW_PREDICATE_NONE
);
4309 dest
.type
= atomic_result
.type
;
4310 bld
.MOV(dest
, atomic_result
);
4314 fs_visitor::nir_emit_texture(const fs_builder
&bld
, nir_tex_instr
*instr
)
4316 unsigned texture
= instr
->texture_index
;
4317 unsigned sampler
= instr
->sampler_index
;
4319 fs_reg srcs
[TEX_LOGICAL_NUM_SRCS
];
4321 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(texture
);
4322 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = brw_imm_ud(sampler
);
4324 int lod_components
= 0;
4326 /* The hardware requires a LOD for buffer textures */
4327 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_BUF
)
4328 srcs
[TEX_LOGICAL_SRC_LOD
] = brw_imm_d(0);
4330 uint32_t header_bits
= 0;
4331 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
4332 fs_reg src
= get_nir_src(instr
->src
[i
].src
);
4333 switch (instr
->src
[i
].src_type
) {
4334 case nir_tex_src_bias
:
4335 srcs
[TEX_LOGICAL_SRC_LOD
] =
4336 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
4338 case nir_tex_src_comparator
:
4339 srcs
[TEX_LOGICAL_SRC_SHADOW_C
] = retype(src
, BRW_REGISTER_TYPE_F
);
4341 case nir_tex_src_coord
:
4342 switch (instr
->op
) {
4344 case nir_texop_txf_ms
:
4345 case nir_texop_txf_ms_mcs
:
4346 case nir_texop_samples_identical
:
4347 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = retype(src
, BRW_REGISTER_TYPE_D
);
4350 srcs
[TEX_LOGICAL_SRC_COORDINATE
] = retype(src
, BRW_REGISTER_TYPE_F
);
4354 case nir_tex_src_ddx
:
4355 srcs
[TEX_LOGICAL_SRC_LOD
] = retype(src
, BRW_REGISTER_TYPE_F
);
4356 lod_components
= nir_tex_instr_src_size(instr
, i
);
4358 case nir_tex_src_ddy
:
4359 srcs
[TEX_LOGICAL_SRC_LOD2
] = retype(src
, BRW_REGISTER_TYPE_F
);
4361 case nir_tex_src_lod
:
4362 switch (instr
->op
) {
4364 srcs
[TEX_LOGICAL_SRC_LOD
] =
4365 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_UD
);
4368 srcs
[TEX_LOGICAL_SRC_LOD
] =
4369 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_D
);
4372 srcs
[TEX_LOGICAL_SRC_LOD
] =
4373 retype(get_nir_src_imm(instr
->src
[i
].src
), BRW_REGISTER_TYPE_F
);
4377 case nir_tex_src_ms_index
:
4378 srcs
[TEX_LOGICAL_SRC_SAMPLE_INDEX
] = retype(src
, BRW_REGISTER_TYPE_UD
);
4381 case nir_tex_src_offset
: {
4382 nir_const_value
*const_offset
=
4383 nir_src_as_const_value(instr
->src
[i
].src
);
4384 unsigned offset_bits
= 0;
4386 brw_texture_offset(const_offset
->i32
,
4387 nir_tex_instr_src_size(instr
, i
),
4389 header_bits
|= offset_bits
;
4391 srcs
[TEX_LOGICAL_SRC_TG4_OFFSET
] =
4392 retype(src
, BRW_REGISTER_TYPE_D
);
4397 case nir_tex_src_projector
:
4398 unreachable("should be lowered");
4400 case nir_tex_src_texture_offset
: {
4401 /* Figure out the highest possible texture index and mark it as used */
4402 uint32_t max_used
= texture
+ instr
->texture_array_size
- 1;
4403 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
< 8) {
4404 max_used
+= stage_prog_data
->binding_table
.gather_texture_start
;
4406 max_used
+= stage_prog_data
->binding_table
.texture_start
;
4408 brw_mark_surface_used(prog_data
, max_used
);
4410 /* Emit code to evaluate the actual indexing expression */
4411 fs_reg tmp
= vgrf(glsl_type::uint_type
);
4412 bld
.ADD(tmp
, src
, brw_imm_ud(texture
));
4413 srcs
[TEX_LOGICAL_SRC_SURFACE
] = bld
.emit_uniformize(tmp
);
4417 case nir_tex_src_sampler_offset
: {
4418 /* Emit code to evaluate the actual indexing expression */
4419 fs_reg tmp
= vgrf(glsl_type::uint_type
);
4420 bld
.ADD(tmp
, src
, brw_imm_ud(sampler
));
4421 srcs
[TEX_LOGICAL_SRC_SAMPLER
] = bld
.emit_uniformize(tmp
);
4425 case nir_tex_src_ms_mcs
:
4426 assert(instr
->op
== nir_texop_txf_ms
);
4427 srcs
[TEX_LOGICAL_SRC_MCS
] = retype(src
, BRW_REGISTER_TYPE_D
);
4430 case nir_tex_src_plane
: {
4431 nir_const_value
*const_plane
=
4432 nir_src_as_const_value(instr
->src
[i
].src
);
4433 const uint32_t plane
= const_plane
->u32
[0];
4434 const uint32_t texture_index
=
4435 instr
->texture_index
+
4436 stage_prog_data
->binding_table
.plane_start
[plane
] -
4437 stage_prog_data
->binding_table
.texture_start
;
4439 srcs
[TEX_LOGICAL_SRC_SURFACE
] = brw_imm_ud(texture_index
);
4444 unreachable("unknown texture source");
4448 if (srcs
[TEX_LOGICAL_SRC_MCS
].file
== BAD_FILE
&&
4449 (instr
->op
== nir_texop_txf_ms
||
4450 instr
->op
== nir_texop_samples_identical
)) {
4451 if (devinfo
->gen
>= 7 &&
4452 key_tex
->compressed_multisample_layout_mask
& (1 << texture
)) {
4453 srcs
[TEX_LOGICAL_SRC_MCS
] =
4454 emit_mcs_fetch(srcs
[TEX_LOGICAL_SRC_COORDINATE
],
4455 instr
->coord_components
,
4456 srcs
[TEX_LOGICAL_SRC_SURFACE
]);
4458 srcs
[TEX_LOGICAL_SRC_MCS
] = brw_imm_ud(0u);
4462 srcs
[TEX_LOGICAL_SRC_COORD_COMPONENTS
] = brw_imm_d(instr
->coord_components
);
4463 srcs
[TEX_LOGICAL_SRC_GRAD_COMPONENTS
] = brw_imm_d(lod_components
);
4466 switch (instr
->op
) {
4468 opcode
= (stage
== MESA_SHADER_FRAGMENT
? SHADER_OPCODE_TEX_LOGICAL
:
4469 SHADER_OPCODE_TXL_LOGICAL
);
4472 opcode
= FS_OPCODE_TXB_LOGICAL
;
4475 opcode
= SHADER_OPCODE_TXL_LOGICAL
;
4478 opcode
= SHADER_OPCODE_TXD_LOGICAL
;
4481 opcode
= SHADER_OPCODE_TXF_LOGICAL
;
4483 case nir_texop_txf_ms
:
4484 if ((key_tex
->msaa_16
& (1 << sampler
)))
4485 opcode
= SHADER_OPCODE_TXF_CMS_W_LOGICAL
;
4487 opcode
= SHADER_OPCODE_TXF_CMS_LOGICAL
;
4489 case nir_texop_txf_ms_mcs
:
4490 opcode
= SHADER_OPCODE_TXF_MCS_LOGICAL
;
4492 case nir_texop_query_levels
:
4494 opcode
= SHADER_OPCODE_TXS_LOGICAL
;
4497 opcode
= SHADER_OPCODE_LOD_LOGICAL
;
4500 if (srcs
[TEX_LOGICAL_SRC_TG4_OFFSET
].file
!= BAD_FILE
)
4501 opcode
= SHADER_OPCODE_TG4_OFFSET_LOGICAL
;
4503 opcode
= SHADER_OPCODE_TG4_LOGICAL
;
4505 case nir_texop_texture_samples
:
4506 opcode
= SHADER_OPCODE_SAMPLEINFO_LOGICAL
;
4508 case nir_texop_samples_identical
: {
4509 fs_reg dst
= retype(get_nir_dest(instr
->dest
), BRW_REGISTER_TYPE_D
);
4511 /* If mcs is an immediate value, it means there is no MCS. In that case
4512 * just return false.
4514 if (srcs
[TEX_LOGICAL_SRC_MCS
].file
== BRW_IMMEDIATE_VALUE
) {
4515 bld
.MOV(dst
, brw_imm_ud(0u));
4516 } else if ((key_tex
->msaa_16
& (1 << sampler
))) {
4517 fs_reg tmp
= vgrf(glsl_type::uint_type
);
4518 bld
.OR(tmp
, srcs
[TEX_LOGICAL_SRC_MCS
],
4519 offset(srcs
[TEX_LOGICAL_SRC_MCS
], bld
, 1));
4520 bld
.CMP(dst
, tmp
, brw_imm_ud(0u), BRW_CONDITIONAL_EQ
);
4522 bld
.CMP(dst
, srcs
[TEX_LOGICAL_SRC_MCS
], brw_imm_ud(0u),
4523 BRW_CONDITIONAL_EQ
);
4528 unreachable("unknown texture opcode");
4531 /* TXS and TXL require a LOD but not everything we implement using those
4532 * two opcodes provides one. Provide a default LOD of 0.
4534 if ((opcode
== SHADER_OPCODE_TXS_LOGICAL
||
4535 opcode
== SHADER_OPCODE_TXL_LOGICAL
) &&
4536 srcs
[TEX_LOGICAL_SRC_LOD
].file
== BAD_FILE
) {
4537 srcs
[TEX_LOGICAL_SRC_LOD
] = brw_imm_ud(0u);
4540 if (instr
->op
== nir_texop_tg4
) {
4541 if (instr
->component
== 1 &&
4542 key_tex
->gather_channel_quirk_mask
& (1 << texture
)) {
4543 /* gather4 sampler is broken for green channel on RG32F --
4544 * we must ask for blue instead.
4546 header_bits
|= 2 << 16;
4548 header_bits
|= instr
->component
<< 16;
4552 fs_reg dst
= bld
.vgrf(brw_type_for_nir_type(devinfo
, instr
->dest_type
), 4);
4553 fs_inst
*inst
= bld
.emit(opcode
, dst
, srcs
, ARRAY_SIZE(srcs
));
4554 inst
->offset
= header_bits
;
4556 const unsigned dest_size
= nir_tex_instr_dest_size(instr
);
4557 if (devinfo
->gen
>= 9 &&
4558 instr
->op
!= nir_texop_tg4
&& instr
->op
!= nir_texop_query_levels
) {
4559 unsigned write_mask
= instr
->dest
.is_ssa
?
4560 nir_ssa_def_components_read(&instr
->dest
.ssa
):
4561 (1 << dest_size
) - 1;
4562 assert(write_mask
!= 0); /* dead code should have been eliminated */
4563 inst
->size_written
= util_last_bit(write_mask
) *
4564 inst
->dst
.component_size(inst
->exec_size
);
4566 inst
->size_written
= 4 * inst
->dst
.component_size(inst
->exec_size
);
4569 if (srcs
[TEX_LOGICAL_SRC_SHADOW_C
].file
!= BAD_FILE
)
4570 inst
->shadow_compare
= true;
4572 if (instr
->op
== nir_texop_tg4
&& devinfo
->gen
== 6)
4573 emit_gen6_gather_wa(key_tex
->gen6_gather_wa
[texture
], dst
);
4576 for (unsigned i
= 0; i
< dest_size
; i
++)
4577 nir_dest
[i
] = offset(dst
, bld
, i
);
4579 if (instr
->op
== nir_texop_query_levels
) {
4580 /* # levels is in .w */
4581 nir_dest
[0] = offset(dst
, bld
, 3);
4582 } else if (instr
->op
== nir_texop_txs
&&
4583 dest_size
>= 3 && devinfo
->gen
< 7) {
4584 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
4585 fs_reg depth
= offset(dst
, bld
, 2);
4586 nir_dest
[2] = vgrf(glsl_type::int_type
);
4587 bld
.emit_minmax(nir_dest
[2], depth
, brw_imm_d(1), BRW_CONDITIONAL_GE
);
4590 bld
.LOAD_PAYLOAD(get_nir_dest(instr
->dest
), nir_dest
, dest_size
, 0);
4594 fs_visitor::nir_emit_jump(const fs_builder
&bld
, nir_jump_instr
*instr
)
4596 switch (instr
->type
) {
4597 case nir_jump_break
:
4598 bld
.emit(BRW_OPCODE_BREAK
);
4600 case nir_jump_continue
:
4601 bld
.emit(BRW_OPCODE_CONTINUE
);
4603 case nir_jump_return
:
4605 unreachable("unknown jump");
4610 * This helper takes the result of a load operation that reads 32-bit elements
4618 * and shuffles the data to get this:
4625 * Which is exactly what we want if the load is reading 64-bit components
4626 * like doubles, where x represents the low 32-bit of the x double component
4627 * and y represents the high 32-bit of the x double component (likewise with
4628 * z and w for double component y). The parameter @components represents
4629 * the number of 64-bit components present in @src. This would typically be
4630 * 2 at most, since we can only fit 2 double elements in the result of a
4633 * Notice that @dst and @src can be the same register.
4636 shuffle_32bit_load_result_to_64bit_data(const fs_builder
&bld
,
4639 uint32_t components
)
4641 assert(type_sz(src
.type
) == 4);
4642 assert(type_sz(dst
.type
) == 8);
4644 /* A temporary that we will use to shuffle the 32-bit data of each
4645 * component in the vector into valid 64-bit data. We can't write directly
4646 * to dst because dst can be (and would usually be) the same as src
4647 * and in that case the first MOV in the loop below would overwrite the
4648 * data read in the second MOV.
4650 fs_reg tmp
= bld
.vgrf(dst
.type
);
4652 for (unsigned i
= 0; i
< components
; i
++) {
4653 const fs_reg component_i
= offset(src
, bld
, 2 * i
);
4655 bld
.MOV(subscript(tmp
, src
.type
, 0), component_i
);
4656 bld
.MOV(subscript(tmp
, src
.type
, 1), offset(component_i
, bld
, 1));
4658 bld
.MOV(offset(dst
, bld
, i
), tmp
);
4663 * This helper does the inverse operation of
4664 * SHUFFLE_32BIT_LOAD_RESULT_TO_64BIT_DATA.
4666 * We need to do this when we are going to use untyped write messsages that
4667 * operate with 32-bit components in order to arrange our 64-bit data to be
4668 * in the expected layout.
4670 * Notice that callers of this function, unlike in the case of the inverse
4671 * operation, would typically need to call this with dst and src being
4672 * different registers, since they would otherwise corrupt the original
4673 * 64-bit data they are about to write. Because of this the function checks
4674 * that the src and dst regions involved in the operation do not overlap.
4677 shuffle_64bit_data_for_32bit_write(const fs_builder
&bld
,
4680 uint32_t components
)
4682 assert(type_sz(src
.type
) == 8);
4683 assert(type_sz(dst
.type
) == 4);
4685 assert(!regions_overlap(
4686 dst
, 2 * components
* dst
.component_size(bld
.dispatch_width()),
4687 src
, components
* src
.component_size(bld
.dispatch_width())));
4689 for (unsigned i
= 0; i
< components
; i
++) {
4690 const fs_reg component_i
= offset(src
, bld
, i
);
4691 bld
.MOV(offset(dst
, bld
, 2 * i
), subscript(component_i
, dst
.type
, 0));
4692 bld
.MOV(offset(dst
, bld
, 2 * i
+ 1), subscript(component_i
, dst
.type
, 1));
4697 setup_imm_df(const fs_builder
&bld
, double v
)
4699 const struct gen_device_info
*devinfo
= bld
.shader
->devinfo
;
4700 assert(devinfo
->gen
>= 7);
4702 if (devinfo
->gen
>= 8)
4703 return brw_imm_df(v
);
4705 /* gen7.5 does not support DF immediates straighforward but the DIM
4706 * instruction allows to set the 64-bit immediate value.
4708 if (devinfo
->is_haswell
) {
4709 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4710 fs_reg dst
= ubld
.vgrf(BRW_REGISTER_TYPE_DF
, 1);
4711 ubld
.DIM(dst
, brw_imm_df(v
));
4712 return component(dst
, 0);
4715 /* gen7 does not support DF immediates, so we generate a 64-bit constant by
4716 * writing the low 32-bit of the constant to suboffset 0 of a VGRF and
4717 * the high 32-bit to suboffset 4 and then applying a stride of 0.
4719 * Alternatively, we could also produce a normal VGRF (without stride 0)
4720 * by writing to all the channels in the VGRF, however, that would hit the
4721 * gen7 bug where we have to split writes that span more than 1 register
4722 * into instructions with a width of 4 (otherwise the write to the second
4723 * register written runs into an execmask hardware bug) which isn't very
4736 const fs_builder ubld
= bld
.exec_all().group(1, 0);
4737 const fs_reg tmp
= ubld
.vgrf(BRW_REGISTER_TYPE_UD
, 2);
4738 ubld
.MOV(tmp
, brw_imm_ud(di
.i1
));
4739 ubld
.MOV(horiz_offset(tmp
, 1), brw_imm_ud(di
.i2
));
4741 return component(retype(tmp
, BRW_REGISTER_TYPE_DF
), 0);