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