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