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