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