intel/fs: Add Fall-through comment
[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 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1262 brw_rnd_mode rnd =
1263 brw_rnd_mode_from_execution_mode(execution_mode);
1264 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1265 brw_imm_d(rnd));
1266 }
1267
1268 inst = bld.MUL(result, op[0], op[1]);
1269 inst->saturate = instr->dest.saturate;
1270 break;
1271
1272 case nir_op_imul_2x32_64:
1273 case nir_op_umul_2x32_64:
1274 bld.MUL(result, op[0], op[1]);
1275 break;
1276
1277 case nir_op_imul:
1278 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1279 bld.MUL(result, op[0], op[1]);
1280 break;
1281
1282 case nir_op_imul_high:
1283 case nir_op_umul_high:
1284 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1285 bld.emit(SHADER_OPCODE_MULH, result, op[0], op[1]);
1286 break;
1287
1288 case nir_op_idiv:
1289 case nir_op_udiv:
1290 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1291 bld.emit(SHADER_OPCODE_INT_QUOTIENT, result, op[0], op[1]);
1292 break;
1293
1294 case nir_op_uadd_carry:
1295 unreachable("Should have been lowered by carry_to_arith().");
1296
1297 case nir_op_usub_borrow:
1298 unreachable("Should have been lowered by borrow_to_arith().");
1299
1300 case nir_op_umod:
1301 case nir_op_irem:
1302 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1303 * appears that our hardware just does the right thing for signed
1304 * remainder.
1305 */
1306 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1307 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
1308 break;
1309
1310 case nir_op_imod: {
1311 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1312 bld.emit(SHADER_OPCODE_INT_REMAINDER, result, op[0], op[1]);
1313
1314 /* Math instructions don't support conditional mod */
1315 inst = bld.MOV(bld.null_reg_d(), result);
1316 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1317
1318 /* Now, we need to determine if signs of the sources are different.
1319 * When we XOR the sources, the top bit is 0 if they are the same and 1
1320 * if they are different. We can then use a conditional modifier to
1321 * turn that into a predicate. This leads us to an XOR.l instruction.
1322 *
1323 * Technically, according to the PRM, you're not allowed to use .l on a
1324 * XOR instruction. However, emperical experiments and Curro's reading
1325 * of the simulator source both indicate that it's safe.
1326 */
1327 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_D);
1328 inst = bld.XOR(tmp, op[0], op[1]);
1329 inst->predicate = BRW_PREDICATE_NORMAL;
1330 inst->conditional_mod = BRW_CONDITIONAL_L;
1331
1332 /* If the result of the initial remainder operation is non-zero and the
1333 * two sources have different signs, add in a copy of op[1] to get the
1334 * final integer modulus value.
1335 */
1336 inst = bld.ADD(result, result, op[1]);
1337 inst->predicate = BRW_PREDICATE_NORMAL;
1338 break;
1339 }
1340
1341 case nir_op_flt32:
1342 case nir_op_fge32:
1343 case nir_op_feq32:
1344 case nir_op_fne32: {
1345 fs_reg dest = result;
1346
1347 const uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
1348 if (bit_size != 32)
1349 dest = bld.vgrf(op[0].type, 1);
1350
1351 bld.CMP(dest, op[0], op[1], brw_cmod_for_nir_comparison(instr->op));
1352
1353 if (bit_size > 32) {
1354 bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
1355 } else if(bit_size < 32) {
1356 /* When we convert the result to 32-bit we need to be careful and do
1357 * it as a signed conversion to get sign extension (for 32-bit true)
1358 */
1359 const brw_reg_type src_type =
1360 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_D);
1361
1362 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), retype(dest, src_type));
1363 }
1364 break;
1365 }
1366
1367 case nir_op_ilt32:
1368 case nir_op_ult32:
1369 case nir_op_ige32:
1370 case nir_op_uge32:
1371 case nir_op_ieq32:
1372 case nir_op_ine32: {
1373 fs_reg dest = result;
1374
1375 /* On Gen11 we have an additional issue being that src1 cannot be a byte
1376 * type. So we convert both operands for the comparison.
1377 */
1378 fs_reg temp_op[2];
1379 temp_op[0] = bld.fix_byte_src(op[0]);
1380 temp_op[1] = bld.fix_byte_src(op[1]);
1381
1382 const uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
1383 if (bit_size != 32)
1384 dest = bld.vgrf(temp_op[0].type, 1);
1385
1386 bld.CMP(dest, temp_op[0], temp_op[1],
1387 brw_cmod_for_nir_comparison(instr->op));
1388
1389 if (bit_size > 32) {
1390 bld.MOV(result, subscript(dest, BRW_REGISTER_TYPE_UD, 0));
1391 } else if (bit_size < 32) {
1392 /* When we convert the result to 32-bit we need to be careful and do
1393 * it as a signed conversion to get sign extension (for 32-bit true)
1394 */
1395 const brw_reg_type src_type =
1396 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_D);
1397
1398 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), retype(dest, src_type));
1399 }
1400 break;
1401 }
1402
1403 case nir_op_inot:
1404 if (devinfo->gen >= 8) {
1405 nir_alu_instr *inot_src_instr = nir_src_as_alu_instr(instr->src[0].src);
1406
1407 if (inot_src_instr != NULL &&
1408 (inot_src_instr->op == nir_op_ior ||
1409 inot_src_instr->op == nir_op_ixor ||
1410 inot_src_instr->op == nir_op_iand) &&
1411 !inot_src_instr->src[0].abs &&
1412 !inot_src_instr->src[0].negate &&
1413 !inot_src_instr->src[1].abs &&
1414 !inot_src_instr->src[1].negate) {
1415 /* The sources of the source logical instruction are now the
1416 * sources of the instruction that will be generated.
1417 */
1418 prepare_alu_destination_and_sources(bld, inot_src_instr, op, false);
1419 resolve_inot_sources(bld, inot_src_instr, op);
1420
1421 /* Smash all of the sources and destination to be signed. This
1422 * doesn't matter for the operation of the instruction, but cmod
1423 * propagation fails on unsigned sources with negation (due to
1424 * fs_inst::can_do_cmod returning false).
1425 */
1426 result.type =
1427 brw_type_for_nir_type(devinfo,
1428 (nir_alu_type)(nir_type_int |
1429 nir_dest_bit_size(instr->dest.dest)));
1430 op[0].type =
1431 brw_type_for_nir_type(devinfo,
1432 (nir_alu_type)(nir_type_int |
1433 nir_src_bit_size(inot_src_instr->src[0].src)));
1434 op[1].type =
1435 brw_type_for_nir_type(devinfo,
1436 (nir_alu_type)(nir_type_int |
1437 nir_src_bit_size(inot_src_instr->src[1].src)));
1438
1439 /* For XOR, only invert one of the sources. Arbitrarily choose
1440 * the first source.
1441 */
1442 op[0].negate = !op[0].negate;
1443 if (inot_src_instr->op != nir_op_ixor)
1444 op[1].negate = !op[1].negate;
1445
1446 switch (inot_src_instr->op) {
1447 case nir_op_ior:
1448 bld.AND(result, op[0], op[1]);
1449 return;
1450
1451 case nir_op_iand:
1452 bld.OR(result, op[0], op[1]);
1453 return;
1454
1455 case nir_op_ixor:
1456 bld.XOR(result, op[0], op[1]);
1457 return;
1458
1459 default:
1460 unreachable("impossible opcode");
1461 }
1462 }
1463 op[0] = resolve_source_modifiers(op[0]);
1464 }
1465 bld.NOT(result, op[0]);
1466 break;
1467 case nir_op_ixor:
1468 if (devinfo->gen >= 8) {
1469 resolve_inot_sources(bld, instr, op);
1470 }
1471 bld.XOR(result, op[0], op[1]);
1472 break;
1473 case nir_op_ior:
1474 if (devinfo->gen >= 8) {
1475 resolve_inot_sources(bld, instr, op);
1476 }
1477 bld.OR(result, op[0], op[1]);
1478 break;
1479 case nir_op_iand:
1480 if (devinfo->gen >= 8) {
1481 resolve_inot_sources(bld, instr, op);
1482 }
1483 bld.AND(result, op[0], op[1]);
1484 break;
1485
1486 case nir_op_fdot2:
1487 case nir_op_fdot3:
1488 case nir_op_fdot4:
1489 case nir_op_b32all_fequal2:
1490 case nir_op_b32all_iequal2:
1491 case nir_op_b32all_fequal3:
1492 case nir_op_b32all_iequal3:
1493 case nir_op_b32all_fequal4:
1494 case nir_op_b32all_iequal4:
1495 case nir_op_b32any_fnequal2:
1496 case nir_op_b32any_inequal2:
1497 case nir_op_b32any_fnequal3:
1498 case nir_op_b32any_inequal3:
1499 case nir_op_b32any_fnequal4:
1500 case nir_op_b32any_inequal4:
1501 unreachable("Lowered by nir_lower_alu_reductions");
1502
1503 case nir_op_fnoise1_1:
1504 case nir_op_fnoise1_2:
1505 case nir_op_fnoise1_3:
1506 case nir_op_fnoise1_4:
1507 case nir_op_fnoise2_1:
1508 case nir_op_fnoise2_2:
1509 case nir_op_fnoise2_3:
1510 case nir_op_fnoise2_4:
1511 case nir_op_fnoise3_1:
1512 case nir_op_fnoise3_2:
1513 case nir_op_fnoise3_3:
1514 case nir_op_fnoise3_4:
1515 case nir_op_fnoise4_1:
1516 case nir_op_fnoise4_2:
1517 case nir_op_fnoise4_3:
1518 case nir_op_fnoise4_4:
1519 unreachable("not reached: should be handled by lower_noise");
1520
1521 case nir_op_ldexp:
1522 unreachable("not reached: should be handled by ldexp_to_arith()");
1523
1524 case nir_op_fsqrt:
1525 inst = bld.emit(SHADER_OPCODE_SQRT, result, op[0]);
1526 inst->saturate = instr->dest.saturate;
1527 break;
1528
1529 case nir_op_frsq:
1530 inst = bld.emit(SHADER_OPCODE_RSQ, result, op[0]);
1531 inst->saturate = instr->dest.saturate;
1532 break;
1533
1534 case nir_op_i2b32:
1535 case nir_op_f2b32: {
1536 uint32_t bit_size = nir_src_bit_size(instr->src[0].src);
1537 if (bit_size == 64) {
1538 /* two-argument instructions can't take 64-bit immediates */
1539 fs_reg zero;
1540 fs_reg tmp;
1541
1542 if (instr->op == nir_op_f2b32) {
1543 zero = vgrf(glsl_type::double_type);
1544 tmp = vgrf(glsl_type::double_type);
1545 bld.MOV(zero, setup_imm_df(bld, 0.0));
1546 } else {
1547 zero = vgrf(glsl_type::int64_t_type);
1548 tmp = vgrf(glsl_type::int64_t_type);
1549 bld.MOV(zero, brw_imm_q(0));
1550 }
1551
1552 /* A SIMD16 execution needs to be split in two instructions, so use
1553 * a vgrf instead of the flag register as dst so instruction splitting
1554 * works
1555 */
1556 bld.CMP(tmp, op[0], zero, BRW_CONDITIONAL_NZ);
1557 bld.MOV(result, subscript(tmp, BRW_REGISTER_TYPE_UD, 0));
1558 } else {
1559 fs_reg zero;
1560 if (bit_size == 32) {
1561 zero = instr->op == nir_op_f2b32 ? brw_imm_f(0.0f) : brw_imm_d(0);
1562 } else {
1563 assert(bit_size == 16);
1564 zero = instr->op == nir_op_f2b32 ?
1565 retype(brw_imm_w(0), BRW_REGISTER_TYPE_HF) : brw_imm_w(0);
1566 }
1567 bld.CMP(result, op[0], zero, BRW_CONDITIONAL_NZ);
1568 }
1569 break;
1570 }
1571
1572 case nir_op_ftrunc:
1573 inst = bld.RNDZ(result, op[0]);
1574 inst->saturate = instr->dest.saturate;
1575 break;
1576
1577 case nir_op_fceil: {
1578 op[0].negate = !op[0].negate;
1579 fs_reg temp = vgrf(glsl_type::float_type);
1580 bld.RNDD(temp, op[0]);
1581 temp.negate = true;
1582 inst = bld.MOV(result, temp);
1583 inst->saturate = instr->dest.saturate;
1584 break;
1585 }
1586 case nir_op_ffloor:
1587 inst = bld.RNDD(result, op[0]);
1588 inst->saturate = instr->dest.saturate;
1589 break;
1590 case nir_op_ffract:
1591 inst = bld.FRC(result, op[0]);
1592 inst->saturate = instr->dest.saturate;
1593 break;
1594 case nir_op_fround_even:
1595 inst = bld.RNDE(result, op[0]);
1596 inst->saturate = instr->dest.saturate;
1597 break;
1598
1599 case nir_op_fquantize2f16: {
1600 fs_reg tmp16 = bld.vgrf(BRW_REGISTER_TYPE_D);
1601 fs_reg tmp32 = bld.vgrf(BRW_REGISTER_TYPE_F);
1602 fs_reg zero = bld.vgrf(BRW_REGISTER_TYPE_F);
1603
1604 /* The destination stride must be at least as big as the source stride. */
1605 tmp16.type = BRW_REGISTER_TYPE_W;
1606 tmp16.stride = 2;
1607
1608 /* Check for denormal */
1609 fs_reg abs_src0 = op[0];
1610 abs_src0.abs = true;
1611 bld.CMP(bld.null_reg_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1612 BRW_CONDITIONAL_L);
1613 /* Get the appropriately signed zero */
1614 bld.AND(retype(zero, BRW_REGISTER_TYPE_UD),
1615 retype(op[0], BRW_REGISTER_TYPE_UD),
1616 brw_imm_ud(0x80000000));
1617 /* Do the actual F32 -> F16 -> F32 conversion */
1618 bld.emit(BRW_OPCODE_F32TO16, tmp16, op[0]);
1619 bld.emit(BRW_OPCODE_F16TO32, tmp32, tmp16);
1620 /* Select that or zero based on normal status */
1621 inst = bld.SEL(result, zero, tmp32);
1622 inst->predicate = BRW_PREDICATE_NORMAL;
1623 inst->saturate = instr->dest.saturate;
1624 break;
1625 }
1626
1627 case nir_op_imin:
1628 case nir_op_umin:
1629 case nir_op_fmin:
1630 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_L);
1631 inst->saturate = instr->dest.saturate;
1632 break;
1633
1634 case nir_op_imax:
1635 case nir_op_umax:
1636 case nir_op_fmax:
1637 inst = bld.emit_minmax(result, op[0], op[1], BRW_CONDITIONAL_GE);
1638 inst->saturate = instr->dest.saturate;
1639 break;
1640
1641 case nir_op_pack_snorm_2x16:
1642 case nir_op_pack_snorm_4x8:
1643 case nir_op_pack_unorm_2x16:
1644 case nir_op_pack_unorm_4x8:
1645 case nir_op_unpack_snorm_2x16:
1646 case nir_op_unpack_snorm_4x8:
1647 case nir_op_unpack_unorm_2x16:
1648 case nir_op_unpack_unorm_4x8:
1649 case nir_op_unpack_half_2x16:
1650 case nir_op_pack_half_2x16:
1651 unreachable("not reached: should be handled by lower_packing_builtins");
1652
1653 case nir_op_unpack_half_2x16_split_x_flush_to_zero:
1654 assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 & execution_mode);
1655 /* Fall-through */
1656 case nir_op_unpack_half_2x16_split_x:
1657 inst = bld.emit(BRW_OPCODE_F16TO32, result,
1658 subscript(op[0], BRW_REGISTER_TYPE_UW, 0));
1659 inst->saturate = instr->dest.saturate;
1660 break;
1661
1662 case nir_op_unpack_half_2x16_split_y_flush_to_zero:
1663 assert(FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16 & execution_mode);
1664 /* Fall-through */
1665 case nir_op_unpack_half_2x16_split_y:
1666 inst = bld.emit(BRW_OPCODE_F16TO32, result,
1667 subscript(op[0], BRW_REGISTER_TYPE_UW, 1));
1668 inst->saturate = instr->dest.saturate;
1669 break;
1670
1671 case nir_op_pack_64_2x32_split:
1672 case nir_op_pack_32_2x16_split:
1673 bld.emit(FS_OPCODE_PACK, result, op[0], op[1]);
1674 break;
1675
1676 case nir_op_unpack_64_2x32_split_x:
1677 case nir_op_unpack_64_2x32_split_y: {
1678 if (instr->op == nir_op_unpack_64_2x32_split_x)
1679 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 0));
1680 else
1681 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UD, 1));
1682 break;
1683 }
1684
1685 case nir_op_unpack_32_2x16_split_x:
1686 case nir_op_unpack_32_2x16_split_y: {
1687 if (instr->op == nir_op_unpack_32_2x16_split_x)
1688 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UW, 0));
1689 else
1690 bld.MOV(result, subscript(op[0], BRW_REGISTER_TYPE_UW, 1));
1691 break;
1692 }
1693
1694 case nir_op_fpow:
1695 inst = bld.emit(SHADER_OPCODE_POW, result, op[0], op[1]);
1696 inst->saturate = instr->dest.saturate;
1697 break;
1698
1699 case nir_op_bitfield_reverse:
1700 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1701 bld.BFREV(result, op[0]);
1702 break;
1703
1704 case nir_op_bit_count:
1705 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1706 bld.CBIT(result, op[0]);
1707 break;
1708
1709 case nir_op_ufind_msb: {
1710 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1711 emit_find_msb_using_lzd(bld, result, op[0], false);
1712 break;
1713 }
1714
1715 case nir_op_ifind_msb: {
1716 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1717
1718 if (devinfo->gen < 7) {
1719 emit_find_msb_using_lzd(bld, result, op[0], true);
1720 } else {
1721 bld.FBH(retype(result, BRW_REGISTER_TYPE_UD), op[0]);
1722
1723 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1724 * count from the LSB side. If FBH didn't return an error
1725 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1726 * count into an LSB count.
1727 */
1728 bld.CMP(bld.null_reg_d(), result, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1729
1730 inst = bld.ADD(result, result, brw_imm_d(31));
1731 inst->predicate = BRW_PREDICATE_NORMAL;
1732 inst->src[0].negate = true;
1733 }
1734 break;
1735 }
1736
1737 case nir_op_find_lsb:
1738 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1739
1740 if (devinfo->gen < 7) {
1741 fs_reg temp = vgrf(glsl_type::int_type);
1742
1743 /* (x & -x) generates a value that consists of only the LSB of x.
1744 * For all powers of 2, findMSB(y) == findLSB(y).
1745 */
1746 fs_reg src = retype(op[0], BRW_REGISTER_TYPE_D);
1747 fs_reg negated_src = src;
1748
1749 /* One must be negated, and the other must be non-negated. It
1750 * doesn't matter which is which.
1751 */
1752 negated_src.negate = true;
1753 src.negate = false;
1754
1755 bld.AND(temp, src, negated_src);
1756 emit_find_msb_using_lzd(bld, result, temp, false);
1757 } else {
1758 bld.FBL(result, op[0]);
1759 }
1760 break;
1761
1762 case nir_op_ubitfield_extract:
1763 case nir_op_ibitfield_extract:
1764 unreachable("should have been lowered");
1765 case nir_op_ubfe:
1766 case nir_op_ibfe:
1767 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1768 bld.BFE(result, op[2], op[1], op[0]);
1769 break;
1770 case nir_op_bfm:
1771 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1772 bld.BFI1(result, op[0], op[1]);
1773 break;
1774 case nir_op_bfi:
1775 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1776 bld.BFI2(result, op[0], op[1], op[2]);
1777 break;
1778
1779 case nir_op_bitfield_insert:
1780 unreachable("not reached: should have been lowered");
1781
1782 case nir_op_ishl:
1783 bld.SHL(result, op[0], op[1]);
1784 break;
1785 case nir_op_ishr:
1786 bld.ASR(result, op[0], op[1]);
1787 break;
1788 case nir_op_ushr:
1789 bld.SHR(result, op[0], op[1]);
1790 break;
1791
1792 case nir_op_urol:
1793 bld.ROL(result, op[0], op[1]);
1794 break;
1795 case nir_op_uror:
1796 bld.ROR(result, op[0], op[1]);
1797 break;
1798
1799 case nir_op_pack_half_2x16_split:
1800 bld.emit(FS_OPCODE_PACK_HALF_2x16_SPLIT, result, op[0], op[1]);
1801 break;
1802
1803 case nir_op_ffma:
1804 if (nir_has_any_rounding_mode_enabled(execution_mode)) {
1805 brw_rnd_mode rnd =
1806 brw_rnd_mode_from_execution_mode(execution_mode);
1807 bld.emit(SHADER_OPCODE_RND_MODE, bld.null_reg_ud(),
1808 brw_imm_d(rnd));
1809 }
1810
1811 inst = bld.MAD(result, op[2], op[1], op[0]);
1812 inst->saturate = instr->dest.saturate;
1813 break;
1814
1815 case nir_op_flrp:
1816 inst = bld.LRP(result, op[0], op[1], op[2]);
1817 inst->saturate = instr->dest.saturate;
1818 break;
1819
1820 case nir_op_b32csel:
1821 if (optimize_frontfacing_ternary(instr, result))
1822 return;
1823
1824 bld.CMP(bld.null_reg_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ);
1825 inst = bld.SEL(result, op[1], op[2]);
1826 inst->predicate = BRW_PREDICATE_NORMAL;
1827 break;
1828
1829 case nir_op_extract_u8:
1830 case nir_op_extract_i8: {
1831 unsigned byte = nir_src_as_uint(instr->src[1].src);
1832
1833 /* The PRMs say:
1834 *
1835 * BDW+
1836 * There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.
1837 * Use two instructions and a word or DWord intermediate integer type.
1838 */
1839 if (nir_dest_bit_size(instr->dest.dest) == 64) {
1840 const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
1841
1842 if (instr->op == nir_op_extract_i8) {
1843 /* If we need to sign extend, extract to a word first */
1844 fs_reg w_temp = bld.vgrf(BRW_REGISTER_TYPE_W);
1845 bld.MOV(w_temp, subscript(op[0], type, byte));
1846 bld.MOV(result, w_temp);
1847 } else if (byte & 1) {
1848 /* Extract the high byte from the word containing the desired byte
1849 * offset.
1850 */
1851 bld.SHR(result,
1852 subscript(op[0], BRW_REGISTER_TYPE_UW, byte / 2),
1853 brw_imm_uw(8));
1854 } else {
1855 /* Otherwise use an AND with 0xff and a word type */
1856 bld.AND(result,
1857 subscript(op[0], BRW_REGISTER_TYPE_UW, byte / 2),
1858 brw_imm_uw(0xff));
1859 }
1860 } else {
1861 const brw_reg_type type = brw_int_type(1, instr->op == nir_op_extract_i8);
1862 bld.MOV(result, subscript(op[0], type, byte));
1863 }
1864 break;
1865 }
1866
1867 case nir_op_extract_u16:
1868 case nir_op_extract_i16: {
1869 const brw_reg_type type = brw_int_type(2, instr->op == nir_op_extract_i16);
1870 unsigned word = nir_src_as_uint(instr->src[1].src);
1871 bld.MOV(result, subscript(op[0], type, word));
1872 break;
1873 }
1874
1875 default:
1876 unreachable("unhandled instruction");
1877 }
1878
1879 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1880 * to sign extend the low bit to 0/~0
1881 */
1882 if (devinfo->gen <= 5 &&
1883 !result.is_null() &&
1884 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) == BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1885 fs_reg masked = vgrf(glsl_type::int_type);
1886 bld.AND(masked, result, brw_imm_d(1));
1887 masked.negate = true;
1888 bld.MOV(retype(result, BRW_REGISTER_TYPE_D), masked);
1889 }
1890 }
1891
1892 void
1893 fs_visitor::nir_emit_load_const(const fs_builder &bld,
1894 nir_load_const_instr *instr)
1895 {
1896 const brw_reg_type reg_type =
1897 brw_reg_type_from_bit_size(instr->def.bit_size, BRW_REGISTER_TYPE_D);
1898 fs_reg reg = bld.vgrf(reg_type, instr->def.num_components);
1899
1900 switch (instr->def.bit_size) {
1901 case 8:
1902 for (unsigned i = 0; i < instr->def.num_components; i++)
1903 bld.MOV(offset(reg, bld, i), setup_imm_b(bld, instr->value[i].i8));
1904 break;
1905
1906 case 16:
1907 for (unsigned i = 0; i < instr->def.num_components; i++)
1908 bld.MOV(offset(reg, bld, i), brw_imm_w(instr->value[i].i16));
1909 break;
1910
1911 case 32:
1912 for (unsigned i = 0; i < instr->def.num_components; i++)
1913 bld.MOV(offset(reg, bld, i), brw_imm_d(instr->value[i].i32));
1914 break;
1915
1916 case 64:
1917 assert(devinfo->gen >= 7);
1918 if (devinfo->gen == 7) {
1919 /* We don't get 64-bit integer types until gen8 */
1920 for (unsigned i = 0; i < instr->def.num_components; i++) {
1921 bld.MOV(retype(offset(reg, bld, i), BRW_REGISTER_TYPE_DF),
1922 setup_imm_df(bld, instr->value[i].f64));
1923 }
1924 } else {
1925 for (unsigned i = 0; i < instr->def.num_components; i++)
1926 bld.MOV(offset(reg, bld, i), brw_imm_q(instr->value[i].i64));
1927 }
1928 break;
1929
1930 default:
1931 unreachable("Invalid bit size");
1932 }
1933
1934 nir_ssa_values[instr->def.index] = reg;
1935 }
1936
1937 fs_reg
1938 fs_visitor::get_nir_src(const nir_src &src)
1939 {
1940 fs_reg reg;
1941 if (src.is_ssa) {
1942 if (src.ssa->parent_instr->type == nir_instr_type_ssa_undef) {
1943 const brw_reg_type reg_type =
1944 brw_reg_type_from_bit_size(src.ssa->bit_size, BRW_REGISTER_TYPE_D);
1945 reg = bld.vgrf(reg_type, src.ssa->num_components);
1946 } else {
1947 reg = nir_ssa_values[src.ssa->index];
1948 }
1949 } else {
1950 /* We don't handle indirects on locals */
1951 assert(src.reg.indirect == NULL);
1952 reg = offset(nir_locals[src.reg.reg->index], bld,
1953 src.reg.base_offset * src.reg.reg->num_components);
1954 }
1955
1956 if (nir_src_bit_size(src) == 64 && devinfo->gen == 7) {
1957 /* The only 64-bit type available on gen7 is DF, so use that. */
1958 reg.type = BRW_REGISTER_TYPE_DF;
1959 } else {
1960 /* To avoid floating-point denorm flushing problems, set the type by
1961 * default to an integer type - instructions that need floating point
1962 * semantics will set this to F if they need to
1963 */
1964 reg.type = brw_reg_type_from_bit_size(nir_src_bit_size(src),
1965 BRW_REGISTER_TYPE_D);
1966 }
1967
1968 return reg;
1969 }
1970
1971 /**
1972 * Return an IMM for constants; otherwise call get_nir_src() as normal.
1973 *
1974 * This function should not be called on any value which may be 64 bits.
1975 * We could theoretically support 64-bit on gen8+ but we choose not to
1976 * because it wouldn't work in general (no gen7 support) and there are
1977 * enough restrictions in 64-bit immediates that you can't take the return
1978 * value and treat it the same as the result of get_nir_src().
1979 */
1980 fs_reg
1981 fs_visitor::get_nir_src_imm(const nir_src &src)
1982 {
1983 assert(nir_src_bit_size(src) == 32);
1984 return nir_src_is_const(src) ?
1985 fs_reg(brw_imm_d(nir_src_as_int(src))) : get_nir_src(src);
1986 }
1987
1988 fs_reg
1989 fs_visitor::get_nir_dest(const nir_dest &dest)
1990 {
1991 if (dest.is_ssa) {
1992 const brw_reg_type reg_type =
1993 brw_reg_type_from_bit_size(dest.ssa.bit_size,
1994 dest.ssa.bit_size == 8 ?
1995 BRW_REGISTER_TYPE_D :
1996 BRW_REGISTER_TYPE_F);
1997 nir_ssa_values[dest.ssa.index] =
1998 bld.vgrf(reg_type, dest.ssa.num_components);
1999 bld.UNDEF(nir_ssa_values[dest.ssa.index]);
2000 return nir_ssa_values[dest.ssa.index];
2001 } else {
2002 /* We don't handle indirects on locals */
2003 assert(dest.reg.indirect == NULL);
2004 return offset(nir_locals[dest.reg.reg->index], bld,
2005 dest.reg.base_offset * dest.reg.reg->num_components);
2006 }
2007 }
2008
2009 void
2010 fs_visitor::emit_percomp(const fs_builder &bld, const fs_inst &inst,
2011 unsigned wr_mask)
2012 {
2013 for (unsigned i = 0; i < 4; i++) {
2014 if (!((wr_mask >> i) & 1))
2015 continue;
2016
2017 fs_inst *new_inst = new(mem_ctx) fs_inst(inst);
2018 new_inst->dst = offset(new_inst->dst, bld, i);
2019 for (unsigned j = 0; j < new_inst->sources; j++)
2020 if (new_inst->src[j].file == VGRF)
2021 new_inst->src[j] = offset(new_inst->src[j], bld, i);
2022
2023 bld.emit(new_inst);
2024 }
2025 }
2026
2027 static fs_inst *
2028 emit_pixel_interpolater_send(const fs_builder &bld,
2029 enum opcode opcode,
2030 const fs_reg &dst,
2031 const fs_reg &src,
2032 const fs_reg &desc,
2033 glsl_interp_mode interpolation)
2034 {
2035 struct brw_wm_prog_data *wm_prog_data =
2036 brw_wm_prog_data(bld.shader->stage_prog_data);
2037
2038 fs_inst *inst = bld.emit(opcode, dst, src, desc);
2039 /* 2 floats per slot returned */
2040 inst->size_written = 2 * dst.component_size(inst->exec_size);
2041 inst->pi_noperspective = interpolation == INTERP_MODE_NOPERSPECTIVE;
2042
2043 wm_prog_data->pulls_bary = true;
2044
2045 return inst;
2046 }
2047
2048 /**
2049 * Computes 1 << x, given a D/UD register containing some value x.
2050 */
2051 static fs_reg
2052 intexp2(const fs_builder &bld, const fs_reg &x)
2053 {
2054 assert(x.type == BRW_REGISTER_TYPE_UD || x.type == BRW_REGISTER_TYPE_D);
2055
2056 fs_reg result = bld.vgrf(x.type, 1);
2057 fs_reg one = bld.vgrf(x.type, 1);
2058
2059 bld.MOV(one, retype(brw_imm_d(1), one.type));
2060 bld.SHL(result, one, x);
2061 return result;
2062 }
2063
2064 void
2065 fs_visitor::emit_gs_end_primitive(const nir_src &vertex_count_nir_src)
2066 {
2067 assert(stage == MESA_SHADER_GEOMETRY);
2068
2069 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2070
2071 if (gs_compile->control_data_header_size_bits == 0)
2072 return;
2073
2074 /* We can only do EndPrimitive() functionality when the control data
2075 * consists of cut bits. Fortunately, the only time it isn't is when the
2076 * output type is points, in which case EndPrimitive() is a no-op.
2077 */
2078 if (gs_prog_data->control_data_format !=
2079 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_CUT) {
2080 return;
2081 }
2082
2083 /* Cut bits use one bit per vertex. */
2084 assert(gs_compile->control_data_bits_per_vertex == 1);
2085
2086 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
2087 vertex_count.type = BRW_REGISTER_TYPE_UD;
2088
2089 /* Cut bit n should be set to 1 if EndPrimitive() was called after emitting
2090 * vertex n, 0 otherwise. So all we need to do here is mark bit
2091 * (vertex_count - 1) % 32 in the cut_bits register to indicate that
2092 * EndPrimitive() was called after emitting vertex (vertex_count - 1);
2093 * vec4_gs_visitor::emit_control_data_bits() will take care of the rest.
2094 *
2095 * Note that if EndPrimitive() is called before emitting any vertices, this
2096 * will cause us to set bit 31 of the control_data_bits register to 1.
2097 * That's fine because:
2098 *
2099 * - If max_vertices < 32, then vertex number 31 (zero-based) will never be
2100 * output, so the hardware will ignore cut bit 31.
2101 *
2102 * - If max_vertices == 32, then vertex number 31 is guaranteed to be the
2103 * last vertex, so setting cut bit 31 has no effect (since the primitive
2104 * is automatically ended when the GS terminates).
2105 *
2106 * - If max_vertices > 32, then the ir_emit_vertex visitor will reset the
2107 * control_data_bits register to 0 when the first vertex is emitted.
2108 */
2109
2110 const fs_builder abld = bld.annotate("end primitive");
2111
2112 /* control_data_bits |= 1 << ((vertex_count - 1) % 32) */
2113 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2114 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
2115 fs_reg mask = intexp2(abld, prev_count);
2116 /* Note: we're relying on the fact that the GEN SHL instruction only pays
2117 * attention to the lower 5 bits of its second source argument, so on this
2118 * architecture, 1 << (vertex_count - 1) is equivalent to 1 <<
2119 * ((vertex_count - 1) % 32).
2120 */
2121 abld.OR(this->control_data_bits, this->control_data_bits, mask);
2122 }
2123
2124 void
2125 fs_visitor::emit_gs_control_data_bits(const fs_reg &vertex_count)
2126 {
2127 assert(stage == MESA_SHADER_GEOMETRY);
2128 assert(gs_compile->control_data_bits_per_vertex != 0);
2129
2130 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2131
2132 const fs_builder abld = bld.annotate("emit control data bits");
2133 const fs_builder fwa_bld = bld.exec_all();
2134
2135 /* We use a single UD register to accumulate control data bits (32 bits
2136 * for each of the SIMD8 channels). So we need to write a DWord (32 bits)
2137 * at a time.
2138 *
2139 * Unfortunately, the URB_WRITE_SIMD8 message uses 128-bit (OWord) offsets.
2140 * We have select a 128-bit group via the Global and Per-Slot Offsets, then
2141 * use the Channel Mask phase to enable/disable which DWord within that
2142 * group to write. (Remember, different SIMD8 channels may have emitted
2143 * different numbers of vertices, so we may need per-slot offsets.)
2144 *
2145 * Channel masking presents an annoying problem: we may have to replicate
2146 * the data up to 4 times:
2147 *
2148 * Msg = Handles, Per-Slot Offsets, Channel Masks, Data, Data, Data, Data.
2149 *
2150 * To avoid penalizing shaders that emit a small number of vertices, we
2151 * can avoid these sometimes: if the size of the control data header is
2152 * <= 128 bits, then there is only 1 OWord. All SIMD8 channels will land
2153 * land in the same 128-bit group, so we can skip per-slot offsets.
2154 *
2155 * Similarly, if the control data header is <= 32 bits, there is only one
2156 * DWord, so we can skip channel masks.
2157 */
2158 enum opcode opcode = SHADER_OPCODE_URB_WRITE_SIMD8;
2159
2160 fs_reg channel_mask, per_slot_offset;
2161
2162 if (gs_compile->control_data_header_size_bits > 32) {
2163 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2164 channel_mask = vgrf(glsl_type::uint_type);
2165 }
2166
2167 if (gs_compile->control_data_header_size_bits > 128) {
2168 opcode = SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT;
2169 per_slot_offset = vgrf(glsl_type::uint_type);
2170 }
2171
2172 /* Figure out which DWord we're trying to write to using the formula:
2173 *
2174 * dword_index = (vertex_count - 1) * bits_per_vertex / 32
2175 *
2176 * Since bits_per_vertex is a power of two, and is known at compile
2177 * time, this can be optimized to:
2178 *
2179 * dword_index = (vertex_count - 1) >> (6 - log2(bits_per_vertex))
2180 */
2181 if (opcode != SHADER_OPCODE_URB_WRITE_SIMD8) {
2182 fs_reg dword_index = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2183 fs_reg prev_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2184 abld.ADD(prev_count, vertex_count, brw_imm_ud(0xffffffffu));
2185 unsigned log2_bits_per_vertex =
2186 util_last_bit(gs_compile->control_data_bits_per_vertex);
2187 abld.SHR(dword_index, prev_count, brw_imm_ud(6u - log2_bits_per_vertex));
2188
2189 if (per_slot_offset.file != BAD_FILE) {
2190 /* Set the per-slot offset to dword_index / 4, so that we'll write to
2191 * the appropriate OWord within the control data header.
2192 */
2193 abld.SHR(per_slot_offset, dword_index, brw_imm_ud(2u));
2194 }
2195
2196 /* Set the channel masks to 1 << (dword_index % 4), so that we'll
2197 * write to the appropriate DWORD within the OWORD.
2198 */
2199 fs_reg channel = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2200 fwa_bld.AND(channel, dword_index, brw_imm_ud(3u));
2201 channel_mask = intexp2(fwa_bld, channel);
2202 /* Then the channel masks need to be in bits 23:16. */
2203 fwa_bld.SHL(channel_mask, channel_mask, brw_imm_ud(16u));
2204 }
2205
2206 /* Store the control data bits in the message payload and send it. */
2207 unsigned mlen = 2;
2208 if (channel_mask.file != BAD_FILE)
2209 mlen += 4; /* channel masks, plus 3 extra copies of the data */
2210 if (per_slot_offset.file != BAD_FILE)
2211 mlen++;
2212
2213 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2214 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, mlen);
2215 unsigned i = 0;
2216 sources[i++] = fs_reg(retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD));
2217 if (per_slot_offset.file != BAD_FILE)
2218 sources[i++] = per_slot_offset;
2219 if (channel_mask.file != BAD_FILE)
2220 sources[i++] = channel_mask;
2221 while (i < mlen) {
2222 sources[i++] = this->control_data_bits;
2223 }
2224
2225 abld.LOAD_PAYLOAD(payload, sources, mlen, mlen);
2226 fs_inst *inst = abld.emit(opcode, reg_undef, payload);
2227 inst->mlen = mlen;
2228 /* We need to increment Global Offset by 256-bits to make room for
2229 * Broadwell's extra "Vertex Count" payload at the beginning of the
2230 * URB entry. Since this is an OWord message, Global Offset is counted
2231 * in 128-bit units, so we must set it to 2.
2232 */
2233 if (gs_prog_data->static_vertex_count == -1)
2234 inst->offset = 2;
2235 }
2236
2237 void
2238 fs_visitor::set_gs_stream_control_data_bits(const fs_reg &vertex_count,
2239 unsigned stream_id)
2240 {
2241 /* control_data_bits |= stream_id << ((2 * (vertex_count - 1)) % 32) */
2242
2243 /* Note: we are calling this *before* increasing vertex_count, so
2244 * this->vertex_count == vertex_count - 1 in the formula above.
2245 */
2246
2247 /* Stream mode uses 2 bits per vertex */
2248 assert(gs_compile->control_data_bits_per_vertex == 2);
2249
2250 /* Must be a valid stream */
2251 assert(stream_id < MAX_VERTEX_STREAMS);
2252
2253 /* Control data bits are initialized to 0 so we don't have to set any
2254 * bits when sending vertices to stream 0.
2255 */
2256 if (stream_id == 0)
2257 return;
2258
2259 const fs_builder abld = bld.annotate("set stream control data bits", NULL);
2260
2261 /* reg::sid = stream_id */
2262 fs_reg sid = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2263 abld.MOV(sid, brw_imm_ud(stream_id));
2264
2265 /* reg:shift_count = 2 * (vertex_count - 1) */
2266 fs_reg shift_count = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2267 abld.SHL(shift_count, vertex_count, brw_imm_ud(1u));
2268
2269 /* Note: we're relying on the fact that the GEN SHL instruction only pays
2270 * attention to the lower 5 bits of its second source argument, so on this
2271 * architecture, stream_id << 2 * (vertex_count - 1) is equivalent to
2272 * stream_id << ((2 * (vertex_count - 1)) % 32).
2273 */
2274 fs_reg mask = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2275 abld.SHL(mask, sid, shift_count);
2276 abld.OR(this->control_data_bits, this->control_data_bits, mask);
2277 }
2278
2279 void
2280 fs_visitor::emit_gs_vertex(const nir_src &vertex_count_nir_src,
2281 unsigned stream_id)
2282 {
2283 assert(stage == MESA_SHADER_GEOMETRY);
2284
2285 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2286
2287 fs_reg vertex_count = get_nir_src(vertex_count_nir_src);
2288 vertex_count.type = BRW_REGISTER_TYPE_UD;
2289
2290 /* Haswell and later hardware ignores the "Render Stream Select" bits
2291 * from the 3DSTATE_STREAMOUT packet when the SOL stage is disabled,
2292 * and instead sends all primitives down the pipeline for rasterization.
2293 * If the SOL stage is enabled, "Render Stream Select" is honored and
2294 * primitives bound to non-zero streams are discarded after stream output.
2295 *
2296 * Since the only purpose of primives sent to non-zero streams is to
2297 * be recorded by transform feedback, we can simply discard all geometry
2298 * bound to these streams when transform feedback is disabled.
2299 */
2300 if (stream_id > 0 && !nir->info.has_transform_feedback_varyings)
2301 return;
2302
2303 /* If we're outputting 32 control data bits or less, then we can wait
2304 * until the shader is over to output them all. Otherwise we need to
2305 * output them as we go. Now is the time to do it, since we're about to
2306 * output the vertex_count'th vertex, so it's guaranteed that the
2307 * control data bits associated with the (vertex_count - 1)th vertex are
2308 * correct.
2309 */
2310 if (gs_compile->control_data_header_size_bits > 32) {
2311 const fs_builder abld =
2312 bld.annotate("emit vertex: emit control data bits");
2313
2314 /* Only emit control data bits if we've finished accumulating a batch
2315 * of 32 bits. This is the case when:
2316 *
2317 * (vertex_count * bits_per_vertex) % 32 == 0
2318 *
2319 * (in other words, when the last 5 bits of vertex_count *
2320 * bits_per_vertex are 0). Assuming bits_per_vertex == 2^n for some
2321 * integer n (which is always the case, since bits_per_vertex is
2322 * always 1 or 2), this is equivalent to requiring that the last 5-n
2323 * bits of vertex_count are 0:
2324 *
2325 * vertex_count & (2^(5-n) - 1) == 0
2326 *
2327 * 2^(5-n) == 2^5 / 2^n == 32 / bits_per_vertex, so this is
2328 * equivalent to:
2329 *
2330 * vertex_count & (32 / bits_per_vertex - 1) == 0
2331 *
2332 * TODO: If vertex_count is an immediate, we could do some of this math
2333 * at compile time...
2334 */
2335 fs_inst *inst =
2336 abld.AND(bld.null_reg_d(), vertex_count,
2337 brw_imm_ud(32u / gs_compile->control_data_bits_per_vertex - 1u));
2338 inst->conditional_mod = BRW_CONDITIONAL_Z;
2339
2340 abld.IF(BRW_PREDICATE_NORMAL);
2341 /* If vertex_count is 0, then no control data bits have been
2342 * accumulated yet, so we can skip emitting them.
2343 */
2344 abld.CMP(bld.null_reg_d(), vertex_count, brw_imm_ud(0u),
2345 BRW_CONDITIONAL_NEQ);
2346 abld.IF(BRW_PREDICATE_NORMAL);
2347 emit_gs_control_data_bits(vertex_count);
2348 abld.emit(BRW_OPCODE_ENDIF);
2349
2350 /* Reset control_data_bits to 0 so we can start accumulating a new
2351 * batch.
2352 *
2353 * Note: in the case where vertex_count == 0, this neutralizes the
2354 * effect of any call to EndPrimitive() that the shader may have
2355 * made before outputting its first vertex.
2356 */
2357 inst = abld.MOV(this->control_data_bits, brw_imm_ud(0u));
2358 inst->force_writemask_all = true;
2359 abld.emit(BRW_OPCODE_ENDIF);
2360 }
2361
2362 emit_urb_writes(vertex_count);
2363
2364 /* In stream mode we have to set control data bits for all vertices
2365 * unless we have disabled control data bits completely (which we do
2366 * do for GL_POINTS outputs that don't use streams).
2367 */
2368 if (gs_compile->control_data_header_size_bits > 0 &&
2369 gs_prog_data->control_data_format ==
2370 GEN7_GS_CONTROL_DATA_FORMAT_GSCTL_SID) {
2371 set_gs_stream_control_data_bits(vertex_count, stream_id);
2372 }
2373 }
2374
2375 void
2376 fs_visitor::emit_gs_input_load(const fs_reg &dst,
2377 const nir_src &vertex_src,
2378 unsigned base_offset,
2379 const nir_src &offset_src,
2380 unsigned num_components,
2381 unsigned first_component)
2382 {
2383 assert(type_sz(dst.type) == 4);
2384 struct brw_gs_prog_data *gs_prog_data = brw_gs_prog_data(prog_data);
2385 const unsigned push_reg_count = gs_prog_data->base.urb_read_length * 8;
2386
2387 /* TODO: figure out push input layout for invocations == 1 */
2388 if (gs_prog_data->invocations == 1 &&
2389 nir_src_is_const(offset_src) && nir_src_is_const(vertex_src) &&
2390 4 * (base_offset + nir_src_as_uint(offset_src)) < push_reg_count) {
2391 int imm_offset = (base_offset + nir_src_as_uint(offset_src)) * 4 +
2392 nir_src_as_uint(vertex_src) * push_reg_count;
2393 for (unsigned i = 0; i < num_components; i++) {
2394 bld.MOV(offset(dst, bld, i),
2395 fs_reg(ATTR, imm_offset + i + first_component, dst.type));
2396 }
2397 return;
2398 }
2399
2400 /* Resort to the pull model. Ensure the VUE handles are provided. */
2401 assert(gs_prog_data->base.include_vue_handles);
2402
2403 unsigned first_icp_handle = gs_prog_data->include_primitive_id ? 3 : 2;
2404 fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2405
2406 if (gs_prog_data->invocations == 1) {
2407 if (nir_src_is_const(vertex_src)) {
2408 /* The vertex index is constant; just select the proper URB handle. */
2409 icp_handle =
2410 retype(brw_vec8_grf(first_icp_handle + nir_src_as_uint(vertex_src), 0),
2411 BRW_REGISTER_TYPE_UD);
2412 } else {
2413 /* The vertex index is non-constant. We need to use indirect
2414 * addressing to fetch the proper URB handle.
2415 *
2416 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2417 * indicating that channel <n> should read the handle from
2418 * DWord <n>. We convert that to bytes by multiplying by 4.
2419 *
2420 * Next, we convert the vertex index to bytes by multiplying
2421 * by 32 (shifting by 5), and add the two together. This is
2422 * the final indirect byte offset.
2423 */
2424 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_UW, 1);
2425 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2426 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2427 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2428
2429 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2430 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
2431 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2432 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
2433 /* Convert vertex_index to bytes (multiply by 32) */
2434 bld.SHL(vertex_offset_bytes,
2435 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2436 brw_imm_ud(5u));
2437 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
2438
2439 /* Use first_icp_handle as the base offset. There is one register
2440 * of URB handles per vertex, so inform the register allocator that
2441 * we might read up to nir->info.gs.vertices_in registers.
2442 */
2443 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2444 retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
2445 fs_reg(icp_offset_bytes),
2446 brw_imm_ud(nir->info.gs.vertices_in * REG_SIZE));
2447 }
2448 } else {
2449 assert(gs_prog_data->invocations > 1);
2450
2451 if (nir_src_is_const(vertex_src)) {
2452 unsigned vertex = nir_src_as_uint(vertex_src);
2453 assert(devinfo->gen >= 9 || vertex <= 5);
2454 bld.MOV(icp_handle,
2455 retype(brw_vec1_grf(first_icp_handle + vertex / 8, vertex % 8),
2456 BRW_REGISTER_TYPE_UD));
2457 } else {
2458 /* The vertex index is non-constant. We need to use indirect
2459 * addressing to fetch the proper URB handle.
2460 *
2461 */
2462 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2463
2464 /* Convert vertex_index to bytes (multiply by 4) */
2465 bld.SHL(icp_offset_bytes,
2466 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2467 brw_imm_ud(2u));
2468
2469 /* Use first_icp_handle as the base offset. There is one DWord
2470 * of URB handles per vertex, so inform the register allocator that
2471 * we might read up to ceil(nir->info.gs.vertices_in / 8) registers.
2472 */
2473 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2474 retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
2475 fs_reg(icp_offset_bytes),
2476 brw_imm_ud(DIV_ROUND_UP(nir->info.gs.vertices_in, 8) *
2477 REG_SIZE));
2478 }
2479 }
2480
2481 fs_inst *inst;
2482 fs_reg indirect_offset = get_nir_src(offset_src);
2483
2484 if (nir_src_is_const(offset_src)) {
2485 /* Constant indexing - use global offset. */
2486 if (first_component != 0) {
2487 unsigned read_components = num_components + first_component;
2488 fs_reg tmp = bld.vgrf(dst.type, read_components);
2489 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
2490 inst->size_written = read_components *
2491 tmp.component_size(inst->exec_size);
2492 for (unsigned i = 0; i < num_components; i++) {
2493 bld.MOV(offset(dst, bld, i),
2494 offset(tmp, bld, i + first_component));
2495 }
2496 } else {
2497 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2498 inst->size_written = num_components *
2499 dst.component_size(inst->exec_size);
2500 }
2501 inst->offset = base_offset + nir_src_as_uint(offset_src);
2502 inst->mlen = 1;
2503 } else {
2504 /* Indirect indexing - use per-slot offsets as well. */
2505 const fs_reg srcs[] = { icp_handle, indirect_offset };
2506 unsigned read_components = num_components + first_component;
2507 fs_reg tmp = bld.vgrf(dst.type, read_components);
2508 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2509 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2510 if (first_component != 0) {
2511 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2512 payload);
2513 inst->size_written = read_components *
2514 tmp.component_size(inst->exec_size);
2515 for (unsigned i = 0; i < num_components; i++) {
2516 bld.MOV(offset(dst, bld, i),
2517 offset(tmp, bld, i + first_component));
2518 }
2519 } else {
2520 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst, payload);
2521 inst->size_written = num_components *
2522 dst.component_size(inst->exec_size);
2523 }
2524 inst->offset = base_offset;
2525 inst->mlen = 2;
2526 }
2527 }
2528
2529 fs_reg
2530 fs_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
2531 {
2532 nir_src *offset_src = nir_get_io_offset_src(instr);
2533
2534 if (nir_src_is_const(*offset_src)) {
2535 /* The only constant offset we should find is 0. brw_nir.c's
2536 * add_const_offset_to_base() will fold other constant offsets
2537 * into instr->const_index[0].
2538 */
2539 assert(nir_src_as_uint(*offset_src) == 0);
2540 return fs_reg();
2541 }
2542
2543 return get_nir_src(*offset_src);
2544 }
2545
2546 void
2547 fs_visitor::nir_emit_vs_intrinsic(const fs_builder &bld,
2548 nir_intrinsic_instr *instr)
2549 {
2550 assert(stage == MESA_SHADER_VERTEX);
2551
2552 fs_reg dest;
2553 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2554 dest = get_nir_dest(instr->dest);
2555
2556 switch (instr->intrinsic) {
2557 case nir_intrinsic_load_vertex_id:
2558 case nir_intrinsic_load_base_vertex:
2559 unreachable("should be lowered by nir_lower_system_values()");
2560
2561 case nir_intrinsic_load_input: {
2562 assert(nir_dest_bit_size(instr->dest) == 32);
2563 fs_reg src = fs_reg(ATTR, nir_intrinsic_base(instr) * 4, dest.type);
2564 src = offset(src, bld, nir_intrinsic_component(instr));
2565 src = offset(src, bld, nir_src_as_uint(instr->src[0]));
2566
2567 for (unsigned i = 0; i < instr->num_components; i++)
2568 bld.MOV(offset(dest, bld, i), offset(src, bld, i));
2569 break;
2570 }
2571
2572 case nir_intrinsic_load_vertex_id_zero_base:
2573 case nir_intrinsic_load_instance_id:
2574 case nir_intrinsic_load_base_instance:
2575 case nir_intrinsic_load_draw_id:
2576 case nir_intrinsic_load_first_vertex:
2577 case nir_intrinsic_load_is_indexed_draw:
2578 unreachable("lowered by brw_nir_lower_vs_inputs");
2579
2580 default:
2581 nir_emit_intrinsic(bld, instr);
2582 break;
2583 }
2584 }
2585
2586 fs_reg
2587 fs_visitor::get_tcs_single_patch_icp_handle(const fs_builder &bld,
2588 nir_intrinsic_instr *instr)
2589 {
2590 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
2591 const nir_src &vertex_src = instr->src[0];
2592 nir_intrinsic_instr *vertex_intrin = nir_src_as_intrinsic(vertex_src);
2593 fs_reg icp_handle;
2594
2595 if (nir_src_is_const(vertex_src)) {
2596 /* Emit a MOV to resolve <0,1,0> regioning. */
2597 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2598 unsigned vertex = nir_src_as_uint(vertex_src);
2599 bld.MOV(icp_handle,
2600 retype(brw_vec1_grf(1 + (vertex >> 3), vertex & 7),
2601 BRW_REGISTER_TYPE_UD));
2602 } else if (tcs_prog_data->instances == 1 && vertex_intrin &&
2603 vertex_intrin->intrinsic == nir_intrinsic_load_invocation_id) {
2604 /* For the common case of only 1 instance, an array index of
2605 * gl_InvocationID means reading g1. Skip all the indirect work.
2606 */
2607 icp_handle = retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2608 } else {
2609 /* The vertex index is non-constant. We need to use indirect
2610 * addressing to fetch the proper URB handle.
2611 */
2612 icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2613
2614 /* Each ICP handle is a single DWord (4 bytes) */
2615 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2616 bld.SHL(vertex_offset_bytes,
2617 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2618 brw_imm_ud(2u));
2619
2620 /* Start at g1. We might read up to 4 registers. */
2621 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2622 retype(brw_vec8_grf(1, 0), icp_handle.type), vertex_offset_bytes,
2623 brw_imm_ud(4 * REG_SIZE));
2624 }
2625
2626 return icp_handle;
2627 }
2628
2629 fs_reg
2630 fs_visitor::get_tcs_eight_patch_icp_handle(const fs_builder &bld,
2631 nir_intrinsic_instr *instr)
2632 {
2633 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
2634 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
2635 const nir_src &vertex_src = instr->src[0];
2636
2637 unsigned first_icp_handle = tcs_prog_data->include_primitive_id ? 3 : 2;
2638
2639 if (nir_src_is_const(vertex_src)) {
2640 return fs_reg(retype(brw_vec8_grf(first_icp_handle +
2641 nir_src_as_uint(vertex_src), 0),
2642 BRW_REGISTER_TYPE_UD));
2643 }
2644
2645 /* The vertex index is non-constant. We need to use indirect
2646 * addressing to fetch the proper URB handle.
2647 *
2648 * First, we start with the sequence <7, 6, 5, 4, 3, 2, 1, 0>
2649 * indicating that channel <n> should read the handle from
2650 * DWord <n>. We convert that to bytes by multiplying by 4.
2651 *
2652 * Next, we convert the vertex index to bytes by multiplying
2653 * by 32 (shifting by 5), and add the two together. This is
2654 * the final indirect byte offset.
2655 */
2656 fs_reg icp_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2657 fs_reg sequence = bld.vgrf(BRW_REGISTER_TYPE_UW, 1);
2658 fs_reg channel_offsets = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2659 fs_reg vertex_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2660 fs_reg icp_offset_bytes = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2661
2662 /* sequence = <7, 6, 5, 4, 3, 2, 1, 0> */
2663 bld.MOV(sequence, fs_reg(brw_imm_v(0x76543210)));
2664 /* channel_offsets = 4 * sequence = <28, 24, 20, 16, 12, 8, 4, 0> */
2665 bld.SHL(channel_offsets, sequence, brw_imm_ud(2u));
2666 /* Convert vertex_index to bytes (multiply by 32) */
2667 bld.SHL(vertex_offset_bytes,
2668 retype(get_nir_src(vertex_src), BRW_REGISTER_TYPE_UD),
2669 brw_imm_ud(5u));
2670 bld.ADD(icp_offset_bytes, vertex_offset_bytes, channel_offsets);
2671
2672 /* Use first_icp_handle as the base offset. There is one register
2673 * of URB handles per vertex, so inform the register allocator that
2674 * we might read up to nir->info.gs.vertices_in registers.
2675 */
2676 bld.emit(SHADER_OPCODE_MOV_INDIRECT, icp_handle,
2677 retype(brw_vec8_grf(first_icp_handle, 0), icp_handle.type),
2678 icp_offset_bytes, brw_imm_ud(tcs_key->input_vertices * REG_SIZE));
2679
2680 return icp_handle;
2681 }
2682
2683 struct brw_reg
2684 fs_visitor::get_tcs_output_urb_handle()
2685 {
2686 struct brw_vue_prog_data *vue_prog_data = brw_vue_prog_data(prog_data);
2687
2688 if (vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_SINGLE_PATCH) {
2689 return retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD);
2690 } else {
2691 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH);
2692 return retype(brw_vec8_grf(1, 0), BRW_REGISTER_TYPE_UD);
2693 }
2694 }
2695
2696 void
2697 fs_visitor::nir_emit_tcs_intrinsic(const fs_builder &bld,
2698 nir_intrinsic_instr *instr)
2699 {
2700 assert(stage == MESA_SHADER_TESS_CTRL);
2701 struct brw_tcs_prog_key *tcs_key = (struct brw_tcs_prog_key *) key;
2702 struct brw_tcs_prog_data *tcs_prog_data = brw_tcs_prog_data(prog_data);
2703 struct brw_vue_prog_data *vue_prog_data = &tcs_prog_data->base;
2704
2705 bool eight_patch =
2706 vue_prog_data->dispatch_mode == DISPATCH_MODE_TCS_8_PATCH;
2707
2708 fs_reg dst;
2709 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2710 dst = get_nir_dest(instr->dest);
2711
2712 switch (instr->intrinsic) {
2713 case nir_intrinsic_load_primitive_id:
2714 bld.MOV(dst, fs_reg(eight_patch ? brw_vec8_grf(2, 0)
2715 : brw_vec1_grf(0, 1)));
2716 break;
2717 case nir_intrinsic_load_invocation_id:
2718 bld.MOV(retype(dst, invocation_id.type), invocation_id);
2719 break;
2720 case nir_intrinsic_load_patch_vertices_in:
2721 bld.MOV(retype(dst, BRW_REGISTER_TYPE_D),
2722 brw_imm_d(tcs_key->input_vertices));
2723 break;
2724
2725 case nir_intrinsic_barrier: {
2726 if (tcs_prog_data->instances == 1)
2727 break;
2728
2729 fs_reg m0 = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2730 fs_reg m0_2 = component(m0, 2);
2731
2732 const fs_builder chanbld = bld.exec_all().group(1, 0);
2733
2734 /* Zero the message header */
2735 bld.exec_all().MOV(m0, brw_imm_ud(0u));
2736
2737 if (devinfo->gen < 11) {
2738 /* Copy "Barrier ID" from r0.2, bits 16:13 */
2739 chanbld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
2740 brw_imm_ud(INTEL_MASK(16, 13)));
2741
2742 /* Shift it up to bits 27:24. */
2743 chanbld.SHL(m0_2, m0_2, brw_imm_ud(11));
2744 } else {
2745 chanbld.AND(m0_2, retype(brw_vec1_grf(0, 2), BRW_REGISTER_TYPE_UD),
2746 brw_imm_ud(INTEL_MASK(30, 24)));
2747 }
2748
2749 /* Set the Barrier Count and the enable bit */
2750 if (devinfo->gen < 11) {
2751 chanbld.OR(m0_2, m0_2,
2752 brw_imm_ud(tcs_prog_data->instances << 9 | (1 << 15)));
2753 } else {
2754 chanbld.OR(m0_2, m0_2,
2755 brw_imm_ud(tcs_prog_data->instances << 8 | (1 << 15)));
2756 }
2757
2758 bld.emit(SHADER_OPCODE_BARRIER, bld.null_reg_ud(), m0);
2759 break;
2760 }
2761
2762 case nir_intrinsic_load_input:
2763 unreachable("nir_lower_io should never give us these.");
2764 break;
2765
2766 case nir_intrinsic_load_per_vertex_input: {
2767 assert(nir_dest_bit_size(instr->dest) == 32);
2768 fs_reg indirect_offset = get_indirect_offset(instr);
2769 unsigned imm_offset = instr->const_index[0];
2770 fs_inst *inst;
2771
2772 fs_reg icp_handle =
2773 eight_patch ? get_tcs_eight_patch_icp_handle(bld, instr)
2774 : get_tcs_single_patch_icp_handle(bld, instr);
2775
2776 /* We can only read two double components with each URB read, so
2777 * we send two read messages in that case, each one loading up to
2778 * two double components.
2779 */
2780 unsigned num_components = instr->num_components;
2781 unsigned first_component = nir_intrinsic_component(instr);
2782
2783 if (indirect_offset.file == BAD_FILE) {
2784 /* Constant indexing - use global offset. */
2785 if (first_component != 0) {
2786 unsigned read_components = num_components + first_component;
2787 fs_reg tmp = bld.vgrf(dst.type, read_components);
2788 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp, icp_handle);
2789 for (unsigned i = 0; i < num_components; i++) {
2790 bld.MOV(offset(dst, bld, i),
2791 offset(tmp, bld, i + first_component));
2792 }
2793 } else {
2794 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst, icp_handle);
2795 }
2796 inst->offset = imm_offset;
2797 inst->mlen = 1;
2798 } else {
2799 /* Indirect indexing - use per-slot offsets as well. */
2800 const fs_reg srcs[] = { icp_handle, indirect_offset };
2801 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2802 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2803 if (first_component != 0) {
2804 unsigned read_components = num_components + first_component;
2805 fs_reg tmp = bld.vgrf(dst.type, read_components);
2806 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2807 payload);
2808 for (unsigned i = 0; i < num_components; i++) {
2809 bld.MOV(offset(dst, bld, i),
2810 offset(tmp, bld, i + first_component));
2811 }
2812 } else {
2813 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
2814 payload);
2815 }
2816 inst->offset = imm_offset;
2817 inst->mlen = 2;
2818 }
2819 inst->size_written = (num_components + first_component) *
2820 inst->dst.component_size(inst->exec_size);
2821
2822 /* Copy the temporary to the destination to deal with writemasking.
2823 *
2824 * Also attempt to deal with gl_PointSize being in the .w component.
2825 */
2826 if (inst->offset == 0 && indirect_offset.file == BAD_FILE) {
2827 assert(type_sz(dst.type) == 4);
2828 inst->dst = bld.vgrf(dst.type, 4);
2829 inst->size_written = 4 * REG_SIZE;
2830 bld.MOV(dst, offset(inst->dst, bld, 3));
2831 }
2832 break;
2833 }
2834
2835 case nir_intrinsic_load_output:
2836 case nir_intrinsic_load_per_vertex_output: {
2837 assert(nir_dest_bit_size(instr->dest) == 32);
2838 fs_reg indirect_offset = get_indirect_offset(instr);
2839 unsigned imm_offset = instr->const_index[0];
2840 unsigned first_component = nir_intrinsic_component(instr);
2841
2842 struct brw_reg output_handles = get_tcs_output_urb_handle();
2843
2844 fs_inst *inst;
2845 if (indirect_offset.file == BAD_FILE) {
2846 /* This MOV replicates the output handle to all enabled channels
2847 * is SINGLE_PATCH mode.
2848 */
2849 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
2850 bld.MOV(patch_handle, output_handles);
2851
2852 {
2853 if (first_component != 0) {
2854 unsigned read_components =
2855 instr->num_components + first_component;
2856 fs_reg tmp = bld.vgrf(dst.type, read_components);
2857 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
2858 patch_handle);
2859 inst->size_written = read_components * REG_SIZE;
2860 for (unsigned i = 0; i < instr->num_components; i++) {
2861 bld.MOV(offset(dst, bld, i),
2862 offset(tmp, bld, i + first_component));
2863 }
2864 } else {
2865 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dst,
2866 patch_handle);
2867 inst->size_written = instr->num_components * REG_SIZE;
2868 }
2869 inst->offset = imm_offset;
2870 inst->mlen = 1;
2871 }
2872 } else {
2873 /* Indirect indexing - use per-slot offsets as well. */
2874 const fs_reg srcs[] = { output_handles, indirect_offset };
2875 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
2876 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
2877 if (first_component != 0) {
2878 unsigned read_components =
2879 instr->num_components + first_component;
2880 fs_reg tmp = bld.vgrf(dst.type, read_components);
2881 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
2882 payload);
2883 inst->size_written = read_components * REG_SIZE;
2884 for (unsigned i = 0; i < instr->num_components; i++) {
2885 bld.MOV(offset(dst, bld, i),
2886 offset(tmp, bld, i + first_component));
2887 }
2888 } else {
2889 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dst,
2890 payload);
2891 inst->size_written = instr->num_components * REG_SIZE;
2892 }
2893 inst->offset = imm_offset;
2894 inst->mlen = 2;
2895 }
2896 break;
2897 }
2898
2899 case nir_intrinsic_store_output:
2900 case nir_intrinsic_store_per_vertex_output: {
2901 assert(nir_src_bit_size(instr->src[0]) == 32);
2902 fs_reg value = get_nir_src(instr->src[0]);
2903 fs_reg indirect_offset = get_indirect_offset(instr);
2904 unsigned imm_offset = instr->const_index[0];
2905 unsigned mask = instr->const_index[1];
2906 unsigned header_regs = 0;
2907 struct brw_reg output_handles = get_tcs_output_urb_handle();
2908
2909 fs_reg srcs[7];
2910 srcs[header_regs++] = output_handles;
2911
2912 if (indirect_offset.file != BAD_FILE) {
2913 srcs[header_regs++] = indirect_offset;
2914 }
2915
2916 if (mask == 0)
2917 break;
2918
2919 unsigned num_components = util_last_bit(mask);
2920 enum opcode opcode;
2921
2922 /* We can only pack two 64-bit components in a single message, so send
2923 * 2 messages if we have more components
2924 */
2925 unsigned first_component = nir_intrinsic_component(instr);
2926 mask = mask << first_component;
2927
2928 if (mask != WRITEMASK_XYZW) {
2929 srcs[header_regs++] = brw_imm_ud(mask << 16);
2930 opcode = indirect_offset.file != BAD_FILE ?
2931 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED_PER_SLOT :
2932 SHADER_OPCODE_URB_WRITE_SIMD8_MASKED;
2933 } else {
2934 opcode = indirect_offset.file != BAD_FILE ?
2935 SHADER_OPCODE_URB_WRITE_SIMD8_PER_SLOT :
2936 SHADER_OPCODE_URB_WRITE_SIMD8;
2937 }
2938
2939 for (unsigned i = 0; i < num_components; i++) {
2940 if (!(mask & (1 << (i + first_component))))
2941 continue;
2942
2943 srcs[header_regs + i + first_component] = offset(value, bld, i);
2944 }
2945
2946 unsigned mlen = header_regs + num_components + first_component;
2947 fs_reg payload =
2948 bld.vgrf(BRW_REGISTER_TYPE_UD, mlen);
2949 bld.LOAD_PAYLOAD(payload, srcs, mlen, header_regs);
2950
2951 fs_inst *inst = bld.emit(opcode, bld.null_reg_ud(), payload);
2952 inst->offset = imm_offset;
2953 inst->mlen = mlen;
2954 break;
2955 }
2956
2957 default:
2958 nir_emit_intrinsic(bld, instr);
2959 break;
2960 }
2961 }
2962
2963 void
2964 fs_visitor::nir_emit_tes_intrinsic(const fs_builder &bld,
2965 nir_intrinsic_instr *instr)
2966 {
2967 assert(stage == MESA_SHADER_TESS_EVAL);
2968 struct brw_tes_prog_data *tes_prog_data = brw_tes_prog_data(prog_data);
2969
2970 fs_reg dest;
2971 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
2972 dest = get_nir_dest(instr->dest);
2973
2974 switch (instr->intrinsic) {
2975 case nir_intrinsic_load_primitive_id:
2976 bld.MOV(dest, fs_reg(brw_vec1_grf(0, 1)));
2977 break;
2978 case nir_intrinsic_load_tess_coord:
2979 /* gl_TessCoord is part of the payload in g1-3 */
2980 for (unsigned i = 0; i < 3; i++) {
2981 bld.MOV(offset(dest, bld, i), fs_reg(brw_vec8_grf(1 + i, 0)));
2982 }
2983 break;
2984
2985 case nir_intrinsic_load_input:
2986 case nir_intrinsic_load_per_vertex_input: {
2987 assert(nir_dest_bit_size(instr->dest) == 32);
2988 fs_reg indirect_offset = get_indirect_offset(instr);
2989 unsigned imm_offset = instr->const_index[0];
2990 unsigned first_component = nir_intrinsic_component(instr);
2991
2992 fs_inst *inst;
2993 if (indirect_offset.file == BAD_FILE) {
2994 /* Arbitrarily only push up to 32 vec4 slots worth of data,
2995 * which is 16 registers (since each holds 2 vec4 slots).
2996 */
2997 const unsigned max_push_slots = 32;
2998 if (imm_offset < max_push_slots) {
2999 fs_reg src = fs_reg(ATTR, imm_offset / 2, dest.type);
3000 for (int i = 0; i < instr->num_components; i++) {
3001 unsigned comp = 4 * (imm_offset % 2) + i + first_component;
3002 bld.MOV(offset(dest, bld, i), component(src, comp));
3003 }
3004
3005 tes_prog_data->base.urb_read_length =
3006 MAX2(tes_prog_data->base.urb_read_length,
3007 (imm_offset / 2) + 1);
3008 } else {
3009 /* Replicate the patch handle to all enabled channels */
3010 const fs_reg srcs[] = {
3011 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD)
3012 };
3013 fs_reg patch_handle = bld.vgrf(BRW_REGISTER_TYPE_UD, 1);
3014 bld.LOAD_PAYLOAD(patch_handle, srcs, ARRAY_SIZE(srcs), 0);
3015
3016 if (first_component != 0) {
3017 unsigned read_components =
3018 instr->num_components + first_component;
3019 fs_reg tmp = bld.vgrf(dest.type, read_components);
3020 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, tmp,
3021 patch_handle);
3022 inst->size_written = read_components * REG_SIZE;
3023 for (unsigned i = 0; i < instr->num_components; i++) {
3024 bld.MOV(offset(dest, bld, i),
3025 offset(tmp, bld, i + first_component));
3026 }
3027 } else {
3028 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8, dest,
3029 patch_handle);
3030 inst->size_written = instr->num_components * REG_SIZE;
3031 }
3032 inst->mlen = 1;
3033 inst->offset = imm_offset;
3034 }
3035 } else {
3036 /* Indirect indexing - use per-slot offsets as well. */
3037
3038 /* We can only read two double components with each URB read, so
3039 * we send two read messages in that case, each one loading up to
3040 * two double components.
3041 */
3042 unsigned num_components = instr->num_components;
3043 const fs_reg srcs[] = {
3044 retype(brw_vec1_grf(0, 0), BRW_REGISTER_TYPE_UD),
3045 indirect_offset
3046 };
3047 fs_reg payload = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
3048 bld.LOAD_PAYLOAD(payload, srcs, ARRAY_SIZE(srcs), 0);
3049
3050 if (first_component != 0) {
3051 unsigned read_components =
3052 num_components + first_component;
3053 fs_reg tmp = bld.vgrf(dest.type, read_components);
3054 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, tmp,
3055 payload);
3056 for (unsigned i = 0; i < num_components; i++) {
3057 bld.MOV(offset(dest, bld, i),
3058 offset(tmp, bld, i + first_component));
3059 }
3060 } else {
3061 inst = bld.emit(SHADER_OPCODE_URB_READ_SIMD8_PER_SLOT, dest,
3062 payload);
3063 }
3064 inst->mlen = 2;
3065 inst->offset = imm_offset;
3066 inst->size_written = (num_components + first_component) *
3067 inst->dst.component_size(inst->exec_size);
3068 }
3069 break;
3070 }
3071 default:
3072 nir_emit_intrinsic(bld, instr);
3073 break;
3074 }
3075 }
3076
3077 void
3078 fs_visitor::nir_emit_gs_intrinsic(const fs_builder &bld,
3079 nir_intrinsic_instr *instr)
3080 {
3081 assert(stage == MESA_SHADER_GEOMETRY);
3082 fs_reg indirect_offset;
3083
3084 fs_reg dest;
3085 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3086 dest = get_nir_dest(instr->dest);
3087
3088 switch (instr->intrinsic) {
3089 case nir_intrinsic_load_primitive_id:
3090 assert(stage == MESA_SHADER_GEOMETRY);
3091 assert(brw_gs_prog_data(prog_data)->include_primitive_id);
3092 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
3093 retype(fs_reg(brw_vec8_grf(2, 0)), BRW_REGISTER_TYPE_UD));
3094 break;
3095
3096 case nir_intrinsic_load_input:
3097 unreachable("load_input intrinsics are invalid for the GS stage");
3098
3099 case nir_intrinsic_load_per_vertex_input:
3100 emit_gs_input_load(dest, instr->src[0], instr->const_index[0],
3101 instr->src[1], instr->num_components,
3102 nir_intrinsic_component(instr));
3103 break;
3104
3105 case nir_intrinsic_emit_vertex_with_counter:
3106 emit_gs_vertex(instr->src[0], instr->const_index[0]);
3107 break;
3108
3109 case nir_intrinsic_end_primitive_with_counter:
3110 emit_gs_end_primitive(instr->src[0]);
3111 break;
3112
3113 case nir_intrinsic_set_vertex_count:
3114 bld.MOV(this->final_gs_vertex_count, get_nir_src(instr->src[0]));
3115 break;
3116
3117 case nir_intrinsic_load_invocation_id: {
3118 fs_reg val = nir_system_values[SYSTEM_VALUE_INVOCATION_ID];
3119 assert(val.file != BAD_FILE);
3120 dest.type = val.type;
3121 bld.MOV(dest, val);
3122 break;
3123 }
3124
3125 default:
3126 nir_emit_intrinsic(bld, instr);
3127 break;
3128 }
3129 }
3130
3131 /**
3132 * Fetch the current render target layer index.
3133 */
3134 static fs_reg
3135 fetch_render_target_array_index(const fs_builder &bld)
3136 {
3137 if (bld.shader->devinfo->gen >= 6) {
3138 /* The render target array index is provided in the thread payload as
3139 * bits 26:16 of r0.0.
3140 */
3141 const fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_UD);
3142 bld.AND(idx, brw_uw1_reg(BRW_GENERAL_REGISTER_FILE, 0, 1),
3143 brw_imm_uw(0x7ff));
3144 return idx;
3145 } else {
3146 /* Pre-SNB we only ever render into the first layer of the framebuffer
3147 * since layered rendering is not implemented.
3148 */
3149 return brw_imm_ud(0);
3150 }
3151 }
3152
3153 /**
3154 * Fake non-coherent framebuffer read implemented using TXF to fetch from the
3155 * framebuffer at the current fragment coordinates and sample index.
3156 */
3157 fs_inst *
3158 fs_visitor::emit_non_coherent_fb_read(const fs_builder &bld, const fs_reg &dst,
3159 unsigned target)
3160 {
3161 const struct gen_device_info *devinfo = bld.shader->devinfo;
3162
3163 assert(bld.shader->stage == MESA_SHADER_FRAGMENT);
3164 const brw_wm_prog_key *wm_key =
3165 reinterpret_cast<const brw_wm_prog_key *>(key);
3166 assert(!wm_key->coherent_fb_fetch);
3167 const struct brw_wm_prog_data *wm_prog_data =
3168 brw_wm_prog_data(stage_prog_data);
3169
3170 /* Calculate the surface index relative to the start of the texture binding
3171 * table block, since that's what the texturing messages expect.
3172 */
3173 const unsigned surface = target +
3174 wm_prog_data->binding_table.render_target_read_start -
3175 wm_prog_data->base.binding_table.texture_start;
3176
3177 /* Calculate the fragment coordinates. */
3178 const fs_reg coords = bld.vgrf(BRW_REGISTER_TYPE_UD, 3);
3179 bld.MOV(offset(coords, bld, 0), pixel_x);
3180 bld.MOV(offset(coords, bld, 1), pixel_y);
3181 bld.MOV(offset(coords, bld, 2), fetch_render_target_array_index(bld));
3182
3183 /* Calculate the sample index and MCS payload when multisampling. Luckily
3184 * the MCS fetch message behaves deterministically for UMS surfaces, so it
3185 * shouldn't be necessary to recompile based on whether the framebuffer is
3186 * CMS or UMS.
3187 */
3188 if (wm_key->multisample_fbo &&
3189 nir_system_values[SYSTEM_VALUE_SAMPLE_ID].file == BAD_FILE)
3190 nir_system_values[SYSTEM_VALUE_SAMPLE_ID] = *emit_sampleid_setup();
3191
3192 const fs_reg sample = nir_system_values[SYSTEM_VALUE_SAMPLE_ID];
3193 const fs_reg mcs = wm_key->multisample_fbo ?
3194 emit_mcs_fetch(coords, 3, brw_imm_ud(surface), fs_reg()) : fs_reg();
3195
3196 /* Use either a normal or a CMS texel fetch message depending on whether
3197 * the framebuffer is single or multisample. On SKL+ use the wide CMS
3198 * message just in case the framebuffer uses 16x multisampling, it should
3199 * be equivalent to the normal CMS fetch for lower multisampling modes.
3200 */
3201 const opcode op = !wm_key->multisample_fbo ? SHADER_OPCODE_TXF_LOGICAL :
3202 devinfo->gen >= 9 ? SHADER_OPCODE_TXF_CMS_W_LOGICAL :
3203 SHADER_OPCODE_TXF_CMS_LOGICAL;
3204
3205 /* Emit the instruction. */
3206 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
3207 srcs[TEX_LOGICAL_SRC_COORDINATE] = coords;
3208 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_ud(0);
3209 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = sample;
3210 srcs[TEX_LOGICAL_SRC_MCS] = mcs;
3211 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(surface);
3212 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(0);
3213 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_ud(3);
3214 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_ud(0);
3215
3216 fs_inst *inst = bld.emit(op, dst, srcs, ARRAY_SIZE(srcs));
3217 inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
3218
3219 return inst;
3220 }
3221
3222 /**
3223 * Actual coherent framebuffer read implemented using the native render target
3224 * read message. Requires SKL+.
3225 */
3226 static fs_inst *
3227 emit_coherent_fb_read(const fs_builder &bld, const fs_reg &dst, unsigned target)
3228 {
3229 assert(bld.shader->devinfo->gen >= 9);
3230 fs_inst *inst = bld.emit(FS_OPCODE_FB_READ_LOGICAL, dst);
3231 inst->target = target;
3232 inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
3233
3234 return inst;
3235 }
3236
3237 static fs_reg
3238 alloc_temporary(const fs_builder &bld, unsigned size, fs_reg *regs, unsigned n)
3239 {
3240 if (n && regs[0].file != BAD_FILE) {
3241 return regs[0];
3242
3243 } else {
3244 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, size);
3245
3246 for (unsigned i = 0; i < n; i++)
3247 regs[i] = tmp;
3248
3249 return tmp;
3250 }
3251 }
3252
3253 static fs_reg
3254 alloc_frag_output(fs_visitor *v, unsigned location)
3255 {
3256 assert(v->stage == MESA_SHADER_FRAGMENT);
3257 const brw_wm_prog_key *const key =
3258 reinterpret_cast<const brw_wm_prog_key *>(v->key);
3259 const unsigned l = GET_FIELD(location, BRW_NIR_FRAG_OUTPUT_LOCATION);
3260 const unsigned i = GET_FIELD(location, BRW_NIR_FRAG_OUTPUT_INDEX);
3261
3262 if (i > 0 || (key->force_dual_color_blend && l == FRAG_RESULT_DATA1))
3263 return alloc_temporary(v->bld, 4, &v->dual_src_output, 1);
3264
3265 else if (l == FRAG_RESULT_COLOR)
3266 return alloc_temporary(v->bld, 4, v->outputs,
3267 MAX2(key->nr_color_regions, 1));
3268
3269 else if (l == FRAG_RESULT_DEPTH)
3270 return alloc_temporary(v->bld, 1, &v->frag_depth, 1);
3271
3272 else if (l == FRAG_RESULT_STENCIL)
3273 return alloc_temporary(v->bld, 1, &v->frag_stencil, 1);
3274
3275 else if (l == FRAG_RESULT_SAMPLE_MASK)
3276 return alloc_temporary(v->bld, 1, &v->sample_mask, 1);
3277
3278 else if (l >= FRAG_RESULT_DATA0 &&
3279 l < FRAG_RESULT_DATA0 + BRW_MAX_DRAW_BUFFERS)
3280 return alloc_temporary(v->bld, 4,
3281 &v->outputs[l - FRAG_RESULT_DATA0], 1);
3282
3283 else
3284 unreachable("Invalid location");
3285 }
3286
3287 /* Annoyingly, we get the barycentrics into the shader in a layout that's
3288 * optimized for PLN but it doesn't work nearly as well as one would like for
3289 * manual interpolation.
3290 */
3291 static void
3292 shuffle_from_pln_layout(const fs_builder &bld, fs_reg dest, fs_reg pln_data)
3293 {
3294 dest.type = BRW_REGISTER_TYPE_F;
3295 pln_data.type = BRW_REGISTER_TYPE_F;
3296 const fs_reg dest_u = offset(dest, bld, 0);
3297 const fs_reg dest_v = offset(dest, bld, 1);
3298
3299 for (unsigned g = 0; g < bld.dispatch_width() / 8; g++) {
3300 const fs_builder gbld = bld.group(8, g);
3301 gbld.MOV(horiz_offset(dest_u, g * 8),
3302 byte_offset(pln_data, (g * 2 + 0) * REG_SIZE));
3303 gbld.MOV(horiz_offset(dest_v, g * 8),
3304 byte_offset(pln_data, (g * 2 + 1) * REG_SIZE));
3305 }
3306 }
3307
3308 static void
3309 shuffle_to_pln_layout(const fs_builder &bld, fs_reg pln_data, fs_reg src)
3310 {
3311 pln_data.type = BRW_REGISTER_TYPE_F;
3312 src.type = BRW_REGISTER_TYPE_F;
3313 const fs_reg src_u = offset(src, bld, 0);
3314 const fs_reg src_v = offset(src, bld, 1);
3315
3316 for (unsigned g = 0; g < bld.dispatch_width() / 8; g++) {
3317 const fs_builder gbld = bld.group(8, g);
3318 gbld.MOV(byte_offset(pln_data, (g * 2 + 0) * REG_SIZE),
3319 horiz_offset(src_u, g * 8));
3320 gbld.MOV(byte_offset(pln_data, (g * 2 + 1) * REG_SIZE),
3321 horiz_offset(src_v, g * 8));
3322 }
3323 }
3324
3325 void
3326 fs_visitor::nir_emit_fs_intrinsic(const fs_builder &bld,
3327 nir_intrinsic_instr *instr)
3328 {
3329 assert(stage == MESA_SHADER_FRAGMENT);
3330
3331 fs_reg dest;
3332 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3333 dest = get_nir_dest(instr->dest);
3334
3335 switch (instr->intrinsic) {
3336 case nir_intrinsic_load_front_face:
3337 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
3338 *emit_frontfacing_interpolation());
3339 break;
3340
3341 case nir_intrinsic_load_sample_pos: {
3342 fs_reg sample_pos = nir_system_values[SYSTEM_VALUE_SAMPLE_POS];
3343 assert(sample_pos.file != BAD_FILE);
3344 dest.type = sample_pos.type;
3345 bld.MOV(dest, sample_pos);
3346 bld.MOV(offset(dest, bld, 1), offset(sample_pos, bld, 1));
3347 break;
3348 }
3349
3350 case nir_intrinsic_load_layer_id:
3351 dest.type = BRW_REGISTER_TYPE_UD;
3352 bld.MOV(dest, fetch_render_target_array_index(bld));
3353 break;
3354
3355 case nir_intrinsic_is_helper_invocation: {
3356 /* Unlike the regular gl_HelperInvocation, that is defined at dispatch,
3357 * the helperInvocationEXT() (aka SpvOpIsHelperInvocationEXT) takes into
3358 * consideration demoted invocations. That information is stored in
3359 * f0.1.
3360 */
3361 dest.type = BRW_REGISTER_TYPE_UD;
3362
3363 bld.MOV(dest, brw_imm_ud(0));
3364
3365 fs_inst *mov = bld.MOV(dest, brw_imm_ud(~0));
3366 mov->predicate = BRW_PREDICATE_NORMAL;
3367 mov->predicate_inverse = true;
3368 mov->flag_subreg = 1;
3369 break;
3370 }
3371
3372 case nir_intrinsic_load_helper_invocation:
3373 case nir_intrinsic_load_sample_mask_in:
3374 case nir_intrinsic_load_sample_id: {
3375 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3376 fs_reg val = nir_system_values[sv];
3377 assert(val.file != BAD_FILE);
3378 dest.type = val.type;
3379 bld.MOV(dest, val);
3380 break;
3381 }
3382
3383 case nir_intrinsic_store_output: {
3384 const fs_reg src = get_nir_src(instr->src[0]);
3385 const unsigned store_offset = nir_src_as_uint(instr->src[1]);
3386 const unsigned location = nir_intrinsic_base(instr) +
3387 SET_FIELD(store_offset, BRW_NIR_FRAG_OUTPUT_LOCATION);
3388 const fs_reg new_dest = retype(alloc_frag_output(this, location),
3389 src.type);
3390
3391 for (unsigned j = 0; j < instr->num_components; j++)
3392 bld.MOV(offset(new_dest, bld, nir_intrinsic_component(instr) + j),
3393 offset(src, bld, j));
3394
3395 break;
3396 }
3397
3398 case nir_intrinsic_load_output: {
3399 const unsigned l = GET_FIELD(nir_intrinsic_base(instr),
3400 BRW_NIR_FRAG_OUTPUT_LOCATION);
3401 assert(l >= FRAG_RESULT_DATA0);
3402 const unsigned load_offset = nir_src_as_uint(instr->src[0]);
3403 const unsigned target = l - FRAG_RESULT_DATA0 + load_offset;
3404 const fs_reg tmp = bld.vgrf(dest.type, 4);
3405
3406 if (reinterpret_cast<const brw_wm_prog_key *>(key)->coherent_fb_fetch)
3407 emit_coherent_fb_read(bld, tmp, target);
3408 else
3409 emit_non_coherent_fb_read(bld, tmp, target);
3410
3411 for (unsigned j = 0; j < instr->num_components; j++) {
3412 bld.MOV(offset(dest, bld, j),
3413 offset(tmp, bld, nir_intrinsic_component(instr) + j));
3414 }
3415
3416 break;
3417 }
3418
3419 case nir_intrinsic_demote:
3420 case nir_intrinsic_discard:
3421 case nir_intrinsic_demote_if:
3422 case nir_intrinsic_discard_if: {
3423 /* We track our discarded pixels in f0.1. By predicating on it, we can
3424 * update just the flag bits that aren't yet discarded. If there's no
3425 * condition, we emit a CMP of g0 != g0, so all currently executing
3426 * channels will get turned off.
3427 */
3428 fs_inst *cmp = NULL;
3429 if (instr->intrinsic == nir_intrinsic_demote_if ||
3430 instr->intrinsic == nir_intrinsic_discard_if) {
3431 nir_alu_instr *alu = nir_src_as_alu_instr(instr->src[0]);
3432
3433 if (alu != NULL &&
3434 alu->op != nir_op_bcsel &&
3435 alu->op != nir_op_inot) {
3436 /* Re-emit the instruction that generated the Boolean value, but
3437 * do not store it. Since this instruction will be conditional,
3438 * other instructions that want to use the real Boolean value may
3439 * get garbage. This was a problem for piglit's fs-discard-exit-2
3440 * test.
3441 *
3442 * Ideally we'd detect that the instruction cannot have a
3443 * conditional modifier before emitting the instructions. Alas,
3444 * that is nigh impossible. Instead, we're going to assume the
3445 * instruction (or last instruction) generated can have a
3446 * conditional modifier. If it cannot, fallback to the old-style
3447 * compare, and hope dead code elimination will clean up the
3448 * extra instructions generated.
3449 */
3450 nir_emit_alu(bld, alu, false);
3451
3452 cmp = (fs_inst *) instructions.get_tail();
3453 if (cmp->conditional_mod == BRW_CONDITIONAL_NONE) {
3454 if (cmp->can_do_cmod())
3455 cmp->conditional_mod = BRW_CONDITIONAL_Z;
3456 else
3457 cmp = NULL;
3458 } else {
3459 /* The old sequence that would have been generated is,
3460 * basically, bool_result == false. This is equivalent to
3461 * !bool_result, so negate the old modifier.
3462 */
3463 cmp->conditional_mod = brw_negate_cmod(cmp->conditional_mod);
3464 }
3465 }
3466
3467 if (cmp == NULL) {
3468 cmp = bld.CMP(bld.null_reg_f(), get_nir_src(instr->src[0]),
3469 brw_imm_d(0), BRW_CONDITIONAL_Z);
3470 }
3471 } else {
3472 fs_reg some_reg = fs_reg(retype(brw_vec8_grf(0, 0),
3473 BRW_REGISTER_TYPE_UW));
3474 cmp = bld.CMP(bld.null_reg_f(), some_reg, some_reg, BRW_CONDITIONAL_NZ);
3475 }
3476
3477 cmp->predicate = BRW_PREDICATE_NORMAL;
3478 cmp->flag_subreg = 1;
3479
3480 if (devinfo->gen >= 6) {
3481 /* Due to the way we implement discard, the jump will only happen
3482 * when the whole quad is discarded. So we can do this even for
3483 * demote as it won't break its uniformity promises.
3484 */
3485 emit_discard_jump();
3486 }
3487
3488 limit_dispatch_width(16, "Fragment discard/demote not implemented in SIMD32 mode.");
3489 break;
3490 }
3491
3492 case nir_intrinsic_load_input: {
3493 /* load_input is only used for flat inputs */
3494 assert(nir_dest_bit_size(instr->dest) == 32);
3495 unsigned base = nir_intrinsic_base(instr);
3496 unsigned comp = nir_intrinsic_component(instr);
3497 unsigned num_components = instr->num_components;
3498
3499 /* Special case fields in the VUE header */
3500 if (base == VARYING_SLOT_LAYER)
3501 comp = 1;
3502 else if (base == VARYING_SLOT_VIEWPORT)
3503 comp = 2;
3504
3505 for (unsigned int i = 0; i < num_components; i++) {
3506 bld.MOV(offset(dest, bld, i),
3507 retype(component(interp_reg(base, comp + i), 3), dest.type));
3508 }
3509 break;
3510 }
3511
3512 case nir_intrinsic_load_fs_input_interp_deltas: {
3513 assert(stage == MESA_SHADER_FRAGMENT);
3514 assert(nir_src_as_uint(instr->src[0]) == 0);
3515 fs_reg interp = interp_reg(nir_intrinsic_base(instr),
3516 nir_intrinsic_component(instr));
3517 dest.type = BRW_REGISTER_TYPE_F;
3518 bld.MOV(offset(dest, bld, 0), component(interp, 3));
3519 bld.MOV(offset(dest, bld, 1), component(interp, 1));
3520 bld.MOV(offset(dest, bld, 2), component(interp, 0));
3521 break;
3522 }
3523
3524 case nir_intrinsic_load_barycentric_pixel:
3525 case nir_intrinsic_load_barycentric_centroid:
3526 case nir_intrinsic_load_barycentric_sample: {
3527 /* Use the delta_xy values computed from the payload */
3528 const glsl_interp_mode interp_mode =
3529 (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
3530 enum brw_barycentric_mode bary =
3531 brw_barycentric_mode(interp_mode, instr->intrinsic);
3532
3533 shuffle_from_pln_layout(bld, dest, this->delta_xy[bary]);
3534 break;
3535 }
3536
3537 case nir_intrinsic_load_barycentric_at_sample: {
3538 const glsl_interp_mode interpolation =
3539 (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
3540
3541 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3542 if (nir_src_is_const(instr->src[0])) {
3543 unsigned msg_data = nir_src_as_uint(instr->src[0]) << 4;
3544
3545 emit_pixel_interpolater_send(bld,
3546 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3547 tmp,
3548 fs_reg(), /* src */
3549 brw_imm_ud(msg_data),
3550 interpolation);
3551 } else {
3552 const fs_reg sample_src = retype(get_nir_src(instr->src[0]),
3553 BRW_REGISTER_TYPE_UD);
3554
3555 if (nir_src_is_dynamically_uniform(instr->src[0])) {
3556 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3557 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3558 bld.exec_all().group(1, 0)
3559 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3560 emit_pixel_interpolater_send(bld,
3561 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3562 tmp,
3563 fs_reg(), /* src */
3564 msg_data,
3565 interpolation);
3566 } else {
3567 /* Make a loop that sends a message to the pixel interpolater
3568 * for the sample number in each live channel. If there are
3569 * multiple channels with the same sample number then these
3570 * will be handled simultaneously with a single interation of
3571 * the loop.
3572 */
3573 bld.emit(BRW_OPCODE_DO);
3574
3575 /* Get the next live sample number into sample_id_reg */
3576 const fs_reg sample_id = bld.emit_uniformize(sample_src);
3577
3578 /* Set the flag register so that we can perform the send
3579 * message on all channels that have the same sample number
3580 */
3581 bld.CMP(bld.null_reg_ud(),
3582 sample_src, sample_id,
3583 BRW_CONDITIONAL_EQ);
3584 const fs_reg msg_data = vgrf(glsl_type::uint_type);
3585 bld.exec_all().group(1, 0)
3586 .SHL(msg_data, sample_id, brw_imm_ud(4u));
3587 fs_inst *inst =
3588 emit_pixel_interpolater_send(bld,
3589 FS_OPCODE_INTERPOLATE_AT_SAMPLE,
3590 tmp,
3591 fs_reg(), /* src */
3592 component(msg_data, 0),
3593 interpolation);
3594 set_predicate(BRW_PREDICATE_NORMAL, inst);
3595
3596 /* Continue the loop if there are any live channels left */
3597 set_predicate_inv(BRW_PREDICATE_NORMAL,
3598 true, /* inverse */
3599 bld.emit(BRW_OPCODE_WHILE));
3600 }
3601 }
3602 shuffle_from_pln_layout(bld, dest, tmp);
3603 break;
3604 }
3605
3606 case nir_intrinsic_load_barycentric_at_offset: {
3607 const glsl_interp_mode interpolation =
3608 (enum glsl_interp_mode) nir_intrinsic_interp_mode(instr);
3609
3610 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
3611
3612 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3613 if (const_offset) {
3614 assert(nir_src_bit_size(instr->src[0]) == 32);
3615 unsigned off_x = MIN2((int)(const_offset[0].f32 * 16), 7) & 0xf;
3616 unsigned off_y = MIN2((int)(const_offset[1].f32 * 16), 7) & 0xf;
3617
3618 emit_pixel_interpolater_send(bld,
3619 FS_OPCODE_INTERPOLATE_AT_SHARED_OFFSET,
3620 tmp,
3621 fs_reg(), /* src */
3622 brw_imm_ud(off_x | (off_y << 4)),
3623 interpolation);
3624 } else {
3625 fs_reg src = vgrf(glsl_type::ivec2_type);
3626 fs_reg offset_src = retype(get_nir_src(instr->src[0]),
3627 BRW_REGISTER_TYPE_F);
3628 for (int i = 0; i < 2; i++) {
3629 fs_reg temp = vgrf(glsl_type::float_type);
3630 bld.MUL(temp, offset(offset_src, bld, i), brw_imm_f(16.0f));
3631 fs_reg itemp = vgrf(glsl_type::int_type);
3632 /* float to int */
3633 bld.MOV(itemp, temp);
3634
3635 /* Clamp the upper end of the range to +7/16.
3636 * ARB_gpu_shader5 requires that we support a maximum offset
3637 * of +0.5, which isn't representable in a S0.4 value -- if
3638 * we didn't clamp it, we'd end up with -8/16, which is the
3639 * opposite of what the shader author wanted.
3640 *
3641 * This is legal due to ARB_gpu_shader5's quantization
3642 * rules:
3643 *
3644 * "Not all values of <offset> may be supported; x and y
3645 * offsets may be rounded to fixed-point values with the
3646 * number of fraction bits given by the
3647 * implementation-dependent constant
3648 * FRAGMENT_INTERPOLATION_OFFSET_BITS"
3649 */
3650 set_condmod(BRW_CONDITIONAL_L,
3651 bld.SEL(offset(src, bld, i), itemp, brw_imm_d(7)));
3652 }
3653
3654 const enum opcode opcode = FS_OPCODE_INTERPOLATE_AT_PER_SLOT_OFFSET;
3655 emit_pixel_interpolater_send(bld,
3656 opcode,
3657 tmp,
3658 src,
3659 brw_imm_ud(0u),
3660 interpolation);
3661 }
3662 shuffle_from_pln_layout(bld, dest, tmp);
3663 break;
3664 }
3665
3666 case nir_intrinsic_load_frag_coord:
3667 emit_fragcoord_interpolation(dest);
3668 break;
3669
3670 case nir_intrinsic_load_interpolated_input: {
3671 assert(instr->src[0].ssa &&
3672 instr->src[0].ssa->parent_instr->type == nir_instr_type_intrinsic);
3673 nir_intrinsic_instr *bary_intrinsic =
3674 nir_instr_as_intrinsic(instr->src[0].ssa->parent_instr);
3675 nir_intrinsic_op bary_intrin = bary_intrinsic->intrinsic;
3676 enum glsl_interp_mode interp_mode =
3677 (enum glsl_interp_mode) nir_intrinsic_interp_mode(bary_intrinsic);
3678 fs_reg dst_xy;
3679
3680 if (bary_intrin == nir_intrinsic_load_barycentric_at_offset ||
3681 bary_intrin == nir_intrinsic_load_barycentric_at_sample) {
3682 /* Use the result of the PI message. Because the load_barycentric
3683 * intrinsics return a regular vec2 and we need it in PLN layout, we
3684 * have to do a translation. Fortunately, copy-prop cleans this up
3685 * reliably.
3686 */
3687 dst_xy = bld.vgrf(BRW_REGISTER_TYPE_F, 2);
3688 shuffle_to_pln_layout(bld, dst_xy, get_nir_src(instr->src[0]));
3689 } else {
3690 /* Use the delta_xy values computed from the payload */
3691 enum brw_barycentric_mode bary =
3692 brw_barycentric_mode(interp_mode, bary_intrin);
3693
3694 dst_xy = this->delta_xy[bary];
3695 }
3696
3697 for (unsigned int i = 0; i < instr->num_components; i++) {
3698 fs_reg interp =
3699 component(interp_reg(nir_intrinsic_base(instr),
3700 nir_intrinsic_component(instr) + i), 0);
3701 interp.type = BRW_REGISTER_TYPE_F;
3702 dest.type = BRW_REGISTER_TYPE_F;
3703
3704 if (devinfo->gen < 6 && interp_mode == INTERP_MODE_SMOOTH) {
3705 fs_reg tmp = vgrf(glsl_type::float_type);
3706 bld.emit(FS_OPCODE_LINTERP, tmp, dst_xy, interp);
3707 bld.MUL(offset(dest, bld, i), tmp, this->pixel_w);
3708 } else {
3709 bld.emit(FS_OPCODE_LINTERP, offset(dest, bld, i), dst_xy, interp);
3710 }
3711 }
3712 break;
3713 }
3714
3715 default:
3716 nir_emit_intrinsic(bld, instr);
3717 break;
3718 }
3719 }
3720
3721 void
3722 fs_visitor::nir_emit_cs_intrinsic(const fs_builder &bld,
3723 nir_intrinsic_instr *instr)
3724 {
3725 assert(stage == MESA_SHADER_COMPUTE);
3726 struct brw_cs_prog_data *cs_prog_data = brw_cs_prog_data(prog_data);
3727
3728 fs_reg dest;
3729 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3730 dest = get_nir_dest(instr->dest);
3731
3732 switch (instr->intrinsic) {
3733 case nir_intrinsic_barrier:
3734 emit_barrier();
3735 cs_prog_data->uses_barrier = true;
3736 break;
3737
3738 case nir_intrinsic_load_subgroup_id:
3739 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD), subgroup_id);
3740 break;
3741
3742 case nir_intrinsic_load_local_invocation_id:
3743 case nir_intrinsic_load_work_group_id: {
3744 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
3745 fs_reg val = nir_system_values[sv];
3746 assert(val.file != BAD_FILE);
3747 dest.type = val.type;
3748 for (unsigned i = 0; i < 3; i++)
3749 bld.MOV(offset(dest, bld, i), offset(val, bld, i));
3750 break;
3751 }
3752
3753 case nir_intrinsic_load_num_work_groups: {
3754 const unsigned surface =
3755 cs_prog_data->binding_table.work_groups_start;
3756
3757 cs_prog_data->uses_num_work_groups = true;
3758
3759 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
3760 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(surface);
3761 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
3762 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(1); /* num components */
3763
3764 /* Read the 3 GLuint components of gl_NumWorkGroups */
3765 for (unsigned i = 0; i < 3; i++) {
3766 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = brw_imm_ud(i << 2);
3767 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
3768 offset(dest, bld, i), srcs, SURFACE_LOGICAL_NUM_SRCS);
3769 }
3770 break;
3771 }
3772
3773 case nir_intrinsic_shared_atomic_add:
3774 case nir_intrinsic_shared_atomic_imin:
3775 case nir_intrinsic_shared_atomic_umin:
3776 case nir_intrinsic_shared_atomic_imax:
3777 case nir_intrinsic_shared_atomic_umax:
3778 case nir_intrinsic_shared_atomic_and:
3779 case nir_intrinsic_shared_atomic_or:
3780 case nir_intrinsic_shared_atomic_xor:
3781 case nir_intrinsic_shared_atomic_exchange:
3782 case nir_intrinsic_shared_atomic_comp_swap:
3783 nir_emit_shared_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
3784 break;
3785 case nir_intrinsic_shared_atomic_fmin:
3786 case nir_intrinsic_shared_atomic_fmax:
3787 case nir_intrinsic_shared_atomic_fcomp_swap:
3788 nir_emit_shared_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
3789 break;
3790
3791 case nir_intrinsic_load_shared: {
3792 assert(devinfo->gen >= 7);
3793 assert(stage == MESA_SHADER_COMPUTE);
3794
3795 const unsigned bit_size = nir_dest_bit_size(instr->dest);
3796 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
3797 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
3798 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[0]);
3799 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
3800
3801 /* Make dest unsigned because that's what the temporary will be */
3802 dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
3803
3804 /* Read the vector */
3805 if (nir_intrinsic_align(instr) >= 4) {
3806 assert(nir_dest_bit_size(instr->dest) == 32);
3807 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
3808 fs_inst *inst =
3809 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
3810 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
3811 inst->size_written = instr->num_components * dispatch_width * 4;
3812 } else {
3813 assert(nir_dest_bit_size(instr->dest) <= 32);
3814 assert(nir_dest_num_components(instr->dest) == 1);
3815 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
3816
3817 fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
3818 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
3819 read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
3820 bld.MOV(dest, subscript(read_result, dest.type, 0));
3821 }
3822 break;
3823 }
3824
3825 case nir_intrinsic_store_shared: {
3826 assert(devinfo->gen >= 7);
3827 assert(stage == MESA_SHADER_COMPUTE);
3828
3829 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
3830 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
3831 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
3832 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
3833 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
3834
3835 fs_reg data = get_nir_src(instr->src[0]);
3836 data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
3837
3838 assert(nir_intrinsic_write_mask(instr) ==
3839 (1u << instr->num_components) - 1);
3840 if (nir_intrinsic_align(instr) >= 4) {
3841 assert(nir_src_bit_size(instr->src[0]) == 32);
3842 assert(nir_src_num_components(instr->src[0]) <= 4);
3843 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
3844 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
3845 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
3846 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
3847 } else {
3848 assert(nir_src_bit_size(instr->src[0]) <= 32);
3849 assert(nir_src_num_components(instr->src[0]) == 1);
3850 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
3851
3852 srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
3853 bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
3854
3855 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
3856 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
3857 }
3858 break;
3859 }
3860
3861 default:
3862 nir_emit_intrinsic(bld, instr);
3863 break;
3864 }
3865 }
3866
3867 static fs_reg
3868 brw_nir_reduction_op_identity(const fs_builder &bld,
3869 nir_op op, brw_reg_type type)
3870 {
3871 nir_const_value value = nir_alu_binop_identity(op, type_sz(type) * 8);
3872 switch (type_sz(type)) {
3873 case 2:
3874 assert(type != BRW_REGISTER_TYPE_HF);
3875 return retype(brw_imm_uw(value.u16), type);
3876 case 4:
3877 return retype(brw_imm_ud(value.u32), type);
3878 case 8:
3879 if (type == BRW_REGISTER_TYPE_DF)
3880 return setup_imm_df(bld, value.f64);
3881 else
3882 return retype(brw_imm_u64(value.u64), type);
3883 default:
3884 unreachable("Invalid type size");
3885 }
3886 }
3887
3888 static opcode
3889 brw_op_for_nir_reduction_op(nir_op op)
3890 {
3891 switch (op) {
3892 case nir_op_iadd: return BRW_OPCODE_ADD;
3893 case nir_op_fadd: return BRW_OPCODE_ADD;
3894 case nir_op_imul: return BRW_OPCODE_MUL;
3895 case nir_op_fmul: return BRW_OPCODE_MUL;
3896 case nir_op_imin: return BRW_OPCODE_SEL;
3897 case nir_op_umin: return BRW_OPCODE_SEL;
3898 case nir_op_fmin: return BRW_OPCODE_SEL;
3899 case nir_op_imax: return BRW_OPCODE_SEL;
3900 case nir_op_umax: return BRW_OPCODE_SEL;
3901 case nir_op_fmax: return BRW_OPCODE_SEL;
3902 case nir_op_iand: return BRW_OPCODE_AND;
3903 case nir_op_ior: return BRW_OPCODE_OR;
3904 case nir_op_ixor: return BRW_OPCODE_XOR;
3905 default:
3906 unreachable("Invalid reduction operation");
3907 }
3908 }
3909
3910 static brw_conditional_mod
3911 brw_cond_mod_for_nir_reduction_op(nir_op op)
3912 {
3913 switch (op) {
3914 case nir_op_iadd: return BRW_CONDITIONAL_NONE;
3915 case nir_op_fadd: return BRW_CONDITIONAL_NONE;
3916 case nir_op_imul: return BRW_CONDITIONAL_NONE;
3917 case nir_op_fmul: return BRW_CONDITIONAL_NONE;
3918 case nir_op_imin: return BRW_CONDITIONAL_L;
3919 case nir_op_umin: return BRW_CONDITIONAL_L;
3920 case nir_op_fmin: return BRW_CONDITIONAL_L;
3921 case nir_op_imax: return BRW_CONDITIONAL_GE;
3922 case nir_op_umax: return BRW_CONDITIONAL_GE;
3923 case nir_op_fmax: return BRW_CONDITIONAL_GE;
3924 case nir_op_iand: return BRW_CONDITIONAL_NONE;
3925 case nir_op_ior: return BRW_CONDITIONAL_NONE;
3926 case nir_op_ixor: return BRW_CONDITIONAL_NONE;
3927 default:
3928 unreachable("Invalid reduction operation");
3929 }
3930 }
3931
3932 fs_reg
3933 fs_visitor::get_nir_image_intrinsic_image(const brw::fs_builder &bld,
3934 nir_intrinsic_instr *instr)
3935 {
3936 fs_reg image = retype(get_nir_src_imm(instr->src[0]), BRW_REGISTER_TYPE_UD);
3937
3938 if (stage_prog_data->binding_table.image_start > 0) {
3939 if (image.file == BRW_IMMEDIATE_VALUE) {
3940 image.d += stage_prog_data->binding_table.image_start;
3941 } else {
3942 bld.ADD(image, image,
3943 brw_imm_d(stage_prog_data->binding_table.image_start));
3944 }
3945 }
3946
3947 return bld.emit_uniformize(image);
3948 }
3949
3950 fs_reg
3951 fs_visitor::get_nir_ssbo_intrinsic_index(const brw::fs_builder &bld,
3952 nir_intrinsic_instr *instr)
3953 {
3954 /* SSBO stores are weird in that their index is in src[1] */
3955 const unsigned src = instr->intrinsic == nir_intrinsic_store_ssbo ? 1 : 0;
3956
3957 fs_reg surf_index;
3958 if (nir_src_is_const(instr->src[src])) {
3959 unsigned index = stage_prog_data->binding_table.ssbo_start +
3960 nir_src_as_uint(instr->src[src]);
3961 surf_index = brw_imm_ud(index);
3962 } else {
3963 surf_index = vgrf(glsl_type::uint_type);
3964 bld.ADD(surf_index, get_nir_src(instr->src[src]),
3965 brw_imm_ud(stage_prog_data->binding_table.ssbo_start));
3966 }
3967
3968 return bld.emit_uniformize(surf_index);
3969 }
3970
3971 static unsigned
3972 image_intrinsic_coord_components(nir_intrinsic_instr *instr)
3973 {
3974 switch (nir_intrinsic_image_dim(instr)) {
3975 case GLSL_SAMPLER_DIM_1D:
3976 return 1 + nir_intrinsic_image_array(instr);
3977 case GLSL_SAMPLER_DIM_2D:
3978 case GLSL_SAMPLER_DIM_RECT:
3979 return 2 + nir_intrinsic_image_array(instr);
3980 case GLSL_SAMPLER_DIM_3D:
3981 case GLSL_SAMPLER_DIM_CUBE:
3982 return 3;
3983 case GLSL_SAMPLER_DIM_BUF:
3984 return 1;
3985 case GLSL_SAMPLER_DIM_MS:
3986 return 2 + nir_intrinsic_image_array(instr);
3987 default:
3988 unreachable("Invalid image dimension");
3989 }
3990 }
3991
3992 void
3993 fs_visitor::nir_emit_intrinsic(const fs_builder &bld, nir_intrinsic_instr *instr)
3994 {
3995 fs_reg dest;
3996 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
3997 dest = get_nir_dest(instr->dest);
3998
3999 switch (instr->intrinsic) {
4000 case nir_intrinsic_image_load:
4001 case nir_intrinsic_image_store:
4002 case nir_intrinsic_image_atomic_add:
4003 case nir_intrinsic_image_atomic_imin:
4004 case nir_intrinsic_image_atomic_umin:
4005 case nir_intrinsic_image_atomic_imax:
4006 case nir_intrinsic_image_atomic_umax:
4007 case nir_intrinsic_image_atomic_and:
4008 case nir_intrinsic_image_atomic_or:
4009 case nir_intrinsic_image_atomic_xor:
4010 case nir_intrinsic_image_atomic_exchange:
4011 case nir_intrinsic_image_atomic_comp_swap:
4012 case nir_intrinsic_bindless_image_load:
4013 case nir_intrinsic_bindless_image_store:
4014 case nir_intrinsic_bindless_image_atomic_add:
4015 case nir_intrinsic_bindless_image_atomic_imin:
4016 case nir_intrinsic_bindless_image_atomic_umin:
4017 case nir_intrinsic_bindless_image_atomic_imax:
4018 case nir_intrinsic_bindless_image_atomic_umax:
4019 case nir_intrinsic_bindless_image_atomic_and:
4020 case nir_intrinsic_bindless_image_atomic_or:
4021 case nir_intrinsic_bindless_image_atomic_xor:
4022 case nir_intrinsic_bindless_image_atomic_exchange:
4023 case nir_intrinsic_bindless_image_atomic_comp_swap: {
4024 if (stage == MESA_SHADER_FRAGMENT &&
4025 instr->intrinsic != nir_intrinsic_image_load)
4026 brw_wm_prog_data(prog_data)->has_side_effects = true;
4027
4028 /* Get some metadata from the image intrinsic. */
4029 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
4030
4031 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4032
4033 switch (instr->intrinsic) {
4034 case nir_intrinsic_image_load:
4035 case nir_intrinsic_image_store:
4036 case nir_intrinsic_image_atomic_add:
4037 case nir_intrinsic_image_atomic_imin:
4038 case nir_intrinsic_image_atomic_umin:
4039 case nir_intrinsic_image_atomic_imax:
4040 case nir_intrinsic_image_atomic_umax:
4041 case nir_intrinsic_image_atomic_and:
4042 case nir_intrinsic_image_atomic_or:
4043 case nir_intrinsic_image_atomic_xor:
4044 case nir_intrinsic_image_atomic_exchange:
4045 case nir_intrinsic_image_atomic_comp_swap:
4046 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4047 get_nir_image_intrinsic_image(bld, instr);
4048 break;
4049
4050 default:
4051 /* Bindless */
4052 srcs[SURFACE_LOGICAL_SRC_SURFACE_HANDLE] =
4053 bld.emit_uniformize(get_nir_src(instr->src[0]));
4054 break;
4055 }
4056
4057 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4058 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] =
4059 brw_imm_ud(image_intrinsic_coord_components(instr));
4060
4061 /* Emit an image load, store or atomic op. */
4062 if (instr->intrinsic == nir_intrinsic_image_load ||
4063 instr->intrinsic == nir_intrinsic_bindless_image_load) {
4064 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4065 fs_inst *inst =
4066 bld.emit(SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL,
4067 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4068 inst->size_written = instr->num_components * dispatch_width * 4;
4069 } else if (instr->intrinsic == nir_intrinsic_image_store ||
4070 instr->intrinsic == nir_intrinsic_bindless_image_store) {
4071 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4072 srcs[SURFACE_LOGICAL_SRC_DATA] = get_nir_src(instr->src[3]);
4073 bld.emit(SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL,
4074 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4075 } else {
4076 unsigned num_srcs = info->num_srcs;
4077 int op = brw_aop_for_nir_intrinsic(instr);
4078 if (op == BRW_AOP_INC || op == BRW_AOP_DEC) {
4079 assert(num_srcs == 4);
4080 num_srcs = 3;
4081 }
4082
4083 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
4084
4085 fs_reg data;
4086 if (num_srcs >= 4)
4087 data = get_nir_src(instr->src[3]);
4088 if (num_srcs >= 5) {
4089 fs_reg tmp = bld.vgrf(data.type, 2);
4090 fs_reg sources[2] = { data, get_nir_src(instr->src[4]) };
4091 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
4092 data = tmp;
4093 }
4094 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
4095
4096 bld.emit(SHADER_OPCODE_TYPED_ATOMIC_LOGICAL,
4097 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4098 }
4099 break;
4100 }
4101
4102 case nir_intrinsic_image_size:
4103 case nir_intrinsic_bindless_image_size: {
4104 /* Unlike the [un]typed load and store opcodes, the TXS that this turns
4105 * into will handle the binding table index for us in the geneerator.
4106 * Incidentally, this means that we can handle bindless with exactly the
4107 * same code.
4108 */
4109 fs_reg image = retype(get_nir_src_imm(instr->src[0]),
4110 BRW_REGISTER_TYPE_UD);
4111 image = bld.emit_uniformize(image);
4112
4113 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
4114 if (instr->intrinsic == nir_intrinsic_image_size)
4115 srcs[TEX_LOGICAL_SRC_SURFACE] = image;
4116 else
4117 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = image;
4118 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_d(0);
4119 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(0);
4120 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(0);
4121
4122 /* Since the image size is always uniform, we can just emit a SIMD8
4123 * query instruction and splat the result out.
4124 */
4125 const fs_builder ubld = bld.exec_all().group(8, 0);
4126
4127 fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
4128 fs_inst *inst = ubld.emit(SHADER_OPCODE_IMAGE_SIZE_LOGICAL,
4129 tmp, srcs, ARRAY_SIZE(srcs));
4130 inst->size_written = 4 * REG_SIZE;
4131
4132 for (unsigned c = 0; c < instr->dest.ssa.num_components; ++c) {
4133 if (c == 2 && nir_intrinsic_image_dim(instr) == GLSL_SAMPLER_DIM_CUBE) {
4134 bld.emit(SHADER_OPCODE_INT_QUOTIENT,
4135 offset(retype(dest, tmp.type), bld, c),
4136 component(offset(tmp, ubld, c), 0), brw_imm_ud(6));
4137 } else {
4138 bld.MOV(offset(retype(dest, tmp.type), bld, c),
4139 component(offset(tmp, ubld, c), 0));
4140 }
4141 }
4142 break;
4143 }
4144
4145 case nir_intrinsic_image_load_raw_intel: {
4146 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4147 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4148 get_nir_image_intrinsic_image(bld, instr);
4149 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4150 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4151 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4152
4153 fs_inst *inst =
4154 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
4155 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4156 inst->size_written = instr->num_components * dispatch_width * 4;
4157 break;
4158 }
4159
4160 case nir_intrinsic_image_store_raw_intel: {
4161 if (stage == MESA_SHADER_FRAGMENT)
4162 brw_wm_prog_data(prog_data)->has_side_effects = true;
4163
4164 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4165 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4166 get_nir_image_intrinsic_image(bld, instr);
4167 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4168 srcs[SURFACE_LOGICAL_SRC_DATA] = get_nir_src(instr->src[2]);
4169 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4170 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4171
4172 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
4173 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4174 break;
4175 }
4176
4177 case nir_intrinsic_group_memory_barrier:
4178 case nir_intrinsic_memory_barrier_shared:
4179 case nir_intrinsic_memory_barrier_atomic_counter:
4180 case nir_intrinsic_memory_barrier_buffer:
4181 case nir_intrinsic_memory_barrier_image:
4182 case nir_intrinsic_memory_barrier: {
4183 bool l3_fence, slm_fence;
4184 if (devinfo->gen >= 11) {
4185 l3_fence = instr->intrinsic != nir_intrinsic_memory_barrier_shared;
4186 slm_fence = instr->intrinsic == nir_intrinsic_group_memory_barrier ||
4187 instr->intrinsic == nir_intrinsic_memory_barrier ||
4188 instr->intrinsic == nir_intrinsic_memory_barrier_shared;
4189 } else {
4190 /* Prior to gen11, we only have one kind of fence. */
4191 l3_fence = true;
4192 slm_fence = false;
4193 }
4194
4195 /* Be conservative in Gen11+ and always stall in a fence. Since there
4196 * are two different fences, and shader might want to synchronize
4197 * between them.
4198 *
4199 * TODO: Improve NIR so that scope and visibility information for the
4200 * barriers is available here to make a better decision.
4201 *
4202 * TODO: When emitting more than one fence, it might help emit all
4203 * the fences first and then generate the stall moves.
4204 */
4205 const bool stall = devinfo->gen >= 11;
4206
4207 const fs_builder ubld = bld.group(8, 0);
4208 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
4209
4210 if (l3_fence) {
4211 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp,
4212 brw_vec8_grf(0, 0), brw_imm_ud(stall),
4213 /* bti */ brw_imm_ud(0))
4214 ->size_written = 2 * REG_SIZE;
4215 }
4216
4217 if (slm_fence) {
4218 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp,
4219 brw_vec8_grf(0, 0), brw_imm_ud(stall),
4220 brw_imm_ud(GEN7_BTI_SLM))
4221 ->size_written = 2 * REG_SIZE;
4222 }
4223
4224 break;
4225 }
4226
4227 case nir_intrinsic_shader_clock: {
4228 /* We cannot do anything if there is an event, so ignore it for now */
4229 const fs_reg shader_clock = get_timestamp(bld);
4230 const fs_reg srcs[] = { component(shader_clock, 0),
4231 component(shader_clock, 1) };
4232 bld.LOAD_PAYLOAD(dest, srcs, ARRAY_SIZE(srcs), 0);
4233 break;
4234 }
4235
4236 case nir_intrinsic_image_samples:
4237 /* The driver does not support multi-sampled images. */
4238 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(1));
4239 break;
4240
4241 case nir_intrinsic_load_uniform: {
4242 /* Offsets are in bytes but they should always aligned to
4243 * the type size
4244 */
4245 assert(instr->const_index[0] % 4 == 0 ||
4246 instr->const_index[0] % type_sz(dest.type) == 0);
4247
4248 fs_reg src(UNIFORM, instr->const_index[0] / 4, dest.type);
4249
4250 if (nir_src_is_const(instr->src[0])) {
4251 unsigned load_offset = nir_src_as_uint(instr->src[0]);
4252 assert(load_offset % type_sz(dest.type) == 0);
4253 /* For 16-bit types we add the module of the const_index[0]
4254 * offset to access to not 32-bit aligned element
4255 */
4256 src.offset = load_offset + instr->const_index[0] % 4;
4257
4258 for (unsigned j = 0; j < instr->num_components; j++) {
4259 bld.MOV(offset(dest, bld, j), offset(src, bld, j));
4260 }
4261 } else {
4262 fs_reg indirect = retype(get_nir_src(instr->src[0]),
4263 BRW_REGISTER_TYPE_UD);
4264
4265 /* We need to pass a size to the MOV_INDIRECT but we don't want it to
4266 * go past the end of the uniform. In order to keep the n'th
4267 * component from running past, we subtract off the size of all but
4268 * one component of the vector.
4269 */
4270 assert(instr->const_index[1] >=
4271 instr->num_components * (int) type_sz(dest.type));
4272 unsigned read_size = instr->const_index[1] -
4273 (instr->num_components - 1) * type_sz(dest.type);
4274
4275 bool supports_64bit_indirects =
4276 !devinfo->is_cherryview && !gen_device_info_is_9lp(devinfo);
4277
4278 if (type_sz(dest.type) != 8 || supports_64bit_indirects) {
4279 for (unsigned j = 0; j < instr->num_components; j++) {
4280 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
4281 offset(dest, bld, j), offset(src, bld, j),
4282 indirect, brw_imm_ud(read_size));
4283 }
4284 } else {
4285 const unsigned num_mov_indirects =
4286 type_sz(dest.type) / type_sz(BRW_REGISTER_TYPE_UD);
4287 /* We read a little bit less per MOV INDIRECT, as they are now
4288 * 32-bits ones instead of 64-bit. Fix read_size then.
4289 */
4290 const unsigned read_size_32bit = read_size -
4291 (num_mov_indirects - 1) * type_sz(BRW_REGISTER_TYPE_UD);
4292 for (unsigned j = 0; j < instr->num_components; j++) {
4293 for (unsigned i = 0; i < num_mov_indirects; i++) {
4294 bld.emit(SHADER_OPCODE_MOV_INDIRECT,
4295 subscript(offset(dest, bld, j), BRW_REGISTER_TYPE_UD, i),
4296 subscript(offset(src, bld, j), BRW_REGISTER_TYPE_UD, i),
4297 indirect, brw_imm_ud(read_size_32bit));
4298 }
4299 }
4300 }
4301 }
4302 break;
4303 }
4304
4305 case nir_intrinsic_load_ubo: {
4306 fs_reg surf_index;
4307 if (nir_src_is_const(instr->src[0])) {
4308 const unsigned index = stage_prog_data->binding_table.ubo_start +
4309 nir_src_as_uint(instr->src[0]);
4310 surf_index = brw_imm_ud(index);
4311 } else {
4312 /* The block index is not a constant. Evaluate the index expression
4313 * per-channel and add the base UBO index; we have to select a value
4314 * from any live channel.
4315 */
4316 surf_index = vgrf(glsl_type::uint_type);
4317 bld.ADD(surf_index, get_nir_src(instr->src[0]),
4318 brw_imm_ud(stage_prog_data->binding_table.ubo_start));
4319 surf_index = bld.emit_uniformize(surf_index);
4320 }
4321
4322 if (!nir_src_is_const(instr->src[1])) {
4323 fs_reg base_offset = retype(get_nir_src(instr->src[1]),
4324 BRW_REGISTER_TYPE_UD);
4325
4326 for (int i = 0; i < instr->num_components; i++)
4327 VARYING_PULL_CONSTANT_LOAD(bld, offset(dest, bld, i), surf_index,
4328 base_offset, i * type_sz(dest.type));
4329
4330 prog_data->has_ubo_pull = true;
4331 } else {
4332 /* Even if we are loading doubles, a pull constant load will load
4333 * a 32-bit vec4, so should only reserve vgrf space for that. If we
4334 * need to load a full dvec4 we will have to emit 2 loads. This is
4335 * similar to demote_pull_constants(), except that in that case we
4336 * see individual accesses to each component of the vector and then
4337 * we let CSE deal with duplicate loads. Here we see a vector access
4338 * and we have to split it if necessary.
4339 */
4340 const unsigned type_size = type_sz(dest.type);
4341 const unsigned load_offset = nir_src_as_uint(instr->src[1]);
4342
4343 /* See if we've selected this as a push constant candidate */
4344 if (nir_src_is_const(instr->src[0])) {
4345 const unsigned ubo_block = nir_src_as_uint(instr->src[0]);
4346 const unsigned offset_256b = load_offset / 32;
4347
4348 fs_reg push_reg;
4349 for (int i = 0; i < 4; i++) {
4350 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
4351 if (range->block == ubo_block &&
4352 offset_256b >= range->start &&
4353 offset_256b < range->start + range->length) {
4354
4355 push_reg = fs_reg(UNIFORM, UBO_START + i, dest.type);
4356 push_reg.offset = load_offset - 32 * range->start;
4357 break;
4358 }
4359 }
4360
4361 if (push_reg.file != BAD_FILE) {
4362 for (unsigned i = 0; i < instr->num_components; i++) {
4363 bld.MOV(offset(dest, bld, i),
4364 byte_offset(push_reg, i * type_size));
4365 }
4366 break;
4367 }
4368 }
4369
4370 prog_data->has_ubo_pull = true;
4371
4372 const unsigned block_sz = 64; /* Fetch one cacheline at a time. */
4373 const fs_builder ubld = bld.exec_all().group(block_sz / 4, 0);
4374 const fs_reg packed_consts = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4375
4376 for (unsigned c = 0; c < instr->num_components;) {
4377 const unsigned base = load_offset + c * type_size;
4378 /* Number of usable components in the next block-aligned load. */
4379 const unsigned count = MIN2(instr->num_components - c,
4380 (block_sz - base % block_sz) / type_size);
4381
4382 ubld.emit(FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD,
4383 packed_consts, surf_index,
4384 brw_imm_ud(base & ~(block_sz - 1)));
4385
4386 const fs_reg consts =
4387 retype(byte_offset(packed_consts, base & (block_sz - 1)),
4388 dest.type);
4389
4390 for (unsigned d = 0; d < count; d++)
4391 bld.MOV(offset(dest, bld, c + d), component(consts, d));
4392
4393 c += count;
4394 }
4395 }
4396 break;
4397 }
4398
4399 case nir_intrinsic_load_global: {
4400 assert(devinfo->gen >= 8);
4401
4402 if (nir_intrinsic_align(instr) >= 4) {
4403 assert(nir_dest_bit_size(instr->dest) == 32);
4404 fs_inst *inst = bld.emit(SHADER_OPCODE_A64_UNTYPED_READ_LOGICAL,
4405 dest,
4406 get_nir_src(instr->src[0]), /* Address */
4407 fs_reg(), /* No source data */
4408 brw_imm_ud(instr->num_components));
4409 inst->size_written = instr->num_components *
4410 inst->dst.component_size(inst->exec_size);
4411 } else {
4412 const unsigned bit_size = nir_dest_bit_size(instr->dest);
4413 assert(bit_size <= 32);
4414 assert(nir_dest_num_components(instr->dest) == 1);
4415 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
4416 bld.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_READ_LOGICAL,
4417 tmp,
4418 get_nir_src(instr->src[0]), /* Address */
4419 fs_reg(), /* No source data */
4420 brw_imm_ud(bit_size));
4421 bld.MOV(dest, subscript(tmp, dest.type, 0));
4422 }
4423 break;
4424 }
4425
4426 case nir_intrinsic_store_global:
4427 assert(devinfo->gen >= 8);
4428
4429 if (stage == MESA_SHADER_FRAGMENT)
4430 brw_wm_prog_data(prog_data)->has_side_effects = true;
4431
4432 if (nir_intrinsic_align(instr) >= 4) {
4433 assert(nir_src_bit_size(instr->src[0]) == 32);
4434 bld.emit(SHADER_OPCODE_A64_UNTYPED_WRITE_LOGICAL,
4435 fs_reg(),
4436 get_nir_src(instr->src[1]), /* Address */
4437 get_nir_src(instr->src[0]), /* Data */
4438 brw_imm_ud(instr->num_components));
4439 } else {
4440 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
4441 assert(bit_size <= 32);
4442 assert(nir_src_num_components(instr->src[0]) == 1);
4443 brw_reg_type data_type =
4444 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
4445 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
4446 bld.MOV(tmp, retype(get_nir_src(instr->src[0]), data_type));
4447 bld.emit(SHADER_OPCODE_A64_BYTE_SCATTERED_WRITE_LOGICAL,
4448 fs_reg(),
4449 get_nir_src(instr->src[1]), /* Address */
4450 tmp, /* Data */
4451 brw_imm_ud(nir_src_bit_size(instr->src[0])));
4452 }
4453 break;
4454
4455 case nir_intrinsic_global_atomic_add:
4456 case nir_intrinsic_global_atomic_imin:
4457 case nir_intrinsic_global_atomic_umin:
4458 case nir_intrinsic_global_atomic_imax:
4459 case nir_intrinsic_global_atomic_umax:
4460 case nir_intrinsic_global_atomic_and:
4461 case nir_intrinsic_global_atomic_or:
4462 case nir_intrinsic_global_atomic_xor:
4463 case nir_intrinsic_global_atomic_exchange:
4464 case nir_intrinsic_global_atomic_comp_swap:
4465 nir_emit_global_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
4466 break;
4467 case nir_intrinsic_global_atomic_fmin:
4468 case nir_intrinsic_global_atomic_fmax:
4469 case nir_intrinsic_global_atomic_fcomp_swap:
4470 nir_emit_global_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
4471 break;
4472
4473 case nir_intrinsic_load_ssbo: {
4474 assert(devinfo->gen >= 7);
4475
4476 const unsigned bit_size = nir_dest_bit_size(instr->dest);
4477 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4478 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4479 get_nir_ssbo_intrinsic_index(bld, instr);
4480 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
4481 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4482
4483 /* Make dest unsigned because that's what the temporary will be */
4484 dest.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
4485
4486 /* Read the vector */
4487 if (nir_intrinsic_align(instr) >= 4) {
4488 assert(nir_dest_bit_size(instr->dest) == 32);
4489 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4490 fs_inst *inst =
4491 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL,
4492 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
4493 inst->size_written = instr->num_components * dispatch_width * 4;
4494 } else {
4495 assert(nir_dest_bit_size(instr->dest) <= 32);
4496 assert(nir_dest_num_components(instr->dest) == 1);
4497 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
4498
4499 fs_reg read_result = bld.vgrf(BRW_REGISTER_TYPE_UD);
4500 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL,
4501 read_result, srcs, SURFACE_LOGICAL_NUM_SRCS);
4502 bld.MOV(dest, subscript(read_result, dest.type, 0));
4503 }
4504 break;
4505 }
4506
4507 case nir_intrinsic_store_ssbo: {
4508 assert(devinfo->gen >= 7);
4509
4510 if (stage == MESA_SHADER_FRAGMENT)
4511 brw_wm_prog_data(prog_data)->has_side_effects = true;
4512
4513 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
4514 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
4515 srcs[SURFACE_LOGICAL_SRC_SURFACE] =
4516 get_nir_ssbo_intrinsic_index(bld, instr);
4517 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[2]);
4518 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
4519
4520 fs_reg data = get_nir_src(instr->src[0]);
4521 data.type = brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_UD);
4522
4523 assert(nir_intrinsic_write_mask(instr) ==
4524 (1u << instr->num_components) - 1);
4525 if (nir_intrinsic_align(instr) >= 4) {
4526 assert(nir_src_bit_size(instr->src[0]) == 32);
4527 assert(nir_src_num_components(instr->src[0]) <= 4);
4528 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
4529 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(instr->num_components);
4530 bld.emit(SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL,
4531 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4532 } else {
4533 assert(nir_src_bit_size(instr->src[0]) <= 32);
4534 assert(nir_src_num_components(instr->src[0]) == 1);
4535 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(bit_size);
4536
4537 srcs[SURFACE_LOGICAL_SRC_DATA] = bld.vgrf(BRW_REGISTER_TYPE_UD);
4538 bld.MOV(srcs[SURFACE_LOGICAL_SRC_DATA], data);
4539
4540 bld.emit(SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL,
4541 fs_reg(), srcs, SURFACE_LOGICAL_NUM_SRCS);
4542 }
4543 break;
4544 }
4545
4546 case nir_intrinsic_store_output: {
4547 assert(nir_src_bit_size(instr->src[0]) == 32);
4548 fs_reg src = get_nir_src(instr->src[0]);
4549
4550 unsigned store_offset = nir_src_as_uint(instr->src[1]);
4551 unsigned num_components = instr->num_components;
4552 unsigned first_component = nir_intrinsic_component(instr);
4553
4554 fs_reg new_dest = retype(offset(outputs[instr->const_index[0]], bld,
4555 4 * store_offset), src.type);
4556 for (unsigned j = 0; j < num_components; j++) {
4557 bld.MOV(offset(new_dest, bld, j + first_component),
4558 offset(src, bld, j));
4559 }
4560 break;
4561 }
4562
4563 case nir_intrinsic_ssbo_atomic_add:
4564 case nir_intrinsic_ssbo_atomic_imin:
4565 case nir_intrinsic_ssbo_atomic_umin:
4566 case nir_intrinsic_ssbo_atomic_imax:
4567 case nir_intrinsic_ssbo_atomic_umax:
4568 case nir_intrinsic_ssbo_atomic_and:
4569 case nir_intrinsic_ssbo_atomic_or:
4570 case nir_intrinsic_ssbo_atomic_xor:
4571 case nir_intrinsic_ssbo_atomic_exchange:
4572 case nir_intrinsic_ssbo_atomic_comp_swap:
4573 nir_emit_ssbo_atomic(bld, brw_aop_for_nir_intrinsic(instr), instr);
4574 break;
4575 case nir_intrinsic_ssbo_atomic_fmin:
4576 case nir_intrinsic_ssbo_atomic_fmax:
4577 case nir_intrinsic_ssbo_atomic_fcomp_swap:
4578 nir_emit_ssbo_atomic_float(bld, brw_aop_for_nir_intrinsic(instr), instr);
4579 break;
4580
4581 case nir_intrinsic_get_buffer_size: {
4582 assert(nir_src_num_components(instr->src[0]) == 1);
4583 unsigned ssbo_index = nir_src_is_const(instr->src[0]) ?
4584 nir_src_as_uint(instr->src[0]) : 0;
4585
4586 /* A resinfo's sampler message is used to get the buffer size. The
4587 * SIMD8's writeback message consists of four registers and SIMD16's
4588 * writeback message consists of 8 destination registers (two per each
4589 * component). Because we are only interested on the first channel of
4590 * the first returned component, where resinfo returns the buffer size
4591 * for SURFTYPE_BUFFER, we can just use the SIMD8 variant regardless of
4592 * the dispatch width.
4593 */
4594 const fs_builder ubld = bld.exec_all().group(8, 0);
4595 fs_reg src_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4596 fs_reg ret_payload = ubld.vgrf(BRW_REGISTER_TYPE_UD, 4);
4597
4598 /* Set LOD = 0 */
4599 ubld.MOV(src_payload, brw_imm_d(0));
4600
4601 const unsigned index = prog_data->binding_table.ssbo_start + ssbo_index;
4602 fs_inst *inst = ubld.emit(SHADER_OPCODE_GET_BUFFER_SIZE, ret_payload,
4603 src_payload, brw_imm_ud(index));
4604 inst->header_size = 0;
4605 inst->mlen = 1;
4606 inst->size_written = 4 * REG_SIZE;
4607
4608 /* SKL PRM, vol07, 3D Media GPGPU Engine, Bounds Checking and Faulting:
4609 *
4610 * "Out-of-bounds checking is always performed at a DWord granularity. If
4611 * any part of the DWord is out-of-bounds then the whole DWord is
4612 * considered out-of-bounds."
4613 *
4614 * This implies that types with size smaller than 4-bytes need to be
4615 * padded if they don't complete the last dword of the buffer. But as we
4616 * need to maintain the original size we need to reverse the padding
4617 * calculation to return the correct size to know the number of elements
4618 * of an unsized array. As we stored in the last two bits of the surface
4619 * size the needed padding for the buffer, we calculate here the
4620 * original buffer_size reversing the surface_size calculation:
4621 *
4622 * surface_size = isl_align(buffer_size, 4) +
4623 * (isl_align(buffer_size) - buffer_size)
4624 *
4625 * buffer_size = surface_size & ~3 - surface_size & 3
4626 */
4627
4628 fs_reg size_aligned4 = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4629 fs_reg size_padding = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4630 fs_reg buffer_size = ubld.vgrf(BRW_REGISTER_TYPE_UD);
4631
4632 ubld.AND(size_padding, ret_payload, brw_imm_ud(3));
4633 ubld.AND(size_aligned4, ret_payload, brw_imm_ud(~3));
4634 ubld.ADD(buffer_size, size_aligned4, negate(size_padding));
4635
4636 bld.MOV(retype(dest, ret_payload.type), component(buffer_size, 0));
4637 break;
4638 }
4639
4640 case nir_intrinsic_load_subgroup_size:
4641 /* This should only happen for fragment shaders because every other case
4642 * is lowered in NIR so we can optimize on it.
4643 */
4644 assert(stage == MESA_SHADER_FRAGMENT);
4645 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), brw_imm_d(dispatch_width));
4646 break;
4647
4648 case nir_intrinsic_load_subgroup_invocation:
4649 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D),
4650 nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION]);
4651 break;
4652
4653 case nir_intrinsic_load_subgroup_eq_mask:
4654 case nir_intrinsic_load_subgroup_ge_mask:
4655 case nir_intrinsic_load_subgroup_gt_mask:
4656 case nir_intrinsic_load_subgroup_le_mask:
4657 case nir_intrinsic_load_subgroup_lt_mask:
4658 unreachable("not reached");
4659
4660 case nir_intrinsic_vote_any: {
4661 const fs_builder ubld = bld.exec_all().group(1, 0);
4662
4663 /* The any/all predicates do not consider channel enables. To prevent
4664 * dead channels from affecting the result, we initialize the flag with
4665 * with the identity value for the logical operation.
4666 */
4667 if (dispatch_width == 32) {
4668 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4669 ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
4670 brw_imm_ud(0));
4671 } else {
4672 ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0));
4673 }
4674 bld.CMP(bld.null_reg_d(), get_nir_src(instr->src[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ);
4675
4676 /* For some reason, the any/all predicates don't work properly with
4677 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4678 * doesn't read the correct subset of the flag register and you end up
4679 * getting garbage in the second half. Work around this by using a pair
4680 * of 1-wide MOVs and scattering the result.
4681 */
4682 fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
4683 ubld.MOV(res1, brw_imm_d(0));
4684 set_predicate(dispatch_width == 8 ? BRW_PREDICATE_ALIGN1_ANY8H :
4685 dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ANY16H :
4686 BRW_PREDICATE_ALIGN1_ANY32H,
4687 ubld.MOV(res1, brw_imm_d(-1)));
4688
4689 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
4690 break;
4691 }
4692 case nir_intrinsic_vote_all: {
4693 const fs_builder ubld = bld.exec_all().group(1, 0);
4694
4695 /* The any/all predicates do not consider channel enables. To prevent
4696 * dead channels from affecting the result, we initialize the flag with
4697 * with the identity value for the logical operation.
4698 */
4699 if (dispatch_width == 32) {
4700 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4701 ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
4702 brw_imm_ud(0xffffffff));
4703 } else {
4704 ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4705 }
4706 bld.CMP(bld.null_reg_d(), get_nir_src(instr->src[0]), brw_imm_d(0), BRW_CONDITIONAL_NZ);
4707
4708 /* For some reason, the any/all predicates don't work properly with
4709 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4710 * doesn't read the correct subset of the flag register and you end up
4711 * getting garbage in the second half. Work around this by using a pair
4712 * of 1-wide MOVs and scattering the result.
4713 */
4714 fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
4715 ubld.MOV(res1, brw_imm_d(0));
4716 set_predicate(dispatch_width == 8 ? BRW_PREDICATE_ALIGN1_ALL8H :
4717 dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ALL16H :
4718 BRW_PREDICATE_ALIGN1_ALL32H,
4719 ubld.MOV(res1, brw_imm_d(-1)));
4720
4721 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
4722 break;
4723 }
4724 case nir_intrinsic_vote_feq:
4725 case nir_intrinsic_vote_ieq: {
4726 fs_reg value = get_nir_src(instr->src[0]);
4727 if (instr->intrinsic == nir_intrinsic_vote_feq) {
4728 const unsigned bit_size = nir_src_bit_size(instr->src[0]);
4729 value.type = bit_size == 8 ? BRW_REGISTER_TYPE_B :
4730 brw_reg_type_from_bit_size(bit_size, BRW_REGISTER_TYPE_F);
4731 }
4732
4733 fs_reg uniformized = bld.emit_uniformize(value);
4734 const fs_builder ubld = bld.exec_all().group(1, 0);
4735
4736 /* The any/all predicates do not consider channel enables. To prevent
4737 * dead channels from affecting the result, we initialize the flag with
4738 * with the identity value for the logical operation.
4739 */
4740 if (dispatch_width == 32) {
4741 /* For SIMD32, we use a UD type so we fill both f0.0 and f0.1. */
4742 ubld.MOV(retype(brw_flag_reg(0, 0), BRW_REGISTER_TYPE_UD),
4743 brw_imm_ud(0xffffffff));
4744 } else {
4745 ubld.MOV(brw_flag_reg(0, 0), brw_imm_uw(0xffff));
4746 }
4747 bld.CMP(bld.null_reg_d(), value, uniformized, BRW_CONDITIONAL_Z);
4748
4749 /* For some reason, the any/all predicates don't work properly with
4750 * SIMD32. In particular, it appears that a SEL with a QtrCtrl of 2H
4751 * doesn't read the correct subset of the flag register and you end up
4752 * getting garbage in the second half. Work around this by using a pair
4753 * of 1-wide MOVs and scattering the result.
4754 */
4755 fs_reg res1 = ubld.vgrf(BRW_REGISTER_TYPE_D);
4756 ubld.MOV(res1, brw_imm_d(0));
4757 set_predicate(dispatch_width == 8 ? BRW_PREDICATE_ALIGN1_ALL8H :
4758 dispatch_width == 16 ? BRW_PREDICATE_ALIGN1_ALL16H :
4759 BRW_PREDICATE_ALIGN1_ALL32H,
4760 ubld.MOV(res1, brw_imm_d(-1)));
4761
4762 bld.MOV(retype(dest, BRW_REGISTER_TYPE_D), component(res1, 0));
4763 break;
4764 }
4765
4766 case nir_intrinsic_ballot: {
4767 const fs_reg value = retype(get_nir_src(instr->src[0]),
4768 BRW_REGISTER_TYPE_UD);
4769 struct brw_reg flag = brw_flag_reg(0, 0);
4770 /* FIXME: For SIMD32 programs, this causes us to stomp on f0.1 as well
4771 * as f0.0. This is a problem for fragment programs as we currently use
4772 * f0.1 for discards. Fortunately, we don't support SIMD32 fragment
4773 * programs yet so this isn't a problem. When we do, something will
4774 * have to change.
4775 */
4776 if (dispatch_width == 32)
4777 flag.type = BRW_REGISTER_TYPE_UD;
4778
4779 bld.exec_all().group(1, 0).MOV(flag, brw_imm_ud(0u));
4780 bld.CMP(bld.null_reg_ud(), value, brw_imm_ud(0u), BRW_CONDITIONAL_NZ);
4781
4782 if (instr->dest.ssa.bit_size > 32) {
4783 dest.type = BRW_REGISTER_TYPE_UQ;
4784 } else {
4785 dest.type = BRW_REGISTER_TYPE_UD;
4786 }
4787 bld.MOV(dest, flag);
4788 break;
4789 }
4790
4791 case nir_intrinsic_read_invocation: {
4792 const fs_reg value = get_nir_src(instr->src[0]);
4793 const fs_reg invocation = get_nir_src(instr->src[1]);
4794 fs_reg tmp = bld.vgrf(value.type);
4795
4796 bld.exec_all().emit(SHADER_OPCODE_BROADCAST, tmp, value,
4797 bld.emit_uniformize(invocation));
4798
4799 bld.MOV(retype(dest, value.type), fs_reg(component(tmp, 0)));
4800 break;
4801 }
4802
4803 case nir_intrinsic_read_first_invocation: {
4804 const fs_reg value = get_nir_src(instr->src[0]);
4805 bld.MOV(retype(dest, value.type), bld.emit_uniformize(value));
4806 break;
4807 }
4808
4809 case nir_intrinsic_shuffle: {
4810 const fs_reg value = get_nir_src(instr->src[0]);
4811 const fs_reg index = get_nir_src(instr->src[1]);
4812
4813 bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, index);
4814 break;
4815 }
4816
4817 case nir_intrinsic_first_invocation: {
4818 fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD);
4819 bld.exec_all().emit(SHADER_OPCODE_FIND_LIVE_CHANNEL, tmp);
4820 bld.MOV(retype(dest, BRW_REGISTER_TYPE_UD),
4821 fs_reg(component(tmp, 0)));
4822 break;
4823 }
4824
4825 case nir_intrinsic_quad_broadcast: {
4826 const fs_reg value = get_nir_src(instr->src[0]);
4827 const unsigned index = nir_src_as_uint(instr->src[1]);
4828
4829 bld.emit(SHADER_OPCODE_CLUSTER_BROADCAST, retype(dest, value.type),
4830 value, brw_imm_ud(index), brw_imm_ud(4));
4831 break;
4832 }
4833
4834 case nir_intrinsic_quad_swap_horizontal: {
4835 const fs_reg value = get_nir_src(instr->src[0]);
4836 const fs_reg tmp = bld.vgrf(value.type);
4837 if (devinfo->gen <= 7) {
4838 /* The hardware doesn't seem to support these crazy regions with
4839 * compressed instructions on gen7 and earlier so we fall back to
4840 * using quad swizzles. Fortunately, we don't support 64-bit
4841 * anything in Vulkan on gen7.
4842 */
4843 assert(nir_src_bit_size(instr->src[0]) == 32);
4844 const fs_builder ubld = bld.exec_all();
4845 ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
4846 brw_imm_ud(BRW_SWIZZLE4(1,0,3,2)));
4847 bld.MOV(retype(dest, value.type), tmp);
4848 } else {
4849 const fs_builder ubld = bld.exec_all().group(dispatch_width / 2, 0);
4850
4851 const fs_reg src_left = horiz_stride(value, 2);
4852 const fs_reg src_right = horiz_stride(horiz_offset(value, 1), 2);
4853 const fs_reg tmp_left = horiz_stride(tmp, 2);
4854 const fs_reg tmp_right = horiz_stride(horiz_offset(tmp, 1), 2);
4855
4856 ubld.MOV(tmp_left, src_right);
4857 ubld.MOV(tmp_right, src_left);
4858
4859 }
4860 bld.MOV(retype(dest, value.type), tmp);
4861 break;
4862 }
4863
4864 case nir_intrinsic_quad_swap_vertical: {
4865 const fs_reg value = get_nir_src(instr->src[0]);
4866 if (nir_src_bit_size(instr->src[0]) == 32) {
4867 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
4868 const fs_reg tmp = bld.vgrf(value.type);
4869 const fs_builder ubld = bld.exec_all();
4870 ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
4871 brw_imm_ud(BRW_SWIZZLE4(2,3,0,1)));
4872 bld.MOV(retype(dest, value.type), tmp);
4873 } else {
4874 /* For larger data types, we have to either emit dispatch_width many
4875 * MOVs or else fall back to doing indirects.
4876 */
4877 fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
4878 bld.XOR(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
4879 brw_imm_w(0x2));
4880 bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, idx);
4881 }
4882 break;
4883 }
4884
4885 case nir_intrinsic_quad_swap_diagonal: {
4886 const fs_reg value = get_nir_src(instr->src[0]);
4887 if (nir_src_bit_size(instr->src[0]) == 32) {
4888 /* For 32-bit, we can use a SIMD4x2 instruction to do this easily */
4889 const fs_reg tmp = bld.vgrf(value.type);
4890 const fs_builder ubld = bld.exec_all();
4891 ubld.emit(SHADER_OPCODE_QUAD_SWIZZLE, tmp, value,
4892 brw_imm_ud(BRW_SWIZZLE4(3,2,1,0)));
4893 bld.MOV(retype(dest, value.type), tmp);
4894 } else {
4895 /* For larger data types, we have to either emit dispatch_width many
4896 * MOVs or else fall back to doing indirects.
4897 */
4898 fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
4899 bld.XOR(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
4900 brw_imm_w(0x3));
4901 bld.emit(SHADER_OPCODE_SHUFFLE, retype(dest, value.type), value, idx);
4902 }
4903 break;
4904 }
4905
4906 case nir_intrinsic_reduce: {
4907 fs_reg src = get_nir_src(instr->src[0]);
4908 nir_op redop = (nir_op)nir_intrinsic_reduction_op(instr);
4909 unsigned cluster_size = nir_intrinsic_cluster_size(instr);
4910 if (cluster_size == 0 || cluster_size > dispatch_width)
4911 cluster_size = dispatch_width;
4912
4913 /* Figure out the source type */
4914 src.type = brw_type_for_nir_type(devinfo,
4915 (nir_alu_type)(nir_op_infos[redop].input_types[0] |
4916 nir_src_bit_size(instr->src[0])));
4917
4918 fs_reg identity = brw_nir_reduction_op_identity(bld, redop, src.type);
4919 opcode brw_op = brw_op_for_nir_reduction_op(redop);
4920 brw_conditional_mod cond_mod = brw_cond_mod_for_nir_reduction_op(redop);
4921
4922 /* Set up a register for all of our scratching around and initialize it
4923 * to reduction operation's identity value.
4924 */
4925 fs_reg scan = bld.vgrf(src.type);
4926 bld.exec_all().emit(SHADER_OPCODE_SEL_EXEC, scan, src, identity);
4927
4928 bld.emit_scan(brw_op, scan, cluster_size, cond_mod);
4929
4930 dest.type = src.type;
4931 if (cluster_size * type_sz(src.type) >= REG_SIZE * 2) {
4932 /* In this case, CLUSTER_BROADCAST instruction isn't needed because
4933 * the distance between clusters is at least 2 GRFs. In this case,
4934 * we don't need the weird striding of the CLUSTER_BROADCAST
4935 * instruction and can just do regular MOVs.
4936 */
4937 assert((cluster_size * type_sz(src.type)) % (REG_SIZE * 2) == 0);
4938 const unsigned groups =
4939 (dispatch_width * type_sz(src.type)) / (REG_SIZE * 2);
4940 const unsigned group_size = dispatch_width / groups;
4941 for (unsigned i = 0; i < groups; i++) {
4942 const unsigned cluster = (i * group_size) / cluster_size;
4943 const unsigned comp = cluster * cluster_size + (cluster_size - 1);
4944 bld.group(group_size, i).MOV(horiz_offset(dest, i * group_size),
4945 component(scan, comp));
4946 }
4947 } else {
4948 bld.emit(SHADER_OPCODE_CLUSTER_BROADCAST, dest, scan,
4949 brw_imm_ud(cluster_size - 1), brw_imm_ud(cluster_size));
4950 }
4951 break;
4952 }
4953
4954 case nir_intrinsic_inclusive_scan:
4955 case nir_intrinsic_exclusive_scan: {
4956 fs_reg src = get_nir_src(instr->src[0]);
4957 nir_op redop = (nir_op)nir_intrinsic_reduction_op(instr);
4958
4959 /* Figure out the source type */
4960 src.type = brw_type_for_nir_type(devinfo,
4961 (nir_alu_type)(nir_op_infos[redop].input_types[0] |
4962 nir_src_bit_size(instr->src[0])));
4963
4964 fs_reg identity = brw_nir_reduction_op_identity(bld, redop, src.type);
4965 opcode brw_op = brw_op_for_nir_reduction_op(redop);
4966 brw_conditional_mod cond_mod = brw_cond_mod_for_nir_reduction_op(redop);
4967
4968 /* Set up a register for all of our scratching around and initialize it
4969 * to reduction operation's identity value.
4970 */
4971 fs_reg scan = bld.vgrf(src.type);
4972 const fs_builder allbld = bld.exec_all();
4973 allbld.emit(SHADER_OPCODE_SEL_EXEC, scan, src, identity);
4974
4975 if (instr->intrinsic == nir_intrinsic_exclusive_scan) {
4976 /* Exclusive scan is a bit harder because we have to do an annoying
4977 * shift of the contents before we can begin. To make things worse,
4978 * we can't do this with a normal stride; we have to use indirects.
4979 */
4980 fs_reg shifted = bld.vgrf(src.type);
4981 fs_reg idx = bld.vgrf(BRW_REGISTER_TYPE_W);
4982 allbld.ADD(idx, nir_system_values[SYSTEM_VALUE_SUBGROUP_INVOCATION],
4983 brw_imm_w(-1));
4984 allbld.emit(SHADER_OPCODE_SHUFFLE, shifted, scan, idx);
4985 allbld.group(1, 0).MOV(component(shifted, 0), identity);
4986 scan = shifted;
4987 }
4988
4989 bld.emit_scan(brw_op, scan, dispatch_width, cond_mod);
4990
4991 bld.MOV(retype(dest, src.type), scan);
4992 break;
4993 }
4994
4995 case nir_intrinsic_begin_invocation_interlock: {
4996 const fs_builder ubld = bld.group(8, 0);
4997 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
4998
4999 ubld.emit(SHADER_OPCODE_INTERLOCK, tmp, brw_vec8_grf(0, 0))
5000 ->size_written = 2 * REG_SIZE;
5001 break;
5002 }
5003
5004 case nir_intrinsic_end_invocation_interlock: {
5005 /* For endInvocationInterlock(), we need to insert a memory fence which
5006 * stalls in the shader until the memory transactions prior to that
5007 * fence are complete. This ensures that the shader does not end before
5008 * any writes from its critical section have landed. Otherwise, you can
5009 * end up with a case where the next invocation on that pixel properly
5010 * stalls for previous FS invocation on its pixel to complete but
5011 * doesn't actually wait for the dataport memory transactions from that
5012 * thread to land before submitting its own.
5013 */
5014 const fs_builder ubld = bld.group(8, 0);
5015 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
5016 ubld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp,
5017 brw_vec8_grf(0, 0), brw_imm_ud(1), brw_imm_ud(0))
5018 ->size_written = 2 * REG_SIZE;
5019 break;
5020 }
5021
5022 default:
5023 unreachable("unknown intrinsic");
5024 }
5025 }
5026
5027 void
5028 fs_visitor::nir_emit_ssbo_atomic(const fs_builder &bld,
5029 int op, nir_intrinsic_instr *instr)
5030 {
5031 if (stage == MESA_SHADER_FRAGMENT)
5032 brw_wm_prog_data(prog_data)->has_side_effects = true;
5033
5034 /* The BTI untyped atomic messages only support 32-bit atomics. If you
5035 * just look at the big table of messages in the Vol 7 of the SKL PRM, they
5036 * appear to exist. However, if you look at Vol 2a, there are no message
5037 * descriptors provided for Qword atomic ops except for A64 messages.
5038 */
5039 assert(nir_dest_bit_size(instr->dest) == 32);
5040
5041 fs_reg dest;
5042 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5043 dest = get_nir_dest(instr->dest);
5044
5045 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5046 srcs[SURFACE_LOGICAL_SRC_SURFACE] = get_nir_ssbo_intrinsic_index(bld, instr);
5047 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
5048 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5049 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5050
5051 fs_reg data;
5052 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
5053 data = get_nir_src(instr->src[2]);
5054
5055 if (op == BRW_AOP_CMPWR) {
5056 fs_reg tmp = bld.vgrf(data.type, 2);
5057 fs_reg sources[2] = { data, get_nir_src(instr->src[3]) };
5058 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5059 data = tmp;
5060 }
5061 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5062
5063 /* Emit the actual atomic operation */
5064
5065 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL,
5066 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5067 }
5068
5069 void
5070 fs_visitor::nir_emit_ssbo_atomic_float(const fs_builder &bld,
5071 int op, nir_intrinsic_instr *instr)
5072 {
5073 if (stage == MESA_SHADER_FRAGMENT)
5074 brw_wm_prog_data(prog_data)->has_side_effects = true;
5075
5076 fs_reg dest;
5077 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5078 dest = get_nir_dest(instr->dest);
5079
5080 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5081 srcs[SURFACE_LOGICAL_SRC_SURFACE] = get_nir_ssbo_intrinsic_index(bld, instr);
5082 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = get_nir_src(instr->src[1]);
5083 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5084 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5085
5086 fs_reg data = get_nir_src(instr->src[2]);
5087 if (op == BRW_AOP_FCMPWR) {
5088 fs_reg tmp = bld.vgrf(data.type, 2);
5089 fs_reg sources[2] = { data, get_nir_src(instr->src[3]) };
5090 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5091 data = tmp;
5092 }
5093 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5094
5095 /* Emit the actual atomic operation */
5096
5097 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL,
5098 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5099 }
5100
5101 void
5102 fs_visitor::nir_emit_shared_atomic(const fs_builder &bld,
5103 int op, nir_intrinsic_instr *instr)
5104 {
5105 fs_reg dest;
5106 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5107 dest = get_nir_dest(instr->dest);
5108
5109 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5110 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
5111 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5112 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5113
5114 fs_reg data;
5115 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
5116 data = get_nir_src(instr->src[1]);
5117 if (op == BRW_AOP_CMPWR) {
5118 fs_reg tmp = bld.vgrf(data.type, 2);
5119 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5120 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5121 data = tmp;
5122 }
5123 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5124
5125 /* Get the offset */
5126 if (nir_src_is_const(instr->src[0])) {
5127 srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
5128 brw_imm_ud(instr->const_index[0] + nir_src_as_uint(instr->src[0]));
5129 } else {
5130 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = vgrf(glsl_type::uint_type);
5131 bld.ADD(srcs[SURFACE_LOGICAL_SRC_ADDRESS],
5132 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
5133 brw_imm_ud(instr->const_index[0]));
5134 }
5135
5136 /* Emit the actual atomic operation operation */
5137
5138 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL,
5139 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5140 }
5141
5142 void
5143 fs_visitor::nir_emit_shared_atomic_float(const fs_builder &bld,
5144 int op, nir_intrinsic_instr *instr)
5145 {
5146 fs_reg dest;
5147 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5148 dest = get_nir_dest(instr->dest);
5149
5150 fs_reg srcs[SURFACE_LOGICAL_NUM_SRCS];
5151 srcs[SURFACE_LOGICAL_SRC_SURFACE] = brw_imm_ud(GEN7_BTI_SLM);
5152 srcs[SURFACE_LOGICAL_SRC_IMM_DIMS] = brw_imm_ud(1);
5153 srcs[SURFACE_LOGICAL_SRC_IMM_ARG] = brw_imm_ud(op);
5154
5155 fs_reg data = get_nir_src(instr->src[1]);
5156 if (op == BRW_AOP_FCMPWR) {
5157 fs_reg tmp = bld.vgrf(data.type, 2);
5158 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5159 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5160 data = tmp;
5161 }
5162 srcs[SURFACE_LOGICAL_SRC_DATA] = data;
5163
5164 /* Get the offset */
5165 if (nir_src_is_const(instr->src[0])) {
5166 srcs[SURFACE_LOGICAL_SRC_ADDRESS] =
5167 brw_imm_ud(instr->const_index[0] + nir_src_as_uint(instr->src[0]));
5168 } else {
5169 srcs[SURFACE_LOGICAL_SRC_ADDRESS] = vgrf(glsl_type::uint_type);
5170 bld.ADD(srcs[SURFACE_LOGICAL_SRC_ADDRESS],
5171 retype(get_nir_src(instr->src[0]), BRW_REGISTER_TYPE_UD),
5172 brw_imm_ud(instr->const_index[0]));
5173 }
5174
5175 /* Emit the actual atomic operation operation */
5176
5177 bld.emit(SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL,
5178 dest, srcs, SURFACE_LOGICAL_NUM_SRCS);
5179 }
5180
5181 void
5182 fs_visitor::nir_emit_global_atomic(const fs_builder &bld,
5183 int op, nir_intrinsic_instr *instr)
5184 {
5185 if (stage == MESA_SHADER_FRAGMENT)
5186 brw_wm_prog_data(prog_data)->has_side_effects = true;
5187
5188 fs_reg dest;
5189 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
5190 dest = get_nir_dest(instr->dest);
5191
5192 fs_reg addr = get_nir_src(instr->src[0]);
5193
5194 fs_reg data;
5195 if (op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC)
5196 data = get_nir_src(instr->src[1]);
5197
5198 if (op == BRW_AOP_CMPWR) {
5199 fs_reg tmp = bld.vgrf(data.type, 2);
5200 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5201 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5202 data = tmp;
5203 }
5204
5205 if (nir_dest_bit_size(instr->dest) == 64) {
5206 bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_INT64_LOGICAL,
5207 dest, addr, data, brw_imm_ud(op));
5208 } else {
5209 assert(nir_dest_bit_size(instr->dest) == 32);
5210 bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL,
5211 dest, addr, data, brw_imm_ud(op));
5212 }
5213 }
5214
5215 void
5216 fs_visitor::nir_emit_global_atomic_float(const fs_builder &bld,
5217 int op, nir_intrinsic_instr *instr)
5218 {
5219 if (stage == MESA_SHADER_FRAGMENT)
5220 brw_wm_prog_data(prog_data)->has_side_effects = true;
5221
5222 assert(nir_intrinsic_infos[instr->intrinsic].has_dest);
5223 fs_reg dest = get_nir_dest(instr->dest);
5224
5225 fs_reg addr = get_nir_src(instr->src[0]);
5226
5227 assert(op != BRW_AOP_INC && op != BRW_AOP_DEC && op != BRW_AOP_PREDEC);
5228 fs_reg data = get_nir_src(instr->src[1]);
5229
5230 if (op == BRW_AOP_FCMPWR) {
5231 fs_reg tmp = bld.vgrf(data.type, 2);
5232 fs_reg sources[2] = { data, get_nir_src(instr->src[2]) };
5233 bld.LOAD_PAYLOAD(tmp, sources, 2, 0);
5234 data = tmp;
5235 }
5236
5237 bld.emit(SHADER_OPCODE_A64_UNTYPED_ATOMIC_LOGICAL,
5238 dest, addr, data, brw_imm_ud(op));
5239 }
5240
5241 void
5242 fs_visitor::nir_emit_texture(const fs_builder &bld, nir_tex_instr *instr)
5243 {
5244 unsigned texture = instr->texture_index;
5245 unsigned sampler = instr->sampler_index;
5246
5247 fs_reg srcs[TEX_LOGICAL_NUM_SRCS];
5248
5249 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture);
5250 srcs[TEX_LOGICAL_SRC_SAMPLER] = brw_imm_ud(sampler);
5251
5252 int lod_components = 0;
5253
5254 /* The hardware requires a LOD for buffer textures */
5255 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
5256 srcs[TEX_LOGICAL_SRC_LOD] = brw_imm_d(0);
5257
5258 uint32_t header_bits = 0;
5259 for (unsigned i = 0; i < instr->num_srcs; i++) {
5260 fs_reg src = get_nir_src(instr->src[i].src);
5261 switch (instr->src[i].src_type) {
5262 case nir_tex_src_bias:
5263 srcs[TEX_LOGICAL_SRC_LOD] =
5264 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
5265 break;
5266 case nir_tex_src_comparator:
5267 srcs[TEX_LOGICAL_SRC_SHADOW_C] = retype(src, BRW_REGISTER_TYPE_F);
5268 break;
5269 case nir_tex_src_coord:
5270 switch (instr->op) {
5271 case nir_texop_txf:
5272 case nir_texop_txf_ms:
5273 case nir_texop_txf_ms_mcs:
5274 case nir_texop_samples_identical:
5275 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_D);
5276 break;
5277 default:
5278 srcs[TEX_LOGICAL_SRC_COORDINATE] = retype(src, BRW_REGISTER_TYPE_F);
5279 break;
5280 }
5281 break;
5282 case nir_tex_src_ddx:
5283 srcs[TEX_LOGICAL_SRC_LOD] = retype(src, BRW_REGISTER_TYPE_F);
5284 lod_components = nir_tex_instr_src_size(instr, i);
5285 break;
5286 case nir_tex_src_ddy:
5287 srcs[TEX_LOGICAL_SRC_LOD2] = retype(src, BRW_REGISTER_TYPE_F);
5288 break;
5289 case nir_tex_src_lod:
5290 switch (instr->op) {
5291 case nir_texop_txs:
5292 srcs[TEX_LOGICAL_SRC_LOD] =
5293 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_UD);
5294 break;
5295 case nir_texop_txf:
5296 srcs[TEX_LOGICAL_SRC_LOD] =
5297 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_D);
5298 break;
5299 default:
5300 srcs[TEX_LOGICAL_SRC_LOD] =
5301 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
5302 break;
5303 }
5304 break;
5305 case nir_tex_src_min_lod:
5306 srcs[TEX_LOGICAL_SRC_MIN_LOD] =
5307 retype(get_nir_src_imm(instr->src[i].src), BRW_REGISTER_TYPE_F);
5308 break;
5309 case nir_tex_src_ms_index:
5310 srcs[TEX_LOGICAL_SRC_SAMPLE_INDEX] = retype(src, BRW_REGISTER_TYPE_UD);
5311 break;
5312
5313 case nir_tex_src_offset: {
5314 uint32_t offset_bits = 0;
5315 if (brw_texture_offset(instr, i, &offset_bits)) {
5316 header_bits |= offset_bits;
5317 } else {
5318 srcs[TEX_LOGICAL_SRC_TG4_OFFSET] =
5319 retype(src, BRW_REGISTER_TYPE_D);
5320 }
5321 break;
5322 }
5323
5324 case nir_tex_src_projector:
5325 unreachable("should be lowered");
5326
5327 case nir_tex_src_texture_offset: {
5328 /* Emit code to evaluate the actual indexing expression */
5329 fs_reg tmp = vgrf(glsl_type::uint_type);
5330 bld.ADD(tmp, src, brw_imm_ud(texture));
5331 srcs[TEX_LOGICAL_SRC_SURFACE] = bld.emit_uniformize(tmp);
5332 break;
5333 }
5334
5335 case nir_tex_src_sampler_offset: {
5336 /* Emit code to evaluate the actual indexing expression */
5337 fs_reg tmp = vgrf(glsl_type::uint_type);
5338 bld.ADD(tmp, src, brw_imm_ud(sampler));
5339 srcs[TEX_LOGICAL_SRC_SAMPLER] = bld.emit_uniformize(tmp);
5340 break;
5341 }
5342
5343 case nir_tex_src_texture_handle:
5344 assert(nir_tex_instr_src_index(instr, nir_tex_src_texture_offset) == -1);
5345 srcs[TEX_LOGICAL_SRC_SURFACE] = fs_reg();
5346 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE] = bld.emit_uniformize(src);
5347 break;
5348
5349 case nir_tex_src_sampler_handle:
5350 assert(nir_tex_instr_src_index(instr, nir_tex_src_sampler_offset) == -1);
5351 srcs[TEX_LOGICAL_SRC_SAMPLER] = fs_reg();
5352 srcs[TEX_LOGICAL_SRC_SAMPLER_HANDLE] = bld.emit_uniformize(src);
5353 break;
5354
5355 case nir_tex_src_ms_mcs:
5356 assert(instr->op == nir_texop_txf_ms);
5357 srcs[TEX_LOGICAL_SRC_MCS] = retype(src, BRW_REGISTER_TYPE_D);
5358 break;
5359
5360 case nir_tex_src_plane: {
5361 const uint32_t plane = nir_src_as_uint(instr->src[i].src);
5362 const uint32_t texture_index =
5363 instr->texture_index +
5364 stage_prog_data->binding_table.plane_start[plane] -
5365 stage_prog_data->binding_table.texture_start;
5366
5367 srcs[TEX_LOGICAL_SRC_SURFACE] = brw_imm_ud(texture_index);
5368 break;
5369 }
5370
5371 default:
5372 unreachable("unknown texture source");
5373 }
5374 }
5375
5376 if (srcs[TEX_LOGICAL_SRC_MCS].file == BAD_FILE &&
5377 (instr->op == nir_texop_txf_ms ||
5378 instr->op == nir_texop_samples_identical)) {
5379 if (devinfo->gen >= 7 &&
5380 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
5381 srcs[TEX_LOGICAL_SRC_MCS] =
5382 emit_mcs_fetch(srcs[TEX_LOGICAL_SRC_COORDINATE],
5383 instr->coord_components,
5384 srcs[TEX_LOGICAL_SRC_SURFACE],
5385 srcs[TEX_LOGICAL_SRC_SURFACE_HANDLE]);
5386 } else {
5387 srcs[TEX_LOGICAL_SRC_MCS] = brw_imm_ud(0u);
5388 }
5389 }
5390
5391 srcs[TEX_LOGICAL_SRC_COORD_COMPONENTS] = brw_imm_d(instr->coord_components);
5392 srcs[TEX_LOGICAL_SRC_GRAD_COMPONENTS] = brw_imm_d(lod_components);
5393
5394 enum opcode opcode;
5395 switch (instr->op) {
5396 case nir_texop_tex:
5397 opcode = SHADER_OPCODE_TEX_LOGICAL;
5398 break;
5399 case nir_texop_txb:
5400 opcode = FS_OPCODE_TXB_LOGICAL;
5401 break;
5402 case nir_texop_txl:
5403 opcode = SHADER_OPCODE_TXL_LOGICAL;
5404 break;
5405 case nir_texop_txd:
5406 opcode = SHADER_OPCODE_TXD_LOGICAL;
5407 break;
5408 case nir_texop_txf:
5409 opcode = SHADER_OPCODE_TXF_LOGICAL;
5410 break;
5411 case nir_texop_txf_ms:
5412 if ((key_tex->msaa_16 & (1 << sampler)))
5413 opcode = SHADER_OPCODE_TXF_CMS_W_LOGICAL;
5414 else
5415 opcode = SHADER_OPCODE_TXF_CMS_LOGICAL;
5416 break;
5417 case nir_texop_txf_ms_mcs:
5418 opcode = SHADER_OPCODE_TXF_MCS_LOGICAL;
5419 break;
5420 case nir_texop_query_levels:
5421 case nir_texop_txs:
5422 opcode = SHADER_OPCODE_TXS_LOGICAL;
5423 break;
5424 case nir_texop_lod:
5425 opcode = SHADER_OPCODE_LOD_LOGICAL;
5426 break;
5427 case nir_texop_tg4:
5428 if (srcs[TEX_LOGICAL_SRC_TG4_OFFSET].file != BAD_FILE)
5429 opcode = SHADER_OPCODE_TG4_OFFSET_LOGICAL;
5430 else
5431 opcode = SHADER_OPCODE_TG4_LOGICAL;
5432 break;
5433 case nir_texop_texture_samples:
5434 opcode = SHADER_OPCODE_SAMPLEINFO_LOGICAL;
5435 break;
5436 case nir_texop_samples_identical: {
5437 fs_reg dst = retype(get_nir_dest(instr->dest), BRW_REGISTER_TYPE_D);
5438
5439 /* If mcs is an immediate value, it means there is no MCS. In that case
5440 * just return false.
5441 */
5442 if (srcs[TEX_LOGICAL_SRC_MCS].file == BRW_IMMEDIATE_VALUE) {
5443 bld.MOV(dst, brw_imm_ud(0u));
5444 } else if ((key_tex->msaa_16 & (1 << sampler))) {
5445 fs_reg tmp = vgrf(glsl_type::uint_type);
5446 bld.OR(tmp, srcs[TEX_LOGICAL_SRC_MCS],
5447 offset(srcs[TEX_LOGICAL_SRC_MCS], bld, 1));
5448 bld.CMP(dst, tmp, brw_imm_ud(0u), BRW_CONDITIONAL_EQ);
5449 } else {
5450 bld.CMP(dst, srcs[TEX_LOGICAL_SRC_MCS], brw_imm_ud(0u),
5451 BRW_CONDITIONAL_EQ);
5452 }
5453 return;
5454 }
5455 default:
5456 unreachable("unknown texture opcode");
5457 }
5458
5459 if (instr->op == nir_texop_tg4) {
5460 if (instr->component == 1 &&
5461 key_tex->gather_channel_quirk_mask & (1 << texture)) {
5462 /* gather4 sampler is broken for green channel on RG32F --
5463 * we must ask for blue instead.
5464 */
5465 header_bits |= 2 << 16;
5466 } else {
5467 header_bits |= instr->component << 16;
5468 }
5469 }
5470
5471 fs_reg dst = bld.vgrf(brw_type_for_nir_type(devinfo, instr->dest_type), 4);
5472 fs_inst *inst = bld.emit(opcode, dst, srcs, ARRAY_SIZE(srcs));
5473 inst->offset = header_bits;
5474
5475 const unsigned dest_size = nir_tex_instr_dest_size(instr);
5476 if (devinfo->gen >= 9 &&
5477 instr->op != nir_texop_tg4 && instr->op != nir_texop_query_levels) {
5478 unsigned write_mask = instr->dest.is_ssa ?
5479 nir_ssa_def_components_read(&instr->dest.ssa):
5480 (1 << dest_size) - 1;
5481 assert(write_mask != 0); /* dead code should have been eliminated */
5482 inst->size_written = util_last_bit(write_mask) *
5483 inst->dst.component_size(inst->exec_size);
5484 } else {
5485 inst->size_written = 4 * inst->dst.component_size(inst->exec_size);
5486 }
5487
5488 if (srcs[TEX_LOGICAL_SRC_SHADOW_C].file != BAD_FILE)
5489 inst->shadow_compare = true;
5490
5491 if (instr->op == nir_texop_tg4 && devinfo->gen == 6)
5492 emit_gen6_gather_wa(key_tex->gen6_gather_wa[texture], dst);
5493
5494 fs_reg nir_dest[4];
5495 for (unsigned i = 0; i < dest_size; i++)
5496 nir_dest[i] = offset(dst, bld, i);
5497
5498 if (instr->op == nir_texop_query_levels) {
5499 /* # levels is in .w */
5500 nir_dest[0] = offset(dst, bld, 3);
5501 } else if (instr->op == nir_texop_txs &&
5502 dest_size >= 3 && devinfo->gen < 7) {
5503 /* Gen4-6 return 0 instead of 1 for single layer surfaces. */
5504 fs_reg depth = offset(dst, bld, 2);
5505 nir_dest[2] = vgrf(glsl_type::int_type);
5506 bld.emit_minmax(nir_dest[2], depth, brw_imm_d(1), BRW_CONDITIONAL_GE);
5507 }
5508
5509 bld.LOAD_PAYLOAD(get_nir_dest(instr->dest), nir_dest, dest_size, 0);
5510 }
5511
5512 void
5513 fs_visitor::nir_emit_jump(const fs_builder &bld, nir_jump_instr *instr)
5514 {
5515 switch (instr->type) {
5516 case nir_jump_break:
5517 bld.emit(BRW_OPCODE_BREAK);
5518 break;
5519 case nir_jump_continue:
5520 bld.emit(BRW_OPCODE_CONTINUE);
5521 break;
5522 case nir_jump_return:
5523 default:
5524 unreachable("unknown jump");
5525 }
5526 }
5527
5528 /*
5529 * This helper takes a source register and un/shuffles it into the destination
5530 * register.
5531 *
5532 * If source type size is smaller than destination type size the operation
5533 * needed is a component shuffle. The opposite case would be an unshuffle. If
5534 * source/destination type size is equal a shuffle is done that would be
5535 * equivalent to a simple MOV.
5536 *
5537 * For example, if source is a 16-bit type and destination is 32-bit. A 3
5538 * components .xyz 16-bit vector on SIMD8 would be.
5539 *
5540 * |x1|x2|x3|x4|x5|x6|x7|x8|y1|y2|y3|y4|y5|y6|y7|y8|
5541 * |z1|z2|z3|z4|z5|z6|z7|z8| | | | | | | | |
5542 *
5543 * This helper will return the following 2 32-bit components with the 16-bit
5544 * values shuffled:
5545 *
5546 * |x1 y1|x2 y2|x3 y3|x4 y4|x5 y5|x6 y6|x7 y7|x8 y8|
5547 * |z1 |z2 |z3 |z4 |z5 |z6 |z7 |z8 |
5548 *
5549 * For unshuffle, the example would be the opposite, a 64-bit type source
5550 * and a 32-bit destination. A 2 component .xy 64-bit vector on SIMD8
5551 * would be:
5552 *
5553 * | x1l x1h | x2l x2h | x3l x3h | x4l x4h |
5554 * | x5l x5h | x6l x6h | x7l x7h | x8l x8h |
5555 * | y1l y1h | y2l y2h | y3l y3h | y4l y4h |
5556 * | y5l y5h | y6l y6h | y7l y7h | y8l y8h |
5557 *
5558 * The returned result would be the following 4 32-bit components unshuffled:
5559 *
5560 * | x1l | x2l | x3l | x4l | x5l | x6l | x7l | x8l |
5561 * | x1h | x2h | x3h | x4h | x5h | x6h | x7h | x8h |
5562 * | y1l | y2l | y3l | y4l | y5l | y6l | y7l | y8l |
5563 * | y1h | y2h | y3h | y4h | y5h | y6h | y7h | y8h |
5564 *
5565 * - Source and destination register must not be overlapped.
5566 * - components units are measured in terms of the smaller type between
5567 * source and destination because we are un/shuffling the smaller
5568 * components from/into the bigger ones.
5569 * - first_component parameter allows skipping source components.
5570 */
5571 void
5572 shuffle_src_to_dst(const fs_builder &bld,
5573 const fs_reg &dst,
5574 const fs_reg &src,
5575 uint32_t first_component,
5576 uint32_t components)
5577 {
5578 if (type_sz(src.type) == type_sz(dst.type)) {
5579 assert(!regions_overlap(dst,
5580 type_sz(dst.type) * bld.dispatch_width() * components,
5581 offset(src, bld, first_component),
5582 type_sz(src.type) * bld.dispatch_width() * components));
5583 for (unsigned i = 0; i < components; i++) {
5584 bld.MOV(retype(offset(dst, bld, i), src.type),
5585 offset(src, bld, i + first_component));
5586 }
5587 } else if (type_sz(src.type) < type_sz(dst.type)) {
5588 /* Source is shuffled into destination */
5589 unsigned size_ratio = type_sz(dst.type) / type_sz(src.type);
5590 assert(!regions_overlap(dst,
5591 type_sz(dst.type) * bld.dispatch_width() *
5592 DIV_ROUND_UP(components, size_ratio),
5593 offset(src, bld, first_component),
5594 type_sz(src.type) * bld.dispatch_width() * components));
5595
5596 brw_reg_type shuffle_type =
5597 brw_reg_type_from_bit_size(8 * type_sz(src.type),
5598 BRW_REGISTER_TYPE_D);
5599 for (unsigned i = 0; i < components; i++) {
5600 fs_reg shuffle_component_i =
5601 subscript(offset(dst, bld, i / size_ratio),
5602 shuffle_type, i % size_ratio);
5603 bld.MOV(shuffle_component_i,
5604 retype(offset(src, bld, i + first_component), shuffle_type));
5605 }
5606 } else {
5607 /* Source is unshuffled into destination */
5608 unsigned size_ratio = type_sz(src.type) / type_sz(dst.type);
5609 assert(!regions_overlap(dst,
5610 type_sz(dst.type) * bld.dispatch_width() * components,
5611 offset(src, bld, first_component / size_ratio),
5612 type_sz(src.type) * bld.dispatch_width() *
5613 DIV_ROUND_UP(components + (first_component % size_ratio),
5614 size_ratio)));
5615
5616 brw_reg_type shuffle_type =
5617 brw_reg_type_from_bit_size(8 * type_sz(dst.type),
5618 BRW_REGISTER_TYPE_D);
5619 for (unsigned i = 0; i < components; i++) {
5620 fs_reg shuffle_component_i =
5621 subscript(offset(src, bld, (first_component + i) / size_ratio),
5622 shuffle_type, (first_component + i) % size_ratio);
5623 bld.MOV(retype(offset(dst, bld, i), shuffle_type),
5624 shuffle_component_i);
5625 }
5626 }
5627 }
5628
5629 void
5630 shuffle_from_32bit_read(const fs_builder &bld,
5631 const fs_reg &dst,
5632 const fs_reg &src,
5633 uint32_t first_component,
5634 uint32_t components)
5635 {
5636 assert(type_sz(src.type) == 4);
5637
5638 /* This function takes components in units of the destination type while
5639 * shuffle_src_to_dst takes components in units of the smallest type
5640 */
5641 if (type_sz(dst.type) > 4) {
5642 assert(type_sz(dst.type) == 8);
5643 first_component *= 2;
5644 components *= 2;
5645 }
5646
5647 shuffle_src_to_dst(bld, dst, src, first_component, components);
5648 }
5649
5650 fs_reg
5651 setup_imm_df(const fs_builder &bld, double v)
5652 {
5653 const struct gen_device_info *devinfo = bld.shader->devinfo;
5654 assert(devinfo->gen >= 7);
5655
5656 if (devinfo->gen >= 8)
5657 return brw_imm_df(v);
5658
5659 /* gen7.5 does not support DF immediates straighforward but the DIM
5660 * instruction allows to set the 64-bit immediate value.
5661 */
5662 if (devinfo->is_haswell) {
5663 const fs_builder ubld = bld.exec_all().group(1, 0);
5664 fs_reg dst = ubld.vgrf(BRW_REGISTER_TYPE_DF, 1);
5665 ubld.DIM(dst, brw_imm_df(v));
5666 return component(dst, 0);
5667 }
5668
5669 /* gen7 does not support DF immediates, so we generate a 64-bit constant by
5670 * writing the low 32-bit of the constant to suboffset 0 of a VGRF and
5671 * the high 32-bit to suboffset 4 and then applying a stride of 0.
5672 *
5673 * Alternatively, we could also produce a normal VGRF (without stride 0)
5674 * by writing to all the channels in the VGRF, however, that would hit the
5675 * gen7 bug where we have to split writes that span more than 1 register
5676 * into instructions with a width of 4 (otherwise the write to the second
5677 * register written runs into an execmask hardware bug) which isn't very
5678 * nice.
5679 */
5680 union {
5681 double d;
5682 struct {
5683 uint32_t i1;
5684 uint32_t i2;
5685 };
5686 } di;
5687
5688 di.d = v;
5689
5690 const fs_builder ubld = bld.exec_all().group(1, 0);
5691 const fs_reg tmp = ubld.vgrf(BRW_REGISTER_TYPE_UD, 2);
5692 ubld.MOV(tmp, brw_imm_ud(di.i1));
5693 ubld.MOV(horiz_offset(tmp, 1), brw_imm_ud(di.i2));
5694
5695 return component(retype(tmp, BRW_REGISTER_TYPE_DF), 0);
5696 }
5697
5698 fs_reg
5699 setup_imm_b(const fs_builder &bld, int8_t v)
5700 {
5701 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_B);
5702 bld.MOV(tmp, brw_imm_w(v));
5703 return tmp;
5704 }
5705
5706 fs_reg
5707 setup_imm_ub(const fs_builder &bld, uint8_t v)
5708 {
5709 const fs_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UB);
5710 bld.MOV(tmp, brw_imm_uw(v));
5711 return tmp;
5712 }