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