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