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