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