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