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