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