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