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