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