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