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