intel/fs/gen11+: Fix CS_OPCODE_CS_TERMINATE codegen.
[mesa.git] / src / intel / compiler / brw_fs_nir.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "compiler/glsl/ir.h"
25 #include "brw_fs.h"
26 #include "brw_nir.h"
27 #include "brw_eu.h"
28 #include "nir_search_helpers.h"
29 #include "util/u_math.h"
30 #include "util/bitscan.h"
31
32 using namespace brw;
33
34 void
35 fs_visitor::emit_nir_code()
36 {
37 emit_shader_float_controls_execution_mode();
38
39 /* emit the arrays used for inputs and outputs - load/store intrinsics will
40 * be converted to reads/writes of these arrays
41 */
42 nir_setup_outputs();
43 nir_setup_uniforms();
44 nir_emit_system_values();
45
46 nir_emit_impl(nir_shader_get_entrypoint((nir_shader *)nir));
47 }
48
49 void
50 fs_visitor::nir_setup_outputs()
51 {
52 if (stage == MESA_SHADER_TESS_CTRL || stage == MESA_SHADER_FRAGMENT)
53 return;
54
55 unsigned vec4s[VARYING_SLOT_TESS_MAX] = { 0, };
56
57 /* Calculate the size of output registers in a separate pass, before
58 * allocating them. With ARB_enhanced_layouts, multiple output variables
59 * may occupy the same slot, but have different type sizes.
60 */
61 nir_foreach_variable(var, &nir->outputs) {
62 const int loc = var->data.driver_location;
63 const unsigned var_vec4s =
64 var->data.compact ? DIV_ROUND_UP(glsl_get_length(var->type), 4)
65 : type_size_vec4(var->type, true);
66 vec4s[loc] = MAX2(vec4s[loc], var_vec4s);
67 }
68
69 for (unsigned loc = 0; loc < ARRAY_SIZE(vec4s);) {
70 if (vec4s[loc] == 0) {
71 loc++;
72 continue;
73 }
74
75 unsigned reg_size = vec4s[loc];
76
77 /* Check if there are any ranges that start within this range and extend
78 * past it. If so, include them in this allocation.
79 */
80 for (unsigned i = 1; i < reg_size; i++)
81 reg_size = MAX2(vec4s[i + loc] + i, reg_size);
82
83 fs_reg reg = bld.vgrf(BRW_REGISTER_TYPE_F, 4 * reg_size);
84 for (unsigned i = 0; i < reg_size; i++)
85 outputs[loc + i] = offset(reg, bld, 4 * i);
86
87 loc += reg_size;
88 }
89 }
90
91 void
92 fs_visitor::nir_setup_uniforms()
93 {
94 /* Only the first compile gets to set up uniforms. */
95 if (push_constant_loc) {
96 assert(pull_constant_loc);
97 return;
98 }
99
100 uniforms = nir->num_uniforms / 4;
101
102 if (stage == MESA_SHADER_COMPUTE) {
103 /* Add a uniform for the thread local id. It must be the last uniform
104 * on the list.
105 */
106 assert(uniforms == prog_data->nr_params);
107 uint32_t *param = brw_stage_prog_data_add_params(prog_data, 1);
108 *param = BRW_PARAM_BUILTIN_SUBGROUP_ID;
109 subgroup_id = fs_reg(UNIFORM, uniforms++, BRW_REGISTER_TYPE_UD);
110 }
111 }
112
113 static bool
114 emit_system_values_block(nir_block *block, fs_visitor *v)
115 {
116 fs_reg *reg;
117
118 nir_foreach_instr(instr, block) {
119 if (instr->type != nir_instr_type_intrinsic)
120 continue;
121
122 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
123 switch (intrin->intrinsic) {
124 case nir_intrinsic_load_vertex_id:
125 case nir_intrinsic_load_base_vertex:
126 unreachable("should be lowered by nir_lower_system_values().");
127
128 case nir_intrinsic_load_vertex_id_zero_base:
129 case nir_intrinsic_load_is_indexed_draw:
130 case nir_intrinsic_load_first_vertex:
131 case nir_intrinsic_load_instance_id:
132 case nir_intrinsic_load_base_instance:
133 case nir_intrinsic_load_draw_id:
134 unreachable("should be lowered by brw_nir_lower_vs_inputs().");
135
136 case nir_intrinsic_load_invocation_id:
137 if (v->stage == MESA_SHADER_TESS_CTRL)
138 break;
139 assert(v->stage == MESA_SHADER_GEOMETRY);
140 reg = &v->nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
141 if (reg->file == BAD_FILE) {
142 const fs_builder abld = v->bld.annotate("gl_InvocationID", NULL);
143 fs_reg g1(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
144 fs_reg iid = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
145 abld.SHR(iid, g1, brw_imm_ud(27u));
146 *reg = iid;
147 }
148 break;
149
150 case nir_intrinsic_load_sample_pos:
151 assert(v->stage == MESA_SHADER_FRAGMENT);
152 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
153 if (reg->file == BAD_FILE)
154 *reg = *v->emit_samplepos_setup();
155 break;
156
157 case nir_intrinsic_load_sample_id:
158 assert(v->stage == MESA_SHADER_FRAGMENT);
159 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
160 if (reg->file == BAD_FILE)
161 *reg = *v->emit_sampleid_setup();
162 break;
163
164 case nir_intrinsic_load_sample_mask_in:
165 assert(v->stage == MESA_SHADER_FRAGMENT);
166 assert(v->devinfo->gen >= 7);
167 reg = &v->nir_system_values[SYSTEM_VALUE_SAMPLE_MASK_IN];
168 if (reg->file == BAD_FILE)
169 *reg = *v->emit_samplemaskin_setup();
170 break;
171
172 case nir_intrinsic_load_work_group_id:
173 assert(v->stage == MESA_SHADER_COMPUTE);
174 reg = &v->nir_system_values[SYSTEM_VALUE_WORK_GROUP_ID];
175 if (reg->file == BAD_FILE)
176 *reg = *v->emit_cs_work_group_id_setup();
177 break;
178
179 case nir_intrinsic_load_helper_invocation:
180 assert(v->stage == MESA_SHADER_FRAGMENT);
181 reg = &v->nir_system_values[SYSTEM_VALUE_HELPER_INVOCATION];
182 if (reg->file == BAD_FILE) {
183 const fs_builder abld =
184 v->bld.annotate("gl_HelperInvocation", NULL);
185
186 /* On Gen6+ (gl_HelperInvocation is only exposed on Gen7+) the
187 * pixel mask is in g1.7 of the thread payload.
188 *
189 * We move the per-channel pixel enable bit to the low bit of each
190 * channel by shifting the byte containing the pixel mask by the
191 * vector immediate 0x76543210UV.
192 *
193 * The region of <1,8,0> reads only 1 byte (the pixel masks for
194 * subspans 0 and 1) in SIMD8 and an additional byte (the pixel
195 * masks for 2 and 3) in SIMD16.
196 */
197 fs_reg shifted = abld.vgrf(BRW_REGISTER_TYPE_UW, 1);
198
199 for (unsigned i = 0; i < DIV_ROUND_UP(v->dispatch_width, 16); i++) {
200 const fs_builder hbld = abld.group(MIN2(16, v->dispatch_width), i);
201 hbld.SHR(offset(shifted, hbld, i),
202 stride(retype(brw_vec1_grf(1 + i, 7),
203 BRW_REGISTER_TYPE_UB),
204 1, 8, 0),
205 brw_imm_v(0x76543210));
206 }
207
208 /* A set bit in the pixel mask means the channel is enabled, but
209 * that is the opposite of gl_HelperInvocation so we need to invert
210 * the mask.
211 *
212 * The negate source-modifier bit of logical instructions on Gen8+
213 * performs 1's complement negation, so we can use that instead of
214 * a NOT instruction.
215 */
216 fs_reg inverted = negate(shifted);
217 if (v->devinfo->gen < 8) {
218 inverted = abld.vgrf(BRW_REGISTER_TYPE_UW);
219 abld.NOT(inverted, shifted);
220 }
221
222 /* We then resolve the 0/1 result to 0/~0 boolean values by ANDing
223 * with 1 and negating.
224 */
225 fs_reg anded = abld.vgrf(BRW_REGISTER_TYPE_UD, 1);
226 abld.AND(anded, inverted, brw_imm_uw(1));
227
228 fs_reg dst = abld.vgrf(BRW_REGISTER_TYPE_D, 1);
229 abld.MOV(dst, negate(retype(anded, BRW_REGISTER_TYPE_D)));
230 *reg = dst;
231 }
232 break;
233
234 default:
235 break;
236 }
237 }
238
239 return true;
240 }
241
242 void
243 fs_visitor::nir_emit_system_values()
244 {
245 nir_system_values = ralloc_array(mem_ctx, fs_reg, SYSTEM_VALUE_MAX);
246 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
247 nir_system_values[i] = fs_reg();
248 }
249
250 /* Always emit SUBGROUP_INVOCATION. Dead code will clean it up if we
251 * never end up using it.
252 */
253 {
254 const fs_builder abld = bld.annotate("gl_SubgroupInvocation", NULL);
255 fs_reg &reg = nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION];
256 reg = abld.vgrf(BRW_REGISTER_TYPE_UW);
257
258 const fs_builder allbld8 = abld.group(8, 0).exec_all();
259 allbld8.MOV(reg, brw_imm_v(0x76543210));
260 if (dispatch_width > 8)
261 allbld8.ADD(byte_offset(reg, 16), reg, brw_imm_uw(8u));
262 if (dispatch_width > 16) {
263 const fs_builder allbld16 = abld.group(16, 0).exec_all();
264 allbld16.ADD(byte_offset(reg, 32), reg, brw_imm_uw(16u));
265 }
266 }
267
268 nir_function_impl *impl = nir_shader_get_entrypoint((nir_shader *)nir);
269 nir_foreach_block(block, impl)
270 emit_system_values_block(block, this);
271 }
272
273 /*
274 * Returns a type based on a reference_type (word, float, half-float) and a
275 * given bit_size.
276 *
277 * Reference BRW_REGISTER_TYPE are HF,F,DF,W,D,UW,UD.
278 *
279 * @FIXME: 64-bit return types are always DF on integer types to maintain
280 * compability with uses of DF previously to the introduction of int64
281 * support.
282 */
283 static brw_reg_type
284 brw_reg_type_from_bit_size(const unsigned bit_size,
285 const brw_reg_type reference_type)
286 {
287 switch(reference_type) {
288 case BRW_REGISTER_TYPE_HF:
289 case BRW_REGISTER_TYPE_F:
290 case BRW_REGISTER_TYPE_DF:
291 switch(bit_size) {
292 case 16:
293 return BRW_REGISTER_TYPE_HF;
294 case 32:
295 return BRW_REGISTER_TYPE_F;
296 case 64:
297 return BRW_REGISTER_TYPE_DF;
298 default:
299 unreachable("Invalid bit size");
300 }
301 case BRW_REGISTER_TYPE_B:
302 case BRW_REGISTER_TYPE_W:
303 case BRW_REGISTER_TYPE_D:
304 case BRW_REGISTER_TYPE_Q:
305 switch(bit_size) {
306 case 8:
307 return BRW_REGISTER_TYPE_B;
308 case 16:
309 return BRW_REGISTER_TYPE_W;
310 case 32:
311 return BRW_REGISTER_TYPE_D;
312 case 64:
313 return BRW_REGISTER_TYPE_Q;
314 default:
315 unreachable("Invalid bit size");
316 }
317 case BRW_REGISTER_TYPE_UB:
318 case BRW_REGISTER_TYPE_UW:
319 case BRW_REGISTER_TYPE_UD:
320 case BRW_REGISTER_TYPE_UQ:
321 switch(bit_size) {
322 case 8:
323 return BRW_REGISTER_TYPE_UB;
324 case 16:
325 return BRW_REGISTER_TYPE_UW;
326 case 32:
327 return BRW_REGISTER_TYPE_UD;
328 case 64:
329 return BRW_REGISTER_TYPE_UQ;
330 default:
331 unreachable("Invalid bit size");
332 }
333 default:
334 unreachable("Unknown type");
335 }
336 }
337
338 void
339 fs_visitor::nir_emit_impl(nir_function_impl *impl)
340 {
341 nir_locals = ralloc_array(mem_ctx, fs_reg, impl->reg_alloc);
342 for (unsigned i = 0; i < impl->reg_alloc; i++) {
343 nir_locals[i] = fs_reg();
344 }
345
346 foreach_list_typed(nir_register, reg, node, &impl->registers) {
347 unsigned array_elems =
348 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
349 unsigned size = array_elems * reg->num_components;
350 const brw_reg_type reg_type = reg->bit_size == 8 ? BRW_REGISTER_TYPE_B :
351 brw_reg_type_from_bit_size(reg->bit_size, BRW_REGISTER_TYPE_F);
352 nir_locals[reg->index] = bld.vgrf(reg_type, size);
353 }
354
355 nir_ssa_values = reralloc(mem_ctx, nir_ssa_values, fs_reg,
356 impl->ssa_alloc);
357
358 nir_emit_cf_list(&impl->body);
359 }
360
361 void
362 fs_visitor::nir_emit_cf_list(exec_list *list)
363 {
364 exec_list_validate(list);
365 foreach_list_typed(nir_cf_node, node, node, list) {
366 switch (node->type) {
367 case nir_cf_node_if:
368 nir_emit_if(nir_cf_node_as_if(node));
369 break;
370
371 case nir_cf_node_loop:
372 nir_emit_loop(nir_cf_node_as_loop(node));
373 break;
374
375 case nir_cf_node_block:
376 nir_emit_block(nir_cf_node_as_block(node));
377 break;
378
379 default:
380 unreachable("Invalid CFG node block");
381 }
382 }
383 }
384
385 void
386 fs_visitor::nir_emit_if(nir_if *if_stmt)
387 {
388 bool invert;
389 fs_reg cond_reg;
390
391 /* If the condition has the form !other_condition, use other_condition as
392 * the source, but invert the predicate on the if instruction.
393 */
394 nir_alu_instr *cond = nir_src_as_alu_instr(if_stmt->condition);
395 if (cond != NULL && cond->op == nir_op_inot) {
396 assert(!cond->src[0].negate);
397 assert(!cond->src[0].abs);
398
399 invert = true;
400 cond_reg = get_nir_src(cond->src[0].src);
401 } else {
402 invert = false;
403 cond_reg = get_nir_src(if_stmt->condition);
404 }
405
406 /* first, put the condition into f0 */
407 fs_inst *inst = bld.MOV(bld.null_reg_d(),
408 retype(cond_reg, BRW_REGISTER_TYPE_D));
409 inst->conditional_mod = BRW_CONDITIONAL_NZ;
410
411 bld.IF(BRW_PREDICATE_NORMAL)->predicate_inverse = invert;
412
413 nir_emit_cf_list(&if_stmt->then_list);
414
415 if (!nir_cf_list_is_empty_block(&if_stmt->else_list)) {
416 bld.emit(BRW_OPCODE_ELSE);
417 nir_emit_cf_list(&if_stmt->else_list);
418 }
419
420 bld.emit(BRW_OPCODE_ENDIF);
421
422 if (devinfo->gen < 7)
423 limit_dispatch_width(16, "Non-uniform control flow unsupported "
424 "in SIMD32 mode.");
425 }
426
427 void
428 fs_visitor::nir_emit_loop(nir_loop *loop)
429 {
430 bld.emit(BRW_OPCODE_DO);
431
432 nir_emit_cf_list(&loop->body);
433
434 bld.emit(BRW_OPCODE_WHILE);
435
436 if (devinfo->gen < 7)
437 limit_dispatch_width(16, "Non-uniform control flow unsupported "
438 "in SIMD32 mode.");
439 }
440
441 void
442 fs_visitor::nir_emit_block(nir_block *block)
443 {
444 nir_foreach_instr(instr, block) {
445 nir_emit_instr(instr);
446 }
447 }
448
449 void
450 fs_visitor::nir_emit_instr(nir_instr *instr)
451 {
452 const fs_builder abld = bld.annotate(NULL, instr);
453
454 switch (instr->type) {
455 case nir_instr_type_alu:
456 nir_emit_alu(abld, nir_instr_as_alu(instr), true);
457 break;
458
459 case nir_instr_type_deref:
460 unreachable("All derefs should've been lowered");
461 break;
462
463 case nir_instr_type_intrinsic:
464 switch (stage) {
465 case MESA_SHADER_VERTEX:
466 nir_emit_vs_intrinsic(abld, nir_instr_as_intrinsic(instr));
467 break;
468 case MESA_SHADER_TESS_CTRL:
469 nir_emit_tcs_intrinsic(abld, nir_instr_as_intrinsic(instr));
470 break;
471 case MESA_SHADER_TESS_EVAL:
472 nir_emit_tes_intrinsic(abld, nir_instr_as_intrinsic(instr));
473 break;
474 case MESA_SHADER_GEOMETRY:
475 nir_emit_gs_intrinsic(abld, nir_instr_as_intrinsic(instr));
476 break;
477 case MESA_SHADER_FRAGMENT:
478 nir_emit_fs_intrinsic(abld, nir_instr_as_intrinsic(instr));
479 break;
480 case MESA_SHADER_COMPUTE:
481 nir_emit_cs_intrinsic(abld, nir_instr_as_intrinsic(instr));
482 break;
483 default:
484 unreachable("unsupported shader stage");
485 }
486 break;
487
488 case nir_instr_type_tex:
489 nir_emit_texture(abld, nir_instr_as_tex(instr));
490 break;
491
492 case nir_instr_type_load_const:
493 nir_emit_load_const(abld, nir_instr_as_load_const(instr));
494 break;
495
496 case nir_instr_type_ssa_undef:
497 /* We create a new VGRF for undefs on every use (by handling
498 * them in get_nir_src()), rather than for each definition.
499 * This helps register coalescing eliminate MOVs from undef.
500 */
501 break;
502
503 case nir_instr_type_jump:
504 nir_emit_jump(abld, nir_instr_as_jump(instr));
505 break;
506
507 default:
508 unreachable("unknown instruction type");
509 }
510 }
511
512 /**
513 * Recognizes a parent instruction of nir_op_extract_* and changes the type to
514 * match instr.
515 */
516 bool
517 fs_visitor::optimize_extract_to_float(nir_alu_instr *instr,
518 const fs_reg &result)
519 {
520 if (!instr->src[0].src.is_ssa ||
521 !instr->src[0].src.ssa->parent_instr)
522 return false;
523
524 if (instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
525 return false;
526
527 nir_alu_instr *src0 =
528 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
529
530 if (src0->op != nir_op_extract_u8 && src0->op != nir_op_extract_u16 &&
531 src0->op != nir_op_extract_i8 && src0->op != nir_op_extract_i16)
532 return false;
533
534 /* If either opcode has source modifiers, bail.
535 *
536 * TODO: We can potentially handle source modifiers if both of the opcodes
537 * we're combining are signed integers.
538 */
539 if (instr->src[0].abs || instr->src[0].negate ||
540 src0->src[0].abs || src0->src[0].negate)
541 return false;
542
543 unsigned element = nir_src_as_uint(src0->src[1].src);
544
545 /* Element type to extract.*/
546 const brw_reg_type type = brw_int_type(
547 src0->op == nir_op_extract_u16 || src0->op == nir_op_extract_i16 ? 2 : 1,
548 src0->op == nir_op_extract_i16 || src0->op == nir_op_extract_i8);
549
550 fs_reg op0 = get_nir_src(src0->src[0].src);
551 op0.type = brw_type_for_nir_type(devinfo,
552 (nir_alu_type)(nir_op_infos[src0->op].input_types[0] |
553 nir_src_bit_size(src0->src[0].src)));
554 op0 = offset(op0, bld, src0->src[0].swizzle[0]);
555
556 set_saturate(instr->dest.saturate,
557 bld.MOV(result, subscript(op0, type, element)));
558 return true;
559 }
560
561 bool
562 fs_visitor::optimize_frontfacing_ternary(nir_alu_instr *instr,
563 const fs_reg &result)
564 {
565 nir_intrinsic_instr *src0 = nir_src_as_intrinsic(instr->src[0].src);
566 if (src0 == NULL || src0->intrinsic != nir_intrinsic_load_front_face)
567 return false;
568
569 if (!nir_src_is_const(instr->src[1].src) ||
570 !nir_src_is_const(instr->src[2].src))
571 return false;
572
573 const float value1 = nir_src_as_float(instr->src[1].src);
574 const float value2 = nir_src_as_float(instr->src[2].src);
575 if (fabsf(value1) != 1.0f || fabsf(value2) != 1.0f)
576 return false;
577
578 /* nir_opt_algebraic should have gotten rid of bcsel(b, a, a) */
579 assert(value1 == -value2);
580
581 fs_reg tmp = vgrf(glsl_type::int_type);
582
583 if (devinfo->gen >= 6) {
584 /* Bit 15 of g0.0 is 0 if the polygon is front facing. */
585 fs_reg g0 = fs_reg(retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_W));
586
587 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
588 *
589 * or(8) tmp.1<2>W g0.0<0,1,0>W 0x00003f80W
590 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
591 *
592 * and negate g0.0<0,1,0>W for (gl_FrontFacing ? -1.0 : 1.0).
593 *
594 * This negation looks like it's safe in practice, because bits 0:4 will
595 * surely be TRIANGLES
596 */
597
598 if (value1 == -1.0f) {
599 g0.negate = true;
600 }
601
602 bld.OR(subscript(tmp, BRW_REGISTER_TYPE_W, 1),
603 g0, brw_imm_uw(0x3f80));
604 } else {
605 /* Bit 31 of g1.6 is 0 if the polygon is front facing. */
606 fs_reg g1_6 = fs_reg(retype(brw_vec1_grf(1, 6), BRW_REGISTER_TYPE_D));
607
608 /* For (gl_FrontFacing ? 1.0 : -1.0), emit:
609 *
610 * or(8) tmp<1>D g1.6<0,1,0>D 0x3f800000D
611 * and(8) dst<1>D tmp<8,8,1>D 0xbf800000D
612 *
613 * and negate g1.6<0,1,0>D for (gl_FrontFacing ? -1.0 : 1.0).
614 *
615 * This negation looks like it's safe in practice, because bits 0:4 will
616 * surely be TRIANGLES
617 */
618
619 if (value1 == -1.0f) {
620 g1_6.negate = true;
621 }
622
623 bld.OR(tmp, g1_6, brw_imm_d(0x3f800000));
624 }
625 bld.AND(retype(result, BRW_REGISTER_TYPE_D), tmp, brw_imm_d(0xbf800000));
626
627 return true;
628 }
629
630 static void
631 emit_find_msb_using_lzd(const fs_builder &bld,
632 const fs_reg &result,
633 const fs_reg &src,
634 bool is_signed)
635 {
636 fs_inst *inst;
637 fs_reg temp = src;
638
639 if (is_signed) {
640 /* LZD of an absolute value source almost always does the right
641 * thing. There are two problem values:
642 *
643 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
644 * 0. However, findMSB(int(0x80000000)) == 30.
645 *
646 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
647 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
648 *
649 * For a value of zero or negative one, -1 will be returned.
650 *
651 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
652 * findMSB(-(1<<x)) should return x-1.
653 *
654 * For all negative number cases, including 0x80000000 and
655 * 0xffffffff, the correct value is obtained from LZD if instead of
656 * negating the (already negative) value the logical-not is used. A
657 * conditonal logical-not can be achieved in two instructions.
658 */
659 temp = bld.vgrf(BRW_REGISTER_TYPE_D);
660
661 bld.ASR(temp, src, brw_imm_d(31));
662 bld.XOR(temp, temp, src);
663 }
664
665 bld.LZD(retype(result, BRW_REGISTER_TYPE_UD),
666 retype(temp, BRW_REGISTER_TYPE_UD));
667
668 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
669 * from the LSB side. Subtract the result from 31 to convert the MSB
670 * count into an LSB count. If no bits are set, LZD will return 32.
671 * 31-32 = -1, which is exactly what findMSB() is supposed to return.
672 */
673 inst = bld.ADD(result, retype(result, BRW_REGISTER_TYPE_D), brw_imm_d(31));
674 inst->src[0].negate = true;
675 }
676
677 static brw_rnd_mode
678 brw_rnd_mode_from_nir_op (const nir_op op) {
679 switch (op) {
680 case nir_op_f2f16_rtz:
681 return BRW_RND_MODE_RTZ;
682 case nir_op_f2f16_rtne:
683 return BRW_RND_MODE_RTNE;
684 default:
685 unreachable("Operation doesn't support rounding mode");
686 }
687 }
688
689 static brw_rnd_mode
690 brw_rnd_mode_from_execution_mode(unsigned execution_mode)
691 {
692 if (nir_has_any_rounding_mode_rtne(execution_mode))
693 return BRW_RND_MODE_RTNE;
694 if (nir_has_any_rounding_mode_rtz(execution_mode))
695 return BRW_RND_MODE_RTZ;
696 return BRW_RND_MODE_UNSPECIFIED;
697 }
698
699 fs_reg
700 fs_visitor::prepare_alu_destination_and_sources(const fs_builder &bld,
701 nir_alu_instr *instr,
702 fs_reg *op,
703 bool need_dest)
704 {
705 fs_reg result =
706 need_dest ? get_nir_dest(instr->dest.dest) : bld.null_reg_ud();
707
708 result.type = brw_type_for_nir_type(devinfo,
709 (nir_alu_type)(nir_op_infos[instr->op].output_type |
710 nir_dest_bit_size(instr->dest.dest)));
711
712 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
713 op[i] = get_nir_src(instr->src[i].src);
714 op[i].type = brw_type_for_nir_type(devinfo,
715 (nir_alu_type)(nir_op_infos[instr->op].input_types[i] |
716 nir_src_bit_size(instr->src[i].src)));
717 op[i].abs = instr->src[i].abs;
718 op[i].negate = instr->src[i].negate;
719 }
720
721 /* Move and vecN instrutions may still be vectored. Return the raw,
722 * vectored source and destination so that fs_visitor::nir_emit_alu can
723 * handle it. Other callers should not have to handle these kinds of
724 * instructions.
725 */
726 switch (instr->op) {
727 case nir_op_mov:
728 case nir_op_vec2:
729 case nir_op_vec3:
730 case nir_op_vec4:
731 return result;
732 default:
733 break;
734 }
735
736 /* At this point, we have dealt with any instruction that operates on
737 * more than a single channel. Therefore, we can just adjust the source
738 * and destination registers for that channel and emit the instruction.
739 */
740 unsigned channel = 0;
741 if (nir_op_infos[instr->op].output_size == 0) {
742 /* Since NIR is doing the scalarizing for us, we should only ever see
743 * vectorized operations with a single channel.
744 */
745 assert(util_bitcount(instr->dest.write_mask) == 1);
746 channel = ffs(instr->dest.write_mask) - 1;
747
748 result = offset(result, bld, channel);
749 }
750
751 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
752 assert(nir_op_infos[instr->op].input_sizes[i] < 2);
753 op[i] = offset(op[i], bld, instr->src[i].swizzle[channel]);
754 }
755
756 return result;
757 }
758
759 void
760 fs_visitor::resolve_inot_sources(const fs_builder &bld, nir_alu_instr *instr,
761 fs_reg *op)
762 {
763 for (unsigned i = 0; i < 2; i++) {
764 nir_alu_instr *inot_instr = nir_src_as_alu_instr(instr->src[i].src);
765
766 if (inot_instr != NULL && inot_instr->op == nir_op_inot &&
767 !inot_instr->src[0].abs && !inot_instr->src[0].negate) {
768 /* The source of the inot is now the source of instr. */
769 prepare_alu_destination_and_sources(bld, inot_instr, &op[i], false);
770
771 assert(!op[i].negate);
772 op[i].negate = true;
773 } else {
774 op[i] = resolve_source_modifiers(op[i]);
775 }
776 }
777 }
778
779 bool
780 fs_visitor::try_emit_b2fi_of_inot(const fs_builder &bld,
781 fs_reg result,
782 nir_alu_instr *instr)
783 {
784 if (devinfo->gen < 6 || devinfo->gen >= 12)
785 return false;
786
787 nir_alu_instr *inot_instr = nir_src_as_alu_instr(instr->src[0].src);
788
789 if (inot_instr == NULL || inot_instr->op != nir_op_inot)
790 return false;
791
792 /* HF is also possible as a destination on BDW+. For nir_op_b2i, the set
793 * of valid size-changing combinations is a bit more complex.
794 *
795 * The source restriction is just because I was lazy about generating the
796 * constant below.
797 */
798 if (nir_dest_bit_size(instr->dest.dest) != 32 ||
799 nir_src_bit_size(inot_instr->src[0].src) != 32)
800 return false;
801
802 /* b2[fi](inot(a)) maps a=0 => 1, a=-1 => 0. Since a can only be 0 or -1,
803 * this is float(1 + a).
804 */
805 fs_reg op;
806
807 prepare_alu_destination_and_sources(bld, inot_instr, &op, false);
808
809 /* Ignore the saturate modifier, if there is one. The result of the
810 * arithmetic can only be 0 or 1, so the clamping will do nothing anyway.
811 */
812 bld.ADD(result, op, brw_imm_d(1));
813
814 return true;
815 }
816
817 /**
818 * Emit code for nir_op_fsign possibly fused with a nir_op_fmul
819 *
820 * If \c instr is not the \c nir_op_fsign, then \c fsign_src is the index of
821 * the source of \c instr that is a \c nir_op_fsign.
822 */
823 void
824 fs_visitor::emit_fsign(const fs_builder &bld, const nir_alu_instr *instr,
825 fs_reg result, fs_reg *op, unsigned fsign_src)
826 {
827 fs_inst *inst;
828
829 assert(instr->op == nir_op_fsign || instr->op == nir_op_fmul);
830 assert(fsign_src < nir_op_infos[instr->op].num_inputs);
831
832 if (instr->op != nir_op_fsign) {
833 const nir_alu_instr *const fsign_instr =
834 nir_src_as_alu_instr(instr->src[fsign_src].src);
835
836 assert(!fsign_instr->dest.saturate);
837
838 /* op[fsign_src] has the nominal result of the fsign, and op[1 -
839 * fsign_src] has the other multiply source. This must be rearranged so
840 * that op[0] is the source of the fsign op[1] is the other multiply
841 * source.
842 */
843 if (fsign_src != 0)
844 op[1] = op[0];
845
846 op[0] = get_nir_src(fsign_instr->src[0].src);
847
848 const nir_alu_type t =
849 (nir_alu_type)(nir_op_infos[instr->op].input_types[0] |
850 nir_src_bit_size(fsign_instr->src[0].src));
851
852 op[0].type = brw_type_for_nir_type(devinfo, t);
853 op[0].abs = fsign_instr->src[0].abs;
854 op[0].negate = fsign_instr->src[0].negate;
855
856 unsigned channel = 0;
857 if (nir_op_infos[instr->op].output_size == 0) {
858 /* Since NIR is doing the scalarizing for us, we should only ever see
859 * vectorized operations with a single channel.
860 */
861 assert(util_bitcount(instr->dest.write_mask) == 1);
862 channel = ffs(instr->dest.write_mask) - 1;
863 }
864
865 op[0] = offset(op[0], bld, fsign_instr->src[0].swizzle[channel]);
866 } else {
867 assert(!instr->dest.saturate);
868 }
869
870 if (op[0].abs) {
871 /* Straightforward since the source can be assumed to be either strictly
872 * >= 0 or strictly <= 0 depending on the setting of the negate flag.
873 */
874 set_condmod(BRW_CONDITIONAL_NZ, bld.MOV(result, op[0]));
875
876 if (instr->op == nir_op_fsign) {
877 inst = (op[0].negate)
878 ? bld.MOV(result, brw_imm_f(-1.0f))
879 : bld.MOV(result, brw_imm_f(1.0f));
880 } else {
881 op[1].negate = (op[0].negate != op[1].negate);
882 inst = bld.MOV(result, op[1]);
883 }
884
885 set_predicate(BRW_PREDICATE_NORMAL, inst);
886 } else if (type_sz(op[0].type) == 2) {
887 /* AND(val, 0x8000) gives the sign bit.
888 *
889 * Predicated OR ORs 1.0 (0x3c00) with the sign bit if val is not zero.
890 */
891 fs_reg zero = retype(brw_imm_uw(0), BRW_REGISTER_TYPE_HF);
892 bld.CMP(bld.null_reg_f(), op[0], zero, BRW_CONDITIONAL_NZ);
893
894 op[0].type = BRW_REGISTER_TYPE_UW;
895 result.type = BRW_REGISTER_TYPE_UW;
896 bld.AND(result, op[0], brw_imm_uw(0x8000u));
897
898 if (instr->op == nir_op_fsign)
899 inst = bld.OR(result, result, brw_imm_uw(0x3c00u));
900 else {
901 /* Use XOR here to get the result sign correct. */
902 inst = bld.XOR(result, result, retype(op[1], BRW_REGISTER_TYPE_UW));
903 }
904
905 inst->predicate = BRW_PREDICATE_NORMAL;
906 } else if (type_sz(op[0].type) == 4) {
907 /* AND(val, 0x80000000) gives the sign bit.
908 *
909 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
910 * zero.
911 */
912 bld.CMP(bld.null_reg_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ);
913
914 op[0].type = BRW_REGISTER_TYPE_UD;
915 result.type = BRW_REGISTER_TYPE_UD;
916 bld.AND(result, op[0], brw_imm_ud(0x80000000u));
917
918 if (instr->op == nir_op_fsign)
919 inst = bld.OR(result, result, brw_imm_ud(0x3f800000u));
920 else {
921 /* Use XOR here to get the result sign correct. */
922 inst = bld.XOR(result, result, retype(op[1], BRW_REGISTER_TYPE_UD));
923 }
924
925 inst->predicate = BRW_PREDICATE_NORMAL;
926 } else {
927 /* For doubles we do the same but we need to consider:
928 *
929 * - 2-src instructions can't operate with 64-bit immediates
930 * - The sign is encoded in the high 32-bit of each DF
931 * - We need to produce a DF result.
932 */
933
934 fs_reg zero = vgrf(glsl_type::double_type);
935 bld.MOV(zero, setup_imm_df(bld, 0.0));
936 bld.CMP(bld.null_reg_df(), op[0], zero, BRW_CONDITIONAL_NZ);
937
938 bld.MOV(result, zero);
939
940 fs_reg r = subscript(result, BRW_REGISTER_TYPE_UD, 1);
941 bld.AND(r, subscript(op[0], BRW_REGISTER_TYPE_UD, 1),
942 brw_imm_ud(0x80000000u));
943
944 if (instr->op == nir_op_fsign) {
945 set_predicate(BRW_PREDICATE_NORMAL,
946 bld.OR(r, r, brw_imm_ud(0x3ff00000u)));
947 } else {
948 /* This could be done better in some cases. If the scale is an
949 * immediate with the low 32-bits all 0, emitting a separate XOR and
950 * OR would allow an algebraic optimization to remove the OR. There
951 * are currently zero instances of fsign(double(x))*IMM in shader-db
952 * or any test suite, so it is hard to care at this time.
953 */
954 fs_reg result_int64 = retype(result, BRW_REGISTER_TYPE_UQ);
955 inst = bld.XOR(result_int64, result_int64,
956 retype(op[1], BRW_REGISTER_TYPE_UQ));
957 }
958 }
959 }
960
961 /**
962 * Deteremine whether sources of a nir_op_fmul can be fused with a nir_op_fsign
963 *
964 * Checks the operands of a \c nir_op_fmul to determine whether or not
965 * \c emit_fsign could fuse the multiplication with the \c sign() calculation.
966 *
967 * \param instr The multiplication instruction
968 *
969 * \param fsign_src The source of \c instr that may or may not be a
970 * \c nir_op_fsign
971 */
972 static bool
973 can_fuse_fmul_fsign(nir_alu_instr *instr, unsigned fsign_src)
974 {
975 assert(instr->op == nir_op_fmul);
976
977 nir_alu_instr *const fsign_instr =
978 nir_src_as_alu_instr(instr->src[fsign_src].src);
979
980 /* Rules:
981 *
982 * 1. instr->src[fsign_src] must be a nir_op_fsign.
983 * 2. The nir_op_fsign can only be used by this multiplication.
984 * 3. The source that is the nir_op_fsign does not have source modifiers.
985 * \c emit_fsign only examines the source modifiers of the source of the
986 * \c nir_op_fsign.
987 *
988 * The nir_op_fsign must also not have the saturate modifier, but steps
989 * have already been taken (in nir_opt_algebraic) to ensure that.
990 */
991 return fsign_instr != NULL && fsign_instr->op == nir_op_fsign &&
992 is_used_once(fsign_instr) &&
993 !instr->src[fsign_src].abs && !instr->src[fsign_src].negate;
994 }
995
996 void
997 fs_visitor::nir_emit_alu(const fs_builder &bld, nir_alu_instr *instr,
998 bool need_dest)
999 {
1000 struct brw_wm_prog_key *fs_key = (struct brw_wm_prog_key *) this->key;
1001 fs_inst *inst;
1002 unsigned execution_mode =
1003 bld.shader->nir->info.float_controls_execution_mode;
1004
1005 fs_reg op[4];
1006 fs_reg result = prepare_alu_destination_and_sources(bld, instr, op, need_dest);
1007
1008 switch (instr->op) {
1009 case nir_op_mov:
1010 case nir_op_vec2:
1011 case nir_op_vec3:
1012 case nir_op_vec4: {
1013 fs_reg temp = result;
1014 bool need_extra_copy = false;
1015 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1016 if (!instr->src[i].src.is_ssa &&
1017 instr->dest.dest.reg.reg == instr->src[i].src.reg.reg) {
1018 need_extra_copy = true;
1019 temp = bld.vgrf(result.type, 4);
1020 break;
1021 }
1022 }
1023
1024 for (unsigned i = 0; i < 4; i++) {
1025 if (!(instr->dest.write_mask & (1 << i)))
1026 continue;
1027
1028 if (instr->op == nir_op_mov) {
1029 inst = bld.MOV(offset(temp, bld, i),
1030 offset(op[0], bld, instr->src[0].swizzle[i]));
1031 } else {
1032 inst = bld.MOV(offset(temp, bld, i),
1033 offset(op[i], bld, instr->src[i].swizzle[0]));
1034 }
1035 inst->saturate = instr->dest.saturate;
1036 }
1037
1038 /* In this case the source and destination registers were the same,
1039 * so we need to insert an extra set of moves in order to deal with
1040 * any swizzling.
1041 */
1042 if (need_extra_copy) {
1043 for (unsigned i = 0; i < 4; i++) {
1044 if (!(instr->dest.write_mask & (1 << i)))
1045 continue;
1046
1047 bld.MOV(offset(result, bld, i), offset(temp, bld, i));
1048 }
1049 }
1050 return;
1051 }
1052
1053 case nir_op_i2f32:
1054 case nir_op_u2f32:
1055 if (optimize_extract_to_float(instr, result))
1056 return;
1057 inst = bld.MOV(result, op[0]);
1058 inst->saturate = instr->dest.saturate;
1059 break;
1060
1061 case nir_op_f2f16_rtne:
1062 case nir_op_f2f16_rtz:
1063 case nir_op_f2f16: {
1064 brw_rnd_mode rnd = BRW_RND_MODE_UNSPECIFIED;
1065
1066 if (nir_op_f2f16 == instr->op)
1067 rnd = brw_rnd_mode_from_execution_mode(execution_mode);
1068 else
1069 rnd = brw_rnd_mode_from_nir_op(instr->op);
1070
1071 if (BRW_RND_MODE_UNSPECIFIED != rnd)
1072 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(), brw_imm_d(rnd));
1073
1074 /* In theory, it would be better to use BRW_OPCODE_F32TO16. Depending
1075 * on the HW gen, it is a special hw opcode or just a MOV, and
1076 * brw_F32TO16 (at brw_eu_emit) would do the work to chose.
1077 *
1078 * But if we want to use that opcode, we need to provide support on
1079 * different optimizations and lowerings. As right now HF support is
1080 * only for gen8+, it will be better to use directly the MOV, and use
1081 * BRW_OPCODE_F32TO16 when/if we work for HF support on gen7.
1082 */
1083 assert(type_sz(op[0].type) < 8); /* brw_nir_lower_conversions */
1084 inst = bld.MOV(result, op[0]);
1085 inst->saturate = instr->dest.saturate;
1086 break;
1087 }
1088
1089 case nir_op_b2i8:
1090 case nir_op_b2i16:
1091 case nir_op_b2i32:
1092 case nir_op_b2i64:
1093 case nir_op_b2f16:
1094 case nir_op_b2f32:
1095 case nir_op_b2f64:
1096 if (try_emit_b2fi_of_inot(bld, result, instr))
1097 break;
1098 op[0].type = BRW_REGISTER_TYPE_D;
1099 op[0].negate = !op[0].negate;
1100 /* fallthrough */
1101 case nir_op_i2f64:
1102 case nir_op_i2i64:
1103 case nir_op_u2f64:
1104 case nir_op_u2u64:
1105 case nir_op_f2f64:
1106 case nir_op_f2i64:
1107 case nir_op_f2u64:
1108 case nir_op_i2i32:
1109 case nir_op_u2u32:
1110 case nir_op_f2i32:
1111 case nir_op_f2u32:
1112 case nir_op_i2f16:
1113 case nir_op_i2i16:
1114 case nir_op_u2f16:
1115 case nir_op_u2u16:
1116 case nir_op_f2i16:
1117 case nir_op_f2u16:
1118 case nir_op_i2i8:
1119 case nir_op_u2u8:
1120 case nir_op_f2i8:
1121 case nir_op_f2u8:
1122 if (result.type == BRW_REGISTER_TYPE_B ||
1123 result.type == BRW_REGISTER_TYPE_UB ||
1124 result.type == BRW_REGISTER_TYPE_HF)
1125 assert(type_sz(op[0].type) < 8); /* brw_nir_lower_conversions */
1126
1127 if (op[0].type == BRW_REGISTER_TYPE_B ||
1128 op[0].type == BRW_REGISTER_TYPE_UB ||
1129 op[0].type == BRW_REGISTER_TYPE_HF)
1130 assert(type_sz(result.type) < 8); /* brw_nir_lower_conversions */
1131
1132 inst = bld.MOV(result, op[0]);
1133 inst->saturate = instr->dest.saturate;
1134 break;
1135
1136 case nir_op_fsat:
1137 inst = bld.MOV(result, op[0]);
1138 inst->saturate = true;
1139 break;
1140
1141 case nir_op_fneg:
1142 case nir_op_ineg:
1143 op[0].negate = true;
1144 inst = bld.MOV(result, op[0]);
1145 if (instr->op == nir_op_fneg)
1146 inst->saturate = instr->dest.saturate;
1147 break;
1148
1149 case nir_op_fabs:
1150 case nir_op_iabs:
1151 op[0].negate = false;
1152 op[0].abs = true;
1153 inst = bld.MOV(result, op[0]);
1154 if (instr->op == nir_op_fabs)
1155 inst->saturate = instr->dest.saturate;
1156 break;
1157
1158 case nir_op_f2f32:
1159 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1160 brw_rnd_mode rnd =
1161 brw_rnd_mode_from_execution_mode(execution_mode);
1162 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1163 brw_imm_d(rnd));
1164 }
1165
1166 if (op[0].type == BRW_REGISTER_TYPE_HF)
1167 assert(type_sz(result.type) < 8); /* brw_nir_lower_conversions */
1168
1169 inst = bld.MOV(result, op[0]);
1170 inst->saturate = instr->dest.saturate;
1171 break;
1172
1173 case nir_op_fsign:
1174 emit_fsign(bld, instr, result, op, 0);
1175 break;
1176
1177 case nir_op_frcp:
1178 inst = bld.emit(SHADER_OPCODE_RCP, result, op[0]);
1179 inst->saturate = instr->dest.saturate;
1180 break;
1181
1182 case nir_op_fexp2:
1183 inst = bld.emit(SHADER_OPCODE_EXP2, result, op[0]);
1184 inst->saturate = instr->dest.saturate;
1185 break;
1186
1187 case nir_op_flog2:
1188 inst = bld.emit(SHADER_OPCODE_LOG2, result, op[0]);
1189 inst->saturate = instr->dest.saturate;
1190 break;
1191
1192 case nir_op_fsin:
1193 inst = bld.emit(SHADER_OPCODE_SIN, result, op[0]);
1194 inst->saturate = instr->dest.saturate;
1195 break;
1196
1197 case nir_op_fcos:
1198 inst = bld.emit(SHADER_OPCODE_COS, result, op[0]);
1199 inst->saturate = instr->dest.saturate;
1200 break;
1201
1202 case nir_op_fddx:
1203 if (fs_key->high_quality_derivatives) {
1204 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
1205 } else {
1206 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
1207 }
1208 inst->saturate = instr->dest.saturate;
1209 break;
1210 case nir_op_fddx_fine:
1211 inst = bld.emit(FS_OPCODE_DDX_FINE, result, op[0]);
1212 inst->saturate = instr->dest.saturate;
1213 break;
1214 case nir_op_fddx_coarse:
1215 inst = bld.emit(FS_OPCODE_DDX_COARSE, result, op[0]);
1216 inst->saturate = instr->dest.saturate;
1217 break;
1218 case nir_op_fddy:
1219 if (fs_key->high_quality_derivatives) {
1220 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
1221 } else {
1222 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
1223 }
1224 inst->saturate = instr->dest.saturate;
1225 break;
1226 case nir_op_fddy_fine:
1227 inst = bld.emit(FS_OPCODE_DDY_FINE, result, op[0]);
1228 inst->saturate = instr->dest.saturate;
1229 break;
1230 case nir_op_fddy_coarse:
1231 inst = bld.emit(FS_OPCODE_DDY_COARSE, result, op[0]);
1232 inst->saturate = instr->dest.saturate;
1233 break;
1234
1235 case nir_op_fadd:
1236 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1237 brw_rnd_mode rnd =
1238 brw_rnd_mode_from_execution_mode(execution_mode);
1239 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1240 brw_imm_d(rnd));
1241 }
1242 /* fallthrough */
1243 case nir_op_iadd:
1244 inst = bld.ADD(result, op[0], op[1]);
1245 inst->saturate = instr->dest.saturate;
1246 break;
1247
1248 case nir_op_uadd_sat:
1249 inst = bld.ADD(result, op[0], op[1]);
1250 inst->saturate = true;
1251 break;
1252
1253 case nir_op_fmul:
1254 for (unsigned i = 0; i < 2; i++) {
1255 if (can_fuse_fmul_fsign(instr, i)) {
1256 emit_fsign(bld, instr, result, op, i);
1257 return;
1258 }
1259 }
1260
1261 /* We emit the rounding mode after the previous fsign optimization since
1262 * it won't result in a MUL, but will try to negate the value by other
1263 * means.
1264 */
1265 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1266 brw_rnd_mode rnd =
1267 brw_rnd_mode_from_execution_mode(execution_mode);
1268 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1269 brw_imm_d(rnd));
1270 }
1271
1272 inst = bld.MUL(result, op[0], op[1]);
1273 inst->saturate = instr->dest.saturate;
1274 break;
1275
1276 case nir_op_imul_2x32_64:
1277 case nir_op_umul_2x32_64:
1278 bld.MUL(result, op[0], op[1]);
1279 break;
1280
1281 case nir_op_imul:
1282 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1283 bld.MUL(result, op[0], op[1]);
1284 break;
1285
1286 case nir_op_imul_high:
1287 case nir_op_umul_high:
1288 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1289 bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
1290 break;
1291
1292 case nir_op_idiv:
1293 case nir_op_udiv:
1294 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1295 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
1296 break;
1297
1298 case nir_op_uadd_carry:
1299 unreachable("Should have been lowered by carry_to_arith().");
1300
1301 case nir_op_usub_borrow:
1302 unreachable("Should have been lowered by borrow_to_arith().");
1303
1304 case nir_op_umod:
1305 case nir_op_irem:
1306 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1307 * appears that our hardware just does the right thing for signed
1308 * remainder.
1309 */
1310 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1311 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
1312 break;
1313
1314 case nir_op_imod: {
1315 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1316 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
1317
1318 /* Math instructions don't support conditional mod */
1319 inst = bld.MOV(bld.null_reg_d(), result);
1320 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1321
1322 /* Now, we need to determine if signs of the sources are different.
1323 * When we XOR the sources, the top bit is 0 if they are the same and 1
1324 * if they are different. We can then use a conditional modifier to
1325 * turn that into a predicate. This leads us to an XOR.l instruction.
1326 *
1327 * Technically, according to the PRM, you're not allowed to use .l on a
1328 * XOR instruction. However, emperical experiments and Curro's reading
1329 * of the simulator source both indicate that it's safe.
1330 */
1331 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
1332 inst = bld.XOR(tmp, op[0], op[1]);
1333 inst->predicate = BRW_PREDICATE_NORMAL;
1334 inst->conditional_mod = BRW_CONDITIONAL_L;
1335
1336 /* If the result of the initial remainder operation is non-zero and the
1337 * two sources have different signs, add in a copy of op[1] to get the
1338 * final integer modulus value.
1339 */
1340 inst = bld.ADD(result, result, op[1]);
1341 inst->predicate = BRW_PREDICATE_NORMAL;
1342 break;
1343 }
1344
1345 case nir_op_flt32:
1346 case nir_op_fge32:
1347 case nir_op_feq32:
1348 case nir_op_fne32: {
1349 fs_reg dest = result;
1350
1351 const uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
1352 if (bit_size != 32)
1353 dest = bld.vgrf(op[0].type, 1);
1354
1355 bld.CMP(dest, op[0], op[1], brw_cmod_for_nir_comparison(instr->op));
1356
1357 if (bit_size > 32) {
1358 bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
1359 } else if(bit_size < 32) {
1360 /* When we convert the result to 32-bit we need to be careful and do
1361 * it as a signed conversion to get sign extension (for 32-bit true)
1362 */
1363 const brw_reg_type src_type =
1364 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_D);
1365
1366 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), retype(dest, src_type));
1367 }
1368 break;
1369 }
1370
1371 case nir_op_ilt32:
1372 case nir_op_ult32:
1373 case nir_op_ige32:
1374 case nir_op_uge32:
1375 case nir_op_ieq32:
1376 case nir_op_ine32: {
1377 fs_reg dest = result;
1378
1379 /* On Gen11 we have an additional issue being that src1 cannot be a byte
1380 * type. So we convert both operands for the comparison.
1381 */
1382 fs_reg temp_op[2];
1383 temp_op[0] = bld.fix_byte_src(op[0]);
1384 temp_op[1] = bld.fix_byte_src(op[1]);
1385
1386 const uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
1387 if (bit_size != 32)
1388 dest = bld.vgrf(temp_op[0].type, 1);
1389
1390 bld.CMP(dest, temp_op[0], temp_op[1],
1391 brw_cmod_for_nir_comparison(instr->op));
1392
1393 if (bit_size > 32) {
1394 bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
1395 } else if (bit_size < 32) {
1396 /* When we convert the result to 32-bit we need to be careful and do
1397 * it as a signed conversion to get sign extension (for 32-bit true)
1398 */
1399 const brw_reg_type src_type =
1400 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_D);
1401
1402 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), retype(dest, src_type));
1403 }
1404 break;
1405 }
1406
1407 case nir_op_inot:
1408 if (devinfo->gen >= 8) {
1409 nir_alu_instr *inot_src_instr = nir_src_as_alu_instr(instr->src[0].src);
1410
1411 if (inot_src_instr != NULL &&
1412 (inot_src_instr->op == nir_op_ior ||
1413 inot_src_instr->op == nir_op_ixor ||
1414 inot_src_instr->op == nir_op_iand) &&
1415 !inot_src_instr->src[0].abs &&
1416 !inot_src_instr->src[0].negate &&
1417 !inot_src_instr->src[1].abs &&
1418 !inot_src_instr->src[1].negate) {
1419 /* The sources of the source logical instruction are now the
1420 * sources of the instruction that will be generated.
1421 */
1422 prepare_alu_destination_and_sources(bld, inot_src_instr, op, false);
1423 resolve_inot_sources(bld, inot_src_instr, op);
1424
1425 /* Smash all of the sources and destination to be signed. This
1426 * doesn't matter for the operation of the instruction, but cmod
1427 * propagation fails on unsigned sources with negation (due to
1428 * fs_inst::can_do_cmod returning false).
1429 */
1430 result.type =
1431 brw_type_for_nir_type(devinfo,
1432 (nir_alu_type)(nir_type_int |
1433 nir_dest_bit_size(instr->dest.dest)));
1434 op[0].type =
1435 brw_type_for_nir_type(devinfo,
1436 (nir_alu_type)(nir_type_int |
1437 nir_src_bit_size(inot_src_instr->src[0].src)));
1438 op[1].type =
1439 brw_type_for_nir_type(devinfo,
1440 (nir_alu_type)(nir_type_int |
1441 nir_src_bit_size(inot_src_instr->src[1].src)));
1442
1443 /* For XOR, only invert one of the sources. Arbitrarily choose
1444 * the first source.
1445 */
1446 op[0].negate = !op[0].negate;
1447 if (inot_src_instr->op != nir_op_ixor)
1448 op[1].negate = !op[1].negate;
1449
1450 switch (inot_src_instr->op) {
1451 case nir_op_ior:
1452 bld.AND(result, op[0], op[1]);
1453 return;
1454
1455 case nir_op_iand:
1456 bld.OR(result, op[0], op[1]);
1457 return;
1458
1459 case nir_op_ixor:
1460 bld.XOR(result, op[0], op[1]);
1461 return;
1462
1463 default:
1464 unreachable("impossible opcode");
1465 }
1466 }
1467 op[0] = resolve_source_modifiers(op[0]);
1468 }
1469 bld.NOT(result, op[0]);
1470 break;
1471 case nir_op_ixor:
1472 if (devinfo->gen >= 8) {
1473 resolve_inot_sources(bld, instr, op);
1474 }
1475 bld.XOR(result, op[0], op[1]);
1476 break;
1477 case nir_op_ior:
1478 if (devinfo->gen >= 8) {
1479 resolve_inot_sources(bld, instr, op);
1480 }
1481 bld.OR(result, op[0], op[1]);
1482 break;
1483 case nir_op_iand:
1484 if (devinfo->gen >= 8) {
1485 resolve_inot_sources(bld, instr, op);
1486 }
1487 bld.AND(result, op[0], op[1]);
1488 break;
1489
1490 case nir_op_fdot2:
1491 case nir_op_fdot3:
1492 case nir_op_fdot4:
1493 case nir_op_b32all_fequal2:
1494 case nir_op_b32all_iequal2:
1495 case nir_op_b32all_fequal3:
1496 case nir_op_b32all_iequal3:
1497 case nir_op_b32all_fequal4:
1498 case nir_op_b32all_iequal4:
1499 case nir_op_b32any_fnequal2:
1500 case nir_op_b32any_inequal2:
1501 case nir_op_b32any_fnequal3:
1502 case nir_op_b32any_inequal3:
1503 case nir_op_b32any_fnequal4:
1504 case nir_op_b32any_inequal4:
1505 unreachable("Lowered by nir_lower_alu_reductions");
1506
1507 case nir_op_fnoise1_1:
1508 case nir_op_fnoise1_2:
1509 case nir_op_fnoise1_3:
1510 case nir_op_fnoise1_4:
1511 case nir_op_fnoise2_1:
1512 case nir_op_fnoise2_2:
1513 case nir_op_fnoise2_3:
1514 case nir_op_fnoise2_4:
1515 case nir_op_fnoise3_1:
1516 case nir_op_fnoise3_2:
1517 case nir_op_fnoise3_3:
1518 case nir_op_fnoise3_4:
1519 case nir_op_fnoise4_1:
1520 case nir_op_fnoise4_2:
1521 case nir_op_fnoise4_3:
1522 case nir_op_fnoise4_4:
1523 unreachable("not reached: should be handled by lower_noise");
1524
1525 case nir_op_ldexp:
1526 unreachable("not reached: should be handled by ldexp_to_arith()");
1527
1528 case nir_op_fsqrt:
1529 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
1530 inst->saturate = instr->dest.saturate;
1531 break;
1532
1533 case nir_op_frsq:
1534 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
1535 inst->saturate = instr->dest.saturate;
1536 break;
1537
1538 case nir_op_i2b32:
1539 case nir_op_f2b32: {
1540 uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
1541 if (bit_size == 64) {
1542 /* two-argument instructions can't take 64-bit immediates */
1543 fs_reg zero;
1544 fs_reg tmp;
1545
1546 if (instr->op == nir_op_f2b32) {
1547 zero = vgrf(glsl_type::double_type);
1548 tmp = vgrf(glsl_type::double_type);
1549 bld.MOV(zero, setup_imm_df(bld, 0.0));
1550 } else {
1551 zero = vgrf(glsl_type::int64_t_type);
1552 tmp = vgrf(glsl_type::int64_t_type);
1553 bld.MOV(zero, brw_imm_q(0));
1554 }
1555
1556 /* A SIMD16 execution needs to be split in two instructions, so use
1557 * a vgrf instead of the flag register as dst so instruction splitting
1558 * works
1559 */
1560 bld.CMP(tmp, op[0], zero, BRW_CONDITIONAL_NZ);
1561 bld.MOV(result, subscript(tmp, BRW_REGISTER_TYPE_UD, 0));
1562 } else {
1563 fs_reg zero;
1564 if (bit_size == 32) {
1565 zero = instr->op == nir_op_f2b32 ? brw_imm_f(0.0f) : brw_imm_d(0);
1566 } else {
1567 assert(bit_size == 16);
1568 zero = instr->op == nir_op_f2b32 ?
1569 retype(brw_imm_w(0), BRW_REGISTER_TYPE_HF) : brw_imm_w(0);
1570 }
1571 bld.CMP(result, op[0], zero, BRW_CONDITIONAL_NZ);
1572 }
1573 break;
1574 }
1575
1576 case nir_op_ftrunc:
1577 inst = bld.RNDZ(result, op[0]);
1578 inst->saturate = instr->dest.saturate;
1579 break;
1580
1581 case nir_op_fceil: {
1582 op[0].negate = !op[0].negate;
1583 fs_reg temp = vgrf(glsl_type::float_type);
1584 bld.RNDD(temp, op[0]);
1585 temp.negate = true;
1586 inst = bld.MOV(result, temp);
1587 inst->saturate = instr->dest.saturate;
1588 break;
1589 }
1590 case nir_op_ffloor:
1591 inst = bld.RNDD(result, op[0]);
1592 inst->saturate = instr->dest.saturate;
1593 break;
1594 case nir_op_ffract:
1595 inst = bld.FRC(result, op[0]);
1596 inst->saturate = instr->dest.saturate;
1597 break;
1598 case nir_op_fround_even:
1599 inst = bld.RNDE(result, op[0]);
1600 inst->saturate = instr->dest.saturate;
1601 break;
1602
1603 case nir_op_fquantize2f16: {
1604 fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
1605 fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
1606 fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
1607
1608 /* The destination stride must be at least as big as the source stride. */
1609 tmp16.type = BRW_REGISTER_TYPE_W;
1610 tmp16.stride = 2;
1611
1612 /* Check for denormal */
1613 fs_reg abs_src0 = op[0];
1614 abs_src0.abs = true;
1615 bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1616 BRW_CONDITIONAL_L);
1617 /* Get the appropriately signed zero */
1618 bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
1619 retype(op[0], BRW_REGISTER_TYPE_UD),
1620 brw_imm_ud(0x80000000));
1621 /* Do the actual F32 -> F16 -> F32 conversion */
1622 bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
1623 bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
1624 /* Select that or zero based on normal status */
1625 inst = bld.SEL(result, zero, tmp32);
1626 inst->predicate = BRW_PREDICATE_NORMAL;
1627 inst->saturate = instr->dest.saturate;
1628 break;
1629 }
1630
1631 case nir_op_imin:
1632 case nir_op_umin:
1633 case nir_op_fmin:
1634 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_L);
1635 inst->saturate = instr->dest.saturate;
1636 break;
1637
1638 case nir_op_imax:
1639 case nir_op_umax:
1640 case nir_op_fmax:
1641 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_GE);
1642 inst->saturate = instr->dest.saturate;
1643 break;
1644
1645 case nir_op_pack_snorm_2x16:
1646 case nir_op_pack_snorm_4x8:
1647 case nir_op_pack_unorm_2x16:
1648 case nir_op_pack_unorm_4x8:
1649 case nir_op_unpack_snorm_2x16:
1650 case nir_op_unpack_snorm_4x8:
1651 case nir_op_unpack_unorm_2x16:
1652 case nir_op_unpack_unorm_4x8:
1653 case nir_op_unpack_half_2x16:
1654 case nir_op_pack_half_2x16:
1655 unreachable("not reached: should be handled by lower_packing_builtins");
1656
1657 case nir_op_unpack_half_2x16_split_x_flush_to_zero:
1658 assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 & execution_mode);
1659 /* Fall-through */
1660 case nir_op_unpack_half_2x16_split_x:
1661 inst = bld.emit(BRW_OPCODE_F16TO32, result,
1662 subscript(op[0], BRW_REGISTER_TYPE_UW, 0));
1663 inst->saturate = instr->dest.saturate;
1664 break;
1665
1666 case nir_op_unpack_half_2x16_split_y_flush_to_zero:
1667 assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 & execution_mode);
1668 /* Fall-through */
1669 case nir_op_unpack_half_2x16_split_y:
1670 inst = bld.emit(BRW_OPCODE_F16TO32, result,
1671 subscript(op[0], BRW_REGISTER_TYPE_UW, 1));
1672 inst->saturate = instr->dest.saturate;
1673 break;
1674
1675 case nir_op_pack_64_2x32_split:
1676 case nir_op_pack_32_2x16_split:
1677 bld.emit(FS_OPCODE_PACK, result, op[0], op[1]);
1678 break;
1679
1680 case nir_op_unpack_64_2x32_split_x:
1681 case nir_op_unpack_64_2x32_split_y: {
1682 if (instr->op == nir_op_unpack_64_2x32_split_x)
1683 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 0));
1684 else
1685 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
1686 break;
1687 }
1688
1689 case nir_op_unpack_32_2x16_split_x:
1690 case nir_op_unpack_32_2x16_split_y: {
1691 if (instr->op == nir_op_unpack_32_2x16_split_x)
1692 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UW, 0));
1693 else
1694 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UW, 1));
1695 break;
1696 }
1697
1698 case nir_op_fpow:
1699 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
1700 inst->saturate = instr->dest.saturate;
1701 break;
1702
1703 case nir_op_bitfield_reverse:
1704 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1705 bld.BFREV(result, op[0]);
1706 break;
1707
1708 case nir_op_bit_count:
1709 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1710 bld.CBIT(result, op[0]);
1711 break;
1712
1713 case nir_op_ufind_msb: {
1714 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1715 emit_find_msb_using_lzd(bld, result, op[0], false);
1716 break;
1717 }
1718
1719 case nir_op_ifind_msb: {
1720 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1721
1722 if (devinfo->gen < 7) {
1723 emit_find_msb_using_lzd(bld, result, op[0], true);
1724 } else {
1725 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
1726
1727 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1728 * count from the LSB side. If FBH didn't return an error
1729 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1730 * count into an LSB count.
1731 */
1732 bld.CMP(bld.null_reg_d(), result, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1733
1734 inst = bld.ADD(result, result, brw_imm_d(31));
1735 inst->predicate = BRW_PREDICATE_NORMAL;
1736 inst->src[0].negate = true;
1737 }
1738 break;
1739 }
1740
1741 case nir_op_find_lsb:
1742 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1743
1744 if (devinfo->gen < 7) {
1745 fs_reg temp = vgrf(glsl_type::int_type);
1746
1747 /* (x & -x) generates a value that consists of only the LSB of x.
1748 * For all powers of 2, findMSB(y) == findLSB(y).
1749 */
1750 fs_reg src = retype(op[0], BRW_REGISTER_TYPE_D);
1751 fs_reg negated_src = src;
1752
1753 /* One must be negated, and the other must be non-negated. It
1754 * doesn't matter which is which.
1755 */
1756 negated_src.negate = true;
1757 src.negate = false;
1758
1759 bld.AND(temp, src, negated_src);
1760 emit_find_msb_using_lzd(bld, result, temp, false);
1761 } else {
1762 bld.FBL(result, op[0]);
1763 }
1764 break;
1765
1766 case nir_op_ubitfield_extract:
1767 case nir_op_ibitfield_extract:
1768 unreachable("should have been lowered");
1769 case nir_op_ubfe:
1770 case nir_op_ibfe:
1771 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1772 bld.BFE(result, op[2], op[1], op[0]);
1773 break;
1774 case nir_op_bfm:
1775 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1776 bld.BFI1(result, op[0], op[1]);
1777 break;
1778 case nir_op_bfi:
1779 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1780 bld.BFI2(result, op[0], op[1], op[2]);
1781 break;
1782
1783 case nir_op_bitfield_insert:
1784 unreachable("not reached: should have been lowered");
1785
1786 case nir_op_ishl:
1787 bld.SHL(result, op[0], op[1]);
1788 break;
1789 case nir_op_ishr:
1790 bld.ASR(result, op[0], op[1]);
1791 break;
1792 case nir_op_ushr:
1793 bld.SHR(result, op[0], op[1]);
1794 break;
1795
1796 case nir_op_urol:
1797 bld.ROL(result, op[0], op[1]);
1798 break;
1799 case nir_op_uror:
1800 bld.ROR(result, op[0], op[1]);
1801 break;
1802
1803 case nir_op_pack_half_2x16_split:
1804 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
1805 break;
1806
1807 case nir_op_ffma:
1808 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1809 brw_rnd_mode rnd =
1810 brw_rnd_mode_from_execution_mode(execution_mode);
1811 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1812 brw_imm_d(rnd));
1813 }
1814
1815 inst = bld.MAD(result, op[2], op[1], op[0]);
1816 inst->saturate = instr->dest.saturate;
1817 break;
1818
1819 case nir_op_flrp:
1820 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1821 brw_rnd_mode rnd =
1822 brw_rnd_mode_from_execution_mode(execution_mode);
1823 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1824 brw_imm_d(rnd));
1825 }
1826
1827 inst = bld.LRP(result, op[0], op[1], op[2]);
1828 inst->saturate = instr->dest.saturate;
1829 break;
1830
1831 case nir_op_b32csel:
1832 if (optimize_frontfacing_ternary(instr, result))
1833 return;
1834
1835 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1836 inst = bld.SEL(result, op[1], op[2]);
1837 inst->predicate = BRW_PREDICATE_NORMAL;
1838 break;
1839
1840 case nir_op_extract_u8:
1841 case nir_op_extract_i8: {
1842 unsigned byte = nir_src_as_uint(instr->src[1].src);
1843
1844 /* The PRMs say:
1845 *
1846 * BDW+
1847 * There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.
1848 * Use two instructions and a word or DWord intermediate integer type.
1849 */
1850 if (nir_dest_bit_size(instr->dest.dest) == 64) {
1851 const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
1852
1853 if (instr->op == nir_op_extract_i8) {
1854 /* If we need to sign extend, extract to a word first */
1855 fs_reg w_temp = bld.vgrf(BRW_REGISTER_TYPE_W);
1856 bld.MOV(w_temp, subscript(op[0], type, byte));
1857 bld.MOV(result, w_temp);
1858 } else if (byte & 1) {
1859 /* Extract the high byte from the word containing the desired byte
1860 * offset.
1861 */
1862 bld.SHR(result,
1863 subscript(op[0], BRW_REGISTER_TYPE_UW, byte / 2),
1864 brw_imm_uw(8));
1865 } else {
1866 /* Otherwise use an AND with 0xff and a word type */
1867 bld.AND(result,
1868 subscript(op[0], BRW_REGISTER_TYPE_UW, byte / 2),
1869 brw_imm_uw(0xff));
1870 }
1871 } else {
1872 const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
1873 bld.MOV(result, subscript(op[0], type, byte));
1874 }
1875 break;
1876 }
1877
1878 case nir_op_extract_u16:
1879 case nir_op_extract_i16: {
1880 const brw_reg_type type = brw_int_type(2, instr->op == nir_op_extract_i16);
1881 unsigned word = nir_src_as_uint(instr->src[1].src);
1882 bld.MOV(result, subscript(op[0], type, word));
1883 break;
1884 }
1885
1886 default:
1887 unreachable("unhandled instruction");
1888 }
1889
1890 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1891 * to sign extend the low bit to 0/~0
1892 */
1893 if (devinfo->gen <= 5 &&
1894 !result.is_null() &&
1895 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1896 fs_reg masked = vgrf(glsl_type::int_type);
1897 bld.AND(masked, result, brw_imm_d(1));
1898 masked.negate = true;
1899 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
1900 }
1901 }
1902
1903 void
1904 fs_visitor::nir_emit_load_const(const fs_builder &bld,
1905 nir_load_const_instr *instr)
1906 {
1907 const brw_reg_type reg_type =
1908 brw_reg_type_from_bit_size(instr->def.bit_size, BRW_REGISTER_TYPE_D);
1909 fs_reg reg = bld.vgrf(reg_type, instr->def.num_components);
1910
1911 switch (instr->def.bit_size) {
1912 case 8:
1913 for (unsigned i = 0; i < instr->def.num_components; i++)
1914 bld.MOV(offset(reg, bld, i), setup_imm_b(bld, instr->value[i].i8));
1915 break;
1916
1917 case 16:
1918 for (unsigned i = 0; i < instr->def.num_components; i++)
1919 bld.MOV(offset(reg, bld, i), brw_imm_w(instr->value[i].i16));
1920 break;
1921
1922 case 32:
1923 for (unsigned i = 0; i < instr->def.num_components; i++)
1924 bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value[i].i32));
1925 break;
1926
1927 case 64:
1928 assert(devinfo->gen >= 7);
1929 if (devinfo->gen == 7) {
1930 /* We don't get 64-bit integer types until gen8 */
1931 for (unsigned i = 0; i < instr->def.num_components; i++) {
1932 bld.MOV(retype(offset(reg, bld, i), BRW_REGISTER_TYPE_DF),
1933 setup_imm_df(bld, instr->value[i].f64));
1934 }
1935 } else {
1936 for (unsigned i = 0; i < instr->def.num_components; i++)
1937 bld.MOV(offset(reg, bld, i), brw_imm_q(instr->value[i].i64));
1938 }
1939 break;
1940
1941 default:
1942 unreachable("Invalid bit size");
1943 }
1944
1945 nir_ssa_values[instr->def.index] = reg;
1946 }
1947
1948 fs_reg
1949 fs_visitor::get_nir_src(const nir_src &src)
1950 {
1951 fs_reg reg;
1952 if (src.is_ssa) {
1953 if (src.ssa->parent_instr->type == nir_instr_type_ssa_undef) {
1954 const brw_reg_type reg_type =
1955 brw_reg_type_from_bit_size(src.ssa->bit_size, BRW_REGISTER_TYPE_D);
1956 reg = bld.vgrf(reg_type, src.ssa->num_components);
1957 } else {
1958 reg = nir_ssa_values[src.ssa->index];
1959 }
1960 } else {
1961 /* We don't handle indirects on locals */
1962 assert(src.reg.indirect == NULL);
1963 reg = offset(nir_locals[src.reg.reg->index], bld,
1964 src.reg.base_offset * src.reg.reg->num_components);
1965 }
1966
1967 if (nir_src_bit_size(src) == 64 && devinfo->gen == 7) {
1968 /* The only 64-bit type available on gen7 is DF, so use that. */
1969 reg.type = BRW_REGISTER_TYPE_DF;
1970 } else {
1971 /* To avoid floating-point denorm flushing problems, set the type by
1972 * default to an integer type - instructions that need floating point
1973 * semantics will set this to F if they need to
1974 */
1975 reg.type = brw_reg_type_from_bit_size(nir_src_bit_size(src),
1976 BRW_REGISTER_TYPE_D);
1977 }
1978
1979 return reg;
1980 }
1981
1982 /**
1983 * Return an IMM for constants; otherwise call get_nir_src() as normal.
1984 *
1985 * This function should not be called on any value which may be 64 bits.
1986 * We could theoretically support 64-bit on gen8+ but we choose not to
1987 * because it wouldn't work in general (no gen7 support) and there are
1988 * enough restrictions in 64-bit immediates that you can't take the return
1989 * value and treat it the same as the result of get_nir_src().
1990 */
1991 fs_reg
1992 fs_visitor::get_nir_src_imm(const nir_src &src)
1993 {
1994 assert(nir_src_bit_size(src) == 32);
1995 return nir_src_is_const(src) ?
1996 fs_reg(brw_imm_d(nir_src_as_int(src))) : get_nir_src(src);
1997 }
1998
1999 fs_reg
2000 fs_visitor::get_nir_dest(const nir_dest &dest)
2001 {
2002 if (dest.is_ssa) {
2003 const brw_reg_type reg_type =
2004 brw_reg_type_from_bit_size(dest.ssa.bit_size,
2005 dest.ssa.bit_size == 8 ?
2006 BRW_REGISTER_TYPE_D :
2007 BRW_REGISTER_TYPE_F);
2008 nir_ssa_values[dest.ssa.index] =
2009 bld.vgrf(reg_type, dest.ssa.num_components);
2010 bld.UNDEF(nir_ssa_values[dest.ssa.index]);
2011 return nir_ssa_values[dest.ssa.index];
2012 } else {
2013 /* We don't handle indirects on locals */
2014 assert(dest.reg.indirect == NULL);
2015 return offset(nir_locals[dest.reg.reg->index], bld,
2016 dest.reg.base_offset * dest.reg.reg->num_components);
2017 }
2018 }
2019
2020 void
2021 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
2022 unsigned wr_mask)
2023 {
2024 for (unsigned i = 0; i < 4; i++) {
2025 if (!((wr_mask >> i) & 1))
2026 continue;
2027
2028 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
2029 new_inst->dst = offset(new_inst->dst, bld, i);
2030 for (unsigned j = 0; j < new_inst->sources; j++)
2031 if (new_inst->src[j].file == VGRF)
2032 new_inst->src[j] = offset(new_inst->src[j], bld, i);
2033
2034 bld.emit(new_inst);
2035 }
2036 }
2037
2038 static fs_inst *
2039 emit_pixel_interpolater_send(const fs_builder &bld,
2040 enum opcode opcode,
2041 const fs_reg &dst,
2042 const fs_reg &src,
2043 const fs_reg &desc,
2044 glsl_interp_mode interpolation)
2045 {
2046 struct brw_wm_prog_data *wm_prog_data =
2047 brw_wm_prog_data(bld.shader->stage_prog_data);
2048
2049 fs_inst *inst = bld.emit(opcode, dst, src, desc);
2050 /* 2 floats per slot returned */
2051 inst->size_written = 2 * dst.component_size(inst->exec_size);
2052 inst->pi_noperspective = interpolation == INTERP_MODE_NOPERSPECTIVE;
2053
2054 wm_prog_data->pulls_bary = true;
2055
2056 return inst;
2057 }
2058
2059 /**
2060 * Computes 1 << x, given a D/UD register containing some value x.
2061 */
2062 static fs_reg
2063 intexp2(const fs_builder &bld, const fs_reg &x)
2064 {
2065 assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
2066
2067 fs_reg result = bld.vgrf(x.type, 1);
2068 fs_reg one = bld.vgrf(x.type, 1);
2069
2070 bld.MOV(one, retype(brw_imm_d(1), one.type));
2071 bld.SHL(result, one, x);
2072 return result;
2073 }
2074
2075 void
2076 fs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
2077 {
2078 assert(stage == MESA_SHADER_GEOMETRY);
2079
2080 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2081
2082 if (gs_compile->control_data_header_size_bits == 0)
2083 return;
2084
2085 /* We can only do EndPrimitive() functionality when the control data
2086 * consists of cut bits. Fortunately, the only time it isn't is when the
2087 * output type is points, in which case EndPrimitive() is a no-op.
2088 */
2089 if (gs_prog_data->control_data_format !=
2090 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
2091 return;
2092 }
2093
2094 /* Cut bits use one bit per vertex. */
2095 assert(gs_compile->control_data_bits_per_vertex == 1);
2096
2097 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
2098 vertex_count.type = BRW_REGISTER_TYPE_UD;
2099
2100 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
2101 * vertex n, 0 otherwise. So all we need to do here is mark bit
2102 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
2103 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
2104 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
2105 *
2106 * Note that if EndPrimitive() is called before emitting any vertices, this
2107 * will cause us to set bit 31 of the control_data_bits register to 1.
2108 * That's fine because:
2109 *
2110 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
2111 * output, so the hardware will ignore cut bit 31.
2112 *
2113 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
2114 * last vertex, so setting cut bit 31 has no effect (since the primitive
2115 * is automatically ended when the GS terminates).
2116 *
2117 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
2118 * control_data_bits register to 0 when the first vertex is emitted.
2119 */
2120
2121 const fs_builder abld = bld.annotate("end primitive");
2122
2123 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
2124 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2125 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
2126 fs_reg mask = intexp2(abld, prev_count);
2127 /* Note: we're relying on the fact that the GEN SHL instruction only pays
2128 * attention to the lower 5 bits of its second source argument, so on this
2129 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
2130 * ((vertex_count - 1) % 32).
2131 */
2132 abld.OR(this->control_data_bits, this->control_data_bits, mask);
2133 }
2134
2135 void
2136 fs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
2137 {
2138 assert(stage == MESA_SHADER_GEOMETRY);
2139 assert(gs_compile->control_data_bits_per_vertex != 0);
2140
2141 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2142
2143 const fs_builder abld = bld.annotate("emit control data bits");
2144 const fs_builder fwa_bld = bld.exec_all();
2145
2146 /* We use a single UD register to accumulate control data bits (32 bits
2147 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
2148 * at a time.
2149 *
2150 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
2151 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
2152 * use the Channel Mask phase to enable/disable which DWord within that
2153 * group to write. (Remember, different SIMD8 channels may have emitted
2154 * different numbers of vertices, so we may need per-slot offsets.)
2155 *
2156 * Channel masking presents an annoying problem: we may have to replicate
2157 * the data up to 4 times:
2158 *
2159 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
2160 *
2161 * To avoid penalizing shaders that emit a small number of vertices, we
2162 * can avoid these sometimes: if the size of the control data header is
2163 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
2164 * land in the same 128-bit group, so we can skip per-slot offsets.
2165 *
2166 * Similarly, if the control data header is <= 32 bits, there is only one
2167 * DWord, so we can skip channel masks.
2168 */
2169 enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
2170
2171 fs_reg channel_mask, per_slot_offset;
2172
2173 if (gs_compile->control_data_header_size_bits > 32) {
2174 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2175 channel_mask = vgrf(glsl_type::uint_type);
2176 }
2177
2178 if (gs_compile->control_data_header_size_bits > 128) {
2179 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
2180 per_slot_offset = vgrf(glsl_type::uint_type);
2181 }
2182
2183 /* Figure out which DWord we're trying to write to using the formula:
2184 *
2185 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
2186 *
2187 * Since bits_per_vertex is a power of two, and is known at compile
2188 * time, this can be optimized to:
2189 *
2190 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
2191 */
2192 if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
2193 fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2194 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2195 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
2196 unsigned log2_bits_per_vertex =
2197 util_last_bit(gs_compile->control_data_bits_per_vertex);
2198 abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
2199
2200 if (per_slot_offset.file != BAD_FILE) {
2201 /* Set the per-slot offset to dword_index / 4, so that we'll write to
2202 * the appropriate OWord within the control data header.
2203 */
2204 abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
2205 }
2206
2207 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
2208 * write to the appropriate DWORD within the OWORD.
2209 */
2210 fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2211 fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
2212 channel_mask = intexp2(fwa_bld, channel);
2213 /* Then the channel masks need to be in bits 23:16. */
2214 fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
2215 }
2216
2217 /* Store the control data bits in the message payload and send it. */
2218 unsigned mlen = 2;
2219 if (channel_mask.file != BAD_FILE)
2220 mlen += 4; /* channel masks, plus 3 extra copies of the data */
2221 if (per_slot_offset.file != BAD_FILE)
2222 mlen++;
2223
2224 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2225 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
2226 unsigned i = 0;
2227 sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
2228 if (per_slot_offset.file != BAD_FILE)
2229 sources[i++] = per_slot_offset;
2230 if (channel_mask.file != BAD_FILE)
2231 sources[i++] = channel_mask;
2232 while (i < mlen) {
2233 sources[i++] = this->control_data_bits;
2234 }
2235
2236 abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
2237 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
2238 inst->mlen = mlen;
2239 /* We need to increment Global Offset by 256-bits to make room for
2240 * Broadwell's extra "Vertex Count" payload at the beginning of the
2241 * URB entry. Since this is an OWord message, Global Offset is counted
2242 * in 128-bit units, so we must set it to 2.
2243 */
2244 if (gs_prog_data->static_vertex_count == -1)
2245 inst->offset = 2;
2246 }
2247
2248 void
2249 fs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
2250 unsigned stream_id)
2251 {
2252 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
2253
2254 /* Note: we are calling this *before* increasing vertex_count, so
2255 * this->vertex_count == vertex_count - 1 in the formula above.
2256 */
2257
2258 /* Stream mode uses 2 bits per vertex */
2259 assert(gs_compile->control_data_bits_per_vertex == 2);
2260
2261 /* Must be a valid stream */
2262 assert(stream_id < MAX_VERTEX_STREAMS);
2263
2264 /* Control data bits are initialized to 0 so we don't have to set any
2265 * bits when sending vertices to stream 0.
2266 */
2267 if (stream_id == 0)
2268 return;
2269
2270 const fs_builder abld = bld.annotate("set stream control data bits", NULL);
2271
2272 /* reg::sid = stream_id */
2273 fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2274 abld.MOV(sid, brw_imm_ud(stream_id));
2275
2276 /* reg:shift_count = 2 * (vertex_count - 1) */
2277 fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2278 abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
2279
2280 /* Note: we're relying on the fact that the GEN SHL instruction only pays
2281 * attention to the lower 5 bits of its second source argument, so on this
2282 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
2283 * stream_id << ((2 * (vertex_count - 1)) % 32).
2284 */
2285 fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2286 abld.SHL(mask, sid, shift_count);
2287 abld.OR(this->control_data_bits, this->control_data_bits, mask);
2288 }
2289
2290 void
2291 fs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
2292 unsigned stream_id)
2293 {
2294 assert(stage == MESA_SHADER_GEOMETRY);
2295
2296 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2297
2298 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
2299 vertex_count.type = BRW_REGISTER_TYPE_UD;
2300
2301 /* Haswell and later hardware ignores the "Render Stream Select" bits
2302 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
2303 * and instead sends all primitives down the pipeline for rasterization.
2304 * If the SOL stage is enabled, "Render Stream Select" is honored and
2305 * primitives bound to non-zero streams are discarded after stream output.
2306 *
2307 * Since the only purpose of primives sent to non-zero streams is to
2308 * be recorded by transform feedback, we can simply discard all geometry
2309 * bound to these streams when transform feedback is disabled.
2310 */
2311 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
2312 return;
2313
2314 /* If we're outputting 32 control data bits or less, then we can wait
2315 * until the shader is over to output them all. Otherwise we need to
2316 * output them as we go. Now is the time to do it, since we're about to
2317 * output the vertex_count'th vertex, so it's guaranteed that the
2318 * control data bits associated with the (vertex_count - 1)th vertex are
2319 * correct.
2320 */
2321 if (gs_compile->control_data_header_size_bits > 32) {
2322 const fs_builder abld =
2323 bld.annotate("emit vertex: emit control data bits");
2324
2325 /* Only emit control data bits if we've finished accumulating a batch
2326 * of 32 bits. This is the case when:
2327 *
2328 * (vertex_count * bits_per_vertex) % 32 == 0
2329 *
2330 * (in other words, when the last 5 bits of vertex_count *
2331 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
2332 * integer n (which is always the case, since bits_per_vertex is
2333 * always 1 or 2), this is equivalent to requiring that the last 5-n
2334 * bits of vertex_count are 0:
2335 *
2336 * vertex_count & (2^(5-n) - 1) == 0
2337 *
2338 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
2339 * equivalent to:
2340 *
2341 * vertex_count & (32 / bits_per_vertex - 1) == 0
2342 *
2343 * TODO: If vertex_count is an immediate, we could do some of this math
2344 * at compile time...
2345 */
2346 fs_inst *inst =
2347 abld.AND(bld.null_reg_d(), vertex_count,
2348 brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
2349 inst->conditional_mod = BRW_CONDITIONAL_Z;
2350
2351 abld.IF(BRW_PREDICATE_NORMAL);
2352 /* If vertex_count is 0, then no control data bits have been
2353 * accumulated yet, so we can skip emitting them.
2354 */
2355 abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
2356 BRW_CONDITIONAL_NEQ);
2357 abld.IF(BRW_PREDICATE_NORMAL);
2358 emit_gs_control_data_bits(vertex_count);
2359 abld.emit(BRW_OPCODE_ENDIF);
2360
2361 /* Reset control_data_bits to 0 so we can start accumulating a new
2362 * batch.
2363 *
2364 * Note: in the case where vertex_count == 0, this neutralizes the
2365 * effect of any call to EndPrimitive() that the shader may have
2366 * made before outputting its first vertex.
2367 */
2368 inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
2369 inst->force_writemask_all = true;
2370 abld.emit(BRW_OPCODE_ENDIF);
2371 }
2372
2373 emit_urb_writes(vertex_count);
2374
2375 /* In stream mode we have to set control data bits for all vertices
2376 * unless we have disabled control data bits completely (which we do
2377 * do for GL_POINTS outputs that don't use streams).
2378 */
2379 if (gs_compile->control_data_header_size_bits > 0 &&
2380 gs_prog_data->control_data_format ==
2381 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
2382 set_gs_stream_control_data_bits(vertex_count, stream_id);
2383 }
2384 }
2385
2386 void
2387 fs_visitor::emit_gs_input_load(const fs_reg &dst,
2388 const nir_src &vertex_src,
2389 unsigned base_offset,
2390 const nir_src &offset_src,
2391 unsigned num_components,
2392 unsigned first_component)
2393 {
2394 assert(type_sz(dst.type) == 4);
2395 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2396 const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
2397
2398 /* TODO: figure out push input layout for invocations == 1 */
2399 if (gs_prog_data->invocations == 1 &&
2400 nir_src_is_const(offset_src) && nir_src_is_const(vertex_src) &&
2401 4 * (base_offset + nir_src_as_uint(offset_src)) < push_reg_count) {
2402 int imm_offset = (base_offset + nir_src_as_uint(offset_src)) * 4 +
2403 nir_src_as_uint(vertex_src) * push_reg_count;
2404 for (unsigned i = 0; i < num_components; i++) {
2405 bld.MOV(offset(dst, bld, i),
2406 fs_reg(ATTR, imm_offset + i + first_component, dst.type));
2407 }
2408 return;
2409 }
2410
2411 /* Resort to the pull model. Ensure the VUE handles are provided. */
2412 assert(gs_prog_data->base.include_vue_handles);
2413
2414 unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
2415 fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2416
2417 if (gs_prog_data->invocations == 1) {
2418 if (nir_src_is_const(vertex_src)) {
2419 /* The vertex index is constant; just select the proper URB handle. */
2420 icp_handle =
2421 retype(brw_vec8_grf(first_icp_handle + nir_src_as_uint(vertex_src), 0),
2422 BRW_REGISTER_TYPE_UD);
2423 } else {
2424 /* The vertex index is non-constant. We need to use indirect
2425 * addressing to fetch the proper URB handle.
2426 *
2427 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2428 * indicating that channel <n> should read the handle from
2429 * DWord <n>. We convert that to bytes by multiplying by 4.
2430 *
2431 * Next, we convert the vertex index to bytes by multiplying
2432 * by 32 (shifting by 5), and add the two together. This is
2433 * the final indirect byte offset.
2434 */
2435 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_UW, 1);
2436 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2437 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2438 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2439
2440 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2441 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
2442 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2443 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
2444 /* Convert vertex_index to bytes (multiply by 32) */
2445 bld.SHL(vertex_offset_bytes,
2446 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2447 brw_imm_ud(5u));
2448 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
2449
2450 /* Use first_icp_handle as the base offset. There is one register
2451 * of URB handles per vertex, so inform the register allocator that
2452 * we might read up to nir->info.gs.vertices_in registers.
2453 */
2454 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2455 retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
2456 fs_reg(icp_offset_bytes),
2457 brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
2458 }
2459 } else {
2460 assert(gs_prog_data->invocations > 1);
2461
2462 if (nir_src_is_const(vertex_src)) {
2463 unsigned vertex = nir_src_as_uint(vertex_src);
2464 assert(devinfo->gen >= 9 || vertex <= 5);
2465 bld.MOV(icp_handle,
2466 retype(brw_vec1_grf(first_icp_handle + vertex / 8, vertex % 8),
2467 BRW_REGISTER_TYPE_UD));
2468 } else {
2469 /* The vertex index is non-constant. We need to use indirect
2470 * addressing to fetch the proper URB handle.
2471 *
2472 */
2473 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2474
2475 /* Convert vertex_index to bytes (multiply by 4) */
2476 bld.SHL(icp_offset_bytes,
2477 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2478 brw_imm_ud(2u));
2479
2480 /* Use first_icp_handle as the base offset. There is one DWord
2481 * of URB handles per vertex, so inform the register allocator that
2482 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2483 */
2484 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2485 retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
2486 fs_reg(icp_offset_bytes),
2487 brw_imm_ud(DIV_ROUND_UP(nir->info.gs.vertices_in, 8) *
2488 REG_SIZE));
2489 }
2490 }
2491
2492 fs_inst *inst;
2493 fs_reg indirect_offset = get_nir_src(offset_src);
2494
2495 if (nir_src_is_const(offset_src)) {
2496 /* Constant indexing - use global offset. */
2497 if (first_component != 0) {
2498 unsigned read_components = num_components + first_component;
2499 fs_reg tmp = bld.vgrf(dst.type, read_components);
2500 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
2501 inst->size_written = read_components *
2502 tmp.component_size(inst->exec_size);
2503 for (unsigned i = 0; i < num_components; i++) {
2504 bld.MOV(offset(dst, bld, i),
2505 offset(tmp, bld, i + first_component));
2506 }
2507 } else {
2508 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2509 inst->size_written = num_components *
2510 dst.component_size(inst->exec_size);
2511 }
2512 inst->offset = base_offset + nir_src_as_uint(offset_src);
2513 inst->mlen = 1;
2514 } else {
2515 /* Indirect indexing - use per-slot offsets as well. */
2516 const fs_reg srcs[] = { icp_handle, indirect_offset };
2517 unsigned read_components = num_components + first_component;
2518 fs_reg tmp = bld.vgrf(dst.type, read_components);
2519 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2520 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2521 if (first_component != 0) {
2522 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2523 payload);
2524 inst->size_written = read_components *
2525 tmp.component_size(inst->exec_size);
2526 for (unsigned i = 0; i < num_components; i++) {
2527 bld.MOV(offset(dst, bld, i),
2528 offset(tmp, bld, i + first_component));
2529 }
2530 } else {
2531 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2532 inst->size_written = num_components *
2533 dst.component_size(inst->exec_size);
2534 }
2535 inst->offset = base_offset;
2536 inst->mlen = 2;
2537 }
2538 }
2539
2540 fs_reg
2541 fs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
2542 {
2543 nir_src *offset_src = nir_get_io_offset_src(instr);
2544
2545 if (nir_src_is_const(*offset_src)) {
2546 /* The only constant offset we should find is 0. brw_nir.c's
2547 * add_const_offset_to_base() will fold other constant offsets
2548 * into instr->const_index[0].
2549 */
2550 assert(nir_src_as_uint(*offset_src) == 0);
2551 return fs_reg();
2552 }
2553
2554 return get_nir_src(*offset_src);
2555 }
2556
2557 void
2558 fs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
2559 nir_intrinsic_instr *instr)
2560 {
2561 assert(stage == MESA_SHADER_VERTEX);
2562
2563 fs_reg dest;
2564 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2565 dest = get_nir_dest(instr->dest);
2566
2567 switch (instr->intrinsic) {
2568 case nir_intrinsic_load_vertex_id:
2569 case nir_intrinsic_load_base_vertex:
2570 unreachable("should be lowered by nir_lower_system_values()");
2571
2572 case nir_intrinsic_load_input: {
2573 assert(nir_dest_bit_size(instr->dest) == 32);
2574 fs_reg src = fs_reg(ATTR, nir_intrinsic_base(instr) * 4, dest.type);
2575 src = offset(src, bld, nir_intrinsic_component(instr));
2576 src = offset(src, bld, nir_src_as_uint(instr->src[0]));
2577
2578 for (unsigned i = 0; i < instr->num_components; i++)
2579 bld.MOV(offset(dest, bld, i), offset(src, bld, i));
2580 break;
2581 }
2582
2583 case nir_intrinsic_load_vertex_id_zero_base:
2584 case nir_intrinsic_load_instance_id:
2585 case nir_intrinsic_load_base_instance:
2586 case nir_intrinsic_load_draw_id:
2587 case nir_intrinsic_load_first_vertex:
2588 case nir_intrinsic_load_is_indexed_draw:
2589 unreachable("lowered by brw_nir_lower_vs_inputs");
2590
2591 default:
2592 nir_emit_intrinsic(bld, instr);
2593 break;
2594 }
2595 }
2596
2597 fs_reg
2598 fs_visitor::get_tcs_single_patch_icp_handle(const fs_builder &bld,
2599 nir_intrinsic_instr *instr)
2600 {
2601 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
2602 const nir_src &vertex_src = instr->src[0];
2603 nir_intrinsic_instr *vertex_intrin = nir_src_as_intrinsic(vertex_src);
2604 fs_reg icp_handle;
2605
2606 if (nir_src_is_const(vertex_src)) {
2607 /* Emit a MOV to resolve <0,1,0> regioning. */
2608 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2609 unsigned vertex = nir_src_as_uint(vertex_src);
2610 bld.MOV(icp_handle,
2611 retype(brw_vec1_grf(1 + (vertex >> 3), vertex & 7),
2612 BRW_REGISTER_TYPE_UD));
2613 } else if (tcs_prog_data->instances == 1 && vertex_intrin &&
2614 vertex_intrin->intrinsic == nir_intrinsic_load_invocation_id) {
2615 /* For the common case of only 1 instance, an array index of
2616 * gl_InvocationID means reading g1. Skip all the indirect work.
2617 */
2618 icp_handle = retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2619 } else {
2620 /* The vertex index is non-constant. We need to use indirect
2621 * addressing to fetch the proper URB handle.
2622 */
2623 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2624
2625 /* Each ICP handle is a single DWord (4 bytes) */
2626 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2627 bld.SHL(vertex_offset_bytes,
2628 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2629 brw_imm_ud(2u));
2630
2631 /* Start at g1. We might read up to 4 registers. */
2632 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2633 retype(brw_vec8_grf(1, 0), icp_handle.type), vertex_offset_bytes,
2634 brw_imm_ud(4 * REG_SIZE));
2635 }
2636
2637 return icp_handle;
2638 }
2639
2640 fs_reg
2641 fs_visitor::get_tcs_eight_patch_icp_handle(const fs_builder &bld,
2642 nir_intrinsic_instr *instr)
2643 {
2644 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
2645 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
2646 const nir_src &vertex_src = instr->src[0];
2647
2648 unsigned first_icp_handle = tcs_prog_data->include_primitive_id ? 3 : 2;
2649
2650 if (nir_src_is_const(vertex_src)) {
2651 return fs_reg(retype(brw_vec8_grf(first_icp_handle +
2652 nir_src_as_uint(vertex_src), 0),
2653 BRW_REGISTER_TYPE_UD));
2654 }
2655
2656 /* The vertex index is non-constant. We need to use indirect
2657 * addressing to fetch the proper URB handle.
2658 *
2659 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2660 * indicating that channel <n> should read the handle from
2661 * DWord <n>. We convert that to bytes by multiplying by 4.
2662 *
2663 * Next, we convert the vertex index to bytes by multiplying
2664 * by 32 (shifting by 5), and add the two together. This is
2665 * the final indirect byte offset.
2666 */
2667 fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2668 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_UW, 1);
2669 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2670 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2671 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2672
2673 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2674 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
2675 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2676 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
2677 /* Convert vertex_index to bytes (multiply by 32) */
2678 bld.SHL(vertex_offset_bytes,
2679 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2680 brw_imm_ud(5u));
2681 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
2682
2683 /* Use first_icp_handle as the base offset. There is one register
2684 * of URB handles per vertex, so inform the register allocator that
2685 * we might read up to nir->info.gs.vertices_in registers.
2686 */
2687 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2688 retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
2689 icp_offset_bytes, brw_imm_ud(tcs_key->input_vertices * REG_SIZE));
2690
2691 return icp_handle;
2692 }
2693
2694 struct brw_reg
2695 fs_visitor::get_tcs_output_urb_handle()
2696 {
2697 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
2698
2699 if (vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH) {
2700 return retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
2701 } else {
2702 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH);
2703 return retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2704 }
2705 }
2706
2707 void
2708 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
2709 nir_intrinsic_instr *instr)
2710 {
2711 assert(stage == MESA_SHADER_TESS_CTRL);
2712 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
2713 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
2714 struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
2715
2716 bool eight_patch =
2717 vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH;
2718
2719 fs_reg dst;
2720 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2721 dst = get_nir_dest(instr->dest);
2722
2723 switch (instr->intrinsic) {
2724 case nir_intrinsic_load_primitive_id:
2725 bld.MOV(dst, fs_reg(eight_patch ? brw_vec8_grf(2, 0)
2726 : brw_vec1_grf(0, 1)));
2727 break;
2728 case nir_intrinsic_load_invocation_id:
2729 bld.MOV(retype(dst, invocation_id.type), invocation_id);
2730 break;
2731 case nir_intrinsic_load_patch_vertices_in:
2732 bld.MOV(retype(dst, BRW_REGISTER_TYPE_D),
2733 brw_imm_d(tcs_key->input_vertices));
2734 break;
2735
2736 case nir_intrinsic_barrier: {
2737 if (tcs_prog_data->instances == 1)
2738 break;
2739
2740 fs_reg m0 = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2741 fs_reg m0_2 = component(m0, 2);
2742
2743 const fs_builder chanbld = bld.exec_all().group(1, 0);
2744
2745 /* Zero the message header */
2746 bld.exec_all().MOV(m0, brw_imm_ud(0u));
2747
2748 if (devinfo->gen < 11) {
2749 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2750 chanbld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
2751 brw_imm_ud(INTEL_MASK(16, 13)));
2752
2753 /* Shift it up to bits 27:24. */
2754 chanbld.SHL(m0_2, m0_2, brw_imm_ud(11));
2755 } else {
2756 chanbld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
2757 brw_imm_ud(INTEL_MASK(30, 24)));
2758 }
2759
2760 /* Set the Barrier Count and the enable bit */
2761 if (devinfo->gen < 11) {
2762 chanbld.OR(m0_2, m0_2,
2763 brw_imm_ud(tcs_prog_data->instances << 9 | (1 << 15)));
2764 } else {
2765 chanbld.OR(m0_2, m0_2,
2766 brw_imm_ud(tcs_prog_data->instances << 8 | (1 << 15)));
2767 }
2768
2769 bld.emit(SHADER_OPCODE_BARRIER, bld.null_reg_ud(), m0);
2770 break;
2771 }
2772
2773 case nir_intrinsic_load_input:
2774 unreachable("nir_lower_io should never give us these.");
2775 break;
2776
2777 case nir_intrinsic_load_per_vertex_input: {
2778 assert(nir_dest_bit_size(instr->dest) == 32);
2779 fs_reg indirect_offset = get_indirect_offset(instr);
2780 unsigned imm_offset = instr->const_index[0];
2781 fs_inst *inst;
2782
2783 fs_reg icp_handle =
2784 eight_patch ? get_tcs_eight_patch_icp_handle(bld, instr)
2785 : get_tcs_single_patch_icp_handle(bld, instr);
2786
2787 /* We can only read two double components with each URB read, so
2788 * we send two read messages in that case, each one loading up to
2789 * two double components.
2790 */
2791 unsigned num_components = instr->num_components;
2792 unsigned first_component = nir_intrinsic_component(instr);
2793
2794 if (indirect_offset.file == BAD_FILE) {
2795 /* Constant indexing - use global offset. */
2796 if (first_component != 0) {
2797 unsigned read_components = num_components + first_component;
2798 fs_reg tmp = bld.vgrf(dst.type, read_components);
2799 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
2800 for (unsigned i = 0; i < num_components; i++) {
2801 bld.MOV(offset(dst, bld, i),
2802 offset(tmp, bld, i + first_component));
2803 }
2804 } else {
2805 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2806 }
2807 inst->offset = imm_offset;
2808 inst->mlen = 1;
2809 } else {
2810 /* Indirect indexing - use per-slot offsets as well. */
2811 const fs_reg srcs[] = { icp_handle, indirect_offset };
2812 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2813 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2814 if (first_component != 0) {
2815 unsigned read_components = num_components + first_component;
2816 fs_reg tmp = bld.vgrf(dst.type, read_components);
2817 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2818 payload);
2819 for (unsigned i = 0; i < num_components; i++) {
2820 bld.MOV(offset(dst, bld, i),
2821 offset(tmp, bld, i + first_component));
2822 }
2823 } else {
2824 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
2825 payload);
2826 }
2827 inst->offset = imm_offset;
2828 inst->mlen = 2;
2829 }
2830 inst->size_written = (num_components + first_component) *
2831 inst->dst.component_size(inst->exec_size);
2832
2833 /* Copy the temporary to the destination to deal with writemasking.
2834 *
2835 * Also attempt to deal with gl_PointSize being in the .w component.
2836 */
2837 if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
2838 assert(type_sz(dst.type) == 4);
2839 inst->dst = bld.vgrf(dst.type, 4);
2840 inst->size_written = 4 * REG_SIZE;
2841 bld.MOV(dst, offset(inst->dst, bld, 3));
2842 }
2843 break;
2844 }
2845
2846 case nir_intrinsic_load_output:
2847 case nir_intrinsic_load_per_vertex_output: {
2848 assert(nir_dest_bit_size(instr->dest) == 32);
2849 fs_reg indirect_offset = get_indirect_offset(instr);
2850 unsigned imm_offset = instr->const_index[0];
2851 unsigned first_component = nir_intrinsic_component(instr);
2852
2853 struct brw_reg output_handles = get_tcs_output_urb_handle();
2854
2855 fs_inst *inst;
2856 if (indirect_offset.file == BAD_FILE) {
2857 /* This MOV replicates the output handle to all enabled channels
2858 * is SINGLE_PATCH mode.
2859 */
2860 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2861 bld.MOV(patch_handle, output_handles);
2862
2863 {
2864 if (first_component != 0) {
2865 unsigned read_components =
2866 instr->num_components + first_component;
2867 fs_reg tmp = bld.vgrf(dst.type, read_components);
2868 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
2869 patch_handle);
2870 inst->size_written = read_components * REG_SIZE;
2871 for (unsigned i = 0; i < instr->num_components; i++) {
2872 bld.MOV(offset(dst, bld, i),
2873 offset(tmp, bld, i + first_component));
2874 }
2875 } else {
2876 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst,
2877 patch_handle);
2878 inst->size_written = instr->num_components * REG_SIZE;
2879 }
2880 inst->offset = imm_offset;
2881 inst->mlen = 1;
2882 }
2883 } else {
2884 /* Indirect indexing - use per-slot offsets as well. */
2885 const fs_reg srcs[] = { output_handles, indirect_offset };
2886 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2887 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2888 if (first_component != 0) {
2889 unsigned read_components =
2890 instr->num_components + first_component;
2891 fs_reg tmp = bld.vgrf(dst.type, read_components);
2892 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2893 payload);
2894 inst->size_written = read_components * REG_SIZE;
2895 for (unsigned i = 0; i < instr->num_components; i++) {
2896 bld.MOV(offset(dst, bld, i),
2897 offset(tmp, bld, i + first_component));
2898 }
2899 } else {
2900 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
2901 payload);
2902 inst->size_written = instr->num_components * REG_SIZE;
2903 }
2904 inst->offset = imm_offset;
2905 inst->mlen = 2;
2906 }
2907 break;
2908 }
2909
2910 case nir_intrinsic_store_output:
2911 case nir_intrinsic_store_per_vertex_output: {
2912 assert(nir_src_bit_size(instr->src[0]) == 32);
2913 fs_reg value = get_nir_src(instr->src[0]);
2914 fs_reg indirect_offset = get_indirect_offset(instr);
2915 unsigned imm_offset = instr->const_index[0];
2916 unsigned mask = instr->const_index[1];
2917 unsigned header_regs = 0;
2918 struct brw_reg output_handles = get_tcs_output_urb_handle();
2919
2920 fs_reg srcs[7];
2921 srcs[header_regs++] = output_handles;
2922
2923 if (indirect_offset.file != BAD_FILE) {
2924 srcs[header_regs++] = indirect_offset;
2925 }
2926
2927 if (mask == 0)
2928 break;
2929
2930 unsigned num_components = util_last_bit(mask);
2931 enum opcode opcode;
2932
2933 /* We can only pack two 64-bit components in a single message, so send
2934 * 2 messages if we have more components
2935 */
2936 unsigned first_component = nir_intrinsic_component(instr);
2937 mask = mask << first_component;
2938
2939 if (mask != WRITEMASK_XYZW) {
2940 srcs[header_regs++] = brw_imm_ud(mask << 16);
2941 opcode = indirect_offset.file != BAD_FILE ?
2942 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2943 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2944 } else {
2945 opcode = indirect_offset.file != BAD_FILE ?
2946 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT :
2947 SHADER_OPCODE_URB_WRITE_SIMD8;
2948 }
2949
2950 for (unsigned i = 0; i < num_components; i++) {
2951 if (!(mask & (1 << (i + first_component))))
2952 continue;
2953
2954 srcs[header_regs + i + first_component] = offset(value, bld, i);
2955 }
2956
2957 unsigned mlen = header_regs + num_components + first_component;
2958 fs_reg payload =
2959 bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2960 bld.LOAD_PAYLOAD(payload, srcs, mlen, header_regs);
2961
2962 fs_inst *inst = bld.emit(opcode, bld.null_reg_ud(), payload);
2963 inst->offset = imm_offset;
2964 inst->mlen = mlen;
2965 break;
2966 }
2967
2968 default:
2969 nir_emit_intrinsic(bld, instr);
2970 break;
2971 }
2972 }
2973
2974 void
2975 fs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
2976 nir_intrinsic_instr *instr)
2977 {
2978 assert(stage == MESA_SHADER_TESS_EVAL);
2979 struct brw_tes_prog_data *tes_prog_data = brw_tes_prog_data(prog_data);
2980
2981 fs_reg dest;
2982 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2983 dest = get_nir_dest(instr->dest);
2984
2985 switch (instr->intrinsic) {
2986 case nir_intrinsic_load_primitive_id:
2987 bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
2988 break;
2989 case nir_intrinsic_load_tess_coord:
2990 /* gl_TessCoord is part of the payload in g1-3 */
2991 for (unsigned i = 0; i < 3; i++) {
2992 bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
2993 }
2994 break;
2995
2996 case nir_intrinsic_load_input:
2997 case nir_intrinsic_load_per_vertex_input: {
2998 assert(nir_dest_bit_size(instr->dest) == 32);
2999 fs_reg indirect_offset = get_indirect_offset(instr);
3000 unsigned imm_offset = instr->const_index[0];
3001 unsigned first_component = nir_intrinsic_component(instr);
3002
3003 fs_inst *inst;
3004 if (indirect_offset.file == BAD_FILE) {
3005 /* Arbitrarily only push up to 32 vec4 slots worth of data,
3006 * which is 16 registers (since each holds 2 vec4 slots).
3007 */
3008 const unsigned max_push_slots = 32;
3009 if (imm_offset < max_push_slots) {
3010 fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
3011 for (int i = 0; i < instr->num_components; i++) {
3012 unsigned comp = 4 * (imm_offset % 2) + i + first_component;
3013 bld.MOV(offset(dest, bld, i), component(src, comp));
3014 }
3015
3016 tes_prog_data->base.urb_read_length =
3017 MAX2(tes_prog_data->base.urb_read_length,
3018 (imm_offset / 2) + 1);
3019 } else {
3020 /* Replicate the patch handle to all enabled channels */
3021 const fs_reg srcs[] = {
3022 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
3023 };
3024 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
3025 bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
3026
3027 if (first_component != 0) {
3028 unsigned read_components =
3029 instr->num_components + first_component;
3030 fs_reg tmp = bld.vgrf(dest.type, read_components);
3031 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
3032 patch_handle);
3033 inst->size_written = read_components * REG_SIZE;
3034 for (unsigned i = 0; i < instr->num_components; i++) {
3035 bld.MOV(offset(dest, bld, i),
3036 offset(tmp, bld, i + first_component));
3037 }
3038 } else {
3039 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest,
3040 patch_handle);
3041 inst->size_written = instr->num_components * REG_SIZE;
3042 }
3043 inst->mlen = 1;
3044 inst->offset = imm_offset;
3045 }
3046 } else {
3047 /* Indirect indexing - use per-slot offsets as well. */
3048
3049 /* We can only read two double components with each URB read, so
3050 * we send two read messages in that case, each one loading up to
3051 * two double components.
3052 */
3053 unsigned num_components = instr->num_components;
3054 const fs_reg srcs[] = {
3055 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
3056 indirect_offset
3057 };
3058 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
3059 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
3060
3061 if (first_component != 0) {
3062 unsigned read_components =
3063 num_components + first_component;
3064 fs_reg tmp = bld.vgrf(dest.type, read_components);
3065 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
3066 payload);
3067 for (unsigned i = 0; i < num_components; i++) {
3068 bld.MOV(offset(dest, bld, i),
3069 offset(tmp, bld, i + first_component));
3070 }
3071 } else {
3072 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest,
3073 payload);
3074 }
3075 inst->mlen = 2;
3076 inst->offset = imm_offset;
3077 inst->size_written = (num_components + first_component) *
3078 inst->dst.component_size(inst->exec_size);
3079 }
3080 break;
3081 }
3082 default:
3083 nir_emit_intrinsic(bld, instr);
3084 break;
3085 }
3086 }
3087
3088 void
3089 fs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
3090 nir_intrinsic_instr *instr)
3091 {
3092 assert(stage == MESA_SHADER_GEOMETRY);
3093 fs_reg indirect_offset;
3094
3095 fs_reg dest;
3096 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3097 dest = get_nir_dest(instr->dest);
3098
3099 switch (instr->intrinsic) {
3100 case nir_intrinsic_load_primitive_id:
3101 assert(stage == MESA_SHADER_GEOMETRY);
3102 assert(brw_gs_prog_data(prog_data)->include_primitive_id);
3103 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
3104 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
3105 break;
3106
3107 case nir_intrinsic_load_input:
3108 unreachable("load_input intrinsics are invalid for the GS stage");
3109
3110 case nir_intrinsic_load_per_vertex_input:
3111 emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
3112 instr->src[1], instr->num_components,
3113 nir_intrinsic_component(instr));
3114 break;
3115
3116 case nir_intrinsic_emit_vertex_with_counter:
3117 emit_gs_vertex(instr->src[0], instr->const_index[0]);
3118 break;
3119
3120 case nir_intrinsic_end_primitive_with_counter:
3121 emit_gs_end_primitive(instr->src[0]);
3122 break;
3123
3124 case nir_intrinsic_set_vertex_count:
3125 bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
3126 break;
3127
3128 case nir_intrinsic_load_invocation_id: {
3129 fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
3130 assert(val.file != BAD_FILE);
3131 dest.type = val.type;
3132 bld.MOV(dest, val);
3133 break;
3134 }
3135
3136 default:
3137 nir_emit_intrinsic(bld, instr);
3138 break;
3139 }
3140 }
3141
3142 /**
3143 * Fetch the current render target layer index.
3144 */
3145 static fs_reg
3146 fetch_render_target_array_index(const fs_builder &bld)
3147 {
3148 if (bld.shader->devinfo->gen >= 6) {
3149 /* The render target array index is provided in the thread payload as
3150 * bits 26:16 of r0.0.
3151 */
3152 const fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_UD);
3153 bld.AND(idx, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, 0, 1),
3154 brw_imm_uw(0x7ff));
3155 return idx;
3156 } else {
3157 /* Pre-SNB we only ever render into the first layer of the framebuffer
3158 * since layered rendering is not implemented.
3159 */
3160 return brw_imm_ud(0);
3161 }
3162 }
3163
3164 /**
3165 * Fake non-coherent framebuffer read implemented using TXF to fetch from the
3166 * framebuffer at the current fragment coordinates and sample index.
3167 */
3168 fs_inst *
3169 fs_visitor::emit_non_coherent_fb_read(const fs_builder &bld, const fs_reg &dst,
3170 unsigned target)
3171 {
3172 const struct gen_device_info *devinfo = bld.shader->devinfo;
3173
3174 assert(bld.shader->stage == MESA_SHADER_FRAGMENT);
3175 const brw_wm_prog_key *wm_key =
3176 reinterpret_cast<const brw_wm_prog_key *>(key);
3177 assert(!wm_key->coherent_fb_fetch);
3178 const struct brw_wm_prog_data *wm_prog_data =
3179 brw_wm_prog_data(stage_prog_data);
3180
3181 /* Calculate the surface index relative to the start of the texture binding
3182 * table block, since that's what the texturing messages expect.
3183 */
3184 const unsigned surface = target +
3185 wm_prog_data->binding_table.render_target_read_start -
3186 wm_prog_data->base.binding_table.texture_start;
3187
3188 /* Calculate the fragment coordinates. */
3189 const fs_reg coords = bld.vgrf(BRW_REGISTER_TYPE_UD, 3);
3190 bld.MOV(offset(coords, bld, 0), pixel_x);
3191 bld.MOV(offset(coords, bld, 1), pixel_y);
3192 bld.MOV(offset(coords, bld, 2), fetch_render_target_array_index(bld));
3193
3194 /* Calculate the sample index and MCS payload when multisampling. Luckily
3195 * the MCS fetch message behaves deterministically for UMS surfaces, so it
3196 * shouldn't be necessary to recompile based on whether the framebuffer is
3197 * CMS or UMS.
3198 */
3199 if (wm_key->multisample_fbo &&
3200 nir_system_values[SYSTEM_VALUE_SAMPLE_ID].file == BAD_FILE)
3201 nir_system_values[SYSTEM_VALUE_SAMPLE_ID] = *emit_sampleid_setup();
3202
3203 const fs_reg sample = nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
3204 const fs_reg mcs = wm_key->multisample_fbo ?
3205 emit_mcs_fetch(coords, 3, brw_imm_ud(surface), fs_reg()) : fs_reg();
3206
3207 /* Use either a normal or a CMS texel fetch message depending on whether
3208 * the framebuffer is single or multisample. On SKL+ use the wide CMS
3209 * message just in case the framebuffer uses 16x multisampling, it should
3210 * be equivalent to the normal CMS fetch for lower multisampling modes.
3211 */
3212 const opcode op = !wm_key->multisample_fbo ? SHADER_OPCODE_TXF_LOGICAL :
3213 devinfo->gen >= 9 ? SHADER_OPCODE_TXF_CMS_W_LOGICAL :
3214 SHADER_OPCODE_TXF_CMS_LOGICAL;
3215
3216 /* Emit the instruction. */
3217 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
3218 srcs[TEX_LOGICAL_SRC_COORDINATE] = coords;
3219 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_ud(0);
3220 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = sample;
3221 srcs[TEX_LOGICAL_SRC_MCS] = mcs;
3222 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(surface);
3223 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(0);
3224 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_ud(3);
3225 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_ud(0);
3226
3227 fs_inst *inst = bld.emit(op, dst, srcs, ARRAY_SIZE(srcs));
3228 inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
3229
3230 return inst;
3231 }
3232
3233 /**
3234 * Actual coherent framebuffer read implemented using the native render target
3235 * read message. Requires SKL+.
3236 */
3237 static fs_inst *
3238 emit_coherent_fb_read(const fs_builder &bld, const fs_reg &dst, unsigned target)
3239 {
3240 assert(bld.shader->devinfo->gen >= 9);
3241 fs_inst *inst = bld.emit(FS_OPCODE_FB_READ_LOGICAL, dst);
3242 inst->target = target;
3243 inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
3244
3245 return inst;
3246 }
3247
3248 static fs_reg
3249 alloc_temporary(const fs_builder &bld, unsigned size, fs_reg *regs, unsigned n)
3250 {
3251 if (n && regs[0].file != BAD_FILE) {
3252 return regs[0];
3253
3254 } else {
3255 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, size);
3256
3257 for (unsigned i = 0; i < n; i++)
3258 regs[i] = tmp;
3259
3260 return tmp;
3261 }
3262 }
3263
3264 static fs_reg
3265 alloc_frag_output(fs_visitor *v, unsigned location)
3266 {
3267 assert(v->stage == MESA_SHADER_FRAGMENT);
3268 const brw_wm_prog_key *const key =
3269 reinterpret_cast<const brw_wm_prog_key *>(v->key);
3270 const unsigned l = GET_FIELD(location, BRW_NIR_FRAG_OUTPUT_LOCATION);
3271 const unsigned i = GET_FIELD(location, BRW_NIR_FRAG_OUTPUT_INDEX);
3272
3273 if (i > 0 || (key->force_dual_color_blend && l == FRAG_RESULT_DATA1))
3274 return alloc_temporary(v->bld, 4, &v->dual_src_output, 1);
3275
3276 else if (l == FRAG_RESULT_COLOR)
3277 return alloc_temporary(v->bld, 4, v->outputs,
3278 MAX2(key->nr_color_regions, 1));
3279
3280 else if (l == FRAG_RESULT_DEPTH)
3281 return alloc_temporary(v->bld, 1, &v->frag_depth, 1);
3282
3283 else if (l == FRAG_RESULT_STENCIL)
3284 return alloc_temporary(v->bld, 1, &v->frag_stencil, 1);
3285
3286 else if (l == FRAG_RESULT_SAMPLE_MASK)
3287 return alloc_temporary(v->bld, 1, &v->sample_mask, 1);
3288
3289 else if (l >= FRAG_RESULT_DATA0 &&
3290 l < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS)
3291 return alloc_temporary(v->bld, 4,
3292 &v->outputs[l - FRAG_RESULT_DATA0], 1);
3293
3294 else
3295 unreachable("Invalid location");
3296 }
3297
3298 /* Annoyingly, we get the barycentrics into the shader in a layout that's
3299 * optimized for PLN but it doesn't work nearly as well as one would like for
3300 * manual interpolation.
3301 */
3302 static void
3303 shuffle_from_pln_layout(const fs_builder &bld, fs_reg dest, fs_reg pln_data)
3304 {
3305 dest.type = BRW_REGISTER_TYPE_F;
3306 pln_data.type = BRW_REGISTER_TYPE_F;
3307 const fs_reg dest_u = offset(dest, bld, 0);
3308 const fs_reg dest_v = offset(dest, bld, 1);
3309
3310 for (unsigned g = 0; g < bld.dispatch_width() / 8; g++) {
3311 const fs_builder gbld = bld.group(8, g);
3312 gbld.MOV(horiz_offset(dest_u, g * 8),
3313 byte_offset(pln_data, (g * 2 + 0) * REG_SIZE));
3314 gbld.MOV(horiz_offset(dest_v, g * 8),
3315 byte_offset(pln_data, (g * 2 + 1) * REG_SIZE));
3316 }
3317 }
3318
3319 static void
3320 shuffle_to_pln_layout(const fs_builder &bld, fs_reg pln_data, fs_reg src)
3321 {
3322 pln_data.type = BRW_REGISTER_TYPE_F;
3323 src.type = BRW_REGISTER_TYPE_F;
3324 const fs_reg src_u = offset(src, bld, 0);
3325 const fs_reg src_v = offset(src, bld, 1);
3326
3327 for (unsigned g = 0; g < bld.dispatch_width() / 8; g++) {
3328 const fs_builder gbld = bld.group(8, g);
3329 gbld.MOV(byte_offset(pln_data, (g * 2 + 0) * REG_SIZE),
3330 horiz_offset(src_u, g * 8));
3331 gbld.MOV(byte_offset(pln_data, (g * 2 + 1) * REG_SIZE),
3332 horiz_offset(src_v, g * 8));
3333 }
3334 }
3335
3336 void
3337 fs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
3338 nir_intrinsic_instr *instr)
3339 {
3340 assert(stage == MESA_SHADER_FRAGMENT);
3341
3342 fs_reg dest;
3343 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3344 dest = get_nir_dest(instr->dest);
3345
3346 switch (instr->intrinsic) {
3347 case nir_intrinsic_load_front_face:
3348 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
3349 *emit_frontfacing_interpolation());
3350 break;
3351
3352 case nir_intrinsic_load_sample_pos: {
3353 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
3354 assert(sample_pos.file != BAD_FILE);
3355 dest.type = sample_pos.type;
3356 bld.MOV(dest, sample_pos);
3357 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
3358 break;
3359 }
3360
3361 case nir_intrinsic_load_layer_id:
3362 dest.type = BRW_REGISTER_TYPE_UD;
3363 bld.MOV(dest, fetch_render_target_array_index(bld));
3364 break;
3365
3366 case nir_intrinsic_is_helper_invocation: {
3367 /* Unlike the regular gl_HelperInvocation, that is defined at dispatch,
3368 * the helperInvocationEXT() (aka SpvOpIsHelperInvocationEXT) takes into
3369 * consideration demoted invocations. That information is stored in
3370 * f0.1.
3371 */
3372 dest.type = BRW_REGISTER_TYPE_UD;
3373
3374 bld.MOV(dest, brw_imm_ud(0));
3375
3376 fs_inst *mov = bld.MOV(dest, brw_imm_ud(~0));
3377 mov->predicate = BRW_PREDICATE_NORMAL;
3378 mov->predicate_inverse = true;
3379 mov->flag_subreg = 1;
3380 break;
3381 }
3382
3383 case nir_intrinsic_load_helper_invocation:
3384 case nir_intrinsic_load_sample_mask_in:
3385 case nir_intrinsic_load_sample_id: {
3386 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3387 fs_reg val = nir_system_values[sv];
3388 assert(val.file != BAD_FILE);
3389 dest.type = val.type;
3390 bld.MOV(dest, val);
3391 break;
3392 }
3393
3394 case nir_intrinsic_store_output: {
3395 const fs_reg src = get_nir_src(instr->src[0]);
3396 const unsigned store_offset = nir_src_as_uint(instr->src[1]);
3397 const unsigned location = nir_intrinsic_base(instr) +
3398 SET_FIELD(store_offset, BRW_NIR_FRAG_OUTPUT_LOCATION);
3399 const fs_reg new_dest = retype(alloc_frag_output(this, location),
3400 src.type);
3401
3402 for (unsigned j = 0; j < instr->num_components; j++)
3403 bld.MOV(offset(new_dest, bld, nir_intrinsic_component(instr) + j),
3404 offset(src, bld, j));
3405
3406 break;
3407 }
3408
3409 case nir_intrinsic_load_output: {
3410 const unsigned l = GET_FIELD(nir_intrinsic_base(instr),
3411 BRW_NIR_FRAG_OUTPUT_LOCATION);
3412 assert(l >= FRAG_RESULT_DATA0);
3413 const unsigned load_offset = nir_src_as_uint(instr->src[0]);
3414 const unsigned target = l - FRAG_RESULT_DATA0 + load_offset;
3415 const fs_reg tmp = bld.vgrf(dest.type, 4);
3416
3417 if (reinterpret_cast<const brw_wm_prog_key *>(key)->coherent_fb_fetch)
3418 emit_coherent_fb_read(bld, tmp, target);
3419 else
3420 emit_non_coherent_fb_read(bld, tmp, target);
3421
3422 for (unsigned j = 0; j < instr->num_components; j++) {
3423 bld.MOV(offset(dest, bld, j),
3424 offset(tmp, bld, nir_intrinsic_component(instr) + j));
3425 }
3426
3427 break;
3428 }
3429
3430 case nir_intrinsic_demote:
3431 case nir_intrinsic_discard:
3432 case nir_intrinsic_demote_if:
3433 case nir_intrinsic_discard_if: {
3434 /* We track our discarded pixels in f0.1. By predicating on it, we can
3435 * update just the flag bits that aren't yet discarded. If there's no
3436 * condition, we emit a CMP of g0 != g0, so all currently executing
3437 * channels will get turned off.
3438 */
3439 fs_inst *cmp = NULL;
3440 if (instr->intrinsic == nir_intrinsic_demote_if ||
3441 instr->intrinsic == nir_intrinsic_discard_if) {
3442 nir_alu_instr *alu = nir_src_as_alu_instr(instr->src[0]);
3443
3444 if (alu != NULL &&
3445 alu->op != nir_op_bcsel &&
3446 alu->op != nir_op_inot) {
3447 /* Re-emit the instruction that generated the Boolean value, but
3448 * do not store it. Since this instruction will be conditional,
3449 * other instructions that want to use the real Boolean value may
3450 * get garbage. This was a problem for piglit's fs-discard-exit-2
3451 * test.
3452 *
3453 * Ideally we'd detect that the instruction cannot have a
3454 * conditional modifier before emitting the instructions. Alas,
3455 * that is nigh impossible. Instead, we're going to assume the
3456 * instruction (or last instruction) generated can have a
3457 * conditional modifier. If it cannot, fallback to the old-style
3458 * compare, and hope dead code elimination will clean up the
3459 * extra instructions generated.
3460 */
3461 nir_emit_alu(bld, alu, false);
3462
3463 cmp = (fs_inst *) instructions.get_tail();
3464 if (cmp->conditional_mod == BRW_CONDITIONAL_NONE) {
3465 if (cmp->can_do_cmod())
3466 cmp->conditional_mod = BRW_CONDITIONAL_Z;
3467 else
3468 cmp = NULL;
3469 } else {
3470 /* The old sequence that would have been generated is,
3471 * basically, bool_result == false. This is equivalent to
3472 * !bool_result, so negate the old modifier.
3473 */
3474 cmp->conditional_mod = brw_negate_cmod(cmp->conditional_mod);
3475 }
3476 }
3477
3478 if (cmp == NULL) {
3479 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
3480 brw_imm_d(0), BRW_CONDITIONAL_Z);
3481 }
3482 } else {
3483 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
3484 BRW_REGISTER_TYPE_UW));
3485 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
3486 }
3487
3488 cmp->predicate = BRW_PREDICATE_NORMAL;
3489 cmp->flag_subreg = 1;
3490
3491 if (devinfo->gen >= 6) {
3492 /* Due to the way we implement discard, the jump will only happen
3493 * when the whole quad is discarded. So we can do this even for
3494 * demote as it won't break its uniformity promises.
3495 */
3496 emit_discard_jump();
3497 }
3498
3499 limit_dispatch_width(16, "Fragment discard/demote not implemented in SIMD32 mode.");
3500 break;
3501 }
3502
3503 case nir_intrinsic_load_input: {
3504 /* load_input is only used for flat inputs */
3505 assert(nir_dest_bit_size(instr->dest) == 32);
3506 unsigned base = nir_intrinsic_base(instr);
3507 unsigned comp = nir_intrinsic_component(instr);
3508 unsigned num_components = instr->num_components;
3509
3510 /* Special case fields in the VUE header */
3511 if (base == VARYING_SLOT_LAYER)
3512 comp = 1;
3513 else if (base == VARYING_SLOT_VIEWPORT)
3514 comp = 2;
3515
3516 for (unsigned int i = 0; i < num_components; i++) {
3517 bld.MOV(offset(dest, bld, i),
3518 retype(component(interp_reg(base, comp + i), 3), dest.type));
3519 }
3520 break;
3521 }
3522
3523 case nir_intrinsic_load_fs_input_interp_deltas: {
3524 assert(stage == MESA_SHADER_FRAGMENT);
3525 assert(nir_src_as_uint(instr->src[0]) == 0);
3526 fs_reg interp = interp_reg(nir_intrinsic_base(instr),
3527 nir_intrinsic_component(instr));
3528 dest.type = BRW_REGISTER_TYPE_F;
3529 bld.MOV(offset(dest, bld, 0), component(interp, 3));
3530 bld.MOV(offset(dest, bld, 1), component(interp, 1));
3531 bld.MOV(offset(dest, bld, 2), component(interp, 0));
3532 break;
3533 }
3534
3535 case nir_intrinsic_load_barycentric_pixel:
3536 case nir_intrinsic_load_barycentric_centroid:
3537 case nir_intrinsic_load_barycentric_sample: {
3538 /* Use the delta_xy values computed from the payload */
3539 const glsl_interp_mode interp_mode =
3540 (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
3541 enum brw_barycentric_mode bary =
3542 brw_barycentric_mode(interp_mode, instr->intrinsic);
3543
3544 shuffle_from_pln_layout(bld, dest, this->delta_xy[bary]);
3545 break;
3546 }
3547
3548 case nir_intrinsic_load_barycentric_at_sample: {
3549 const glsl_interp_mode interpolation =
3550 (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
3551
3552 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3553 if (nir_src_is_const(instr->src[0])) {
3554 unsigned msg_data = nir_src_as_uint(instr->src[0]) << 4;
3555
3556 emit_pixel_interpolater_send(bld,
3557 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3558 tmp,
3559 fs_reg(), /* src */
3560 brw_imm_ud(msg_data),
3561 interpolation);
3562 } else {
3563 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
3564 BRW_REGISTER_TYPE_UD);
3565
3566 if (nir_src_is_dynamically_uniform(instr->src[0])) {
3567 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3568 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3569 bld.exec_all().group(1, 0)
3570 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3571 emit_pixel_interpolater_send(bld,
3572 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3573 tmp,
3574 fs_reg(), /* src */
3575 msg_data,
3576 interpolation);
3577 } else {
3578 /* Make a loop that sends a message to the pixel interpolater
3579 * for the sample number in each live channel. If there are
3580 * multiple channels with the same sample number then these
3581 * will be handled simultaneously with a single interation of
3582 * the loop.
3583 */
3584 bld.emit(BRW_OPCODE_DO);
3585
3586 /* Get the next live sample number into sample_id_reg */
3587 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3588
3589 /* Set the flag register so that we can perform the send
3590 * message on all channels that have the same sample number
3591 */
3592 bld.CMP(bld.null_reg_ud(),
3593 sample_src, sample_id,
3594 BRW_CONDITIONAL_EQ);
3595 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3596 bld.exec_all().group(1, 0)
3597 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3598 fs_inst *inst =
3599 emit_pixel_interpolater_send(bld,
3600 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3601 tmp,
3602 fs_reg(), /* src */
3603 component(msg_data, 0),
3604 interpolation);
3605 set_predicate(BRW_PREDICATE_NORMAL, inst);
3606
3607 /* Continue the loop if there are any live channels left */
3608 set_predicate_inv(BRW_PREDICATE_NORMAL,
3609 true, /* inverse */
3610 bld.emit(BRW_OPCODE_WHILE));
3611 }
3612 }
3613 shuffle_from_pln_layout(bld, dest, tmp);
3614 break;
3615 }
3616
3617 case nir_intrinsic_load_barycentric_at_offset: {
3618 const glsl_interp_mode interpolation =
3619 (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
3620
3621 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3622
3623 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3624 if (const_offset) {
3625 assert(nir_src_bit_size(instr->src[0]) == 32);
3626 unsigned off_x = MIN2((int)(const_offset[0].f32 * 16), 7) & 0xf;
3627 unsigned off_y = MIN2((int)(const_offset[1].f32 * 16), 7) & 0xf;
3628
3629 emit_pixel_interpolater_send(bld,
3630 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
3631 tmp,
3632 fs_reg(), /* src */
3633 brw_imm_ud(off_x | (off_y << 4)),
3634 interpolation);
3635 } else {
3636 fs_reg src = vgrf(glsl_type::ivec2_type);
3637 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
3638 BRW_REGISTER_TYPE_F);
3639 for (int i = 0; i < 2; i++) {
3640 fs_reg temp = vgrf(glsl_type::float_type);
3641 bld.MUL(temp, offset(offset_src, bld, i), brw_imm_f(16.0f));
3642 fs_reg itemp = vgrf(glsl_type::int_type);
3643 /* float to int */
3644 bld.MOV(itemp, temp);
3645
3646 /* Clamp the upper end of the range to +7/16.
3647 * ARB_gpu_shader5 requires that we support a maximum offset
3648 * of +0.5, which isn't representable in a S0.4 value -- if
3649 * we didn't clamp it, we'd end up with -8/16, which is the
3650 * opposite of what the shader author wanted.
3651 *
3652 * This is legal due to ARB_gpu_shader5's quantization
3653 * rules:
3654 *
3655 * "Not all values of <offset> may be supported; x and y
3656 * offsets may be rounded to fixed-point values with the
3657 * number of fraction bits given by the
3658 * implementation-dependent constant
3659 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3660 */
3661 set_condmod(BRW_CONDITIONAL_L,
3662 bld.SEL(offset(src, bld, i), itemp, brw_imm_d(7)));
3663 }
3664
3665 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
3666 emit_pixel_interpolater_send(bld,
3667 opcode,
3668 tmp,
3669 src,
3670 brw_imm_ud(0u),
3671 interpolation);
3672 }
3673 shuffle_from_pln_layout(bld, dest, tmp);
3674 break;
3675 }
3676
3677 case nir_intrinsic_load_frag_coord:
3678 emit_fragcoord_interpolation(dest);
3679 break;
3680
3681 case nir_intrinsic_load_interpolated_input: {
3682 assert(instr->src[0].ssa &&
3683 instr->src[0].ssa->parent_instr->type == nir_instr_type_intrinsic);
3684 nir_intrinsic_instr *bary_intrinsic =
3685 nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
3686 nir_intrinsic_op bary_intrin = bary_intrinsic->intrinsic;
3687 enum glsl_interp_mode interp_mode =
3688 (enum glsl_interp_mode) nir_intrinsic_interp_mode(bary_intrinsic);
3689 fs_reg dst_xy;
3690
3691 if (bary_intrin == nir_intrinsic_load_barycentric_at_offset ||
3692 bary_intrin == nir_intrinsic_load_barycentric_at_sample) {
3693 /* Use the result of the PI message. Because the load_barycentric
3694 * intrinsics return a regular vec2 and we need it in PLN layout, we
3695 * have to do a translation. Fortunately, copy-prop cleans this up
3696 * reliably.
3697 */
3698 dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3699 shuffle_to_pln_layout(bld, dst_xy, get_nir_src(instr->src[0]));
3700 } else {
3701 /* Use the delta_xy values computed from the payload */
3702 enum brw_barycentric_mode bary =
3703 brw_barycentric_mode(interp_mode, bary_intrin);
3704
3705 dst_xy = this->delta_xy[bary];
3706 }
3707
3708 for (unsigned int i = 0; i < instr->num_components; i++) {
3709 fs_reg interp =
3710 component(interp_reg(nir_intrinsic_base(instr),
3711 nir_intrinsic_component(instr) + i), 0);
3712 interp.type = BRW_REGISTER_TYPE_F;
3713 dest.type = BRW_REGISTER_TYPE_F;
3714
3715 if (devinfo->gen < 6 && interp_mode == INTERP_MODE_SMOOTH) {
3716 fs_reg tmp = vgrf(glsl_type::float_type);
3717 bld.emit(FS_OPCODE_LINTERP, tmp, dst_xy, interp);
3718 bld.MUL(offset(dest, bld, i), tmp, this->pixel_w);
3719 } else {
3720 bld.emit(FS_OPCODE_LINTERP, offset(dest, bld, i), dst_xy, interp);
3721 }
3722 }
3723 break;
3724 }
3725
3726 default:
3727 nir_emit_intrinsic(bld, instr);
3728 break;
3729 }
3730 }
3731
3732 void
3733 fs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
3734 nir_intrinsic_instr *instr)
3735 {
3736 assert(stage == MESA_SHADER_COMPUTE);
3737 struct brw_cs_prog_data *cs_prog_data = brw_cs_prog_data(prog_data);
3738
3739 fs_reg dest;
3740 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3741 dest = get_nir_dest(instr->dest);
3742
3743 switch (instr->intrinsic) {
3744 case nir_intrinsic_barrier:
3745 emit_barrier();
3746 cs_prog_data->uses_barrier = true;
3747 break;
3748
3749 case nir_intrinsic_load_subgroup_id:
3750 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), subgroup_id);
3751 break;
3752
3753 case nir_intrinsic_load_local_invocation_id:
3754 case nir_intrinsic_load_work_group_id: {
3755 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3756 fs_reg val = nir_system_values[sv];
3757 assert(val.file != BAD_FILE);
3758 dest.type = val.type;
3759 for (unsigned i = 0; i < 3; i++)
3760 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
3761 break;
3762 }
3763
3764 case nir_intrinsic_load_num_work_groups: {
3765 const unsigned surface =
3766 cs_prog_data->binding_table.work_groups_start;
3767
3768 cs_prog_data->uses_num_work_groups = true;
3769
3770 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
3771 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(surface);
3772 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
3773 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(1); /* num components */
3774
3775 /* Read the 3 GLuint components of gl_NumWorkGroups */
3776 for (unsigned i = 0; i < 3; i++) {
3777 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = brw_imm_ud(i << 2);
3778 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
3779 offset(dest, bld, i), srcs, SURFACE_LOGICAL_NUM_SRCS);
3780 }
3781 break;
3782 }
3783
3784 case nir_intrinsic_shared_atomic_add:
3785 case nir_intrinsic_shared_atomic_imin:
3786 case nir_intrinsic_shared_atomic_umin:
3787 case nir_intrinsic_shared_atomic_imax:
3788 case nir_intrinsic_shared_atomic_umax:
3789 case nir_intrinsic_shared_atomic_and:
3790 case nir_intrinsic_shared_atomic_or:
3791 case nir_intrinsic_shared_atomic_xor:
3792 case nir_intrinsic_shared_atomic_exchange:
3793 case nir_intrinsic_shared_atomic_comp_swap:
3794 nir_emit_shared_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
3795 break;
3796 case nir_intrinsic_shared_atomic_fmin:
3797 case nir_intrinsic_shared_atomic_fmax:
3798 case nir_intrinsic_shared_atomic_fcomp_swap:
3799 nir_emit_shared_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
3800 break;
3801
3802 case nir_intrinsic_load_shared: {
3803 assert(devinfo->gen >= 7);
3804 assert(stage == MESA_SHADER_COMPUTE);
3805
3806 const unsigned bit_size = nir_dest_bit_size(instr->dest);
3807 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
3808 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
3809 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[0]);
3810 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
3811
3812 /* Make dest unsigned because that's what the temporary will be */
3813 dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
3814
3815 /* Read the vector */
3816 if (nir_intrinsic_align(instr) >= 4) {
3817 assert(nir_dest_bit_size(instr->dest) == 32);
3818 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
3819 fs_inst *inst =
3820 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
3821 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
3822 inst->size_written = instr->num_components * dispatch_width * 4;
3823 } else {
3824 assert(nir_dest_bit_size(instr->dest) <= 32);
3825 assert(nir_dest_num_components(instr->dest) == 1);
3826 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
3827
3828 fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
3829 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
3830 read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
3831 bld.MOV(dest, subscript(read_result, dest.type, 0));
3832 }
3833 break;
3834 }
3835
3836 case nir_intrinsic_store_shared: {
3837 assert(devinfo->gen >= 7);
3838 assert(stage == MESA_SHADER_COMPUTE);
3839
3840 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
3841 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
3842 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
3843 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
3844 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
3845
3846 fs_reg data = get_nir_src(instr->src[0]);
3847 data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
3848
3849 assert(nir_intrinsic_write_mask(instr) ==
3850 (1u << instr->num_components) - 1);
3851 if (nir_intrinsic_align(instr) >= 4) {
3852 assert(nir_src_bit_size(instr->src[0]) == 32);
3853 assert(nir_src_num_components(instr->src[0]) <= 4);
3854 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
3855 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
3856 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
3857 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
3858 } else {
3859 assert(nir_src_bit_size(instr->src[0]) <= 32);
3860 assert(nir_src_num_components(instr->src[0]) == 1);
3861 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
3862
3863 srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
3864 bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
3865
3866 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
3867 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
3868 }
3869 break;
3870 }
3871
3872 default:
3873 nir_emit_intrinsic(bld, instr);
3874 break;
3875 }
3876 }
3877
3878 static fs_reg
3879 brw_nir_reduction_op_identity(const fs_builder &bld,
3880 nir_op op, brw_reg_type type)
3881 {
3882 nir_const_value value = nir_alu_binop_identity(op, type_sz(type) * 8);
3883 switch (type_sz(type)) {
3884 case 1:
3885 if (type == BRW_REGISTER_TYPE_UB) {
3886 return brw_imm_uw(value.u8);
3887 } else {
3888 assert(type == BRW_REGISTER_TYPE_B);
3889 return brw_imm_w(value.i8);
3890 }
3891 case 2:
3892 return retype(brw_imm_uw(value.u16), type);
3893 case 4:
3894 return retype(brw_imm_ud(value.u32), type);
3895 case 8:
3896 if (type == BRW_REGISTER_TYPE_DF)
3897 return setup_imm_df(bld, value.f64);
3898 else
3899 return retype(brw_imm_u64(value.u64), type);
3900 default:
3901 unreachable("Invalid type size");
3902 }
3903 }
3904
3905 static opcode
3906 brw_op_for_nir_reduction_op(nir_op op)
3907 {
3908 switch (op) {
3909 case nir_op_iadd: return BRW_OPCODE_ADD;
3910 case nir_op_fadd: return BRW_OPCODE_ADD;
3911 case nir_op_imul: return BRW_OPCODE_MUL;
3912 case nir_op_fmul: return BRW_OPCODE_MUL;
3913 case nir_op_imin: return BRW_OPCODE_SEL;
3914 case nir_op_umin: return BRW_OPCODE_SEL;
3915 case nir_op_fmin: return BRW_OPCODE_SEL;
3916 case nir_op_imax: return BRW_OPCODE_SEL;
3917 case nir_op_umax: return BRW_OPCODE_SEL;
3918 case nir_op_fmax: return BRW_OPCODE_SEL;
3919 case nir_op_iand: return BRW_OPCODE_AND;
3920 case nir_op_ior: return BRW_OPCODE_OR;
3921 case nir_op_ixor: return BRW_OPCODE_XOR;
3922 default:
3923 unreachable("Invalid reduction operation");
3924 }
3925 }
3926
3927 static brw_conditional_mod
3928 brw_cond_mod_for_nir_reduction_op(nir_op op)
3929 {
3930 switch (op) {
3931 case nir_op_iadd: return BRW_CONDITIONAL_NONE;
3932 case nir_op_fadd: return BRW_CONDITIONAL_NONE;
3933 case nir_op_imul: return BRW_CONDITIONAL_NONE;
3934 case nir_op_fmul: return BRW_CONDITIONAL_NONE;
3935 case nir_op_imin: return BRW_CONDITIONAL_L;
3936 case nir_op_umin: return BRW_CONDITIONAL_L;
3937 case nir_op_fmin: return BRW_CONDITIONAL_L;
3938 case nir_op_imax: return BRW_CONDITIONAL_GE;
3939 case nir_op_umax: return BRW_CONDITIONAL_GE;
3940 case nir_op_fmax: return BRW_CONDITIONAL_GE;
3941 case nir_op_iand: return BRW_CONDITIONAL_NONE;
3942 case nir_op_ior: return BRW_CONDITIONAL_NONE;
3943 case nir_op_ixor: return BRW_CONDITIONAL_NONE;
3944 default:
3945 unreachable("Invalid reduction operation");
3946 }
3947 }
3948
3949 fs_reg
3950 fs_visitor::get_nir_image_intrinsic_image(const brw::fs_builder &bld,
3951 nir_intrinsic_instr *instr)
3952 {
3953 fs_reg image = retype(get_nir_src_imm(instr->src[0]), BRW_REGISTER_TYPE_UD);
3954
3955 if (stage_prog_data->binding_table.image_start > 0) {
3956 if (image.file == BRW_IMMEDIATE_VALUE) {
3957 image.d += stage_prog_data->binding_table.image_start;
3958 } else {
3959 bld.ADD(image, image,
3960 brw_imm_d(stage_prog_data->binding_table.image_start));
3961 }
3962 }
3963
3964 return bld.emit_uniformize(image);
3965 }
3966
3967 fs_reg
3968 fs_visitor::get_nir_ssbo_intrinsic_index(const brw::fs_builder &bld,
3969 nir_intrinsic_instr *instr)
3970 {
3971 /* SSBO stores are weird in that their index is in src[1] */
3972 const unsigned src = instr->intrinsic == nir_intrinsic_store_ssbo ? 1 : 0;
3973
3974 fs_reg surf_index;
3975 if (nir_src_is_const(instr->src[src])) {
3976 unsigned index = stage_prog_data->binding_table.ssbo_start +
3977 nir_src_as_uint(instr->src[src]);
3978 surf_index = brw_imm_ud(index);
3979 } else {
3980 surf_index = vgrf(glsl_type::uint_type);
3981 bld.ADD(surf_index, get_nir_src(instr->src[src]),
3982 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3983 }
3984
3985 return bld.emit_uniformize(surf_index);
3986 }
3987
3988 static unsigned
3989 image_intrinsic_coord_components(nir_intrinsic_instr *instr)
3990 {
3991 switch (nir_intrinsic_image_dim(instr)) {
3992 case GLSL_SAMPLER_DIM_1D:
3993 return 1 + nir_intrinsic_image_array(instr);
3994 case GLSL_SAMPLER_DIM_2D:
3995 case GLSL_SAMPLER_DIM_RECT:
3996 return 2 + nir_intrinsic_image_array(instr);
3997 case GLSL_SAMPLER_DIM_3D:
3998 case GLSL_SAMPLER_DIM_CUBE:
3999 return 3;
4000 case GLSL_SAMPLER_DIM_BUF:
4001 return 1;
4002 case GLSL_SAMPLER_DIM_MS:
4003 return 2 + nir_intrinsic_image_array(instr);
4004 default:
4005 unreachable("Invalid image dimension");
4006 }
4007 }
4008
4009 void
4010 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
4011 {
4012 fs_reg dest;
4013 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
4014 dest = get_nir_dest(instr->dest);
4015
4016 switch (instr->intrinsic) {
4017 case nir_intrinsic_image_load:
4018 case nir_intrinsic_image_store:
4019 case nir_intrinsic_image_atomic_add:
4020 case nir_intrinsic_image_atomic_imin:
4021 case nir_intrinsic_image_atomic_umin:
4022 case nir_intrinsic_image_atomic_imax:
4023 case nir_intrinsic_image_atomic_umax:
4024 case nir_intrinsic_image_atomic_and:
4025 case nir_intrinsic_image_atomic_or:
4026 case nir_intrinsic_image_atomic_xor:
4027 case nir_intrinsic_image_atomic_exchange:
4028 case nir_intrinsic_image_atomic_comp_swap:
4029 case nir_intrinsic_bindless_image_load:
4030 case nir_intrinsic_bindless_image_store:
4031 case nir_intrinsic_bindless_image_atomic_add:
4032 case nir_intrinsic_bindless_image_atomic_imin:
4033 case nir_intrinsic_bindless_image_atomic_umin:
4034 case nir_intrinsic_bindless_image_atomic_imax:
4035 case nir_intrinsic_bindless_image_atomic_umax:
4036 case nir_intrinsic_bindless_image_atomic_and:
4037 case nir_intrinsic_bindless_image_atomic_or:
4038 case nir_intrinsic_bindless_image_atomic_xor:
4039 case nir_intrinsic_bindless_image_atomic_exchange:
4040 case nir_intrinsic_bindless_image_atomic_comp_swap: {
4041 if (stage == MESA_SHADER_FRAGMENT &&
4042 instr->intrinsic != nir_intrinsic_image_load)
4043 brw_wm_prog_data(prog_data)->has_side_effects = true;
4044
4045 /* Get some metadata from the image intrinsic. */
4046 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
4047
4048 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4049
4050 switch (instr->intrinsic) {
4051 case nir_intrinsic_image_load:
4052 case nir_intrinsic_image_store:
4053 case nir_intrinsic_image_atomic_add:
4054 case nir_intrinsic_image_atomic_imin:
4055 case nir_intrinsic_image_atomic_umin:
4056 case nir_intrinsic_image_atomic_imax:
4057 case nir_intrinsic_image_atomic_umax:
4058 case nir_intrinsic_image_atomic_and:
4059 case nir_intrinsic_image_atomic_or:
4060 case nir_intrinsic_image_atomic_xor:
4061 case nir_intrinsic_image_atomic_exchange:
4062 case nir_intrinsic_image_atomic_comp_swap:
4063 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4064 get_nir_image_intrinsic_image(bld, instr);
4065 break;
4066
4067 default:
4068 /* Bindless */
4069 srcs[SURFACE_LOGICAL_SRC_SURFACE_HANDLE] =
4070 bld.emit_uniformize(get_nir_src(instr->src[0]));
4071 break;
4072 }
4073
4074 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4075 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] =
4076 brw_imm_ud(image_intrinsic_coord_components(instr));
4077
4078 /* Emit an image load, store or atomic op. */
4079 if (instr->intrinsic == nir_intrinsic_image_load ||
4080 instr->intrinsic == nir_intrinsic_bindless_image_load) {
4081 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4082 fs_inst *inst =
4083 bld.emit(SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL,
4084 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4085 inst->size_written = instr->num_components * dispatch_width * 4;
4086 } else if (instr->intrinsic == nir_intrinsic_image_store ||
4087 instr->intrinsic == nir_intrinsic_bindless_image_store) {
4088 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4089 srcs[SURFACE_LOGICAL_SRC_DATA] = get_nir_src(instr->src[3]);
4090 bld.emit(SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL,
4091 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4092 } else {
4093 unsigned num_srcs = info->num_srcs;
4094 int op = brw_aop_for_nir_intrinsic(instr);
4095 if (op == BRW_AOP_INC || op == BRW_AOP_DEC) {
4096 assert(num_srcs == 4);
4097 num_srcs = 3;
4098 }
4099
4100 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
4101
4102 fs_reg data;
4103 if (num_srcs >= 4)
4104 data = get_nir_src(instr->src[3]);
4105 if (num_srcs >= 5) {
4106 fs_reg tmp = bld.vgrf(data.type, 2);
4107 fs_reg sources[2] = { data, get_nir_src(instr->src[4]) };
4108 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
4109 data = tmp;
4110 }
4111 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
4112
4113 bld.emit(SHADER_OPCODE_TYPED_ATOMIC_LOGICAL,
4114 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4115 }
4116 break;
4117 }
4118
4119 case nir_intrinsic_image_size:
4120 case nir_intrinsic_bindless_image_size: {
4121 /* Unlike the [un]typed load and store opcodes, the TXS that this turns
4122 * into will handle the binding table index for us in the geneerator.
4123 * Incidentally, this means that we can handle bindless with exactly the
4124 * same code.
4125 */
4126 fs_reg image = retype(get_nir_src_imm(instr->src[0]),
4127 BRW_REGISTER_TYPE_UD);
4128 image = bld.emit_uniformize(image);
4129
4130 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
4131 if (instr->intrinsic == nir_intrinsic_image_size)
4132 srcs[TEX_LOGICAL_SRC_SURFACE] = image;
4133 else
4134 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = image;
4135 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_d(0);
4136 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(0);
4137 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(0);
4138
4139 /* Since the image size is always uniform, we can just emit a SIMD8
4140 * query instruction and splat the result out.
4141 */
4142 const fs_builder ubld = bld.exec_all().group(8, 0);
4143
4144 fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
4145 fs_inst *inst = ubld.emit(SHADER_OPCODE_IMAGE_SIZE_LOGICAL,
4146 tmp, srcs, ARRAY_SIZE(srcs));
4147 inst->size_written = 4 * REG_SIZE;
4148
4149 for (unsigned c = 0; c < instr->dest.ssa.num_components; ++c) {
4150 if (c == 2 && nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_CUBE) {
4151 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
4152 offset(retype(dest, tmp.type), bld, c),
4153 component(offset(tmp, ubld, c), 0), brw_imm_ud(6));
4154 } else {
4155 bld.MOV(offset(retype(dest, tmp.type), bld, c),
4156 component(offset(tmp, ubld, c), 0));
4157 }
4158 }
4159 break;
4160 }
4161
4162 case nir_intrinsic_image_load_raw_intel: {
4163 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4164 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4165 get_nir_image_intrinsic_image(bld, instr);
4166 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4167 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4168 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4169
4170 fs_inst *inst =
4171 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
4172 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4173 inst->size_written = instr->num_components * dispatch_width * 4;
4174 break;
4175 }
4176
4177 case nir_intrinsic_image_store_raw_intel: {
4178 if (stage == MESA_SHADER_FRAGMENT)
4179 brw_wm_prog_data(prog_data)->has_side_effects = true;
4180
4181 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4182 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4183 get_nir_image_intrinsic_image(bld, instr);
4184 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4185 srcs[SURFACE_LOGICAL_SRC_DATA] = get_nir_src(instr->src[2]);
4186 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4187 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4188
4189 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
4190 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4191 break;
4192 }
4193
4194 case nir_intrinsic_group_memory_barrier:
4195 case nir_intrinsic_memory_barrier_shared:
4196 case nir_intrinsic_memory_barrier_atomic_counter:
4197 case nir_intrinsic_memory_barrier_buffer:
4198 case nir_intrinsic_memory_barrier_image:
4199 case nir_intrinsic_memory_barrier: {
4200 bool l3_fence, slm_fence;
4201 if (devinfo->gen >= 11) {
4202 l3_fence = instr->intrinsic != nir_intrinsic_memory_barrier_shared;
4203 slm_fence = instr->intrinsic == nir_intrinsic_group_memory_barrier ||
4204 instr->intrinsic == nir_intrinsic_memory_barrier ||
4205 instr->intrinsic == nir_intrinsic_memory_barrier_shared;
4206 } else {
4207 /* Prior to gen11, we only have one kind of fence. */
4208 l3_fence = true;
4209 slm_fence = false;
4210 }
4211
4212 /* Be conservative in Gen11+ and always stall in a fence. Since there
4213 * are two different fences, and shader might want to synchronize
4214 * between them.
4215 *
4216 * TODO: Improve NIR so that scope and visibility information for the
4217 * barriers is available here to make a better decision.
4218 *
4219 * TODO: When emitting more than one fence, it might help emit all
4220 * the fences first and then generate the stall moves.
4221 */
4222 const bool stall = devinfo->gen >= 11;
4223
4224 const fs_builder ubld = bld.group(8, 0);
4225 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
4226
4227 if (l3_fence) {
4228 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp,
4229 brw_vec8_grf(0, 0), brw_imm_ud(stall),
4230 /* bti */ brw_imm_ud(0))
4231 ->size_written = 2 * REG_SIZE;
4232 }
4233
4234 if (slm_fence) {
4235 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp,
4236 brw_vec8_grf(0, 0), brw_imm_ud(stall),
4237 brw_imm_ud(GEN7_BTI_SLM))
4238 ->size_written = 2 * REG_SIZE;
4239 }
4240
4241 break;
4242 }
4243
4244 case nir_intrinsic_shader_clock: {
4245 /* We cannot do anything if there is an event, so ignore it for now */
4246 const fs_reg shader_clock = get_timestamp(bld);
4247 const fs_reg srcs[] = { component(shader_clock, 0),
4248 component(shader_clock, 1) };
4249 bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
4250 break;
4251 }
4252
4253 case nir_intrinsic_image_samples:
4254 /* The driver does not support multi-sampled images. */
4255 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
4256 break;
4257
4258 case nir_intrinsic_load_uniform: {
4259 /* Offsets are in bytes but they should always aligned to
4260 * the type size
4261 */
4262 assert(instr->const_index[0] % 4 == 0 ||
4263 instr->const_index[0] % type_sz(dest.type) == 0);
4264
4265 fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
4266
4267 if (nir_src_is_const(instr->src[0])) {
4268 unsigned load_offset = nir_src_as_uint(instr->src[0]);
4269 assert(load_offset % type_sz(dest.type) == 0);
4270 /* For 16-bit types we add the module of the const_index[0]
4271 * offset to access to not 32-bit aligned element
4272 */
4273 src.offset = load_offset + instr->const_index[0] % 4;
4274
4275 for (unsigned j = 0; j < instr->num_components; j++) {
4276 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
4277 }
4278 } else {
4279 fs_reg indirect = retype(get_nir_src(instr->src[0]),
4280 BRW_REGISTER_TYPE_UD);
4281
4282 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
4283 * go past the end of the uniform. In order to keep the n'th
4284 * component from running past, we subtract off the size of all but
4285 * one component of the vector.
4286 */
4287 assert(instr->const_index[1] >=
4288 instr->num_components * (int) type_sz(dest.type));
4289 unsigned read_size = instr->const_index[1] -
4290 (instr->num_components - 1) * type_sz(dest.type);
4291
4292 bool supports_64bit_indirects =
4293 !devinfo->is_cherryview && !gen_device_info_is_9lp(devinfo);
4294
4295 if (type_sz(dest.type) != 8 || supports_64bit_indirects) {
4296 for (unsigned j = 0; j < instr->num_components; j++) {
4297 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
4298 offset(dest, bld, j), offset(src, bld, j),
4299 indirect, brw_imm_ud(read_size));
4300 }
4301 } else {
4302 const unsigned num_mov_indirects =
4303 type_sz(dest.type) / type_sz(BRW_REGISTER_TYPE_UD);
4304 /* We read a little bit less per MOV INDIRECT, as they are now
4305 * 32-bits ones instead of 64-bit. Fix read_size then.
4306 */
4307 const unsigned read_size_32bit = read_size -
4308 (num_mov_indirects - 1) * type_sz(BRW_REGISTER_TYPE_UD);
4309 for (unsigned j = 0; j < instr->num_components; j++) {
4310 for (unsigned i = 0; i < num_mov_indirects; i++) {
4311 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
4312 subscript(offset(dest, bld, j), BRW_REGISTER_TYPE_UD, i),
4313 subscript(offset(src, bld, j), BRW_REGISTER_TYPE_UD, i),
4314 indirect, brw_imm_ud(read_size_32bit));
4315 }
4316 }
4317 }
4318 }
4319 break;
4320 }
4321
4322 case nir_intrinsic_load_ubo: {
4323 fs_reg surf_index;
4324 if (nir_src_is_const(instr->src[0])) {
4325 const unsigned index = stage_prog_data->binding_table.ubo_start +
4326 nir_src_as_uint(instr->src[0]);
4327 surf_index = brw_imm_ud(index);
4328 } else {
4329 /* The block index is not a constant. Evaluate the index expression
4330 * per-channel and add the base UBO index; we have to select a value
4331 * from any live channel.
4332 */
4333 surf_index = vgrf(glsl_type::uint_type);
4334 bld.ADD(surf_index, get_nir_src(instr->src[0]),
4335 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
4336 surf_index = bld.emit_uniformize(surf_index);
4337 }
4338
4339 if (!nir_src_is_const(instr->src[1])) {
4340 fs_reg base_offset = retype(get_nir_src(instr->src[1]),
4341 BRW_REGISTER_TYPE_UD);
4342
4343 for (int i = 0; i < instr->num_components; i++)
4344 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
4345 base_offset, i * type_sz(dest.type));
4346
4347 prog_data->has_ubo_pull = true;
4348 } else {
4349 /* Even if we are loading doubles, a pull constant load will load
4350 * a 32-bit vec4, so should only reserve vgrf space for that. If we
4351 * need to load a full dvec4 we will have to emit 2 loads. This is
4352 * similar to demote_pull_constants(), except that in that case we
4353 * see individual accesses to each component of the vector and then
4354 * we let CSE deal with duplicate loads. Here we see a vector access
4355 * and we have to split it if necessary.
4356 */
4357 const unsigned type_size = type_sz(dest.type);
4358 const unsigned load_offset = nir_src_as_uint(instr->src[1]);
4359
4360 /* See if we've selected this as a push constant candidate */
4361 if (nir_src_is_const(instr->src[0])) {
4362 const unsigned ubo_block = nir_src_as_uint(instr->src[0]);
4363 const unsigned offset_256b = load_offset / 32;
4364
4365 fs_reg push_reg;
4366 for (int i = 0; i < 4; i++) {
4367 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
4368 if (range->block == ubo_block &&
4369 offset_256b >= range->start &&
4370 offset_256b < range->start + range->length) {
4371
4372 push_reg = fs_reg(UNIFORM, UBO_START + i, dest.type);
4373 push_reg.offset = load_offset - 32 * range->start;
4374 break;
4375 }
4376 }
4377
4378 if (push_reg.file != BAD_FILE) {
4379 for (unsigned i = 0; i < instr->num_components; i++) {
4380 bld.MOV(offset(dest, bld, i),
4381 byte_offset(push_reg, i * type_size));
4382 }
4383 break;
4384 }
4385 }
4386
4387 prog_data->has_ubo_pull = true;
4388
4389 const unsigned block_sz = 64; /* Fetch one cacheline at a time. */
4390 const fs_builder ubld = bld.exec_all().group(block_sz / 4, 0);
4391 const fs_reg packed_consts = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4392
4393 for (unsigned c = 0; c < instr->num_components;) {
4394 const unsigned base = load_offset + c * type_size;
4395 /* Number of usable components in the next block-aligned load. */
4396 const unsigned count = MIN2(instr->num_components - c,
4397 (block_sz - base % block_sz) / type_size);
4398
4399 ubld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
4400 packed_consts, surf_index,
4401 brw_imm_ud(base & ~(block_sz - 1)));
4402
4403 const fs_reg consts =
4404 retype(byte_offset(packed_consts, base & (block_sz - 1)),
4405 dest.type);
4406
4407 for (unsigned d = 0; d < count; d++)
4408 bld.MOV(offset(dest, bld, c + d), component(consts, d));
4409
4410 c += count;
4411 }
4412 }
4413 break;
4414 }
4415
4416 case nir_intrinsic_load_global: {
4417 assert(devinfo->gen >= 8);
4418
4419 if (nir_intrinsic_align(instr) >= 4) {
4420 assert(nir_dest_bit_size(instr->dest) == 32);
4421 fs_inst *inst = bld.emit(SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL,
4422 dest,
4423 get_nir_src(instr->src[0]), /* Address */
4424 fs_reg(), /* No source data */
4425 brw_imm_ud(instr->num_components));
4426 inst->size_written = instr->num_components *
4427 inst->dst.component_size(inst->exec_size);
4428 } else {
4429 const unsigned bit_size = nir_dest_bit_size(instr->dest);
4430 assert(bit_size <= 32);
4431 assert(nir_dest_num_components(instr->dest) == 1);
4432 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
4433 bld.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL,
4434 tmp,
4435 get_nir_src(instr->src[0]), /* Address */
4436 fs_reg(), /* No source data */
4437 brw_imm_ud(bit_size));
4438 bld.MOV(dest, subscript(tmp, dest.type, 0));
4439 }
4440 break;
4441 }
4442
4443 case nir_intrinsic_store_global:
4444 assert(devinfo->gen >= 8);
4445
4446 if (stage == MESA_SHADER_FRAGMENT)
4447 brw_wm_prog_data(prog_data)->has_side_effects = true;
4448
4449 if (nir_intrinsic_align(instr) >= 4) {
4450 assert(nir_src_bit_size(instr->src[0]) == 32);
4451 bld.emit(SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL,
4452 fs_reg(),
4453 get_nir_src(instr->src[1]), /* Address */
4454 get_nir_src(instr->src[0]), /* Data */
4455 brw_imm_ud(instr->num_components));
4456 } else {
4457 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
4458 assert(bit_size <= 32);
4459 assert(nir_src_num_components(instr->src[0]) == 1);
4460 brw_reg_type data_type =
4461 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
4462 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
4463 bld.MOV(tmp, retype(get_nir_src(instr->src[0]), data_type));
4464 bld.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL,
4465 fs_reg(),
4466 get_nir_src(instr->src[1]), /* Address */
4467 tmp, /* Data */
4468 brw_imm_ud(nir_src_bit_size(instr->src[0])));
4469 }
4470 break;
4471
4472 case nir_intrinsic_global_atomic_add:
4473 case nir_intrinsic_global_atomic_imin:
4474 case nir_intrinsic_global_atomic_umin:
4475 case nir_intrinsic_global_atomic_imax:
4476 case nir_intrinsic_global_atomic_umax:
4477 case nir_intrinsic_global_atomic_and:
4478 case nir_intrinsic_global_atomic_or:
4479 case nir_intrinsic_global_atomic_xor:
4480 case nir_intrinsic_global_atomic_exchange:
4481 case nir_intrinsic_global_atomic_comp_swap:
4482 nir_emit_global_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
4483 break;
4484 case nir_intrinsic_global_atomic_fmin:
4485 case nir_intrinsic_global_atomic_fmax:
4486 case nir_intrinsic_global_atomic_fcomp_swap:
4487 nir_emit_global_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
4488 break;
4489
4490 case nir_intrinsic_load_ssbo: {
4491 assert(devinfo->gen >= 7);
4492
4493 const unsigned bit_size = nir_dest_bit_size(instr->dest);
4494 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4495 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4496 get_nir_ssbo_intrinsic_index(bld, instr);
4497 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4498 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4499
4500 /* Make dest unsigned because that's what the temporary will be */
4501 dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
4502
4503 /* Read the vector */
4504 if (nir_intrinsic_align(instr) >= 4) {
4505 assert(nir_dest_bit_size(instr->dest) == 32);
4506 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4507 fs_inst *inst =
4508 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
4509 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4510 inst->size_written = instr->num_components * dispatch_width * 4;
4511 } else {
4512 assert(nir_dest_bit_size(instr->dest) <= 32);
4513 assert(nir_dest_num_components(instr->dest) == 1);
4514 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
4515
4516 fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
4517 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
4518 read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
4519 bld.MOV(dest, subscript(read_result, dest.type, 0));
4520 }
4521 break;
4522 }
4523
4524 case nir_intrinsic_store_ssbo: {
4525 assert(devinfo->gen >= 7);
4526
4527 if (stage == MESA_SHADER_FRAGMENT)
4528 brw_wm_prog_data(prog_data)->has_side_effects = true;
4529
4530 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
4531 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4532 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4533 get_nir_ssbo_intrinsic_index(bld, instr);
4534 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[2]);
4535 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4536
4537 fs_reg data = get_nir_src(instr->src[0]);
4538 data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
4539
4540 assert(nir_intrinsic_write_mask(instr) ==
4541 (1u << instr->num_components) - 1);
4542 if (nir_intrinsic_align(instr) >= 4) {
4543 assert(nir_src_bit_size(instr->src[0]) == 32);
4544 assert(nir_src_num_components(instr->src[0]) <= 4);
4545 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
4546 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4547 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
4548 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4549 } else {
4550 assert(nir_src_bit_size(instr->src[0]) <= 32);
4551 assert(nir_src_num_components(instr->src[0]) == 1);
4552 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
4553
4554 srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
4555 bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
4556
4557 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
4558 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4559 }
4560 break;
4561 }
4562
4563 case nir_intrinsic_store_output: {
4564 assert(nir_src_bit_size(instr->src[0]) == 32);
4565 fs_reg src = get_nir_src(instr->src[0]);
4566
4567 unsigned store_offset = nir_src_as_uint(instr->src[1]);
4568 unsigned num_components = instr->num_components;
4569 unsigned first_component = nir_intrinsic_component(instr);
4570
4571 fs_reg new_dest = retype(offset(outputs[instr->const_index[0]], bld,
4572 4 * store_offset), src.type);
4573 for (unsigned j = 0; j < num_components; j++) {
4574 bld.MOV(offset(new_dest, bld, j + first_component),
4575 offset(src, bld, j));
4576 }
4577 break;
4578 }
4579
4580 case nir_intrinsic_ssbo_atomic_add:
4581 case nir_intrinsic_ssbo_atomic_imin:
4582 case nir_intrinsic_ssbo_atomic_umin:
4583 case nir_intrinsic_ssbo_atomic_imax:
4584 case nir_intrinsic_ssbo_atomic_umax:
4585 case nir_intrinsic_ssbo_atomic_and:
4586 case nir_intrinsic_ssbo_atomic_or:
4587 case nir_intrinsic_ssbo_atomic_xor:
4588 case nir_intrinsic_ssbo_atomic_exchange:
4589 case nir_intrinsic_ssbo_atomic_comp_swap:
4590 nir_emit_ssbo_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
4591 break;
4592 case nir_intrinsic_ssbo_atomic_fmin:
4593 case nir_intrinsic_ssbo_atomic_fmax:
4594 case nir_intrinsic_ssbo_atomic_fcomp_swap:
4595 nir_emit_ssbo_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
4596 break;
4597
4598 case nir_intrinsic_get_buffer_size: {
4599 assert(nir_src_num_components(instr->src[0]) == 1);
4600 unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
4601 nir_src_as_uint(instr->src[0]) : 0;
4602
4603 /* A resinfo's sampler message is used to get the buffer size. The
4604 * SIMD8's writeback message consists of four registers and SIMD16's
4605 * writeback message consists of 8 destination registers (two per each
4606 * component). Because we are only interested on the first channel of
4607 * the first returned component, where resinfo returns the buffer size
4608 * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
4609 * the dispatch width.
4610 */
4611 const fs_builder ubld = bld.exec_all().group(8, 0);
4612 fs_reg src_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4613 fs_reg ret_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
4614
4615 /* Set LOD = 0 */
4616 ubld.MOV(src_payload, brw_imm_d(0));
4617
4618 const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
4619 fs_inst *inst = ubld.emit(SHADER_OPCODE_GET_BUFFER_SIZE, ret_payload,
4620 src_payload, brw_imm_ud(index));
4621 inst->header_size = 0;
4622 inst->mlen = 1;
4623 inst->size_written = 4 * REG_SIZE;
4624
4625 /* SKL PRM, vol07, 3D Media GPGPU Engine, Bounds Checking and Faulting:
4626 *
4627 * "Out-of-bounds checking is always performed at a DWord granularity. If
4628 * any part of the DWord is out-of-bounds then the whole DWord is
4629 * considered out-of-bounds."
4630 *
4631 * This implies that types with size smaller than 4-bytes need to be
4632 * padded if they don't complete the last dword of the buffer. But as we
4633 * need to maintain the original size we need to reverse the padding
4634 * calculation to return the correct size to know the number of elements
4635 * of an unsized array. As we stored in the last two bits of the surface
4636 * size the needed padding for the buffer, we calculate here the
4637 * original buffer_size reversing the surface_size calculation:
4638 *
4639 * surface_size = isl_align(buffer_size, 4) +
4640 * (isl_align(buffer_size) - buffer_size)
4641 *
4642 * buffer_size = surface_size & ~3 - surface_size & 3
4643 */
4644
4645 fs_reg size_aligned4 = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4646 fs_reg size_padding = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4647 fs_reg buffer_size = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4648
4649 ubld.AND(size_padding, ret_payload, brw_imm_ud(3));
4650 ubld.AND(size_aligned4, ret_payload, brw_imm_ud(~3));
4651 ubld.ADD(buffer_size, size_aligned4, negate(size_padding));
4652
4653 bld.MOV(retype(dest, ret_payload.type), component(buffer_size, 0));
4654 break;
4655 }
4656
4657 case nir_intrinsic_load_subgroup_size:
4658 /* This should only happen for fragment shaders because every other case
4659 * is lowered in NIR so we can optimize on it.
4660 */
4661 assert(stage == MESA_SHADER_FRAGMENT);
4662 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(dispatch_width));
4663 break;
4664
4665 case nir_intrinsic_load_subgroup_invocation:
4666 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
4667 nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION]);
4668 break;
4669
4670 case nir_intrinsic_load_subgroup_eq_mask:
4671 case nir_intrinsic_load_subgroup_ge_mask:
4672 case nir_intrinsic_load_subgroup_gt_mask:
4673 case nir_intrinsic_load_subgroup_le_mask:
4674 case nir_intrinsic_load_subgroup_lt_mask:
4675 unreachable("not reached");
4676
4677 case nir_intrinsic_vote_any: {
4678 const fs_builder ubld = bld.exec_all().group(1, 0);
4679
4680 /* The any/all predicates do not consider channel enables. To prevent
4681 * dead channels from affecting the result, we initialize the flag with
4682 * with the identity value for the logical operation.
4683 */
4684 if (dispatch_width == 32) {
4685 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4686 ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
4687 brw_imm_ud(0));
4688 } else {
4689 ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0));
4690 }
4691 bld.CMP(bld.null_reg_d(), get_nir_src(instr->src[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ);
4692
4693 /* For some reason, the any/all predicates don't work properly with
4694 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4695 * doesn't read the correct subset of the flag register and you end up
4696 * getting garbage in the second half. Work around this by using a pair
4697 * of 1-wide MOVs and scattering the result.
4698 */
4699 fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
4700 ubld.MOV(res1, brw_imm_d(0));
4701 set_predicate(dispatch_width == 8 ? BRW_PREDICATE_ALIGN1_ANY8H :
4702 dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ANY16H :
4703 BRW_PREDICATE_ALIGN1_ANY32H,
4704 ubld.MOV(res1, brw_imm_d(-1)));
4705
4706 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
4707 break;
4708 }
4709 case nir_intrinsic_vote_all: {
4710 const fs_builder ubld = bld.exec_all().group(1, 0);
4711
4712 /* The any/all predicates do not consider channel enables. To prevent
4713 * dead channels from affecting the result, we initialize the flag with
4714 * with the identity value for the logical operation.
4715 */
4716 if (dispatch_width == 32) {
4717 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4718 ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
4719 brw_imm_ud(0xffffffff));
4720 } else {
4721 ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4722 }
4723 bld.CMP(bld.null_reg_d(), get_nir_src(instr->src[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ);
4724
4725 /* For some reason, the any/all predicates don't work properly with
4726 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4727 * doesn't read the correct subset of the flag register and you end up
4728 * getting garbage in the second half. Work around this by using a pair
4729 * of 1-wide MOVs and scattering the result.
4730 */
4731 fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
4732 ubld.MOV(res1, brw_imm_d(0));
4733 set_predicate(dispatch_width == 8 ? BRW_PREDICATE_ALIGN1_ALL8H :
4734 dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ALL16H :
4735 BRW_PREDICATE_ALIGN1_ALL32H,
4736 ubld.MOV(res1, brw_imm_d(-1)));
4737
4738 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
4739 break;
4740 }
4741 case nir_intrinsic_vote_feq:
4742 case nir_intrinsic_vote_ieq: {
4743 fs_reg value = get_nir_src(instr->src[0]);
4744 if (instr->intrinsic == nir_intrinsic_vote_feq) {
4745 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
4746 value.type = bit_size == 8 ? BRW_REGISTER_TYPE_B :
4747 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_F);
4748 }
4749
4750 fs_reg uniformized = bld.emit_uniformize(value);
4751 const fs_builder ubld = bld.exec_all().group(1, 0);
4752
4753 /* The any/all predicates do not consider channel enables. To prevent
4754 * dead channels from affecting the result, we initialize the flag with
4755 * with the identity value for the logical operation.
4756 */
4757 if (dispatch_width == 32) {
4758 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4759 ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
4760 brw_imm_ud(0xffffffff));
4761 } else {
4762 ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4763 }
4764 bld.CMP(bld.null_reg_d(), value, uniformized, BRW_CONDITIONAL_Z);
4765
4766 /* For some reason, the any/all predicates don't work properly with
4767 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4768 * doesn't read the correct subset of the flag register and you end up
4769 * getting garbage in the second half. Work around this by using a pair
4770 * of 1-wide MOVs and scattering the result.
4771 */
4772 fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
4773 ubld.MOV(res1, brw_imm_d(0));
4774 set_predicate(dispatch_width == 8 ? BRW_PREDICATE_ALIGN1_ALL8H :
4775 dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ALL16H :
4776 BRW_PREDICATE_ALIGN1_ALL32H,
4777 ubld.MOV(res1, brw_imm_d(-1)));
4778
4779 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
4780 break;
4781 }
4782
4783 case nir_intrinsic_ballot: {
4784 const fs_reg value = retype(get_nir_src(instr->src[0]),
4785 BRW_REGISTER_TYPE_UD);
4786 struct brw_reg flag = brw_flag_reg(0, 0);
4787 /* FIXME: For SIMD32 programs, this causes us to stomp on f0.1 as well
4788 * as f0.0. This is a problem for fragment programs as we currently use
4789 * f0.1 for discards. Fortunately, we don't support SIMD32 fragment
4790 * programs yet so this isn't a problem. When we do, something will
4791 * have to change.
4792 */
4793 if (dispatch_width == 32)
4794 flag.type = BRW_REGISTER_TYPE_UD;
4795
4796 bld.exec_all().group(1, 0).MOV(flag, brw_imm_ud(0u));
4797 bld.CMP(bld.null_reg_ud(), value, brw_imm_ud(0u), BRW_CONDITIONAL_NZ);
4798
4799 if (instr->dest.ssa.bit_size > 32) {
4800 dest.type = BRW_REGISTER_TYPE_UQ;
4801 } else {
4802 dest.type = BRW_REGISTER_TYPE_UD;
4803 }
4804 bld.MOV(dest, flag);
4805 break;
4806 }
4807
4808 case nir_intrinsic_read_invocation: {
4809 const fs_reg value = get_nir_src(instr->src[0]);
4810 const fs_reg invocation = get_nir_src(instr->src[1]);
4811 fs_reg tmp = bld.vgrf(value.type);
4812
4813 bld.exec_all().emit(SHADER_OPCODE_BROADCAST, tmp, value,
4814 bld.emit_uniformize(invocation));
4815
4816 bld.MOV(retype(dest, value.type), fs_reg(component(tmp, 0)));
4817 break;
4818 }
4819
4820 case nir_intrinsic_read_first_invocation: {
4821 const fs_reg value = get_nir_src(instr->src[0]);
4822 bld.MOV(retype(dest, value.type), bld.emit_uniformize(value));
4823 break;
4824 }
4825
4826 case nir_intrinsic_shuffle: {
4827 const fs_reg value = get_nir_src(instr->src[0]);
4828 const fs_reg index = get_nir_src(instr->src[1]);
4829
4830 bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, index);
4831 break;
4832 }
4833
4834 case nir_intrinsic_first_invocation: {
4835 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
4836 bld.exec_all().emit(SHADER_OPCODE_FIND_LIVE_CHANNEL, tmp);
4837 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
4838 fs_reg(component(tmp, 0)));
4839 break;
4840 }
4841
4842 case nir_intrinsic_quad_broadcast: {
4843 const fs_reg value = get_nir_src(instr->src[0]);
4844 const unsigned index = nir_src_as_uint(instr->src[1]);
4845
4846 bld.emit(SHADER_OPCODE_CLUSTER_BROADCAST, retype(dest, value.type),
4847 value, brw_imm_ud(index), brw_imm_ud(4));
4848 break;
4849 }
4850
4851 case nir_intrinsic_quad_swap_horizontal: {
4852 const fs_reg value = get_nir_src(instr->src[0]);
4853 const fs_reg tmp = bld.vgrf(value.type);
4854 if (devinfo->gen <= 7) {
4855 /* The hardware doesn't seem to support these crazy regions with
4856 * compressed instructions on gen7 and earlier so we fall back to
4857 * using quad swizzles. Fortunately, we don't support 64-bit
4858 * anything in Vulkan on gen7.
4859 */
4860 assert(nir_src_bit_size(instr->src[0]) == 32);
4861 const fs_builder ubld = bld.exec_all();
4862 ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
4863 brw_imm_ud(BRW_SWIZZLE4(1,0,3,2)));
4864 bld.MOV(retype(dest, value.type), tmp);
4865 } else {
4866 const fs_builder ubld = bld.exec_all().group(dispatch_width / 2, 0);
4867
4868 const fs_reg src_left = horiz_stride(value, 2);
4869 const fs_reg src_right = horiz_stride(horiz_offset(value, 1), 2);
4870 const fs_reg tmp_left = horiz_stride(tmp, 2);
4871 const fs_reg tmp_right = horiz_stride(horiz_offset(tmp, 1), 2);
4872
4873 ubld.MOV(tmp_left, src_right);
4874 ubld.MOV(tmp_right, src_left);
4875
4876 }
4877 bld.MOV(retype(dest, value.type), tmp);
4878 break;
4879 }
4880
4881 case nir_intrinsic_quad_swap_vertical: {
4882 const fs_reg value = get_nir_src(instr->src[0]);
4883 if (nir_src_bit_size(instr->src[0]) == 32) {
4884 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
4885 const fs_reg tmp = bld.vgrf(value.type);
4886 const fs_builder ubld = bld.exec_all();
4887 ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
4888 brw_imm_ud(BRW_SWIZZLE4(2,3,0,1)));
4889 bld.MOV(retype(dest, value.type), tmp);
4890 } else {
4891 /* For larger data types, we have to either emit dispatch_width many
4892 * MOVs or else fall back to doing indirects.
4893 */
4894 fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
4895 bld.XOR(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
4896 brw_imm_w(0x2));
4897 bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, idx);
4898 }
4899 break;
4900 }
4901
4902 case nir_intrinsic_quad_swap_diagonal: {
4903 const fs_reg value = get_nir_src(instr->src[0]);
4904 if (nir_src_bit_size(instr->src[0]) == 32) {
4905 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
4906 const fs_reg tmp = bld.vgrf(value.type);
4907 const fs_builder ubld = bld.exec_all();
4908 ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
4909 brw_imm_ud(BRW_SWIZZLE4(3,2,1,0)));
4910 bld.MOV(retype(dest, value.type), tmp);
4911 } else {
4912 /* For larger data types, we have to either emit dispatch_width many
4913 * MOVs or else fall back to doing indirects.
4914 */
4915 fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
4916 bld.XOR(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
4917 brw_imm_w(0x3));
4918 bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, idx);
4919 }
4920 break;
4921 }
4922
4923 case nir_intrinsic_reduce: {
4924 fs_reg src = get_nir_src(instr->src[0]);
4925 nir_op redop = (nir_op)nir_intrinsic_reduction_op(instr);
4926 unsigned cluster_size = nir_intrinsic_cluster_size(instr);
4927 if (cluster_size == 0 || cluster_size > dispatch_width)
4928 cluster_size = dispatch_width;
4929
4930 /* Figure out the source type */
4931 src.type = brw_type_for_nir_type(devinfo,
4932 (nir_alu_type)(nir_op_infos[redop].input_types[0] |
4933 nir_src_bit_size(instr->src[0])));
4934
4935 fs_reg identity = brw_nir_reduction_op_identity(bld, redop, src.type);
4936 opcode brw_op = brw_op_for_nir_reduction_op(redop);
4937 brw_conditional_mod cond_mod = brw_cond_mod_for_nir_reduction_op(redop);
4938
4939 /* There are a couple of register region issues that make things
4940 * complicated for 8-bit types:
4941 *
4942 * 1. Only raw moves are allowed to write to a packed 8-bit
4943 * destination.
4944 * 2. If we use a strided destination, the efficient way to do scan
4945 * operations ends up using strides that are too big to encode in
4946 * an instruction.
4947 *
4948 * To get around these issues, we just do all 8-bit scan operations in
4949 * 16 bits. It's actually fewer instructions than what we'd have to do
4950 * if we were trying to do it in native 8-bit types and the results are
4951 * the same once we truncate to 8 bits at the end.
4952 */
4953 brw_reg_type scan_type = src.type;
4954 if (type_sz(scan_type) == 1)
4955 scan_type = brw_reg_type_from_bit_size(16, src.type);
4956
4957 /* Set up a register for all of our scratching around and initialize it
4958 * to reduction operation's identity value.
4959 */
4960 fs_reg scan = bld.vgrf(scan_type);
4961 bld.exec_all().emit(SHADER_OPCODE_SEL_EXEC, scan, src, identity);
4962
4963 bld.emit_scan(brw_op, scan, cluster_size, cond_mod);
4964
4965 dest.type = src.type;
4966 if (cluster_size * type_sz(src.type) >= REG_SIZE * 2) {
4967 /* In this case, CLUSTER_BROADCAST instruction isn't needed because
4968 * the distance between clusters is at least 2 GRFs. In this case,
4969 * we don't need the weird striding of the CLUSTER_BROADCAST
4970 * instruction and can just do regular MOVs.
4971 */
4972 assert((cluster_size * type_sz(src.type)) % (REG_SIZE * 2) == 0);
4973 const unsigned groups =
4974 (dispatch_width * type_sz(src.type)) / (REG_SIZE * 2);
4975 const unsigned group_size = dispatch_width / groups;
4976 for (unsigned i = 0; i < groups; i++) {
4977 const unsigned cluster = (i * group_size) / cluster_size;
4978 const unsigned comp = cluster * cluster_size + (cluster_size - 1);
4979 bld.group(group_size, i).MOV(horiz_offset(dest, i * group_size),
4980 component(scan, comp));
4981 }
4982 } else {
4983 bld.emit(SHADER_OPCODE_CLUSTER_BROADCAST, dest, scan,
4984 brw_imm_ud(cluster_size - 1), brw_imm_ud(cluster_size));
4985 }
4986 break;
4987 }
4988
4989 case nir_intrinsic_inclusive_scan:
4990 case nir_intrinsic_exclusive_scan: {
4991 fs_reg src = get_nir_src(instr->src[0]);
4992 nir_op redop = (nir_op)nir_intrinsic_reduction_op(instr);
4993
4994 /* Figure out the source type */
4995 src.type = brw_type_for_nir_type(devinfo,
4996 (nir_alu_type)(nir_op_infos[redop].input_types[0] |
4997 nir_src_bit_size(instr->src[0])));
4998
4999 fs_reg identity = brw_nir_reduction_op_identity(bld, redop, src.type);
5000 opcode brw_op = brw_op_for_nir_reduction_op(redop);
5001 brw_conditional_mod cond_mod = brw_cond_mod_for_nir_reduction_op(redop);
5002
5003 /* There are a couple of register region issues that make things
5004 * complicated for 8-bit types:
5005 *
5006 * 1. Only raw moves are allowed to write to a packed 8-bit
5007 * destination.
5008 * 2. If we use a strided destination, the efficient way to do scan
5009 * operations ends up using strides that are too big to encode in
5010 * an instruction.
5011 *
5012 * To get around these issues, we just do all 8-bit scan operations in
5013 * 16 bits. It's actually fewer instructions than what we'd have to do
5014 * if we were trying to do it in native 8-bit types and the results are
5015 * the same once we truncate to 8 bits at the end.
5016 */
5017 brw_reg_type scan_type = src.type;
5018 if (type_sz(scan_type) == 1)
5019 scan_type = brw_reg_type_from_bit_size(16, src.type);
5020
5021 /* Set up a register for all of our scratching around and initialize it
5022 * to reduction operation's identity value.
5023 */
5024 fs_reg scan = bld.vgrf(scan_type);
5025 const fs_builder allbld = bld.exec_all();
5026 allbld.emit(SHADER_OPCODE_SEL_EXEC, scan, src, identity);
5027
5028 if (instr->intrinsic == nir_intrinsic_exclusive_scan) {
5029 /* Exclusive scan is a bit harder because we have to do an annoying
5030 * shift of the contents before we can begin. To make things worse,
5031 * we can't do this with a normal stride; we have to use indirects.
5032 */
5033 fs_reg shifted = bld.vgrf(scan_type);
5034 fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
5035 allbld.ADD(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
5036 brw_imm_w(-1));
5037 allbld.emit(SHADER_OPCODE_SHUFFLE, shifted, scan, idx);
5038 allbld.group(1, 0).MOV(component(shifted, 0), identity);
5039 scan = shifted;
5040 }
5041
5042 bld.emit_scan(brw_op, scan, dispatch_width, cond_mod);
5043
5044 bld.MOV(retype(dest, src.type), scan);
5045 break;
5046 }
5047
5048 case nir_intrinsic_begin_invocation_interlock: {
5049 const fs_builder ubld = bld.group(8, 0);
5050 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
5051
5052 ubld.emit(SHADER_OPCODE_INTERLOCK, tmp, brw_vec8_grf(0, 0))
5053 ->size_written = 2 * REG_SIZE;
5054 break;
5055 }
5056
5057 case nir_intrinsic_end_invocation_interlock: {
5058 /* For endInvocationInterlock(), we need to insert a memory fence which
5059 * stalls in the shader until the memory transactions prior to that
5060 * fence are complete. This ensures that the shader does not end before
5061 * any writes from its critical section have landed. Otherwise, you can
5062 * end up with a case where the next invocation on that pixel properly
5063 * stalls for previous FS invocation on its pixel to complete but
5064 * doesn't actually wait for the dataport memory transactions from that
5065 * thread to land before submitting its own.
5066 */
5067 const fs_builder ubld = bld.group(8, 0);
5068 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
5069 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp,
5070 brw_vec8_grf(0, 0), brw_imm_ud(1), brw_imm_ud(0))
5071 ->size_written = 2 * REG_SIZE;
5072 break;
5073 }
5074
5075 default:
5076 unreachable("unknown intrinsic");
5077 }
5078 }
5079
5080 void
5081 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
5082 int op, nir_intrinsic_instr *instr)
5083 {
5084 if (stage == MESA_SHADER_FRAGMENT)
5085 brw_wm_prog_data(prog_data)->has_side_effects = true;
5086
5087 /* The BTI untyped atomic messages only support 32-bit atomics. If you
5088 * just look at the big table of messages in the Vol 7 of the SKL PRM, they
5089 * appear to exist. However, if you look at Vol 2a, there are no message
5090 * descriptors provided for Qword atomic ops except for A64 messages.
5091 */
5092 assert(nir_dest_bit_size(instr->dest) == 32);
5093
5094 fs_reg dest;
5095 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5096 dest = get_nir_dest(instr->dest);
5097
5098 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5099 srcs[SURFACE_LOGICAL_SRC_SURFACE] = get_nir_ssbo_intrinsic_index(bld, instr);
5100 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
5101 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5102 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5103
5104 fs_reg data;
5105 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
5106 data = get_nir_src(instr->src[2]);
5107
5108 if (op == BRW_AOP_CMPWR) {
5109 fs_reg tmp = bld.vgrf(data.type, 2);
5110 fs_reg sources[2] = { data, get_nir_src(instr->src[3]) };
5111 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5112 data = tmp;
5113 }
5114 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5115
5116 /* Emit the actual atomic operation */
5117
5118 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL,
5119 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5120 }
5121
5122 void
5123 fs_visitor::nir_emit_ssbo_atomic_float(const fs_builder &bld,
5124 int op, nir_intrinsic_instr *instr)
5125 {
5126 if (stage == MESA_SHADER_FRAGMENT)
5127 brw_wm_prog_data(prog_data)->has_side_effects = true;
5128
5129 fs_reg dest;
5130 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5131 dest = get_nir_dest(instr->dest);
5132
5133 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5134 srcs[SURFACE_LOGICAL_SRC_SURFACE] = get_nir_ssbo_intrinsic_index(bld, instr);
5135 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
5136 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5137 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5138
5139 fs_reg data = get_nir_src(instr->src[2]);
5140 if (op == BRW_AOP_FCMPWR) {
5141 fs_reg tmp = bld.vgrf(data.type, 2);
5142 fs_reg sources[2] = { data, get_nir_src(instr->src[3]) };
5143 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5144 data = tmp;
5145 }
5146 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5147
5148 /* Emit the actual atomic operation */
5149
5150 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL,
5151 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5152 }
5153
5154 void
5155 fs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
5156 int op, nir_intrinsic_instr *instr)
5157 {
5158 fs_reg dest;
5159 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5160 dest = get_nir_dest(instr->dest);
5161
5162 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5163 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
5164 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5165 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5166
5167 fs_reg data;
5168 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
5169 data = get_nir_src(instr->src[1]);
5170 if (op == BRW_AOP_CMPWR) {
5171 fs_reg tmp = bld.vgrf(data.type, 2);
5172 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5173 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5174 data = tmp;
5175 }
5176 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5177
5178 /* Get the offset */
5179 if (nir_src_is_const(instr->src[0])) {
5180 srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
5181 brw_imm_ud(instr->const_index[0] + nir_src_as_uint(instr->src[0]));
5182 } else {
5183 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = vgrf(glsl_type::uint_type);
5184 bld.ADD(srcs[SURFACE_LOGICAL_SRC_ADDRESS],
5185 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
5186 brw_imm_ud(instr->const_index[0]));
5187 }
5188
5189 /* Emit the actual atomic operation operation */
5190
5191 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL,
5192 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5193 }
5194
5195 void
5196 fs_visitor::nir_emit_shared_atomic_float(const fs_builder &bld,
5197 int op, nir_intrinsic_instr *instr)
5198 {
5199 fs_reg dest;
5200 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5201 dest = get_nir_dest(instr->dest);
5202
5203 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5204 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
5205 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5206 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5207
5208 fs_reg data = get_nir_src(instr->src[1]);
5209 if (op == BRW_AOP_FCMPWR) {
5210 fs_reg tmp = bld.vgrf(data.type, 2);
5211 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5212 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5213 data = tmp;
5214 }
5215 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5216
5217 /* Get the offset */
5218 if (nir_src_is_const(instr->src[0])) {
5219 srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
5220 brw_imm_ud(instr->const_index[0] + nir_src_as_uint(instr->src[0]));
5221 } else {
5222 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = vgrf(glsl_type::uint_type);
5223 bld.ADD(srcs[SURFACE_LOGICAL_SRC_ADDRESS],
5224 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
5225 brw_imm_ud(instr->const_index[0]));
5226 }
5227
5228 /* Emit the actual atomic operation operation */
5229
5230 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL,
5231 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5232 }
5233
5234 void
5235 fs_visitor::nir_emit_global_atomic(const fs_builder &bld,
5236 int op, nir_intrinsic_instr *instr)
5237 {
5238 if (stage == MESA_SHADER_FRAGMENT)
5239 brw_wm_prog_data(prog_data)->has_side_effects = true;
5240
5241 fs_reg dest;
5242 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5243 dest = get_nir_dest(instr->dest);
5244
5245 fs_reg addr = get_nir_src(instr->src[0]);
5246
5247 fs_reg data;
5248 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
5249 data = get_nir_src(instr->src[1]);
5250
5251 if (op == BRW_AOP_CMPWR) {
5252 fs_reg tmp = bld.vgrf(data.type, 2);
5253 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5254 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5255 data = tmp;
5256 }
5257
5258 if (nir_dest_bit_size(instr->dest) == 64) {
5259 bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL,
5260 dest, addr, data, brw_imm_ud(op));
5261 } else {
5262 assert(nir_dest_bit_size(instr->dest) == 32);
5263 bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL,
5264 dest, addr, data, brw_imm_ud(op));
5265 }
5266 }
5267
5268 void
5269 fs_visitor::nir_emit_global_atomic_float(const fs_builder &bld,
5270 int op, nir_intrinsic_instr *instr)
5271 {
5272 if (stage == MESA_SHADER_FRAGMENT)
5273 brw_wm_prog_data(prog_data)->has_side_effects = true;
5274
5275 assert(nir_intrinsic_infos[instr->intrinsic].has_dest);
5276 fs_reg dest = get_nir_dest(instr->dest);
5277
5278 fs_reg addr = get_nir_src(instr->src[0]);
5279
5280 assert(op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC);
5281 fs_reg data = get_nir_src(instr->src[1]);
5282
5283 if (op == BRW_AOP_FCMPWR) {
5284 fs_reg tmp = bld.vgrf(data.type, 2);
5285 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5286 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5287 data = tmp;
5288 }
5289
5290 bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL,
5291 dest, addr, data, brw_imm_ud(op));
5292 }
5293
5294 void
5295 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
5296 {
5297 unsigned texture = instr->texture_index;
5298 unsigned sampler = instr->sampler_index;
5299
5300 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
5301
5302 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture);
5303 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(sampler);
5304
5305 int lod_components = 0;
5306
5307 /* The hardware requires a LOD for buffer textures */
5308 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
5309 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_d(0);
5310
5311 uint32_t header_bits = 0;
5312 for (unsigned i = 0; i < instr->num_srcs; i++) {
5313 fs_reg src = get_nir_src(instr->src[i].src);
5314 switch (instr->src[i].src_type) {
5315 case nir_tex_src_bias:
5316 srcs[TEX_LOGICAL_SRC_LOD] =
5317 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
5318 break;
5319 case nir_tex_src_comparator:
5320 srcs[TEX_LOGICAL_SRC_SHADOW_C] = retype(src, BRW_REGISTER_TYPE_F);
5321 break;
5322 case nir_tex_src_coord:
5323 switch (instr->op) {
5324 case nir_texop_txf:
5325 case nir_texop_txf_ms:
5326 case nir_texop_txf_ms_mcs:
5327 case nir_texop_samples_identical:
5328 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_D);
5329 break;
5330 default:
5331 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_F);
5332 break;
5333 }
5334 break;
5335 case nir_tex_src_ddx:
5336 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
5337 lod_components = nir_tex_instr_src_size(instr, i);
5338 break;
5339 case nir_tex_src_ddy:
5340 srcs[TEX_LOGICAL_SRC_LOD2] = retype(src, BRW_REGISTER_TYPE_F);
5341 break;
5342 case nir_tex_src_lod:
5343 switch (instr->op) {
5344 case nir_texop_txs:
5345 srcs[TEX_LOGICAL_SRC_LOD] =
5346 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_UD);
5347 break;
5348 case nir_texop_txf:
5349 srcs[TEX_LOGICAL_SRC_LOD] =
5350 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_D);
5351 break;
5352 default:
5353 srcs[TEX_LOGICAL_SRC_LOD] =
5354 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
5355 break;
5356 }
5357 break;
5358 case nir_tex_src_min_lod:
5359 srcs[TEX_LOGICAL_SRC_MIN_LOD] =
5360 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
5361 break;
5362 case nir_tex_src_ms_index:
5363 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = retype(src, BRW_REGISTER_TYPE_UD);
5364 break;
5365
5366 case nir_tex_src_offset: {
5367 uint32_t offset_bits = 0;
5368 if (brw_texture_offset(instr, i, &offset_bits)) {
5369 header_bits |= offset_bits;
5370 } else {
5371 srcs[TEX_LOGICAL_SRC_TG4_OFFSET] =
5372 retype(src, BRW_REGISTER_TYPE_D);
5373 }
5374 break;
5375 }
5376
5377 case nir_tex_src_projector:
5378 unreachable("should be lowered");
5379
5380 case nir_tex_src_texture_offset: {
5381 /* Emit code to evaluate the actual indexing expression */
5382 fs_reg tmp = vgrf(glsl_type::uint_type);
5383 bld.ADD(tmp, src, brw_imm_ud(texture));
5384 srcs[TEX_LOGICAL_SRC_SURFACE] = bld.emit_uniformize(tmp);
5385 break;
5386 }
5387
5388 case nir_tex_src_sampler_offset: {
5389 /* Emit code to evaluate the actual indexing expression */
5390 fs_reg tmp = vgrf(glsl_type::uint_type);
5391 bld.ADD(tmp, src, brw_imm_ud(sampler));
5392 srcs[TEX_LOGICAL_SRC_SAMPLER] = bld.emit_uniformize(tmp);
5393 break;
5394 }
5395
5396 case nir_tex_src_texture_handle:
5397 assert(nir_tex_instr_src_index(instr, nir_tex_src_texture_offset) == -1);
5398 srcs[TEX_LOGICAL_SRC_SURFACE] = fs_reg();
5399 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = bld.emit_uniformize(src);
5400 break;
5401
5402 case nir_tex_src_sampler_handle:
5403 assert(nir_tex_instr_src_index(instr, nir_tex_src_sampler_offset) == -1);
5404 srcs[TEX_LOGICAL_SRC_SAMPLER] = fs_reg();
5405 srcs[TEX_LOGICAL_SRC_SAMPLER_HANDLE] = bld.emit_uniformize(src);
5406 break;
5407
5408 case nir_tex_src_ms_mcs:
5409 assert(instr->op == nir_texop_txf_ms);
5410 srcs[TEX_LOGICAL_SRC_MCS] = retype(src, BRW_REGISTER_TYPE_D);
5411 break;
5412
5413 case nir_tex_src_plane: {
5414 const uint32_t plane = nir_src_as_uint(instr->src[i].src);
5415 const uint32_t texture_index =
5416 instr->texture_index +
5417 stage_prog_data->binding_table.plane_start[plane] -
5418 stage_prog_data->binding_table.texture_start;
5419
5420 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture_index);
5421 break;
5422 }
5423
5424 default:
5425 unreachable("unknown texture source");
5426 }
5427 }
5428
5429 if (srcs[TEX_LOGICAL_SRC_MCS].file == BAD_FILE &&
5430 (instr->op == nir_texop_txf_ms ||
5431 instr->op == nir_texop_samples_identical)) {
5432 if (devinfo->gen >= 7 &&
5433 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
5434 srcs[TEX_LOGICAL_SRC_MCS] =
5435 emit_mcs_fetch(srcs[TEX_LOGICAL_SRC_COORDINATE],
5436 instr->coord_components,
5437 srcs[TEX_LOGICAL_SRC_SURFACE],
5438 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE]);
5439 } else {
5440 srcs[TEX_LOGICAL_SRC_MCS] = brw_imm_ud(0u);
5441 }
5442 }
5443
5444 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(instr->coord_components);
5445 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(lod_components);
5446
5447 enum opcode opcode;
5448 switch (instr->op) {
5449 case nir_texop_tex:
5450 opcode = SHADER_OPCODE_TEX_LOGICAL;
5451 break;
5452 case nir_texop_txb:
5453 opcode = FS_OPCODE_TXB_LOGICAL;
5454 break;
5455 case nir_texop_txl:
5456 opcode = SHADER_OPCODE_TXL_LOGICAL;
5457 break;
5458 case nir_texop_txd:
5459 opcode = SHADER_OPCODE_TXD_LOGICAL;
5460 break;
5461 case nir_texop_txf:
5462 opcode = SHADER_OPCODE_TXF_LOGICAL;
5463 break;
5464 case nir_texop_txf_ms:
5465 if ((key_tex->msaa_16 & (1 << sampler)))
5466 opcode = SHADER_OPCODE_TXF_CMS_W_LOGICAL;
5467 else
5468 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
5469 break;
5470 case nir_texop_txf_ms_mcs:
5471 opcode = SHADER_OPCODE_TXF_MCS_LOGICAL;
5472 break;
5473 case nir_texop_query_levels:
5474 case nir_texop_txs:
5475 opcode = SHADER_OPCODE_TXS_LOGICAL;
5476 break;
5477 case nir_texop_lod:
5478 opcode = SHADER_OPCODE_LOD_LOGICAL;
5479 break;
5480 case nir_texop_tg4:
5481 if (srcs[TEX_LOGICAL_SRC_TG4_OFFSET].file != BAD_FILE)
5482 opcode = SHADER_OPCODE_TG4_OFFSET_LOGICAL;
5483 else
5484 opcode = SHADER_OPCODE_TG4_LOGICAL;
5485 break;
5486 case nir_texop_texture_samples:
5487 opcode = SHADER_OPCODE_SAMPLEINFO_LOGICAL;
5488 break;
5489 case nir_texop_samples_identical: {
5490 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
5491
5492 /* If mcs is an immediate value, it means there is no MCS. In that case
5493 * just return false.
5494 */
5495 if (srcs[TEX_LOGICAL_SRC_MCS].file == BRW_IMMEDIATE_VALUE) {
5496 bld.MOV(dst, brw_imm_ud(0u));
5497 } else if ((key_tex->msaa_16 & (1 << sampler))) {
5498 fs_reg tmp = vgrf(glsl_type::uint_type);
5499 bld.OR(tmp, srcs[TEX_LOGICAL_SRC_MCS],
5500 offset(srcs[TEX_LOGICAL_SRC_MCS], bld, 1));
5501 bld.CMP(dst, tmp, brw_imm_ud(0u), BRW_CONDITIONAL_EQ);
5502 } else {
5503 bld.CMP(dst, srcs[TEX_LOGICAL_SRC_MCS], brw_imm_ud(0u),
5504 BRW_CONDITIONAL_EQ);
5505 }
5506 return;
5507 }
5508 default:
5509 unreachable("unknown texture opcode");
5510 }
5511
5512 if (instr->op == nir_texop_tg4) {
5513 if (instr->component == 1 &&
5514 key_tex->gather_channel_quirk_mask & (1 << texture)) {
5515 /* gather4 sampler is broken for green channel on RG32F --
5516 * we must ask for blue instead.
5517 */
5518 header_bits |= 2 << 16;
5519 } else {
5520 header_bits |= instr->component << 16;
5521 }
5522 }
5523
5524 fs_reg dst = bld.vgrf(brw_type_for_nir_type(devinfo, instr->dest_type), 4);
5525 fs_inst *inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
5526 inst->offset = header_bits;
5527
5528 const unsigned dest_size = nir_tex_instr_dest_size(instr);
5529 if (devinfo->gen >= 9 &&
5530 instr->op != nir_texop_tg4 && instr->op != nir_texop_query_levels) {
5531 unsigned write_mask = instr->dest.is_ssa ?
5532 nir_ssa_def_components_read(&instr->dest.ssa):
5533 (1 << dest_size) - 1;
5534 assert(write_mask != 0); /* dead code should have been eliminated */
5535 inst->size_written = util_last_bit(write_mask) *
5536 inst->dst.component_size(inst->exec_size);
5537 } else {
5538 inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
5539 }
5540
5541 if (srcs[TEX_LOGICAL_SRC_SHADOW_C].file != BAD_FILE)
5542 inst->shadow_compare = true;
5543
5544 if (instr->op == nir_texop_tg4 && devinfo->gen == 6)
5545 emit_gen6_gather_wa(key_tex->gen6_gather_wa[texture], dst);
5546
5547 fs_reg nir_dest[4];
5548 for (unsigned i = 0; i < dest_size; i++)
5549 nir_dest[i] = offset(dst, bld, i);
5550
5551 if (instr->op == nir_texop_query_levels) {
5552 /* # levels is in .w */
5553 nir_dest[0] = offset(dst, bld, 3);
5554 } else if (instr->op == nir_texop_txs &&
5555 dest_size >= 3 && devinfo->gen < 7) {
5556 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
5557 fs_reg depth = offset(dst, bld, 2);
5558 nir_dest[2] = vgrf(glsl_type::int_type);
5559 bld.emit_minmax(nir_dest[2], depth, brw_imm_d(1), BRW_CONDITIONAL_GE);
5560 }
5561
5562 bld.LOAD_PAYLOAD(get_nir_dest(instr->dest), nir_dest, dest_size, 0);
5563 }
5564
5565 void
5566 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
5567 {
5568 switch (instr->type) {
5569 case nir_jump_break:
5570 bld.emit(BRW_OPCODE_BREAK);
5571 break;
5572 case nir_jump_continue:
5573 bld.emit(BRW_OPCODE_CONTINUE);
5574 break;
5575 case nir_jump_return:
5576 default:
5577 unreachable("unknown jump");
5578 }
5579 }
5580
5581 /*
5582 * This helper takes a source register and un/shuffles it into the destination
5583 * register.
5584 *
5585 * If source type size is smaller than destination type size the operation
5586 * needed is a component shuffle. The opposite case would be an unshuffle. If
5587 * source/destination type size is equal a shuffle is done that would be
5588 * equivalent to a simple MOV.
5589 *
5590 * For example, if source is a 16-bit type and destination is 32-bit. A 3
5591 * components .xyz 16-bit vector on SIMD8 would be.
5592 *
5593 * |x1|x2|x3|x4|x5|x6|x7|x8|y1|y2|y3|y4|y5|y6|y7|y8|
5594 * |z1|z2|z3|z4|z5|z6|z7|z8| | | | | | | | |
5595 *
5596 * This helper will return the following 2 32-bit components with the 16-bit
5597 * values shuffled:
5598 *
5599 * |x1 y1|x2 y2|x3 y3|x4 y4|x5 y5|x6 y6|x7 y7|x8 y8|
5600 * |z1 |z2 |z3 |z4 |z5 |z6 |z7 |z8 |
5601 *
5602 * For unshuffle, the example would be the opposite, a 64-bit type source
5603 * and a 32-bit destination. A 2 component .xy 64-bit vector on SIMD8
5604 * would be:
5605 *
5606 * | x1l x1h | x2l x2h | x3l x3h | x4l x4h |
5607 * | x5l x5h | x6l x6h | x7l x7h | x8l x8h |
5608 * | y1l y1h | y2l y2h | y3l y3h | y4l y4h |
5609 * | y5l y5h | y6l y6h | y7l y7h | y8l y8h |
5610 *
5611 * The returned result would be the following 4 32-bit components unshuffled:
5612 *
5613 * | x1l | x2l | x3l | x4l | x5l | x6l | x7l | x8l |
5614 * | x1h | x2h | x3h | x4h | x5h | x6h | x7h | x8h |
5615 * | y1l | y2l | y3l | y4l | y5l | y6l | y7l | y8l |
5616 * | y1h | y2h | y3h | y4h | y5h | y6h | y7h | y8h |
5617 *
5618 * - Source and destination register must not be overlapped.
5619 * - components units are measured in terms of the smaller type between
5620 * source and destination because we are un/shuffling the smaller
5621 * components from/into the bigger ones.
5622 * - first_component parameter allows skipping source components.
5623 */
5624 void
5625 shuffle_src_to_dst(const fs_builder &bld,
5626 const fs_reg &dst,
5627 const fs_reg &src,
5628 uint32_t first_component,
5629 uint32_t components)
5630 {
5631 if (type_sz(src.type) == type_sz(dst.type)) {
5632 assert(!regions_overlap(dst,
5633 type_sz(dst.type) * bld.dispatch_width() * components,
5634 offset(src, bld, first_component),
5635 type_sz(src.type) * bld.dispatch_width() * components));
5636 for (unsigned i = 0; i < components; i++) {
5637 bld.MOV(retype(offset(dst, bld, i), src.type),
5638 offset(src, bld, i + first_component));
5639 }
5640 } else if (type_sz(src.type) < type_sz(dst.type)) {
5641 /* Source is shuffled into destination */
5642 unsigned size_ratio = type_sz(dst.type) / type_sz(src.type);
5643 assert(!regions_overlap(dst,
5644 type_sz(dst.type) * bld.dispatch_width() *
5645 DIV_ROUND_UP(components, size_ratio),
5646 offset(src, bld, first_component),
5647 type_sz(src.type) * bld.dispatch_width() * components));
5648
5649 brw_reg_type shuffle_type =
5650 brw_reg_type_from_bit_size(8 * type_sz(src.type),
5651 BRW_REGISTER_TYPE_D);
5652 for (unsigned i = 0; i < components; i++) {
5653 fs_reg shuffle_component_i =
5654 subscript(offset(dst, bld, i / size_ratio),
5655 shuffle_type, i % size_ratio);
5656 bld.MOV(shuffle_component_i,
5657 retype(offset(src, bld, i + first_component), shuffle_type));
5658 }
5659 } else {
5660 /* Source is unshuffled into destination */
5661 unsigned size_ratio = type_sz(src.type) / type_sz(dst.type);
5662 assert(!regions_overlap(dst,
5663 type_sz(dst.type) * bld.dispatch_width() * components,
5664 offset(src, bld, first_component / size_ratio),
5665 type_sz(src.type) * bld.dispatch_width() *
5666 DIV_ROUND_UP(components + (first_component % size_ratio),
5667 size_ratio)));
5668
5669 brw_reg_type shuffle_type =
5670 brw_reg_type_from_bit_size(8 * type_sz(dst.type),
5671 BRW_REGISTER_TYPE_D);
5672 for (unsigned i = 0; i < components; i++) {
5673 fs_reg shuffle_component_i =
5674 subscript(offset(src, bld, (first_component + i) / size_ratio),
5675 shuffle_type, (first_component + i) % size_ratio);
5676 bld.MOV(retype(offset(dst, bld, i), shuffle_type),
5677 shuffle_component_i);
5678 }
5679 }
5680 }
5681
5682 void
5683 shuffle_from_32bit_read(const fs_builder &bld,
5684 const fs_reg &dst,
5685 const fs_reg &src,
5686 uint32_t first_component,
5687 uint32_t components)
5688 {
5689 assert(type_sz(src.type) == 4);
5690
5691 /* This function takes components in units of the destination type while
5692 * shuffle_src_to_dst takes components in units of the smallest type
5693 */
5694 if (type_sz(dst.type) > 4) {
5695 assert(type_sz(dst.type) == 8);
5696 first_component *= 2;
5697 components *= 2;
5698 }
5699
5700 shuffle_src_to_dst(bld, dst, src, first_component, components);
5701 }
5702
5703 fs_reg
5704 setup_imm_df(const fs_builder &bld, double v)
5705 {
5706 const struct gen_device_info *devinfo = bld.shader->devinfo;
5707 assert(devinfo->gen >= 7);
5708
5709 if (devinfo->gen >= 8)
5710 return brw_imm_df(v);
5711
5712 /* gen7.5 does not support DF immediates straighforward but the DIM
5713 * instruction allows to set the 64-bit immediate value.
5714 */
5715 if (devinfo->is_haswell) {
5716 const fs_builder ubld = bld.exec_all().group(1, 0);
5717 fs_reg dst = ubld.vgrf(BRW_REGISTER_TYPE_DF, 1);
5718 ubld.DIM(dst, brw_imm_df(v));
5719 return component(dst, 0);
5720 }
5721
5722 /* gen7 does not support DF immediates, so we generate a 64-bit constant by
5723 * writing the low 32-bit of the constant to suboffset 0 of a VGRF and
5724 * the high 32-bit to suboffset 4 and then applying a stride of 0.
5725 *
5726 * Alternatively, we could also produce a normal VGRF (without stride 0)
5727 * by writing to all the channels in the VGRF, however, that would hit the
5728 * gen7 bug where we have to split writes that span more than 1 register
5729 * into instructions with a width of 4 (otherwise the write to the second
5730 * register written runs into an execmask hardware bug) which isn't very
5731 * nice.
5732 */
5733 union {
5734 double d;
5735 struct {
5736 uint32_t i1;
5737 uint32_t i2;
5738 };
5739 } di;
5740
5741 di.d = v;
5742
5743 const fs_builder ubld = bld.exec_all().group(1, 0);
5744 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
5745 ubld.MOV(tmp, brw_imm_ud(di.i1));
5746 ubld.MOV(horiz_offset(tmp, 1), brw_imm_ud(di.i2));
5747
5748 return component(retype(tmp, BRW_REGISTER_TYPE_DF), 0);
5749 }
5750
5751 fs_reg
5752 setup_imm_b(const fs_builder &bld, int8_t v)
5753 {
5754 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_B);
5755 bld.MOV(tmp, brw_imm_w(v));
5756 return tmp;
5757 }
5758
5759 fs_reg
5760 setup_imm_ub(const fs_builder &bld, uint8_t v)
5761 {
5762 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UB);
5763 bld.MOV(tmp, brw_imm_uw(v));
5764 return tmp;
5765 }