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