i965/vec4: Use MOV_INDIRECT instead of reladdr for indirect push constants
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_nir.cpp
1 /*
2 * Copyright © 2015 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 "brw_nir.h"
25 #include "brw_vec4.h"
26 #include "brw_vec4_builder.h"
27 #include "brw_vec4_surface_builder.h"
28 #include "brw_program.h"
29
30 using namespace brw;
31 using namespace brw::surface_access;
32
33 namespace brw {
34
35 void
36 vec4_visitor::emit_nir_code()
37 {
38 if (nir->num_uniforms > 0)
39 nir_setup_uniforms();
40
41 nir_setup_system_values();
42
43 /* get the main function and emit it */
44 nir_foreach_function(nir, function) {
45 assert(strcmp(function->name, "main") == 0);
46 assert(function->impl);
47 nir_emit_impl(function->impl);
48 }
49 }
50
51 void
52 vec4_visitor::nir_setup_system_value_intrinsic(nir_intrinsic_instr *instr)
53 {
54 dst_reg *reg;
55
56 switch (instr->intrinsic) {
57 case nir_intrinsic_load_vertex_id:
58 unreachable("should be lowered by lower_vertex_id().");
59
60 case nir_intrinsic_load_vertex_id_zero_base:
61 reg = &nir_system_values[SYSTEM_VALUE_VERTEX_ID_ZERO_BASE];
62 if (reg->file == BAD_FILE)
63 *reg = *make_reg_for_system_value(SYSTEM_VALUE_VERTEX_ID_ZERO_BASE,
64 glsl_type::int_type);
65 break;
66
67 case nir_intrinsic_load_base_vertex:
68 reg = &nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
69 if (reg->file == BAD_FILE)
70 *reg = *make_reg_for_system_value(SYSTEM_VALUE_BASE_VERTEX,
71 glsl_type::int_type);
72 break;
73
74 case nir_intrinsic_load_instance_id:
75 reg = &nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
76 if (reg->file == BAD_FILE)
77 *reg = *make_reg_for_system_value(SYSTEM_VALUE_INSTANCE_ID,
78 glsl_type::int_type);
79 break;
80
81 case nir_intrinsic_load_base_instance:
82 reg = &nir_system_values[SYSTEM_VALUE_BASE_INSTANCE];
83 if (reg->file == BAD_FILE)
84 *reg = *make_reg_for_system_value(SYSTEM_VALUE_BASE_INSTANCE,
85 glsl_type::int_type);
86 break;
87
88 case nir_intrinsic_load_draw_id:
89 reg = &nir_system_values[SYSTEM_VALUE_DRAW_ID];
90 if (reg->file == BAD_FILE)
91 *reg = *make_reg_for_system_value(SYSTEM_VALUE_DRAW_ID,
92 glsl_type::int_type);
93 break;
94
95 default:
96 break;
97 }
98 }
99
100 static bool
101 setup_system_values_block(nir_block *block, void *void_visitor)
102 {
103 vec4_visitor *v = (vec4_visitor *)void_visitor;
104
105 nir_foreach_instr(block, instr) {
106 if (instr->type != nir_instr_type_intrinsic)
107 continue;
108
109 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
110 v->nir_setup_system_value_intrinsic(intrin);
111 }
112
113 return true;
114 }
115
116 void
117 vec4_visitor::nir_setup_system_values()
118 {
119 nir_system_values = ralloc_array(mem_ctx, dst_reg, SYSTEM_VALUE_MAX);
120 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
121 nir_system_values[i] = dst_reg();
122 }
123
124 nir_foreach_function(nir, function) {
125 assert(strcmp(function->name, "main") == 0);
126 assert(function->impl);
127 nir_foreach_block(function->impl, setup_system_values_block, this);
128 }
129 }
130
131 void
132 vec4_visitor::nir_setup_uniforms()
133 {
134 uniforms = nir->num_uniforms / 16;
135
136 nir_foreach_variable(var, &nir->uniforms) {
137 /* UBO's and atomics don't take up space in the uniform file */
138 if (var->interface_type != NULL || var->type->contains_atomic())
139 continue;
140
141 if (type_size_vec4(var->type) > 0)
142 uniform_size[var->data.driver_location / 16] = type_size_vec4(var->type);
143 }
144 }
145
146 void
147 vec4_visitor::nir_emit_impl(nir_function_impl *impl)
148 {
149 nir_locals = ralloc_array(mem_ctx, dst_reg, impl->reg_alloc);
150 for (unsigned i = 0; i < impl->reg_alloc; i++) {
151 nir_locals[i] = dst_reg();
152 }
153
154 foreach_list_typed(nir_register, reg, node, &impl->registers) {
155 unsigned array_elems =
156 reg->num_array_elems == 0 ? 1 : reg->num_array_elems;
157
158 nir_locals[reg->index] = dst_reg(VGRF, alloc.allocate(array_elems));
159 }
160
161 nir_ssa_values = ralloc_array(mem_ctx, dst_reg, impl->ssa_alloc);
162
163 nir_emit_cf_list(&impl->body);
164 }
165
166 void
167 vec4_visitor::nir_emit_cf_list(exec_list *list)
168 {
169 exec_list_validate(list);
170 foreach_list_typed(nir_cf_node, node, node, list) {
171 switch (node->type) {
172 case nir_cf_node_if:
173 nir_emit_if(nir_cf_node_as_if(node));
174 break;
175
176 case nir_cf_node_loop:
177 nir_emit_loop(nir_cf_node_as_loop(node));
178 break;
179
180 case nir_cf_node_block:
181 nir_emit_block(nir_cf_node_as_block(node));
182 break;
183
184 default:
185 unreachable("Invalid CFG node block");
186 }
187 }
188 }
189
190 void
191 vec4_visitor::nir_emit_if(nir_if *if_stmt)
192 {
193 /* First, put the condition in f0 */
194 src_reg condition = get_nir_src(if_stmt->condition, BRW_REGISTER_TYPE_D, 1);
195 vec4_instruction *inst = emit(MOV(dst_null_d(), condition));
196 inst->conditional_mod = BRW_CONDITIONAL_NZ;
197
198 /* We can just predicate based on the X channel, as the condition only
199 * goes on its own line */
200 emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X));
201
202 nir_emit_cf_list(&if_stmt->then_list);
203
204 /* note: if the else is empty, dead CF elimination will remove it */
205 emit(BRW_OPCODE_ELSE);
206
207 nir_emit_cf_list(&if_stmt->else_list);
208
209 emit(BRW_OPCODE_ENDIF);
210 }
211
212 void
213 vec4_visitor::nir_emit_loop(nir_loop *loop)
214 {
215 emit(BRW_OPCODE_DO);
216
217 nir_emit_cf_list(&loop->body);
218
219 emit(BRW_OPCODE_WHILE);
220 }
221
222 void
223 vec4_visitor::nir_emit_block(nir_block *block)
224 {
225 nir_foreach_instr(block, instr) {
226 nir_emit_instr(instr);
227 }
228 }
229
230 void
231 vec4_visitor::nir_emit_instr(nir_instr *instr)
232 {
233 base_ir = instr;
234
235 switch (instr->type) {
236 case nir_instr_type_load_const:
237 nir_emit_load_const(nir_instr_as_load_const(instr));
238 break;
239
240 case nir_instr_type_intrinsic:
241 nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
242 break;
243
244 case nir_instr_type_alu:
245 nir_emit_alu(nir_instr_as_alu(instr));
246 break;
247
248 case nir_instr_type_jump:
249 nir_emit_jump(nir_instr_as_jump(instr));
250 break;
251
252 case nir_instr_type_tex:
253 nir_emit_texture(nir_instr_as_tex(instr));
254 break;
255
256 case nir_instr_type_ssa_undef:
257 nir_emit_undef(nir_instr_as_ssa_undef(instr));
258 break;
259
260 default:
261 fprintf(stderr, "VS instruction not yet implemented by NIR->vec4\n");
262 break;
263 }
264 }
265
266 static dst_reg
267 dst_reg_for_nir_reg(vec4_visitor *v, nir_register *nir_reg,
268 unsigned base_offset, nir_src *indirect)
269 {
270 dst_reg reg;
271
272 reg = v->nir_locals[nir_reg->index];
273 reg = offset(reg, base_offset);
274 if (indirect) {
275 reg.reladdr =
276 new(v->mem_ctx) src_reg(v->get_nir_src(*indirect,
277 BRW_REGISTER_TYPE_D,
278 1));
279 }
280 return reg;
281 }
282
283 dst_reg
284 vec4_visitor::get_nir_dest(nir_dest dest)
285 {
286 if (dest.is_ssa) {
287 dst_reg dst = dst_reg(VGRF, alloc.allocate(1));
288 nir_ssa_values[dest.ssa.index] = dst;
289 return dst;
290 } else {
291 return dst_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
292 dest.reg.indirect);
293 }
294 }
295
296 dst_reg
297 vec4_visitor::get_nir_dest(nir_dest dest, enum brw_reg_type type)
298 {
299 return retype(get_nir_dest(dest), type);
300 }
301
302 dst_reg
303 vec4_visitor::get_nir_dest(nir_dest dest, nir_alu_type type)
304 {
305 return get_nir_dest(dest, brw_type_for_nir_type(type));
306 }
307
308 src_reg
309 vec4_visitor::get_nir_src(nir_src src, enum brw_reg_type type,
310 unsigned num_components)
311 {
312 dst_reg reg;
313
314 if (src.is_ssa) {
315 assert(src.ssa != NULL);
316 reg = nir_ssa_values[src.ssa->index];
317 }
318 else {
319 reg = dst_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
320 src.reg.indirect);
321 }
322
323 reg = retype(reg, type);
324
325 src_reg reg_as_src = src_reg(reg);
326 reg_as_src.swizzle = brw_swizzle_for_size(num_components);
327 return reg_as_src;
328 }
329
330 src_reg
331 vec4_visitor::get_nir_src(nir_src src, nir_alu_type type,
332 unsigned num_components)
333 {
334 return get_nir_src(src, brw_type_for_nir_type(type), num_components);
335 }
336
337 src_reg
338 vec4_visitor::get_nir_src(nir_src src, unsigned num_components)
339 {
340 /* if type is not specified, default to signed int */
341 return get_nir_src(src, nir_type_int, num_components);
342 }
343
344 src_reg
345 vec4_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
346 {
347 nir_src *offset_src = nir_get_io_offset_src(instr);
348 nir_const_value *const_value = nir_src_as_const_value(*offset_src);
349
350 if (const_value) {
351 /* The only constant offset we should find is 0. brw_nir.c's
352 * add_const_offset_to_base() will fold other constant offsets
353 * into instr->const_index[0].
354 */
355 assert(const_value->u32[0] == 0);
356 return src_reg();
357 }
358
359 return get_nir_src(*offset_src, BRW_REGISTER_TYPE_UD, 1);
360 }
361
362 void
363 vec4_visitor::nir_emit_load_const(nir_load_const_instr *instr)
364 {
365 dst_reg reg = dst_reg(VGRF, alloc.allocate(1));
366 reg.type = BRW_REGISTER_TYPE_D;
367
368 unsigned remaining = brw_writemask_for_size(instr->def.num_components);
369
370 /* @FIXME: consider emitting vector operations to save some MOVs in
371 * cases where the components are representable in 8 bits.
372 * For now, we emit a MOV for each distinct value.
373 */
374 for (unsigned i = 0; i < instr->def.num_components; i++) {
375 unsigned writemask = 1 << i;
376
377 if ((remaining & writemask) == 0)
378 continue;
379
380 for (unsigned j = i; j < instr->def.num_components; j++) {
381 if (instr->value.u32[i] == instr->value.u32[j]) {
382 writemask |= 1 << j;
383 }
384 }
385
386 reg.writemask = writemask;
387 emit(MOV(reg, brw_imm_d(instr->value.i32[i])));
388
389 remaining &= ~writemask;
390 }
391
392 /* Set final writemask */
393 reg.writemask = brw_writemask_for_size(instr->def.num_components);
394
395 nir_ssa_values[instr->def.index] = reg;
396 }
397
398 void
399 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
400 {
401 dst_reg dest;
402 src_reg src;
403
404 switch (instr->intrinsic) {
405
406 case nir_intrinsic_load_input: {
407 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
408
409 /* We set EmitNoIndirectInput for VS */
410 assert(const_offset);
411
412 src = src_reg(ATTR, instr->const_index[0] + const_offset->u32[0],
413 glsl_type::uvec4_type);
414
415 dest = get_nir_dest(instr->dest, src.type);
416 dest.writemask = brw_writemask_for_size(instr->num_components);
417
418 emit(MOV(dest, src));
419 break;
420 }
421
422 case nir_intrinsic_store_output: {
423 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
424 assert(const_offset);
425
426 int varying = instr->const_index[0] + const_offset->u32[0];
427
428 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
429 instr->num_components);
430
431 output_reg[varying] = dst_reg(src);
432 break;
433 }
434
435 case nir_intrinsic_get_buffer_size: {
436 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
437 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u32[0] : 0;
438
439 const unsigned index =
440 prog_data->base.binding_table.ssbo_start + ssbo_index;
441 dst_reg result_dst = get_nir_dest(instr->dest);
442 vec4_instruction *inst = new(mem_ctx)
443 vec4_instruction(VS_OPCODE_GET_BUFFER_SIZE, result_dst);
444
445 inst->base_mrf = 2;
446 inst->mlen = 1; /* always at least one */
447 inst->src[1] = brw_imm_ud(index);
448
449 /* MRF for the first parameter */
450 src_reg lod = brw_imm_d(0);
451 int param_base = inst->base_mrf;
452 int writemask = WRITEMASK_X;
453 emit(MOV(dst_reg(MRF, param_base, glsl_type::int_type, writemask), lod));
454
455 emit(inst);
456
457 brw_mark_surface_used(&prog_data->base, index);
458 break;
459 }
460
461 case nir_intrinsic_store_ssbo: {
462 assert(devinfo->gen >= 7);
463
464 /* Block index */
465 src_reg surf_index;
466 nir_const_value *const_uniform_block =
467 nir_src_as_const_value(instr->src[1]);
468 if (const_uniform_block) {
469 unsigned index = prog_data->base.binding_table.ssbo_start +
470 const_uniform_block->u32[0];
471 surf_index = brw_imm_ud(index);
472 brw_mark_surface_used(&prog_data->base, index);
473 } else {
474 surf_index = src_reg(this, glsl_type::uint_type);
475 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[1], 1),
476 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
477 surf_index = emit_uniformize(surf_index);
478
479 brw_mark_surface_used(&prog_data->base,
480 prog_data->base.binding_table.ssbo_start +
481 nir->info.num_ssbos - 1);
482 }
483
484 /* Offset */
485 src_reg offset_reg;
486 nir_const_value *const_offset = nir_src_as_const_value(instr->src[2]);
487 if (const_offset) {
488 offset_reg = brw_imm_ud(const_offset->u32[0]);
489 } else {
490 offset_reg = get_nir_src(instr->src[2], 1);
491 }
492
493 /* Value */
494 src_reg val_reg = get_nir_src(instr->src[0], 4);
495
496 /* Writemask */
497 unsigned write_mask = instr->const_index[0];
498
499 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
500 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
501 * typed and untyped messages and across hardware platforms, the
502 * current implementation of the untyped messages will transparently convert
503 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
504 * and enabling only channel X on the SEND instruction.
505 *
506 * The above, works well for full vector writes, but not for partial writes
507 * where we want to write some channels and not others, like when we have
508 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
509 * quite restrictive with regards to the channel enables we can configure in
510 * the message descriptor (not all combinations are allowed) we cannot simply
511 * implement these scenarios with a single message while keeping the
512 * aforementioned symmetry in the implementation. For now we de decided that
513 * it is better to keep the symmetry to reduce complexity, so in situations
514 * such as the one described we end up emitting two untyped write messages
515 * (one for xy and another for w).
516 *
517 * The code below packs consecutive channels into a single write message,
518 * detects gaps in the vector write and if needed, sends a second message
519 * with the remaining channels. If in the future we decide that we want to
520 * emit a single message at the expense of losing the symmetry in the
521 * implementation we can:
522 *
523 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
524 * message payload. In this mode we can write up to 8 offsets and dwords
525 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
526 * and select which of the 8 channels carry data to write by setting the
527 * appropriate writemask in the dst register of the SEND instruction.
528 * It would require to write a new generator opcode specifically for
529 * IvyBridge since we would need to prepare a SIMD8 payload that could
530 * use any channel, not just X.
531 *
532 * 2) For Haswell+: Simply send a single write message but set the writemask
533 * on the dst of the SEND instruction to select the channels we want to
534 * write. It would require to modify the current messages to receive
535 * and honor the writemask provided.
536 */
537 const vec4_builder bld = vec4_builder(this).at_end()
538 .annotate(current_annotation, base_ir);
539
540 int swizzle[4] = { 0, 0, 0, 0};
541 int num_channels = 0;
542 unsigned skipped_channels = 0;
543 int num_components = instr->num_components;
544 for (int i = 0; i < num_components; i++) {
545 /* Check if this channel needs to be written. If so, record the
546 * channel we need to take the data from in the swizzle array
547 */
548 int component_mask = 1 << i;
549 int write_test = write_mask & component_mask;
550 if (write_test)
551 swizzle[num_channels++] = i;
552
553 /* If we don't have to write this channel it means we have a gap in the
554 * vector, so write the channels we accumulated until now, if any. Do
555 * the same if this was the last component in the vector.
556 */
557 if (!write_test || i == num_components - 1) {
558 if (num_channels > 0) {
559 /* We have channels to write, so update the offset we need to
560 * write at to skip the channels we skipped, if any.
561 */
562 if (skipped_channels > 0) {
563 if (offset_reg.file == IMM) {
564 offset_reg.ud += 4 * skipped_channels;
565 } else {
566 emit(ADD(dst_reg(offset_reg), offset_reg,
567 brw_imm_ud(4 * skipped_channels)));
568 }
569 }
570
571 /* Swizzle the data register so we take the data from the channels
572 * we need to write and send the write message. This will write
573 * num_channels consecutive dwords starting at offset.
574 */
575 val_reg.swizzle =
576 BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
577 emit_untyped_write(bld, surf_index, offset_reg, val_reg,
578 1 /* dims */, num_channels /* size */,
579 BRW_PREDICATE_NONE);
580
581 /* If we have to do a second write we will have to update the
582 * offset so that we jump over the channels we have just written
583 * now.
584 */
585 skipped_channels = num_channels;
586
587 /* Restart the count for the next write message */
588 num_channels = 0;
589 }
590
591 /* We did not write the current channel, so increase skipped count */
592 skipped_channels++;
593 }
594 }
595
596 break;
597 }
598
599 case nir_intrinsic_load_ssbo: {
600 assert(devinfo->gen >= 7);
601
602 nir_const_value *const_uniform_block =
603 nir_src_as_const_value(instr->src[0]);
604
605 src_reg surf_index;
606 if (const_uniform_block) {
607 unsigned index = prog_data->base.binding_table.ssbo_start +
608 const_uniform_block->u32[0];
609 surf_index = brw_imm_ud(index);
610
611 brw_mark_surface_used(&prog_data->base, index);
612 } else {
613 surf_index = src_reg(this, glsl_type::uint_type);
614 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], 1),
615 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
616 surf_index = emit_uniformize(surf_index);
617
618 /* Assume this may touch any UBO. It would be nice to provide
619 * a tighter bound, but the array information is already lowered away.
620 */
621 brw_mark_surface_used(&prog_data->base,
622 prog_data->base.binding_table.ssbo_start +
623 nir->info.num_ssbos - 1);
624 }
625
626 src_reg offset_reg;
627 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
628 if (const_offset) {
629 offset_reg = brw_imm_ud(const_offset->u32[0]);
630 } else {
631 offset_reg = get_nir_src(instr->src[1], 1);
632 }
633
634 /* Read the vector */
635 const vec4_builder bld = vec4_builder(this).at_end()
636 .annotate(current_annotation, base_ir);
637
638 src_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
639 1 /* dims */, 4 /* size*/,
640 BRW_PREDICATE_NONE);
641 dst_reg dest = get_nir_dest(instr->dest);
642 read_result.type = dest.type;
643 read_result.swizzle = brw_swizzle_for_size(instr->num_components);
644 emit(MOV(dest, read_result));
645
646 break;
647 }
648
649 case nir_intrinsic_ssbo_atomic_add:
650 nir_emit_ssbo_atomic(BRW_AOP_ADD, instr);
651 break;
652 case nir_intrinsic_ssbo_atomic_imin:
653 nir_emit_ssbo_atomic(BRW_AOP_IMIN, instr);
654 break;
655 case nir_intrinsic_ssbo_atomic_umin:
656 nir_emit_ssbo_atomic(BRW_AOP_UMIN, instr);
657 break;
658 case nir_intrinsic_ssbo_atomic_imax:
659 nir_emit_ssbo_atomic(BRW_AOP_IMAX, instr);
660 break;
661 case nir_intrinsic_ssbo_atomic_umax:
662 nir_emit_ssbo_atomic(BRW_AOP_UMAX, instr);
663 break;
664 case nir_intrinsic_ssbo_atomic_and:
665 nir_emit_ssbo_atomic(BRW_AOP_AND, instr);
666 break;
667 case nir_intrinsic_ssbo_atomic_or:
668 nir_emit_ssbo_atomic(BRW_AOP_OR, instr);
669 break;
670 case nir_intrinsic_ssbo_atomic_xor:
671 nir_emit_ssbo_atomic(BRW_AOP_XOR, instr);
672 break;
673 case nir_intrinsic_ssbo_atomic_exchange:
674 nir_emit_ssbo_atomic(BRW_AOP_MOV, instr);
675 break;
676 case nir_intrinsic_ssbo_atomic_comp_swap:
677 nir_emit_ssbo_atomic(BRW_AOP_CMPWR, instr);
678 break;
679
680 case nir_intrinsic_load_vertex_id:
681 unreachable("should be lowered by lower_vertex_id()");
682
683 case nir_intrinsic_load_vertex_id_zero_base:
684 case nir_intrinsic_load_base_vertex:
685 case nir_intrinsic_load_instance_id:
686 case nir_intrinsic_load_base_instance:
687 case nir_intrinsic_load_draw_id:
688 case nir_intrinsic_load_invocation_id: {
689 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
690 src_reg val = src_reg(nir_system_values[sv]);
691 assert(val.file != BAD_FILE);
692 dest = get_nir_dest(instr->dest, val.type);
693 emit(MOV(dest, val));
694 break;
695 }
696
697 case nir_intrinsic_load_uniform: {
698 /* Offsets are in bytes but they should always be multiples of 16 */
699 assert(instr->const_index[0] % 16 == 0);
700
701 dest = get_nir_dest(instr->dest);
702
703 src = src_reg(dst_reg(UNIFORM, instr->const_index[0] / 16));
704 src.type = dest.type;
705
706 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
707 if (const_offset) {
708 /* Offsets are in bytes but they should always be multiples of 16 */
709 assert(const_offset->u32[0] % 16 == 0);
710 src.reg_offset = const_offset->u32[0] / 16;
711
712 emit(MOV(dest, src));
713 } else {
714 src_reg indirect = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_UD, 1);
715
716 emit(SHADER_OPCODE_MOV_INDIRECT, dest, src,
717 indirect, brw_imm_ud(instr->const_index[1]));
718 }
719 break;
720 }
721
722 case nir_intrinsic_atomic_counter_read:
723 case nir_intrinsic_atomic_counter_inc:
724 case nir_intrinsic_atomic_counter_dec: {
725 unsigned surf_index = prog_data->base.binding_table.abo_start +
726 (unsigned) instr->const_index[0];
727 src_reg offset = get_nir_src(instr->src[0], nir_type_int,
728 instr->num_components);
729 const src_reg surface = brw_imm_ud(surf_index);
730 const vec4_builder bld =
731 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
732 src_reg tmp;
733
734 dest = get_nir_dest(instr->dest);
735
736 switch (instr->intrinsic) {
737 case nir_intrinsic_atomic_counter_inc:
738 tmp = emit_untyped_atomic(bld, surface, offset,
739 src_reg(), src_reg(),
740 1, 1,
741 BRW_AOP_INC);
742 break;
743 case nir_intrinsic_atomic_counter_dec:
744 tmp = emit_untyped_atomic(bld, surface, offset,
745 src_reg(), src_reg(),
746 1, 1,
747 BRW_AOP_PREDEC);
748 break;
749 case nir_intrinsic_atomic_counter_read:
750 tmp = emit_untyped_read(bld, surface, offset, 1, 1);
751 break;
752 default:
753 unreachable("Unreachable");
754 }
755
756 bld.MOV(retype(dest, tmp.type), tmp);
757 brw_mark_surface_used(stage_prog_data, surf_index);
758 break;
759 }
760
761 case nir_intrinsic_load_ubo: {
762 nir_const_value *const_block_index = nir_src_as_const_value(instr->src[0]);
763 src_reg surf_index;
764
765 dest = get_nir_dest(instr->dest);
766
767 if (const_block_index) {
768 /* The block index is a constant, so just emit the binding table entry
769 * as an immediate.
770 */
771 const unsigned index = prog_data->base.binding_table.ubo_start +
772 const_block_index->u32[0];
773 surf_index = brw_imm_ud(index);
774 brw_mark_surface_used(&prog_data->base, index);
775 } else {
776 /* The block index is not a constant. Evaluate the index expression
777 * per-channel and add the base UBO index; we have to select a value
778 * from any live channel.
779 */
780 surf_index = src_reg(this, glsl_type::uint_type);
781 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int,
782 instr->num_components),
783 brw_imm_ud(prog_data->base.binding_table.ubo_start)));
784 surf_index = emit_uniformize(surf_index);
785
786 /* Assume this may touch any UBO. It would be nice to provide
787 * a tighter bound, but the array information is already lowered away.
788 */
789 brw_mark_surface_used(&prog_data->base,
790 prog_data->base.binding_table.ubo_start +
791 nir->info.num_ubos - 1);
792 }
793
794 src_reg offset;
795 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
796 if (const_offset) {
797 offset = brw_imm_ud(const_offset->u32[0] & ~15);
798 } else {
799 offset = get_nir_src(instr->src[1], nir_type_int, 1);
800 }
801
802 src_reg packed_consts = src_reg(this, glsl_type::vec4_type);
803 packed_consts.type = dest.type;
804
805 emit_pull_constant_load_reg(dst_reg(packed_consts),
806 surf_index,
807 offset,
808 NULL, NULL /* before_block/inst */);
809
810 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
811 if (const_offset) {
812 packed_consts.swizzle += BRW_SWIZZLE4(const_offset->u32[0] % 16 / 4,
813 const_offset->u32[0] % 16 / 4,
814 const_offset->u32[0] % 16 / 4,
815 const_offset->u32[0] % 16 / 4);
816 }
817
818 emit(MOV(dest, packed_consts));
819 break;
820 }
821
822 case nir_intrinsic_memory_barrier: {
823 const vec4_builder bld =
824 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
825 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
826 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
827 ->regs_written = 2;
828 break;
829 }
830
831 case nir_intrinsic_shader_clock: {
832 /* We cannot do anything if there is an event, so ignore it for now */
833 const src_reg shader_clock = get_timestamp();
834 const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
835
836 dest = get_nir_dest(instr->dest, type);
837 emit(MOV(dest, shader_clock));
838 break;
839 }
840
841 default:
842 unreachable("Unknown intrinsic");
843 }
844 }
845
846 void
847 vec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
848 {
849 dst_reg dest;
850 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
851 dest = get_nir_dest(instr->dest);
852
853 src_reg surface;
854 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
855 if (const_surface) {
856 unsigned surf_index = prog_data->base.binding_table.ssbo_start +
857 const_surface->u32[0];
858 surface = brw_imm_ud(surf_index);
859 brw_mark_surface_used(&prog_data->base, surf_index);
860 } else {
861 surface = src_reg(this, glsl_type::uint_type);
862 emit(ADD(dst_reg(surface), get_nir_src(instr->src[0]),
863 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
864
865 /* Assume this may touch any UBO. This is the same we do for other
866 * UBO/SSBO accesses with non-constant surface.
867 */
868 brw_mark_surface_used(&prog_data->base,
869 prog_data->base.binding_table.ssbo_start +
870 nir->info.num_ssbos - 1);
871 }
872
873 src_reg offset = get_nir_src(instr->src[1], 1);
874 src_reg data1 = get_nir_src(instr->src[2], 1);
875 src_reg data2;
876 if (op == BRW_AOP_CMPWR)
877 data2 = get_nir_src(instr->src[3], 1);
878
879 /* Emit the actual atomic operation operation */
880 const vec4_builder bld =
881 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
882
883 src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
884 data1, data2,
885 1 /* dims */, 1 /* rsize */,
886 op,
887 BRW_PREDICATE_NONE);
888 dest.type = atomic_result.type;
889 bld.MOV(dest, atomic_result);
890 }
891
892 static unsigned
893 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
894 {
895 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
896 }
897
898 static enum brw_conditional_mod
899 brw_conditional_for_nir_comparison(nir_op op)
900 {
901 switch (op) {
902 case nir_op_flt:
903 case nir_op_ilt:
904 case nir_op_ult:
905 return BRW_CONDITIONAL_L;
906
907 case nir_op_fge:
908 case nir_op_ige:
909 case nir_op_uge:
910 return BRW_CONDITIONAL_GE;
911
912 case nir_op_feq:
913 case nir_op_ieq:
914 case nir_op_ball_fequal2:
915 case nir_op_ball_iequal2:
916 case nir_op_ball_fequal3:
917 case nir_op_ball_iequal3:
918 case nir_op_ball_fequal4:
919 case nir_op_ball_iequal4:
920 return BRW_CONDITIONAL_Z;
921
922 case nir_op_fne:
923 case nir_op_ine:
924 case nir_op_bany_fnequal2:
925 case nir_op_bany_inequal2:
926 case nir_op_bany_fnequal3:
927 case nir_op_bany_inequal3:
928 case nir_op_bany_fnequal4:
929 case nir_op_bany_inequal4:
930 return BRW_CONDITIONAL_NZ;
931
932 default:
933 unreachable("not reached: bad operation for comparison");
934 }
935 }
936
937 bool
938 vec4_visitor::optimize_predicate(nir_alu_instr *instr,
939 enum brw_predicate *predicate)
940 {
941 if (!instr->src[0].src.is_ssa ||
942 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
943 return false;
944
945 nir_alu_instr *cmp_instr =
946 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
947
948 switch (cmp_instr->op) {
949 case nir_op_bany_fnequal2:
950 case nir_op_bany_inequal2:
951 case nir_op_bany_fnequal3:
952 case nir_op_bany_inequal3:
953 case nir_op_bany_fnequal4:
954 case nir_op_bany_inequal4:
955 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
956 break;
957 case nir_op_ball_fequal2:
958 case nir_op_ball_iequal2:
959 case nir_op_ball_fequal3:
960 case nir_op_ball_iequal3:
961 case nir_op_ball_fequal4:
962 case nir_op_ball_iequal4:
963 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
964 break;
965 default:
966 return false;
967 }
968
969 unsigned size_swizzle =
970 brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
971
972 src_reg op[2];
973 assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
974 for (unsigned i = 0; i < 2; i++) {
975 op[i] = get_nir_src(cmp_instr->src[i].src,
976 nir_op_infos[cmp_instr->op].input_types[i], 4);
977 unsigned base_swizzle =
978 brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
979 op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
980 op[i].abs = cmp_instr->src[i].abs;
981 op[i].negate = cmp_instr->src[i].negate;
982 }
983
984 emit(CMP(dst_null_d(), op[0], op[1],
985 brw_conditional_for_nir_comparison(cmp_instr->op)));
986
987 return true;
988 }
989
990 void
991 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
992 {
993 vec4_instruction *inst;
994
995 dst_reg dst = get_nir_dest(instr->dest.dest,
996 nir_op_infos[instr->op].output_type);
997 dst.writemask = instr->dest.write_mask;
998
999 src_reg op[4];
1000 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1001 op[i] = get_nir_src(instr->src[i].src,
1002 nir_op_infos[instr->op].input_types[i], 4);
1003 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1004 op[i].abs = instr->src[i].abs;
1005 op[i].negate = instr->src[i].negate;
1006 }
1007
1008 switch (instr->op) {
1009 case nir_op_imov:
1010 case nir_op_fmov:
1011 inst = emit(MOV(dst, op[0]));
1012 inst->saturate = instr->dest.saturate;
1013 break;
1014
1015 case nir_op_vec2:
1016 case nir_op_vec3:
1017 case nir_op_vec4:
1018 unreachable("not reached: should be handled by lower_vec_to_movs()");
1019
1020 case nir_op_i2f:
1021 case nir_op_u2f:
1022 inst = emit(MOV(dst, op[0]));
1023 inst->saturate = instr->dest.saturate;
1024 break;
1025
1026 case nir_op_f2i:
1027 case nir_op_f2u:
1028 inst = emit(MOV(dst, op[0]));
1029 break;
1030
1031 case nir_op_fadd:
1032 /* fall through */
1033 case nir_op_iadd:
1034 inst = emit(ADD(dst, op[0], op[1]));
1035 inst->saturate = instr->dest.saturate;
1036 break;
1037
1038 case nir_op_fmul:
1039 inst = emit(MUL(dst, op[0], op[1]));
1040 inst->saturate = instr->dest.saturate;
1041 break;
1042
1043 case nir_op_imul: {
1044 if (devinfo->gen < 8) {
1045 nir_const_value *value0 = nir_src_as_const_value(instr->src[0].src);
1046 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
1047
1048 /* For integer multiplication, the MUL uses the low 16 bits of one of
1049 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1050 * accumulates in the contribution of the upper 16 bits of that
1051 * operand. If we can determine that one of the args is in the low
1052 * 16 bits, though, we can just emit a single MUL.
1053 */
1054 if (value0 && value0->u32[0] < (1 << 16)) {
1055 if (devinfo->gen < 7)
1056 emit(MUL(dst, op[0], op[1]));
1057 else
1058 emit(MUL(dst, op[1], op[0]));
1059 } else if (value1 && value1->u32[0] < (1 << 16)) {
1060 if (devinfo->gen < 7)
1061 emit(MUL(dst, op[1], op[0]));
1062 else
1063 emit(MUL(dst, op[0], op[1]));
1064 } else {
1065 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1066
1067 emit(MUL(acc, op[0], op[1]));
1068 emit(MACH(dst_null_d(), op[0], op[1]));
1069 emit(MOV(dst, src_reg(acc)));
1070 }
1071 } else {
1072 emit(MUL(dst, op[0], op[1]));
1073 }
1074 break;
1075 }
1076
1077 case nir_op_imul_high:
1078 case nir_op_umul_high: {
1079 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1080
1081 if (devinfo->gen >= 8)
1082 emit(MUL(acc, op[0], retype(op[1], BRW_REGISTER_TYPE_UW)));
1083 else
1084 emit(MUL(acc, op[0], op[1]));
1085
1086 emit(MACH(dst, op[0], op[1]));
1087 break;
1088 }
1089
1090 case nir_op_frcp:
1091 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1092 inst->saturate = instr->dest.saturate;
1093 break;
1094
1095 case nir_op_fexp2:
1096 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1097 inst->saturate = instr->dest.saturate;
1098 break;
1099
1100 case nir_op_flog2:
1101 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1102 inst->saturate = instr->dest.saturate;
1103 break;
1104
1105 case nir_op_fsin:
1106 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1107 inst->saturate = instr->dest.saturate;
1108 break;
1109
1110 case nir_op_fcos:
1111 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1112 inst->saturate = instr->dest.saturate;
1113 break;
1114
1115 case nir_op_idiv:
1116 case nir_op_udiv:
1117 emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
1118 break;
1119
1120 case nir_op_umod:
1121 case nir_op_irem:
1122 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1123 * appears that our hardware just does the right thing for signed
1124 * remainder.
1125 */
1126 emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1127 break;
1128
1129 case nir_op_imod: {
1130 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1131 inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1132
1133 /* Math instructions don't support conditional mod */
1134 inst = emit(MOV(dst_null_d(), src_reg(dst)));
1135 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1136
1137 /* Now, we need to determine if signs of the sources are different.
1138 * When we XOR the sources, the top bit is 0 if they are the same and 1
1139 * if they are different. We can then use a conditional modifier to
1140 * turn that into a predicate. This leads us to an XOR.l instruction.
1141 *
1142 * Technically, according to the PRM, you're not allowed to use .l on a
1143 * XOR instruction. However, emperical experiments and Curro's reading
1144 * of the simulator source both indicate that it's safe.
1145 */
1146 src_reg tmp = src_reg(this, glsl_type::ivec4_type);
1147 inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
1148 inst->predicate = BRW_PREDICATE_NORMAL;
1149 inst->conditional_mod = BRW_CONDITIONAL_L;
1150
1151 /* If the result of the initial remainder operation is non-zero and the
1152 * two sources have different signs, add in a copy of op[1] to get the
1153 * final integer modulus value.
1154 */
1155 inst = emit(ADD(dst, src_reg(dst), op[1]));
1156 inst->predicate = BRW_PREDICATE_NORMAL;
1157 break;
1158 }
1159
1160 case nir_op_ldexp:
1161 unreachable("not reached: should be handled by ldexp_to_arith()");
1162
1163 case nir_op_fsqrt:
1164 inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
1165 inst->saturate = instr->dest.saturate;
1166 break;
1167
1168 case nir_op_frsq:
1169 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1170 inst->saturate = instr->dest.saturate;
1171 break;
1172
1173 case nir_op_fpow:
1174 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1175 inst->saturate = instr->dest.saturate;
1176 break;
1177
1178 case nir_op_uadd_carry: {
1179 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1180
1181 emit(ADDC(dst_null_ud(), op[0], op[1]));
1182 emit(MOV(dst, src_reg(acc)));
1183 break;
1184 }
1185
1186 case nir_op_usub_borrow: {
1187 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1188
1189 emit(SUBB(dst_null_ud(), op[0], op[1]));
1190 emit(MOV(dst, src_reg(acc)));
1191 break;
1192 }
1193
1194 case nir_op_ftrunc:
1195 inst = emit(RNDZ(dst, op[0]));
1196 inst->saturate = instr->dest.saturate;
1197 break;
1198
1199 case nir_op_fceil: {
1200 src_reg tmp = src_reg(this, glsl_type::float_type);
1201 tmp.swizzle =
1202 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1203 instr->src[0].src.ssa->num_components :
1204 instr->src[0].src.reg.reg->num_components);
1205
1206 op[0].negate = !op[0].negate;
1207 emit(RNDD(dst_reg(tmp), op[0]));
1208 tmp.negate = true;
1209 inst = emit(MOV(dst, tmp));
1210 inst->saturate = instr->dest.saturate;
1211 break;
1212 }
1213
1214 case nir_op_ffloor:
1215 inst = emit(RNDD(dst, op[0]));
1216 inst->saturate = instr->dest.saturate;
1217 break;
1218
1219 case nir_op_ffract:
1220 inst = emit(FRC(dst, op[0]));
1221 inst->saturate = instr->dest.saturate;
1222 break;
1223
1224 case nir_op_fround_even:
1225 inst = emit(RNDE(dst, op[0]));
1226 inst->saturate = instr->dest.saturate;
1227 break;
1228
1229 case nir_op_fquantize2f16: {
1230 /* See also vec4_visitor::emit_pack_half_2x16() */
1231 src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1232 src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1233 src_reg zero = src_reg(this, glsl_type::vec4_type);
1234
1235 /* Check for denormal */
1236 src_reg abs_src0 = op[0];
1237 abs_src0.abs = true;
1238 emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1239 BRW_CONDITIONAL_L));
1240 /* Get the appropriately signed zero */
1241 emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1242 retype(op[0], BRW_REGISTER_TYPE_UD),
1243 brw_imm_ud(0x80000000)));
1244 /* Do the actual F32 -> F16 -> F32 conversion */
1245 emit(F32TO16(dst_reg(tmp16), op[0]));
1246 emit(F16TO32(dst_reg(tmp32), tmp16));
1247 /* Select that or zero based on normal status */
1248 inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1249 inst->predicate = BRW_PREDICATE_NORMAL;
1250 inst->saturate = instr->dest.saturate;
1251 break;
1252 }
1253
1254 case nir_op_fmin:
1255 case nir_op_imin:
1256 case nir_op_umin:
1257 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1258 inst->saturate = instr->dest.saturate;
1259 break;
1260
1261 case nir_op_fmax:
1262 case nir_op_imax:
1263 case nir_op_umax:
1264 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1265 inst->saturate = instr->dest.saturate;
1266 break;
1267
1268 case nir_op_fddx:
1269 case nir_op_fddx_coarse:
1270 case nir_op_fddx_fine:
1271 case nir_op_fddy:
1272 case nir_op_fddy_coarse:
1273 case nir_op_fddy_fine:
1274 unreachable("derivatives are not valid in vertex shaders");
1275
1276 case nir_op_flt:
1277 case nir_op_ilt:
1278 case nir_op_ult:
1279 case nir_op_fge:
1280 case nir_op_ige:
1281 case nir_op_uge:
1282 case nir_op_feq:
1283 case nir_op_ieq:
1284 case nir_op_fne:
1285 case nir_op_ine:
1286 emit(CMP(dst, op[0], op[1],
1287 brw_conditional_for_nir_comparison(instr->op)));
1288 break;
1289
1290 case nir_op_ball_fequal2:
1291 case nir_op_ball_iequal2:
1292 case nir_op_ball_fequal3:
1293 case nir_op_ball_iequal3:
1294 case nir_op_ball_fequal4:
1295 case nir_op_ball_iequal4: {
1296 unsigned swiz =
1297 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1298
1299 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1300 brw_conditional_for_nir_comparison(instr->op)));
1301 emit(MOV(dst, brw_imm_d(0)));
1302 inst = emit(MOV(dst, brw_imm_d(~0)));
1303 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1304 break;
1305 }
1306
1307 case nir_op_bany_fnequal2:
1308 case nir_op_bany_inequal2:
1309 case nir_op_bany_fnequal3:
1310 case nir_op_bany_inequal3:
1311 case nir_op_bany_fnequal4:
1312 case nir_op_bany_inequal4: {
1313 unsigned swiz =
1314 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1315
1316 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1317 brw_conditional_for_nir_comparison(instr->op)));
1318
1319 emit(MOV(dst, brw_imm_d(0)));
1320 inst = emit(MOV(dst, brw_imm_d(~0)));
1321 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1322 break;
1323 }
1324
1325 case nir_op_inot:
1326 if (devinfo->gen >= 8) {
1327 op[0] = resolve_source_modifiers(op[0]);
1328 }
1329 emit(NOT(dst, op[0]));
1330 break;
1331
1332 case nir_op_ixor:
1333 if (devinfo->gen >= 8) {
1334 op[0] = resolve_source_modifiers(op[0]);
1335 op[1] = resolve_source_modifiers(op[1]);
1336 }
1337 emit(XOR(dst, op[0], op[1]));
1338 break;
1339
1340 case nir_op_ior:
1341 if (devinfo->gen >= 8) {
1342 op[0] = resolve_source_modifiers(op[0]);
1343 op[1] = resolve_source_modifiers(op[1]);
1344 }
1345 emit(OR(dst, op[0], op[1]));
1346 break;
1347
1348 case nir_op_iand:
1349 if (devinfo->gen >= 8) {
1350 op[0] = resolve_source_modifiers(op[0]);
1351 op[1] = resolve_source_modifiers(op[1]);
1352 }
1353 emit(AND(dst, op[0], op[1]));
1354 break;
1355
1356 case nir_op_b2i:
1357 case nir_op_b2f:
1358 emit(MOV(dst, negate(op[0])));
1359 break;
1360
1361 case nir_op_f2b:
1362 emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1363 break;
1364
1365 case nir_op_i2b:
1366 emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1367 break;
1368
1369 case nir_op_fnoise1_1:
1370 case nir_op_fnoise1_2:
1371 case nir_op_fnoise1_3:
1372 case nir_op_fnoise1_4:
1373 case nir_op_fnoise2_1:
1374 case nir_op_fnoise2_2:
1375 case nir_op_fnoise2_3:
1376 case nir_op_fnoise2_4:
1377 case nir_op_fnoise3_1:
1378 case nir_op_fnoise3_2:
1379 case nir_op_fnoise3_3:
1380 case nir_op_fnoise3_4:
1381 case nir_op_fnoise4_1:
1382 case nir_op_fnoise4_2:
1383 case nir_op_fnoise4_3:
1384 case nir_op_fnoise4_4:
1385 unreachable("not reached: should be handled by lower_noise");
1386
1387 case nir_op_unpack_half_2x16_split_x:
1388 case nir_op_unpack_half_2x16_split_y:
1389 case nir_op_pack_half_2x16_split:
1390 unreachable("not reached: should not occur in vertex shader");
1391
1392 case nir_op_unpack_snorm_2x16:
1393 case nir_op_unpack_unorm_2x16:
1394 case nir_op_pack_snorm_2x16:
1395 case nir_op_pack_unorm_2x16:
1396 unreachable("not reached: should be handled by lower_packing_builtins");
1397
1398 case nir_op_pack_uvec4_to_uint:
1399 unreachable("not reached");
1400
1401 case nir_op_pack_uvec2_to_uint: {
1402 dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1403 tmp1.writemask = WRITEMASK_X;
1404 op[0].swizzle = BRW_SWIZZLE_YYYY;
1405 emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1406
1407 dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1408 tmp2.writemask = WRITEMASK_X;
1409 op[0].swizzle = BRW_SWIZZLE_XXXX;
1410 emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1411
1412 emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1413 break;
1414 }
1415
1416 case nir_op_unpack_half_2x16:
1417 /* As NIR does not guarantee that we have a correct swizzle outside the
1418 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1419 * uses the source operand in an operation with WRITEMASK_Y while our
1420 * source operand has only size 1, it accessed incorrect data producing
1421 * regressions in Piglit. We repeat the swizzle of the first component on the
1422 * rest of components to avoid regressions. In the vec4_visitor IR code path
1423 * this is not needed because the operand has already the correct swizzle.
1424 */
1425 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1426 emit_unpack_half_2x16(dst, op[0]);
1427 break;
1428
1429 case nir_op_pack_half_2x16:
1430 emit_pack_half_2x16(dst, op[0]);
1431 break;
1432
1433 case nir_op_unpack_unorm_4x8:
1434 emit_unpack_unorm_4x8(dst, op[0]);
1435 break;
1436
1437 case nir_op_pack_unorm_4x8:
1438 emit_pack_unorm_4x8(dst, op[0]);
1439 break;
1440
1441 case nir_op_unpack_snorm_4x8:
1442 emit_unpack_snorm_4x8(dst, op[0]);
1443 break;
1444
1445 case nir_op_pack_snorm_4x8:
1446 emit_pack_snorm_4x8(dst, op[0]);
1447 break;
1448
1449 case nir_op_bitfield_reverse:
1450 emit(BFREV(dst, op[0]));
1451 break;
1452
1453 case nir_op_bit_count:
1454 emit(CBIT(dst, op[0]));
1455 break;
1456
1457 case nir_op_ufind_msb:
1458 case nir_op_ifind_msb: {
1459 emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1460
1461 /* FBH counts from the MSB side, while GLSL's findMSB() wants the count
1462 * from the LSB side. If FBH didn't return an error (0xFFFFFFFF), then
1463 * subtract the result from 31 to convert the MSB count into an LSB count.
1464 */
1465 src_reg src(dst);
1466 emit(CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ));
1467
1468 inst = emit(ADD(dst, src, brw_imm_d(31)));
1469 inst->predicate = BRW_PREDICATE_NORMAL;
1470 inst->src[0].negate = true;
1471 break;
1472 }
1473
1474 case nir_op_find_lsb:
1475 emit(FBL(dst, op[0]));
1476 break;
1477
1478 case nir_op_ubitfield_extract:
1479 case nir_op_ibitfield_extract:
1480 unreachable("should have been lowered");
1481 case nir_op_ubfe:
1482 case nir_op_ibfe:
1483 op[0] = fix_3src_operand(op[0]);
1484 op[1] = fix_3src_operand(op[1]);
1485 op[2] = fix_3src_operand(op[2]);
1486
1487 emit(BFE(dst, op[2], op[1], op[0]));
1488 break;
1489
1490 case nir_op_bfm:
1491 emit(BFI1(dst, op[0], op[1]));
1492 break;
1493
1494 case nir_op_bfi:
1495 op[0] = fix_3src_operand(op[0]);
1496 op[1] = fix_3src_operand(op[1]);
1497 op[2] = fix_3src_operand(op[2]);
1498
1499 emit(BFI2(dst, op[0], op[1], op[2]));
1500 break;
1501
1502 case nir_op_bitfield_insert:
1503 unreachable("not reached: should have been lowered");
1504
1505 case nir_op_fsign:
1506 /* AND(val, 0x80000000) gives the sign bit.
1507 *
1508 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1509 * zero.
1510 */
1511 emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1512
1513 op[0].type = BRW_REGISTER_TYPE_UD;
1514 dst.type = BRW_REGISTER_TYPE_UD;
1515 emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1516
1517 inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1518 inst->predicate = BRW_PREDICATE_NORMAL;
1519 dst.type = BRW_REGISTER_TYPE_F;
1520
1521 if (instr->dest.saturate) {
1522 inst = emit(MOV(dst, src_reg(dst)));
1523 inst->saturate = true;
1524 }
1525 break;
1526
1527 case nir_op_isign:
1528 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1529 * -> non-negative val generates 0x00000000.
1530 * Predicated OR sets 1 if val is positive.
1531 */
1532 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G));
1533 emit(ASR(dst, op[0], brw_imm_d(31)));
1534 inst = emit(OR(dst, src_reg(dst), brw_imm_d(1)));
1535 inst->predicate = BRW_PREDICATE_NORMAL;
1536 break;
1537
1538 case nir_op_ishl:
1539 emit(SHL(dst, op[0], op[1]));
1540 break;
1541
1542 case nir_op_ishr:
1543 emit(ASR(dst, op[0], op[1]));
1544 break;
1545
1546 case nir_op_ushr:
1547 emit(SHR(dst, op[0], op[1]));
1548 break;
1549
1550 case nir_op_ffma:
1551 op[0] = fix_3src_operand(op[0]);
1552 op[1] = fix_3src_operand(op[1]);
1553 op[2] = fix_3src_operand(op[2]);
1554
1555 inst = emit(MAD(dst, op[2], op[1], op[0]));
1556 inst->saturate = instr->dest.saturate;
1557 break;
1558
1559 case nir_op_flrp:
1560 inst = emit_lrp(dst, op[0], op[1], op[2]);
1561 inst->saturate = instr->dest.saturate;
1562 break;
1563
1564 case nir_op_bcsel:
1565 enum brw_predicate predicate;
1566 if (!optimize_predicate(instr, &predicate)) {
1567 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1568 switch (dst.writemask) {
1569 case WRITEMASK_X:
1570 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1571 break;
1572 case WRITEMASK_Y:
1573 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1574 break;
1575 case WRITEMASK_Z:
1576 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1577 break;
1578 case WRITEMASK_W:
1579 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1580 break;
1581 default:
1582 predicate = BRW_PREDICATE_NORMAL;
1583 break;
1584 }
1585 }
1586 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1587 inst->predicate = predicate;
1588 break;
1589
1590 case nir_op_fdot_replicated2:
1591 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1592 inst->saturate = instr->dest.saturate;
1593 break;
1594
1595 case nir_op_fdot_replicated3:
1596 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1597 inst->saturate = instr->dest.saturate;
1598 break;
1599
1600 case nir_op_fdot_replicated4:
1601 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1602 inst->saturate = instr->dest.saturate;
1603 break;
1604
1605 case nir_op_fdph_replicated:
1606 inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1607 inst->saturate = instr->dest.saturate;
1608 break;
1609
1610 case nir_op_fabs:
1611 case nir_op_iabs:
1612 case nir_op_fneg:
1613 case nir_op_ineg:
1614 case nir_op_fsat:
1615 unreachable("not reached: should be lowered by lower_source mods");
1616
1617 case nir_op_fdiv:
1618 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1619
1620 case nir_op_fmod:
1621 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1622
1623 case nir_op_fsub:
1624 case nir_op_isub:
1625 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1626
1627 default:
1628 unreachable("Unimplemented ALU operation");
1629 }
1630
1631 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1632 * to sign extend the low bit to 0/~0
1633 */
1634 if (devinfo->gen <= 5 &&
1635 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1636 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1637 dst_reg masked = dst_reg(this, glsl_type::int_type);
1638 masked.writemask = dst.writemask;
1639 emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1640 src_reg masked_neg = src_reg(masked);
1641 masked_neg.negate = true;
1642 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1643 }
1644 }
1645
1646 void
1647 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1648 {
1649 switch (instr->type) {
1650 case nir_jump_break:
1651 emit(BRW_OPCODE_BREAK);
1652 break;
1653
1654 case nir_jump_continue:
1655 emit(BRW_OPCODE_CONTINUE);
1656 break;
1657
1658 case nir_jump_return:
1659 /* fall through */
1660 default:
1661 unreachable("unknown jump");
1662 }
1663 }
1664
1665 enum ir_texture_opcode
1666 ir_texture_opcode_for_nir_texop(nir_texop texop)
1667 {
1668 enum ir_texture_opcode op;
1669
1670 switch (texop) {
1671 case nir_texop_lod: op = ir_lod; break;
1672 case nir_texop_query_levels: op = ir_query_levels; break;
1673 case nir_texop_texture_samples: op = ir_texture_samples; break;
1674 case nir_texop_tex: op = ir_tex; break;
1675 case nir_texop_tg4: op = ir_tg4; break;
1676 case nir_texop_txb: op = ir_txb; break;
1677 case nir_texop_txd: op = ir_txd; break;
1678 case nir_texop_txf: op = ir_txf; break;
1679 case nir_texop_txf_ms: op = ir_txf_ms; break;
1680 case nir_texop_txl: op = ir_txl; break;
1681 case nir_texop_txs: op = ir_txs; break;
1682 case nir_texop_samples_identical: op = ir_samples_identical; break;
1683 default:
1684 unreachable("unknown texture opcode");
1685 }
1686
1687 return op;
1688 }
1689 const glsl_type *
1690 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
1691 unsigned components)
1692 {
1693 switch (alu_type) {
1694 case nir_type_float:
1695 return glsl_type::vec(components);
1696 case nir_type_int:
1697 return glsl_type::ivec(components);
1698 case nir_type_uint:
1699 return glsl_type::uvec(components);
1700 case nir_type_bool:
1701 return glsl_type::bvec(components);
1702 default:
1703 return glsl_type::error_type;
1704 }
1705
1706 return glsl_type::error_type;
1707 }
1708
1709 void
1710 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
1711 {
1712 unsigned texture = instr->texture_index;
1713 unsigned sampler = instr->sampler_index;
1714 src_reg texture_reg = brw_imm_ud(texture);
1715 src_reg sampler_reg = brw_imm_ud(sampler);
1716 src_reg coordinate;
1717 const glsl_type *coord_type = NULL;
1718 src_reg shadow_comparitor;
1719 src_reg offset_value;
1720 src_reg lod, lod2;
1721 src_reg sample_index;
1722 src_reg mcs;
1723
1724 const glsl_type *dest_type =
1725 glsl_type_for_nir_alu_type(instr->dest_type,
1726 nir_tex_instr_dest_size(instr));
1727 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
1728
1729 /* The hardware requires a LOD for buffer textures */
1730 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
1731 lod = brw_imm_d(0);
1732
1733 /* Load the texture operation sources */
1734 uint32_t constant_offset = 0;
1735 for (unsigned i = 0; i < instr->num_srcs; i++) {
1736 switch (instr->src[i].src_type) {
1737 case nir_tex_src_comparitor:
1738 shadow_comparitor = get_nir_src(instr->src[i].src,
1739 BRW_REGISTER_TYPE_F, 1);
1740 break;
1741
1742 case nir_tex_src_coord: {
1743 unsigned src_size = nir_tex_instr_src_size(instr, i);
1744
1745 switch (instr->op) {
1746 case nir_texop_txf:
1747 case nir_texop_txf_ms:
1748 case nir_texop_samples_identical:
1749 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
1750 src_size);
1751 coord_type = glsl_type::ivec(src_size);
1752 break;
1753
1754 default:
1755 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1756 src_size);
1757 coord_type = glsl_type::vec(src_size);
1758 break;
1759 }
1760 break;
1761 }
1762
1763 case nir_tex_src_ddx:
1764 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1765 nir_tex_instr_src_size(instr, i));
1766 break;
1767
1768 case nir_tex_src_ddy:
1769 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1770 nir_tex_instr_src_size(instr, i));
1771 break;
1772
1773 case nir_tex_src_lod:
1774 switch (instr->op) {
1775 case nir_texop_txs:
1776 case nir_texop_txf:
1777 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1778 break;
1779
1780 default:
1781 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
1782 break;
1783 }
1784 break;
1785
1786 case nir_tex_src_ms_index: {
1787 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1788 break;
1789 }
1790
1791 case nir_tex_src_offset: {
1792 nir_const_value *const_offset =
1793 nir_src_as_const_value(instr->src[i].src);
1794 if (const_offset) {
1795 constant_offset = brw_texture_offset(const_offset->i32, 3);
1796 } else {
1797 offset_value =
1798 get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
1799 }
1800 break;
1801 }
1802
1803 case nir_tex_src_texture_offset: {
1804 /* The highest texture which may be used by this operation is
1805 * the last element of the array. Mark it here, because the generator
1806 * doesn't have enough information to determine the bound.
1807 */
1808 uint32_t array_size = instr->texture_array_size;
1809 uint32_t max_used = texture + array_size - 1;
1810 if (instr->op == nir_texop_tg4) {
1811 max_used += prog_data->base.binding_table.gather_texture_start;
1812 } else {
1813 max_used += prog_data->base.binding_table.texture_start;
1814 }
1815
1816 brw_mark_surface_used(&prog_data->base, max_used);
1817
1818 /* Emit code to evaluate the actual indexing expression */
1819 src_reg src = get_nir_src(instr->src[i].src, 1);
1820 src_reg temp(this, glsl_type::uint_type);
1821 emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
1822 texture_reg = emit_uniformize(temp);
1823 break;
1824 }
1825
1826 case nir_tex_src_sampler_offset: {
1827 /* Emit code to evaluate the actual indexing expression */
1828 src_reg src = get_nir_src(instr->src[i].src, 1);
1829 src_reg temp(this, glsl_type::uint_type);
1830 emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
1831 sampler_reg = emit_uniformize(temp);
1832 break;
1833 }
1834
1835 case nir_tex_src_projector:
1836 unreachable("Should be lowered by do_lower_texture_projection");
1837
1838 case nir_tex_src_bias:
1839 unreachable("LOD bias is not valid for vertex shaders.\n");
1840
1841 default:
1842 unreachable("unknown texture source");
1843 }
1844 }
1845
1846 if (instr->op == nir_texop_txf_ms ||
1847 instr->op == nir_texop_samples_identical) {
1848 assert(coord_type != NULL);
1849 if (devinfo->gen >= 7 &&
1850 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
1851 mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
1852 } else {
1853 mcs = brw_imm_ud(0u);
1854 }
1855 }
1856
1857 /* Stuff the channel select bits in the top of the texture offset */
1858 if (instr->op == nir_texop_tg4) {
1859 if (instr->component == 1 &&
1860 (key_tex->gather_channel_quirk_mask & (1 << texture))) {
1861 /* gather4 sampler is broken for green channel on RG32F --
1862 * we must ask for blue instead.
1863 */
1864 constant_offset |= 2 << 16;
1865 } else {
1866 constant_offset |= instr->component << 16;
1867 }
1868 }
1869
1870 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
1871
1872 bool is_cube_array =
1873 instr->op == nir_texop_txs &&
1874 instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE &&
1875 instr->is_array;
1876
1877 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
1878 shadow_comparitor,
1879 lod, lod2, sample_index,
1880 constant_offset, offset_value,
1881 mcs, is_cube_array,
1882 texture, texture_reg, sampler, sampler_reg);
1883 }
1884
1885 void
1886 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
1887 {
1888 nir_ssa_values[instr->def.index] = dst_reg(VGRF, alloc.allocate(1));
1889 }
1890
1891 }