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