i965/vec4/nir: implement double comparisons
[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(function, nir) {
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 break;
65
66 case nir_intrinsic_load_base_vertex:
67 reg = &nir_system_values[SYSTEM_VALUE_BASE_VERTEX];
68 if (reg->file == BAD_FILE)
69 *reg = *make_reg_for_system_value(SYSTEM_VALUE_BASE_VERTEX);
70 break;
71
72 case nir_intrinsic_load_instance_id:
73 reg = &nir_system_values[SYSTEM_VALUE_INSTANCE_ID];
74 if (reg->file == BAD_FILE)
75 *reg = *make_reg_for_system_value(SYSTEM_VALUE_INSTANCE_ID);
76 break;
77
78 case nir_intrinsic_load_base_instance:
79 reg = &nir_system_values[SYSTEM_VALUE_BASE_INSTANCE];
80 if (reg->file == BAD_FILE)
81 *reg = *make_reg_for_system_value(SYSTEM_VALUE_BASE_INSTANCE);
82 break;
83
84 case nir_intrinsic_load_draw_id:
85 reg = &nir_system_values[SYSTEM_VALUE_DRAW_ID];
86 if (reg->file == BAD_FILE)
87 *reg = *make_reg_for_system_value(SYSTEM_VALUE_DRAW_ID);
88 break;
89
90 default:
91 break;
92 }
93 }
94
95 static bool
96 setup_system_values_block(nir_block *block, vec4_visitor *v)
97 {
98 nir_foreach_instr(instr, block) {
99 if (instr->type != nir_instr_type_intrinsic)
100 continue;
101
102 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
103 v->nir_setup_system_value_intrinsic(intrin);
104 }
105
106 return true;
107 }
108
109 void
110 vec4_visitor::nir_setup_system_values()
111 {
112 nir_system_values = ralloc_array(mem_ctx, dst_reg, SYSTEM_VALUE_MAX);
113 for (unsigned i = 0; i < SYSTEM_VALUE_MAX; i++) {
114 nir_system_values[i] = dst_reg();
115 }
116
117 nir_foreach_function(function, nir) {
118 assert(strcmp(function->name, "main") == 0);
119 assert(function->impl);
120 nir_foreach_block(block, function->impl) {
121 setup_system_values_block(block, this);
122 }
123 }
124 }
125
126 void
127 vec4_visitor::nir_setup_uniforms()
128 {
129 uniforms = nir->num_uniforms / 16;
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 const unsigned num_regs = array_elems * DIV_ROUND_UP(reg->bit_size, 32);
144 nir_locals[reg->index] = dst_reg(VGRF, alloc.allocate(num_regs));
145
146 if (reg->bit_size == 64)
147 nir_locals[reg->index].type = BRW_REGISTER_TYPE_DF;
148 }
149
150 nir_ssa_values = ralloc_array(mem_ctx, dst_reg, impl->ssa_alloc);
151
152 nir_emit_cf_list(&impl->body);
153 }
154
155 void
156 vec4_visitor::nir_emit_cf_list(exec_list *list)
157 {
158 exec_list_validate(list);
159 foreach_list_typed(nir_cf_node, node, node, list) {
160 switch (node->type) {
161 case nir_cf_node_if:
162 nir_emit_if(nir_cf_node_as_if(node));
163 break;
164
165 case nir_cf_node_loop:
166 nir_emit_loop(nir_cf_node_as_loop(node));
167 break;
168
169 case nir_cf_node_block:
170 nir_emit_block(nir_cf_node_as_block(node));
171 break;
172
173 default:
174 unreachable("Invalid CFG node block");
175 }
176 }
177 }
178
179 void
180 vec4_visitor::nir_emit_if(nir_if *if_stmt)
181 {
182 /* First, put the condition in f0 */
183 src_reg condition = get_nir_src(if_stmt->condition, BRW_REGISTER_TYPE_D, 1);
184 vec4_instruction *inst = emit(MOV(dst_null_d(), condition));
185 inst->conditional_mod = BRW_CONDITIONAL_NZ;
186
187 /* We can just predicate based on the X channel, as the condition only
188 * goes on its own line */
189 emit(IF(BRW_PREDICATE_ALIGN16_REPLICATE_X));
190
191 nir_emit_cf_list(&if_stmt->then_list);
192
193 /* note: if the else is empty, dead CF elimination will remove it */
194 emit(BRW_OPCODE_ELSE);
195
196 nir_emit_cf_list(&if_stmt->else_list);
197
198 emit(BRW_OPCODE_ENDIF);
199 }
200
201 void
202 vec4_visitor::nir_emit_loop(nir_loop *loop)
203 {
204 emit(BRW_OPCODE_DO);
205
206 nir_emit_cf_list(&loop->body);
207
208 emit(BRW_OPCODE_WHILE);
209 }
210
211 void
212 vec4_visitor::nir_emit_block(nir_block *block)
213 {
214 nir_foreach_instr(instr, block) {
215 nir_emit_instr(instr);
216 }
217 }
218
219 void
220 vec4_visitor::nir_emit_instr(nir_instr *instr)
221 {
222 base_ir = instr;
223
224 switch (instr->type) {
225 case nir_instr_type_load_const:
226 nir_emit_load_const(nir_instr_as_load_const(instr));
227 break;
228
229 case nir_instr_type_intrinsic:
230 nir_emit_intrinsic(nir_instr_as_intrinsic(instr));
231 break;
232
233 case nir_instr_type_alu:
234 nir_emit_alu(nir_instr_as_alu(instr));
235 break;
236
237 case nir_instr_type_jump:
238 nir_emit_jump(nir_instr_as_jump(instr));
239 break;
240
241 case nir_instr_type_tex:
242 nir_emit_texture(nir_instr_as_tex(instr));
243 break;
244
245 case nir_instr_type_ssa_undef:
246 nir_emit_undef(nir_instr_as_ssa_undef(instr));
247 break;
248
249 default:
250 fprintf(stderr, "VS instruction not yet implemented by NIR->vec4\n");
251 break;
252 }
253 }
254
255 static dst_reg
256 dst_reg_for_nir_reg(vec4_visitor *v, nir_register *nir_reg,
257 unsigned base_offset, nir_src *indirect)
258 {
259 dst_reg reg;
260
261 reg = v->nir_locals[nir_reg->index];
262 reg = offset(reg, 8, base_offset);
263 if (indirect) {
264 reg.reladdr =
265 new(v->mem_ctx) src_reg(v->get_nir_src(*indirect,
266 BRW_REGISTER_TYPE_D,
267 1));
268 }
269 return reg;
270 }
271
272 dst_reg
273 vec4_visitor::get_nir_dest(const nir_dest &dest)
274 {
275 if (dest.is_ssa) {
276 dst_reg dst =
277 dst_reg(VGRF, alloc.allocate(DIV_ROUND_UP(dest.ssa.bit_size, 32)));
278 nir_ssa_values[dest.ssa.index] = dst;
279 return dst;
280 } else {
281 return dst_reg_for_nir_reg(this, dest.reg.reg, dest.reg.base_offset,
282 dest.reg.indirect);
283 }
284 }
285
286 dst_reg
287 vec4_visitor::get_nir_dest(const nir_dest &dest, enum brw_reg_type type)
288 {
289 return retype(get_nir_dest(dest), type);
290 }
291
292 dst_reg
293 vec4_visitor::get_nir_dest(const nir_dest &dest, nir_alu_type type)
294 {
295 return get_nir_dest(dest, brw_type_for_nir_type(type));
296 }
297
298 src_reg
299 vec4_visitor::get_nir_src(const nir_src &src, enum brw_reg_type type,
300 unsigned num_components)
301 {
302 dst_reg reg;
303
304 if (src.is_ssa) {
305 assert(src.ssa != NULL);
306 reg = nir_ssa_values[src.ssa->index];
307 }
308 else {
309 reg = dst_reg_for_nir_reg(this, src.reg.reg, src.reg.base_offset,
310 src.reg.indirect);
311 }
312
313 reg = retype(reg, type);
314
315 src_reg reg_as_src = src_reg(reg);
316 reg_as_src.swizzle = brw_swizzle_for_size(num_components);
317 return reg_as_src;
318 }
319
320 src_reg
321 vec4_visitor::get_nir_src(const nir_src &src, nir_alu_type type,
322 unsigned num_components)
323 {
324 return get_nir_src(src, brw_type_for_nir_type(type), num_components);
325 }
326
327 src_reg
328 vec4_visitor::get_nir_src(const nir_src &src, unsigned num_components)
329 {
330 /* if type is not specified, default to signed int */
331 return get_nir_src(src, nir_type_int32, num_components);
332 }
333
334 src_reg
335 vec4_visitor::get_indirect_offset(nir_intrinsic_instr *instr)
336 {
337 nir_src *offset_src = nir_get_io_offset_src(instr);
338 nir_const_value *const_value = nir_src_as_const_value(*offset_src);
339
340 if (const_value) {
341 /* The only constant offset we should find is 0. brw_nir.c's
342 * add_const_offset_to_base() will fold other constant offsets
343 * into instr->const_index[0].
344 */
345 assert(const_value->u32[0] == 0);
346 return src_reg();
347 }
348
349 return get_nir_src(*offset_src, BRW_REGISTER_TYPE_UD, 1);
350 }
351
352 void
353 vec4_visitor::nir_emit_load_const(nir_load_const_instr *instr)
354 {
355 dst_reg reg;
356
357 if (instr->def.bit_size == 64) {
358 reg = dst_reg(VGRF, alloc.allocate(2));
359 reg.type = BRW_REGISTER_TYPE_DF;
360 } else {
361 reg = dst_reg(VGRF, alloc.allocate(1));
362 reg.type = BRW_REGISTER_TYPE_D;
363 }
364
365 unsigned remaining = brw_writemask_for_size(instr->def.num_components);
366
367 /* @FIXME: consider emitting vector operations to save some MOVs in
368 * cases where the components are representable in 8 bits.
369 * For now, we emit a MOV for each distinct value.
370 */
371 for (unsigned i = 0; i < instr->def.num_components; i++) {
372 unsigned writemask = 1 << i;
373
374 if ((remaining & writemask) == 0)
375 continue;
376
377 for (unsigned j = i; j < instr->def.num_components; j++) {
378 if ((instr->def.bit_size == 32 &&
379 instr->value.u32[i] == instr->value.u32[j]) ||
380 (instr->def.bit_size == 64 &&
381 instr->value.f64[i] == instr->value.f64[j])) {
382 writemask |= 1 << j;
383 }
384 }
385
386 reg.writemask = writemask;
387 if (instr->def.bit_size == 64) {
388 emit(MOV(reg, brw_imm_df(instr->value.f64[i])));
389 } else {
390 emit(MOV(reg, brw_imm_d(instr->value.i32[i])));
391 }
392
393 remaining &= ~writemask;
394 }
395
396 /* Set final writemask */
397 reg.writemask = brw_writemask_for_size(instr->def.num_components);
398
399 nir_ssa_values[instr->def.index] = reg;
400 }
401
402 void
403 vec4_visitor::nir_emit_intrinsic(nir_intrinsic_instr *instr)
404 {
405 dst_reg dest;
406 src_reg src;
407
408 switch (instr->intrinsic) {
409
410 case nir_intrinsic_load_input: {
411 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
412
413 /* We set EmitNoIndirectInput for VS */
414 assert(const_offset);
415
416 src = src_reg(ATTR, instr->const_index[0] + const_offset->u32[0],
417 glsl_type::uvec4_type);
418 /* Swizzle source based on component layout qualifier */
419 src.swizzle = BRW_SWZ_COMP_INPUT(nir_intrinsic_component(instr));
420
421 dest = get_nir_dest(instr->dest, src.type);
422 dest.writemask = brw_writemask_for_size(instr->num_components);
423
424 emit(MOV(dest, src));
425 break;
426 }
427
428 case nir_intrinsic_store_output: {
429 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
430 assert(const_offset);
431
432 int varying = instr->const_index[0] + const_offset->u32[0];
433
434 src = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_F,
435 instr->num_components);
436
437 unsigned c = nir_intrinsic_component(instr);
438 output_reg[varying][c] = dst_reg(src);
439 output_num_components[varying][c] = instr->num_components;
440 break;
441 }
442
443 case nir_intrinsic_get_buffer_size: {
444 nir_const_value *const_uniform_block = nir_src_as_const_value(instr->src[0]);
445 unsigned ssbo_index = const_uniform_block ? const_uniform_block->u32[0] : 0;
446
447 const unsigned index =
448 prog_data->base.binding_table.ssbo_start + ssbo_index;
449 dst_reg result_dst = get_nir_dest(instr->dest);
450 vec4_instruction *inst = new(mem_ctx)
451 vec4_instruction(VS_OPCODE_GET_BUFFER_SIZE, result_dst);
452
453 inst->base_mrf = 2;
454 inst->mlen = 1; /* always at least one */
455 inst->src[1] = brw_imm_ud(index);
456
457 /* MRF for the first parameter */
458 src_reg lod = brw_imm_d(0);
459 int param_base = inst->base_mrf;
460 int writemask = WRITEMASK_X;
461 emit(MOV(dst_reg(MRF, param_base, glsl_type::int_type, writemask), lod));
462
463 emit(inst);
464
465 brw_mark_surface_used(&prog_data->base, index);
466 break;
467 }
468
469 case nir_intrinsic_store_ssbo: {
470 assert(devinfo->gen >= 7);
471
472 /* Block index */
473 src_reg surf_index;
474 nir_const_value *const_uniform_block =
475 nir_src_as_const_value(instr->src[1]);
476 if (const_uniform_block) {
477 unsigned index = prog_data->base.binding_table.ssbo_start +
478 const_uniform_block->u32[0];
479 surf_index = brw_imm_ud(index);
480 brw_mark_surface_used(&prog_data->base, index);
481 } else {
482 surf_index = src_reg(this, glsl_type::uint_type);
483 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[1], 1),
484 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
485 surf_index = emit_uniformize(surf_index);
486
487 brw_mark_surface_used(&prog_data->base,
488 prog_data->base.binding_table.ssbo_start +
489 nir->info->num_ssbos - 1);
490 }
491
492 /* Offset */
493 src_reg offset_reg;
494 nir_const_value *const_offset = nir_src_as_const_value(instr->src[2]);
495 if (const_offset) {
496 offset_reg = brw_imm_ud(const_offset->u32[0]);
497 } else {
498 offset_reg = get_nir_src(instr->src[2], 1);
499 }
500
501 /* Value */
502 src_reg val_reg = get_nir_src(instr->src[0], 4);
503
504 /* Writemask */
505 unsigned write_mask = instr->const_index[0];
506
507 /* IvyBridge does not have a native SIMD4x2 untyped write message so untyped
508 * writes will use SIMD8 mode. In order to hide this and keep symmetry across
509 * typed and untyped messages and across hardware platforms, the
510 * current implementation of the untyped messages will transparently convert
511 * the SIMD4x2 payload into an equivalent SIMD8 payload by transposing it
512 * and enabling only channel X on the SEND instruction.
513 *
514 * The above, works well for full vector writes, but not for partial writes
515 * where we want to write some channels and not others, like when we have
516 * code such as v.xyw = vec3(1,2,4). Because the untyped write messages are
517 * quite restrictive with regards to the channel enables we can configure in
518 * the message descriptor (not all combinations are allowed) we cannot simply
519 * implement these scenarios with a single message while keeping the
520 * aforementioned symmetry in the implementation. For now we de decided that
521 * it is better to keep the symmetry to reduce complexity, so in situations
522 * such as the one described we end up emitting two untyped write messages
523 * (one for xy and another for w).
524 *
525 * The code below packs consecutive channels into a single write message,
526 * detects gaps in the vector write and if needed, sends a second message
527 * with the remaining channels. If in the future we decide that we want to
528 * emit a single message at the expense of losing the symmetry in the
529 * implementation we can:
530 *
531 * 1) For IvyBridge: Only use the red channel of the untyped write SIMD8
532 * message payload. In this mode we can write up to 8 offsets and dwords
533 * to the red channel only (for the two vec4s in the SIMD4x2 execution)
534 * and select which of the 8 channels carry data to write by setting the
535 * appropriate writemask in the dst register of the SEND instruction.
536 * It would require to write a new generator opcode specifically for
537 * IvyBridge since we would need to prepare a SIMD8 payload that could
538 * use any channel, not just X.
539 *
540 * 2) For Haswell+: Simply send a single write message but set the writemask
541 * on the dst of the SEND instruction to select the channels we want to
542 * write. It would require to modify the current messages to receive
543 * and honor the writemask provided.
544 */
545 const vec4_builder bld = vec4_builder(this).at_end()
546 .annotate(current_annotation, base_ir);
547
548 int swizzle[4] = { 0, 0, 0, 0};
549 int num_channels = 0;
550 unsigned skipped_channels = 0;
551 int num_components = instr->num_components;
552 for (int i = 0; i < num_components; i++) {
553 /* Check if this channel needs to be written. If so, record the
554 * channel we need to take the data from in the swizzle array
555 */
556 int component_mask = 1 << i;
557 int write_test = write_mask & component_mask;
558 if (write_test)
559 swizzle[num_channels++] = i;
560
561 /* If we don't have to write this channel it means we have a gap in the
562 * vector, so write the channels we accumulated until now, if any. Do
563 * the same if this was the last component in the vector.
564 */
565 if (!write_test || i == num_components - 1) {
566 if (num_channels > 0) {
567 /* We have channels to write, so update the offset we need to
568 * write at to skip the channels we skipped, if any.
569 */
570 if (skipped_channels > 0) {
571 if (offset_reg.file == IMM) {
572 offset_reg.ud += 4 * skipped_channels;
573 } else {
574 emit(ADD(dst_reg(offset_reg), offset_reg,
575 brw_imm_ud(4 * skipped_channels)));
576 }
577 }
578
579 /* Swizzle the data register so we take the data from the channels
580 * we need to write and send the write message. This will write
581 * num_channels consecutive dwords starting at offset.
582 */
583 val_reg.swizzle =
584 BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
585 emit_untyped_write(bld, surf_index, offset_reg, val_reg,
586 1 /* dims */, num_channels /* size */,
587 BRW_PREDICATE_NONE);
588
589 /* If we have to do a second write we will have to update the
590 * offset so that we jump over the channels we have just written
591 * now.
592 */
593 skipped_channels = num_channels;
594
595 /* Restart the count for the next write message */
596 num_channels = 0;
597 }
598
599 /* We did not write the current channel, so increase skipped count */
600 skipped_channels++;
601 }
602 }
603
604 break;
605 }
606
607 case nir_intrinsic_load_ssbo: {
608 assert(devinfo->gen >= 7);
609
610 nir_const_value *const_uniform_block =
611 nir_src_as_const_value(instr->src[0]);
612
613 src_reg surf_index;
614 if (const_uniform_block) {
615 unsigned index = prog_data->base.binding_table.ssbo_start +
616 const_uniform_block->u32[0];
617 surf_index = brw_imm_ud(index);
618
619 brw_mark_surface_used(&prog_data->base, index);
620 } else {
621 surf_index = src_reg(this, glsl_type::uint_type);
622 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], 1),
623 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
624 surf_index = emit_uniformize(surf_index);
625
626 /* Assume this may touch any UBO. It would be nice to provide
627 * a tighter bound, but the array information is already lowered away.
628 */
629 brw_mark_surface_used(&prog_data->base,
630 prog_data->base.binding_table.ssbo_start +
631 nir->info->num_ssbos - 1);
632 }
633
634 src_reg offset_reg;
635 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
636 if (const_offset) {
637 offset_reg = brw_imm_ud(const_offset->u32[0]);
638 } else {
639 offset_reg = get_nir_src(instr->src[1], 1);
640 }
641
642 /* Read the vector */
643 const vec4_builder bld = vec4_builder(this).at_end()
644 .annotate(current_annotation, base_ir);
645
646 src_reg read_result = emit_untyped_read(bld, surf_index, offset_reg,
647 1 /* dims */, 4 /* size*/,
648 BRW_PREDICATE_NONE);
649 dst_reg dest = get_nir_dest(instr->dest);
650 read_result.type = dest.type;
651 read_result.swizzle = brw_swizzle_for_size(instr->num_components);
652 emit(MOV(dest, read_result));
653
654 break;
655 }
656
657 case nir_intrinsic_ssbo_atomic_add:
658 nir_emit_ssbo_atomic(BRW_AOP_ADD, instr);
659 break;
660 case nir_intrinsic_ssbo_atomic_imin:
661 nir_emit_ssbo_atomic(BRW_AOP_IMIN, instr);
662 break;
663 case nir_intrinsic_ssbo_atomic_umin:
664 nir_emit_ssbo_atomic(BRW_AOP_UMIN, instr);
665 break;
666 case nir_intrinsic_ssbo_atomic_imax:
667 nir_emit_ssbo_atomic(BRW_AOP_IMAX, instr);
668 break;
669 case nir_intrinsic_ssbo_atomic_umax:
670 nir_emit_ssbo_atomic(BRW_AOP_UMAX, instr);
671 break;
672 case nir_intrinsic_ssbo_atomic_and:
673 nir_emit_ssbo_atomic(BRW_AOP_AND, instr);
674 break;
675 case nir_intrinsic_ssbo_atomic_or:
676 nir_emit_ssbo_atomic(BRW_AOP_OR, instr);
677 break;
678 case nir_intrinsic_ssbo_atomic_xor:
679 nir_emit_ssbo_atomic(BRW_AOP_XOR, instr);
680 break;
681 case nir_intrinsic_ssbo_atomic_exchange:
682 nir_emit_ssbo_atomic(BRW_AOP_MOV, instr);
683 break;
684 case nir_intrinsic_ssbo_atomic_comp_swap:
685 nir_emit_ssbo_atomic(BRW_AOP_CMPWR, instr);
686 break;
687
688 case nir_intrinsic_load_vertex_id:
689 unreachable("should be lowered by lower_vertex_id()");
690
691 case nir_intrinsic_load_vertex_id_zero_base:
692 case nir_intrinsic_load_base_vertex:
693 case nir_intrinsic_load_instance_id:
694 case nir_intrinsic_load_base_instance:
695 case nir_intrinsic_load_draw_id:
696 case nir_intrinsic_load_invocation_id: {
697 gl_system_value sv = nir_system_value_from_intrinsic(instr->intrinsic);
698 src_reg val = src_reg(nir_system_values[sv]);
699 assert(val.file != BAD_FILE);
700 dest = get_nir_dest(instr->dest, val.type);
701 emit(MOV(dest, val));
702 break;
703 }
704
705 case nir_intrinsic_load_uniform: {
706 /* Offsets are in bytes but they should always be multiples of 4 */
707 assert(nir_intrinsic_base(instr) % 4 == 0);
708
709 dest = get_nir_dest(instr->dest);
710
711 src = src_reg(dst_reg(UNIFORM, nir_intrinsic_base(instr) / 16));
712 src.type = dest.type;
713
714 /* Uniforms don't actually have to be vec4 aligned. In the case that
715 * it isn't, we have to use a swizzle to shift things around. They
716 * do still have the std140 alignment requirement that vec2's have to
717 * be vec2-aligned and vec3's and vec4's have to be vec4-aligned.
718 *
719 * The swizzle also works in the indirect case as the generator adds
720 * the swizzle to the offset for us.
721 */
722 unsigned shift = (nir_intrinsic_base(instr) % 16) / 4;
723 assert(shift + instr->num_components <= 4);
724
725 nir_const_value *const_offset = nir_src_as_const_value(instr->src[0]);
726 if (const_offset) {
727 /* Offsets are in bytes but they should always be multiples of 4 */
728 assert(const_offset->u32[0] % 4 == 0);
729
730 unsigned offset = const_offset->u32[0] + shift * 4;
731 src.offset = ROUND_DOWN_TO(offset, 16);
732 shift = (offset % 16) / 4;
733 src.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
734
735 emit(MOV(dest, src));
736 } else {
737 src.swizzle += BRW_SWIZZLE4(shift, shift, shift, shift);
738
739 src_reg indirect = get_nir_src(instr->src[0], BRW_REGISTER_TYPE_UD, 1);
740
741 /* MOV_INDIRECT is going to stomp the whole thing anyway */
742 dest.writemask = WRITEMASK_XYZW;
743
744 emit(SHADER_OPCODE_MOV_INDIRECT, dest, src,
745 indirect, brw_imm_ud(instr->const_index[1]));
746 }
747 break;
748 }
749
750 case nir_intrinsic_atomic_counter_read:
751 case nir_intrinsic_atomic_counter_inc:
752 case nir_intrinsic_atomic_counter_dec: {
753 unsigned surf_index = prog_data->base.binding_table.abo_start +
754 (unsigned) instr->const_index[0];
755 const vec4_builder bld =
756 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
757
758 /* Get some metadata from the image intrinsic. */
759 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
760
761 /* Get the arguments of the atomic intrinsic. */
762 src_reg offset = get_nir_src(instr->src[0], nir_type_int32,
763 instr->num_components);
764 const src_reg surface = brw_imm_ud(surf_index);
765 const src_reg src0 = (info->num_srcs >= 2
766 ? get_nir_src(instr->src[1]) : src_reg());
767 const src_reg src1 = (info->num_srcs >= 3
768 ? get_nir_src(instr->src[2]) : src_reg());
769
770 src_reg tmp;
771
772 dest = get_nir_dest(instr->dest);
773
774 if (instr->intrinsic == nir_intrinsic_atomic_counter_read) {
775 tmp = emit_untyped_read(bld, surface, offset, 1, 1);
776 } else {
777 tmp = emit_untyped_atomic(bld, surface, offset,
778 src0, src1,
779 1, 1,
780 get_atomic_counter_op(instr->intrinsic));
781 }
782
783 bld.MOV(retype(dest, tmp.type), tmp);
784 brw_mark_surface_used(stage_prog_data, surf_index);
785 break;
786 }
787
788 case nir_intrinsic_load_ubo: {
789 nir_const_value *const_block_index = nir_src_as_const_value(instr->src[0]);
790 src_reg surf_index;
791
792 dest = get_nir_dest(instr->dest);
793
794 if (const_block_index) {
795 /* The block index is a constant, so just emit the binding table entry
796 * as an immediate.
797 */
798 const unsigned index = prog_data->base.binding_table.ubo_start +
799 const_block_index->u32[0];
800 surf_index = brw_imm_ud(index);
801 brw_mark_surface_used(&prog_data->base, index);
802 } else {
803 /* The block index is not a constant. Evaluate the index expression
804 * per-channel and add the base UBO index; we have to select a value
805 * from any live channel.
806 */
807 surf_index = src_reg(this, glsl_type::uint_type);
808 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int32,
809 instr->num_components),
810 brw_imm_ud(prog_data->base.binding_table.ubo_start)));
811 surf_index = emit_uniformize(surf_index);
812
813 /* Assume this may touch any UBO. It would be nice to provide
814 * a tighter bound, but the array information is already lowered away.
815 */
816 brw_mark_surface_used(&prog_data->base,
817 prog_data->base.binding_table.ubo_start +
818 nir->info->num_ubos - 1);
819 }
820
821 src_reg offset;
822 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
823 if (const_offset) {
824 offset = brw_imm_ud(const_offset->u32[0] & ~15);
825 } else {
826 offset = get_nir_src(instr->src[1], nir_type_uint32, 1);
827 }
828
829 src_reg packed_consts = src_reg(this, glsl_type::vec4_type);
830 packed_consts.type = dest.type;
831
832 emit_pull_constant_load_reg(dst_reg(packed_consts),
833 surf_index,
834 offset,
835 NULL, NULL /* before_block/inst */);
836
837 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
838 if (const_offset) {
839 packed_consts.swizzle += BRW_SWIZZLE4(const_offset->u32[0] % 16 / 4,
840 const_offset->u32[0] % 16 / 4,
841 const_offset->u32[0] % 16 / 4,
842 const_offset->u32[0] % 16 / 4);
843 }
844
845 emit(MOV(dest, packed_consts));
846 break;
847 }
848
849 case nir_intrinsic_memory_barrier: {
850 const vec4_builder bld =
851 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
852 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
853 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
854 ->size_written = 2 * REG_SIZE;
855 break;
856 }
857
858 case nir_intrinsic_shader_clock: {
859 /* We cannot do anything if there is an event, so ignore it for now */
860 const src_reg shader_clock = get_timestamp();
861 const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
862
863 dest = get_nir_dest(instr->dest, type);
864 emit(MOV(dest, shader_clock));
865 break;
866 }
867
868 default:
869 unreachable("Unknown intrinsic");
870 }
871 }
872
873 void
874 vec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
875 {
876 dst_reg dest;
877 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
878 dest = get_nir_dest(instr->dest);
879
880 src_reg surface;
881 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
882 if (const_surface) {
883 unsigned surf_index = prog_data->base.binding_table.ssbo_start +
884 const_surface->u32[0];
885 surface = brw_imm_ud(surf_index);
886 brw_mark_surface_used(&prog_data->base, surf_index);
887 } else {
888 surface = src_reg(this, glsl_type::uint_type);
889 emit(ADD(dst_reg(surface), get_nir_src(instr->src[0]),
890 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
891
892 /* Assume this may touch any UBO. This is the same we do for other
893 * UBO/SSBO accesses with non-constant surface.
894 */
895 brw_mark_surface_used(&prog_data->base,
896 prog_data->base.binding_table.ssbo_start +
897 nir->info->num_ssbos - 1);
898 }
899
900 src_reg offset = get_nir_src(instr->src[1], 1);
901 src_reg data1 = get_nir_src(instr->src[2], 1);
902 src_reg data2;
903 if (op == BRW_AOP_CMPWR)
904 data2 = get_nir_src(instr->src[3], 1);
905
906 /* Emit the actual atomic operation operation */
907 const vec4_builder bld =
908 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
909
910 src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
911 data1, data2,
912 1 /* dims */, 1 /* rsize */,
913 op,
914 BRW_PREDICATE_NONE);
915 dest.type = atomic_result.type;
916 bld.MOV(dest, atomic_result);
917 }
918
919 static unsigned
920 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
921 {
922 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
923 }
924
925 static enum brw_conditional_mod
926 brw_conditional_for_nir_comparison(nir_op op)
927 {
928 switch (op) {
929 case nir_op_flt:
930 case nir_op_ilt:
931 case nir_op_ult:
932 return BRW_CONDITIONAL_L;
933
934 case nir_op_fge:
935 case nir_op_ige:
936 case nir_op_uge:
937 return BRW_CONDITIONAL_GE;
938
939 case nir_op_feq:
940 case nir_op_ieq:
941 case nir_op_ball_fequal2:
942 case nir_op_ball_iequal2:
943 case nir_op_ball_fequal3:
944 case nir_op_ball_iequal3:
945 case nir_op_ball_fequal4:
946 case nir_op_ball_iequal4:
947 return BRW_CONDITIONAL_Z;
948
949 case nir_op_fne:
950 case nir_op_ine:
951 case nir_op_bany_fnequal2:
952 case nir_op_bany_inequal2:
953 case nir_op_bany_fnequal3:
954 case nir_op_bany_inequal3:
955 case nir_op_bany_fnequal4:
956 case nir_op_bany_inequal4:
957 return BRW_CONDITIONAL_NZ;
958
959 default:
960 unreachable("not reached: bad operation for comparison");
961 }
962 }
963
964 bool
965 vec4_visitor::optimize_predicate(nir_alu_instr *instr,
966 enum brw_predicate *predicate)
967 {
968 if (!instr->src[0].src.is_ssa ||
969 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
970 return false;
971
972 nir_alu_instr *cmp_instr =
973 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
974
975 switch (cmp_instr->op) {
976 case nir_op_bany_fnequal2:
977 case nir_op_bany_inequal2:
978 case nir_op_bany_fnequal3:
979 case nir_op_bany_inequal3:
980 case nir_op_bany_fnequal4:
981 case nir_op_bany_inequal4:
982 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
983 break;
984 case nir_op_ball_fequal2:
985 case nir_op_ball_iequal2:
986 case nir_op_ball_fequal3:
987 case nir_op_ball_iequal3:
988 case nir_op_ball_fequal4:
989 case nir_op_ball_iequal4:
990 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
991 break;
992 default:
993 return false;
994 }
995
996 unsigned size_swizzle =
997 brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
998
999 src_reg op[2];
1000 assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
1001 for (unsigned i = 0; i < 2; i++) {
1002 op[i] = get_nir_src(cmp_instr->src[i].src,
1003 nir_op_infos[cmp_instr->op].input_types[i], 4);
1004 unsigned base_swizzle =
1005 brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
1006 op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
1007 op[i].abs = cmp_instr->src[i].abs;
1008 op[i].negate = cmp_instr->src[i].negate;
1009 }
1010
1011 emit(CMP(dst_null_d(), op[0], op[1],
1012 brw_conditional_for_nir_comparison(cmp_instr->op)));
1013
1014 return true;
1015 }
1016
1017 static void
1018 emit_find_msb_using_lzd(const vec4_builder &bld,
1019 const dst_reg &dst,
1020 const src_reg &src,
1021 bool is_signed)
1022 {
1023 vec4_instruction *inst;
1024 src_reg temp = src;
1025
1026 if (is_signed) {
1027 /* LZD of an absolute value source almost always does the right
1028 * thing. There are two problem values:
1029 *
1030 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
1031 * 0. However, findMSB(int(0x80000000)) == 30.
1032 *
1033 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
1034 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1035 *
1036 * For a value of zero or negative one, -1 will be returned.
1037 *
1038 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
1039 * findMSB(-(1<<x)) should return x-1.
1040 *
1041 * For all negative number cases, including 0x80000000 and
1042 * 0xffffffff, the correct value is obtained from LZD if instead of
1043 * negating the (already negative) value the logical-not is used. A
1044 * conditonal logical-not can be achieved in two instructions.
1045 */
1046 temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
1047
1048 bld.ASR(dst_reg(temp), src, brw_imm_d(31));
1049 bld.XOR(dst_reg(temp), temp, src);
1050 }
1051
1052 bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
1053 retype(temp, BRW_REGISTER_TYPE_UD));
1054
1055 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
1056 * from the LSB side. Subtract the result from 31 to convert the MSB count
1057 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
1058 * which is exactly what findMSB() is supposed to return.
1059 */
1060 inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
1061 brw_imm_d(31));
1062 inst->src[0].negate = true;
1063 }
1064
1065 void
1066 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
1067 {
1068 vec4_instruction *inst;
1069
1070 nir_alu_type dst_type = (nir_alu_type) (nir_op_infos[instr->op].output_type |
1071 nir_dest_bit_size(instr->dest.dest));
1072 dst_reg dst = get_nir_dest(instr->dest.dest, dst_type);
1073 dst.writemask = instr->dest.write_mask;
1074
1075 src_reg op[4];
1076 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1077 nir_alu_type src_type = (nir_alu_type)
1078 (nir_op_infos[instr->op].input_types[i] |
1079 nir_src_bit_size(instr->src[i].src));
1080 op[i] = get_nir_src(instr->src[i].src, src_type, 4);
1081 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1082 op[i].abs = instr->src[i].abs;
1083 op[i].negate = instr->src[i].negate;
1084 }
1085
1086 switch (instr->op) {
1087 case nir_op_imov:
1088 case nir_op_fmov:
1089 inst = emit(MOV(dst, op[0]));
1090 inst->saturate = instr->dest.saturate;
1091 break;
1092
1093 case nir_op_vec2:
1094 case nir_op_vec3:
1095 case nir_op_vec4:
1096 unreachable("not reached: should be handled by lower_vec_to_movs()");
1097
1098 case nir_op_i2f:
1099 case nir_op_u2f:
1100 inst = emit(MOV(dst, op[0]));
1101 inst->saturate = instr->dest.saturate;
1102 break;
1103
1104 case nir_op_f2i:
1105 case nir_op_f2u:
1106 inst = emit(MOV(dst, op[0]));
1107 break;
1108
1109 case nir_op_d2f: {
1110 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1111 emit(MOV(temp, op[0]));
1112
1113 dst_reg temp2 = dst_reg(this, glsl_type::dvec4_type);
1114 temp2 = retype(temp2, BRW_REGISTER_TYPE_F);
1115 emit(VEC4_OPCODE_DOUBLE_TO_FLOAT, temp2, src_reg(temp))
1116 ->size_written = 2 * REG_SIZE;
1117
1118 vec4_instruction *inst = emit(MOV(dst, src_reg(temp2)));
1119 inst->saturate = instr->dest.saturate;
1120 break;
1121 }
1122
1123 case nir_op_f2d: {
1124 dst_reg tmp_dst = dst_reg(src_reg(this, glsl_type::dvec4_type));
1125 src_reg tmp_src = src_reg(this, glsl_type::vec4_type);
1126 emit(MOV(dst_reg(tmp_src), retype(op[0], BRW_REGISTER_TYPE_F)));
1127 emit(VEC4_OPCODE_FLOAT_TO_DOUBLE, tmp_dst, tmp_src);
1128 vec4_instruction *inst = emit(MOV(dst, src_reg(tmp_dst)));
1129 inst->saturate = instr->dest.saturate;
1130 break;
1131 }
1132
1133 case nir_op_iadd:
1134 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1135 case nir_op_fadd:
1136 inst = emit(ADD(dst, op[0], op[1]));
1137 inst->saturate = instr->dest.saturate;
1138 break;
1139
1140 case nir_op_fmul:
1141 inst = emit(MUL(dst, op[0], op[1]));
1142 inst->saturate = instr->dest.saturate;
1143 break;
1144
1145 case nir_op_imul: {
1146 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1147 if (devinfo->gen < 8) {
1148 nir_const_value *value0 = nir_src_as_const_value(instr->src[0].src);
1149 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
1150
1151 /* For integer multiplication, the MUL uses the low 16 bits of one of
1152 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1153 * accumulates in the contribution of the upper 16 bits of that
1154 * operand. If we can determine that one of the args is in the low
1155 * 16 bits, though, we can just emit a single MUL.
1156 */
1157 if (value0 && value0->u32[0] < (1 << 16)) {
1158 if (devinfo->gen < 7)
1159 emit(MUL(dst, op[0], op[1]));
1160 else
1161 emit(MUL(dst, op[1], op[0]));
1162 } else if (value1 && value1->u32[0] < (1 << 16)) {
1163 if (devinfo->gen < 7)
1164 emit(MUL(dst, op[1], op[0]));
1165 else
1166 emit(MUL(dst, op[0], op[1]));
1167 } else {
1168 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1169
1170 emit(MUL(acc, op[0], op[1]));
1171 emit(MACH(dst_null_d(), op[0], op[1]));
1172 emit(MOV(dst, src_reg(acc)));
1173 }
1174 } else {
1175 emit(MUL(dst, op[0], op[1]));
1176 }
1177 break;
1178 }
1179
1180 case nir_op_imul_high:
1181 case nir_op_umul_high: {
1182 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1183 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1184
1185 if (devinfo->gen >= 8)
1186 emit(MUL(acc, op[0], retype(op[1], BRW_REGISTER_TYPE_UW)));
1187 else
1188 emit(MUL(acc, op[0], op[1]));
1189
1190 emit(MACH(dst, op[0], op[1]));
1191 break;
1192 }
1193
1194 case nir_op_frcp:
1195 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1196 inst->saturate = instr->dest.saturate;
1197 break;
1198
1199 case nir_op_fexp2:
1200 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1201 inst->saturate = instr->dest.saturate;
1202 break;
1203
1204 case nir_op_flog2:
1205 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1206 inst->saturate = instr->dest.saturate;
1207 break;
1208
1209 case nir_op_fsin:
1210 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1211 inst->saturate = instr->dest.saturate;
1212 break;
1213
1214 case nir_op_fcos:
1215 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1216 inst->saturate = instr->dest.saturate;
1217 break;
1218
1219 case nir_op_idiv:
1220 case nir_op_udiv:
1221 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1222 emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
1223 break;
1224
1225 case nir_op_umod:
1226 case nir_op_irem:
1227 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1228 * appears that our hardware just does the right thing for signed
1229 * remainder.
1230 */
1231 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1232 emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1233 break;
1234
1235 case nir_op_imod: {
1236 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1237 inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1238
1239 /* Math instructions don't support conditional mod */
1240 inst = emit(MOV(dst_null_d(), src_reg(dst)));
1241 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1242
1243 /* Now, we need to determine if signs of the sources are different.
1244 * When we XOR the sources, the top bit is 0 if they are the same and 1
1245 * if they are different. We can then use a conditional modifier to
1246 * turn that into a predicate. This leads us to an XOR.l instruction.
1247 *
1248 * Technically, according to the PRM, you're not allowed to use .l on a
1249 * XOR instruction. However, emperical experiments and Curro's reading
1250 * of the simulator source both indicate that it's safe.
1251 */
1252 src_reg tmp = src_reg(this, glsl_type::ivec4_type);
1253 inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
1254 inst->predicate = BRW_PREDICATE_NORMAL;
1255 inst->conditional_mod = BRW_CONDITIONAL_L;
1256
1257 /* If the result of the initial remainder operation is non-zero and the
1258 * two sources have different signs, add in a copy of op[1] to get the
1259 * final integer modulus value.
1260 */
1261 inst = emit(ADD(dst, src_reg(dst), op[1]));
1262 inst->predicate = BRW_PREDICATE_NORMAL;
1263 break;
1264 }
1265
1266 case nir_op_ldexp:
1267 unreachable("not reached: should be handled by ldexp_to_arith()");
1268
1269 case nir_op_fsqrt:
1270 inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
1271 inst->saturate = instr->dest.saturate;
1272 break;
1273
1274 case nir_op_frsq:
1275 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1276 inst->saturate = instr->dest.saturate;
1277 break;
1278
1279 case nir_op_fpow:
1280 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1281 inst->saturate = instr->dest.saturate;
1282 break;
1283
1284 case nir_op_uadd_carry: {
1285 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1286 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1287
1288 emit(ADDC(dst_null_ud(), op[0], op[1]));
1289 emit(MOV(dst, src_reg(acc)));
1290 break;
1291 }
1292
1293 case nir_op_usub_borrow: {
1294 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1295 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1296
1297 emit(SUBB(dst_null_ud(), op[0], op[1]));
1298 emit(MOV(dst, src_reg(acc)));
1299 break;
1300 }
1301
1302 case nir_op_ftrunc:
1303 inst = emit(RNDZ(dst, op[0]));
1304 inst->saturate = instr->dest.saturate;
1305 break;
1306
1307 case nir_op_fceil: {
1308 src_reg tmp = src_reg(this, glsl_type::float_type);
1309 tmp.swizzle =
1310 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1311 instr->src[0].src.ssa->num_components :
1312 instr->src[0].src.reg.reg->num_components);
1313
1314 op[0].negate = !op[0].negate;
1315 emit(RNDD(dst_reg(tmp), op[0]));
1316 tmp.negate = true;
1317 inst = emit(MOV(dst, tmp));
1318 inst->saturate = instr->dest.saturate;
1319 break;
1320 }
1321
1322 case nir_op_ffloor:
1323 inst = emit(RNDD(dst, op[0]));
1324 inst->saturate = instr->dest.saturate;
1325 break;
1326
1327 case nir_op_ffract:
1328 inst = emit(FRC(dst, op[0]));
1329 inst->saturate = instr->dest.saturate;
1330 break;
1331
1332 case nir_op_fround_even:
1333 inst = emit(RNDE(dst, op[0]));
1334 inst->saturate = instr->dest.saturate;
1335 break;
1336
1337 case nir_op_fquantize2f16: {
1338 /* See also vec4_visitor::emit_pack_half_2x16() */
1339 src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1340 src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1341 src_reg zero = src_reg(this, glsl_type::vec4_type);
1342
1343 /* Check for denormal */
1344 src_reg abs_src0 = op[0];
1345 abs_src0.abs = true;
1346 emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1347 BRW_CONDITIONAL_L));
1348 /* Get the appropriately signed zero */
1349 emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1350 retype(op[0], BRW_REGISTER_TYPE_UD),
1351 brw_imm_ud(0x80000000)));
1352 /* Do the actual F32 -> F16 -> F32 conversion */
1353 emit(F32TO16(dst_reg(tmp16), op[0]));
1354 emit(F16TO32(dst_reg(tmp32), tmp16));
1355 /* Select that or zero based on normal status */
1356 inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1357 inst->predicate = BRW_PREDICATE_NORMAL;
1358 inst->saturate = instr->dest.saturate;
1359 break;
1360 }
1361
1362 case nir_op_imin:
1363 case nir_op_umin:
1364 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1365 case nir_op_fmin:
1366 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1367 inst->saturate = instr->dest.saturate;
1368 break;
1369
1370 case nir_op_imax:
1371 case nir_op_umax:
1372 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1373 case nir_op_fmax:
1374 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1375 inst->saturate = instr->dest.saturate;
1376 break;
1377
1378 case nir_op_fddx:
1379 case nir_op_fddx_coarse:
1380 case nir_op_fddx_fine:
1381 case nir_op_fddy:
1382 case nir_op_fddy_coarse:
1383 case nir_op_fddy_fine:
1384 unreachable("derivatives are not valid in vertex shaders");
1385
1386 case nir_op_ilt:
1387 case nir_op_ult:
1388 case nir_op_ige:
1389 case nir_op_uge:
1390 case nir_op_ieq:
1391 case nir_op_ine:
1392 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1393 /* Fallthrough */
1394 case nir_op_flt:
1395 case nir_op_fge:
1396 case nir_op_feq:
1397 case nir_op_fne: {
1398 enum brw_conditional_mod conditional_mod =
1399 brw_conditional_for_nir_comparison(instr->op);
1400
1401 if (nir_src_bit_size(instr->src[0].src) < 64) {
1402 emit(CMP(dst, op[0], op[1], conditional_mod));
1403 } else {
1404 /* Produce a 32-bit boolean result from the DF comparison by selecting
1405 * only the low 32-bit in each DF produced. Do this in a temporary
1406 * so we can then move from there to the result using align16 again
1407 * to honor the original writemask.
1408 */
1409 dst_reg temp = dst_reg(this, glsl_type::dvec4_type);
1410 emit(CMP(temp, op[0], op[1], conditional_mod));
1411 dst_reg result = dst_reg(this, glsl_type::bvec4_type);
1412 emit(VEC4_OPCODE_PICK_LOW_32BIT, result, src_reg(temp));
1413 emit(MOV(dst, src_reg(result)));
1414 }
1415 break;
1416 }
1417
1418 case nir_op_ball_iequal2:
1419 case nir_op_ball_iequal3:
1420 case nir_op_ball_iequal4:
1421 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1422 /* Fallthrough */
1423 case nir_op_ball_fequal2:
1424 case nir_op_ball_fequal3:
1425 case nir_op_ball_fequal4: {
1426 unsigned swiz =
1427 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1428
1429 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1430 brw_conditional_for_nir_comparison(instr->op)));
1431 emit(MOV(dst, brw_imm_d(0)));
1432 inst = emit(MOV(dst, brw_imm_d(~0)));
1433 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1434 break;
1435 }
1436
1437 case nir_op_bany_inequal2:
1438 case nir_op_bany_inequal3:
1439 case nir_op_bany_inequal4:
1440 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1441 /* Fallthrough */
1442 case nir_op_bany_fnequal2:
1443 case nir_op_bany_fnequal3:
1444 case nir_op_bany_fnequal4: {
1445 unsigned swiz =
1446 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1447
1448 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1449 brw_conditional_for_nir_comparison(instr->op)));
1450
1451 emit(MOV(dst, brw_imm_d(0)));
1452 inst = emit(MOV(dst, brw_imm_d(~0)));
1453 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1454 break;
1455 }
1456
1457 case nir_op_inot:
1458 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1459 if (devinfo->gen >= 8) {
1460 op[0] = resolve_source_modifiers(op[0]);
1461 }
1462 emit(NOT(dst, op[0]));
1463 break;
1464
1465 case nir_op_ixor:
1466 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1467 if (devinfo->gen >= 8) {
1468 op[0] = resolve_source_modifiers(op[0]);
1469 op[1] = resolve_source_modifiers(op[1]);
1470 }
1471 emit(XOR(dst, op[0], op[1]));
1472 break;
1473
1474 case nir_op_ior:
1475 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1476 if (devinfo->gen >= 8) {
1477 op[0] = resolve_source_modifiers(op[0]);
1478 op[1] = resolve_source_modifiers(op[1]);
1479 }
1480 emit(OR(dst, op[0], op[1]));
1481 break;
1482
1483 case nir_op_iand:
1484 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1485 if (devinfo->gen >= 8) {
1486 op[0] = resolve_source_modifiers(op[0]);
1487 op[1] = resolve_source_modifiers(op[1]);
1488 }
1489 emit(AND(dst, op[0], op[1]));
1490 break;
1491
1492 case nir_op_b2i:
1493 case nir_op_b2f:
1494 emit(MOV(dst, negate(op[0])));
1495 break;
1496
1497 case nir_op_f2b:
1498 emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1499 break;
1500
1501 case nir_op_i2b:
1502 emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1503 break;
1504
1505 case nir_op_fnoise1_1:
1506 case nir_op_fnoise1_2:
1507 case nir_op_fnoise1_3:
1508 case nir_op_fnoise1_4:
1509 case nir_op_fnoise2_1:
1510 case nir_op_fnoise2_2:
1511 case nir_op_fnoise2_3:
1512 case nir_op_fnoise2_4:
1513 case nir_op_fnoise3_1:
1514 case nir_op_fnoise3_2:
1515 case nir_op_fnoise3_3:
1516 case nir_op_fnoise3_4:
1517 case nir_op_fnoise4_1:
1518 case nir_op_fnoise4_2:
1519 case nir_op_fnoise4_3:
1520 case nir_op_fnoise4_4:
1521 unreachable("not reached: should be handled by lower_noise");
1522
1523 case nir_op_unpack_half_2x16_split_x:
1524 case nir_op_unpack_half_2x16_split_y:
1525 case nir_op_pack_half_2x16_split:
1526 unreachable("not reached: should not occur in vertex shader");
1527
1528 case nir_op_unpack_snorm_2x16:
1529 case nir_op_unpack_unorm_2x16:
1530 case nir_op_pack_snorm_2x16:
1531 case nir_op_pack_unorm_2x16:
1532 unreachable("not reached: should be handled by lower_packing_builtins");
1533
1534 case nir_op_pack_uvec4_to_uint:
1535 unreachable("not reached");
1536
1537 case nir_op_pack_uvec2_to_uint: {
1538 dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1539 tmp1.writemask = WRITEMASK_X;
1540 op[0].swizzle = BRW_SWIZZLE_YYYY;
1541 emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1542
1543 dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1544 tmp2.writemask = WRITEMASK_X;
1545 op[0].swizzle = BRW_SWIZZLE_XXXX;
1546 emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1547
1548 emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1549 break;
1550 }
1551
1552 case nir_op_pack_double_2x32_split: {
1553 dst_reg result = dst_reg(this, glsl_type::dvec4_type);
1554 dst_reg tmp = dst_reg(this, glsl_type::uvec4_type);
1555 emit(MOV(tmp, retype(op[0], BRW_REGISTER_TYPE_UD)));
1556 emit(VEC4_OPCODE_SET_LOW_32BIT, result, src_reg(tmp));
1557 emit(MOV(tmp, retype(op[1], BRW_REGISTER_TYPE_UD)));
1558 emit(VEC4_OPCODE_SET_HIGH_32BIT, result, src_reg(tmp));
1559 emit(MOV(dst, src_reg(result)));
1560 break;
1561 }
1562
1563 case nir_op_unpack_double_2x32_split_x:
1564 case nir_op_unpack_double_2x32_split_y: {
1565 enum opcode oper = (instr->op == nir_op_unpack_double_2x32_split_x) ?
1566 VEC4_OPCODE_PICK_LOW_32BIT : VEC4_OPCODE_PICK_HIGH_32BIT;
1567 dst_reg tmp = dst_reg(this, glsl_type::dvec4_type);
1568 emit(MOV(tmp, op[0]));
1569 dst_reg tmp2 = dst_reg(this, glsl_type::uvec4_type);
1570 emit(oper, tmp2, src_reg(tmp));
1571 emit(MOV(dst, src_reg(tmp2)));
1572 break;
1573 }
1574
1575 case nir_op_unpack_half_2x16:
1576 /* As NIR does not guarantee that we have a correct swizzle outside the
1577 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1578 * uses the source operand in an operation with WRITEMASK_Y while our
1579 * source operand has only size 1, it accessed incorrect data producing
1580 * regressions in Piglit. We repeat the swizzle of the first component on the
1581 * rest of components to avoid regressions. In the vec4_visitor IR code path
1582 * this is not needed because the operand has already the correct swizzle.
1583 */
1584 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1585 emit_unpack_half_2x16(dst, op[0]);
1586 break;
1587
1588 case nir_op_pack_half_2x16:
1589 emit_pack_half_2x16(dst, op[0]);
1590 break;
1591
1592 case nir_op_unpack_unorm_4x8:
1593 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1594 emit_unpack_unorm_4x8(dst, op[0]);
1595 break;
1596
1597 case nir_op_pack_unorm_4x8:
1598 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1599 emit_pack_unorm_4x8(dst, op[0]);
1600 break;
1601
1602 case nir_op_unpack_snorm_4x8:
1603 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1604 emit_unpack_snorm_4x8(dst, op[0]);
1605 break;
1606
1607 case nir_op_pack_snorm_4x8:
1608 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1609 emit_pack_snorm_4x8(dst, op[0]);
1610 break;
1611
1612 case nir_op_bitfield_reverse:
1613 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1614 emit(BFREV(dst, op[0]));
1615 break;
1616
1617 case nir_op_bit_count:
1618 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1619 emit(CBIT(dst, op[0]));
1620 break;
1621
1622 case nir_op_ufind_msb:
1623 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1624 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
1625 break;
1626
1627 case nir_op_ifind_msb: {
1628 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1629 vec4_builder bld = vec4_builder(this).at_end();
1630 src_reg src(dst);
1631
1632 if (devinfo->gen < 7) {
1633 emit_find_msb_using_lzd(bld, dst, op[0], true);
1634 } else {
1635 emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1636
1637 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1638 * count from the LSB side. If FBH didn't return an error
1639 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1640 * count into an LSB count.
1641 */
1642 bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1643
1644 inst = bld.ADD(dst, src, brw_imm_d(31));
1645 inst->predicate = BRW_PREDICATE_NORMAL;
1646 inst->src[0].negate = true;
1647 }
1648 break;
1649 }
1650
1651 case nir_op_find_lsb: {
1652 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1653 vec4_builder bld = vec4_builder(this).at_end();
1654
1655 if (devinfo->gen < 7) {
1656 dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
1657
1658 /* (x & -x) generates a value that consists of only the LSB of x.
1659 * For all powers of 2, findMSB(y) == findLSB(y).
1660 */
1661 src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
1662 src_reg negated_src = src;
1663
1664 /* One must be negated, and the other must be non-negated. It
1665 * doesn't matter which is which.
1666 */
1667 negated_src.negate = true;
1668 src.negate = false;
1669
1670 bld.AND(temp, src, negated_src);
1671 emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
1672 } else {
1673 bld.FBL(dst, op[0]);
1674 }
1675 break;
1676 }
1677
1678 case nir_op_ubitfield_extract:
1679 case nir_op_ibitfield_extract:
1680 unreachable("should have been lowered");
1681 case nir_op_ubfe:
1682 case nir_op_ibfe:
1683 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1684 op[0] = fix_3src_operand(op[0]);
1685 op[1] = fix_3src_operand(op[1]);
1686 op[2] = fix_3src_operand(op[2]);
1687
1688 emit(BFE(dst, op[2], op[1], op[0]));
1689 break;
1690
1691 case nir_op_bfm:
1692 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1693 emit(BFI1(dst, op[0], op[1]));
1694 break;
1695
1696 case nir_op_bfi:
1697 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1698 op[0] = fix_3src_operand(op[0]);
1699 op[1] = fix_3src_operand(op[1]);
1700 op[2] = fix_3src_operand(op[2]);
1701
1702 emit(BFI2(dst, op[0], op[1], op[2]));
1703 break;
1704
1705 case nir_op_bitfield_insert:
1706 unreachable("not reached: should have been lowered");
1707
1708 case nir_op_fsign:
1709 /* AND(val, 0x80000000) gives the sign bit.
1710 *
1711 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1712 * zero.
1713 */
1714 emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1715
1716 op[0].type = BRW_REGISTER_TYPE_UD;
1717 dst.type = BRW_REGISTER_TYPE_UD;
1718 emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1719
1720 inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1721 inst->predicate = BRW_PREDICATE_NORMAL;
1722 dst.type = BRW_REGISTER_TYPE_F;
1723
1724 if (instr->dest.saturate) {
1725 inst = emit(MOV(dst, src_reg(dst)));
1726 inst->saturate = true;
1727 }
1728 break;
1729
1730 case nir_op_isign:
1731 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1732 * -> non-negative val generates 0x00000000.
1733 * Predicated OR sets 1 if val is positive.
1734 */
1735 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1736 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G));
1737 emit(ASR(dst, op[0], brw_imm_d(31)));
1738 inst = emit(OR(dst, src_reg(dst), brw_imm_d(1)));
1739 inst->predicate = BRW_PREDICATE_NORMAL;
1740 break;
1741
1742 case nir_op_ishl:
1743 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1744 emit(SHL(dst, op[0], op[1]));
1745 break;
1746
1747 case nir_op_ishr:
1748 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1749 emit(ASR(dst, op[0], op[1]));
1750 break;
1751
1752 case nir_op_ushr:
1753 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1754 emit(SHR(dst, op[0], op[1]));
1755 break;
1756
1757 case nir_op_ffma:
1758 op[0] = fix_3src_operand(op[0]);
1759 op[1] = fix_3src_operand(op[1]);
1760 op[2] = fix_3src_operand(op[2]);
1761
1762 inst = emit(MAD(dst, op[2], op[1], op[0]));
1763 inst->saturate = instr->dest.saturate;
1764 break;
1765
1766 case nir_op_flrp:
1767 inst = emit_lrp(dst, op[0], op[1], op[2]);
1768 inst->saturate = instr->dest.saturate;
1769 break;
1770
1771 case nir_op_bcsel:
1772 enum brw_predicate predicate;
1773 if (!optimize_predicate(instr, &predicate)) {
1774 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1775 switch (dst.writemask) {
1776 case WRITEMASK_X:
1777 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1778 break;
1779 case WRITEMASK_Y:
1780 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1781 break;
1782 case WRITEMASK_Z:
1783 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1784 break;
1785 case WRITEMASK_W:
1786 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1787 break;
1788 default:
1789 predicate = BRW_PREDICATE_NORMAL;
1790 break;
1791 }
1792 }
1793 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1794 inst->predicate = predicate;
1795 break;
1796
1797 case nir_op_fdot_replicated2:
1798 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1799 inst->saturate = instr->dest.saturate;
1800 break;
1801
1802 case nir_op_fdot_replicated3:
1803 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1804 inst->saturate = instr->dest.saturate;
1805 break;
1806
1807 case nir_op_fdot_replicated4:
1808 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1809 inst->saturate = instr->dest.saturate;
1810 break;
1811
1812 case nir_op_fdph_replicated:
1813 inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1814 inst->saturate = instr->dest.saturate;
1815 break;
1816
1817 case nir_op_iabs:
1818 case nir_op_ineg:
1819 assert(nir_dest_bit_size(instr->dest.dest) < 64);
1820 case nir_op_fabs:
1821 case nir_op_fneg:
1822 case nir_op_fsat:
1823 unreachable("not reached: should be lowered by lower_source mods");
1824
1825 case nir_op_fdiv:
1826 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1827
1828 case nir_op_fmod:
1829 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1830
1831 case nir_op_fsub:
1832 case nir_op_isub:
1833 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1834
1835 default:
1836 unreachable("Unimplemented ALU operation");
1837 }
1838
1839 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1840 * to sign extend the low bit to 0/~0
1841 */
1842 if (devinfo->gen <= 5 &&
1843 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1844 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1845 dst_reg masked = dst_reg(this, glsl_type::int_type);
1846 masked.writemask = dst.writemask;
1847 emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1848 src_reg masked_neg = src_reg(masked);
1849 masked_neg.negate = true;
1850 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1851 }
1852 }
1853
1854 void
1855 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1856 {
1857 switch (instr->type) {
1858 case nir_jump_break:
1859 emit(BRW_OPCODE_BREAK);
1860 break;
1861
1862 case nir_jump_continue:
1863 emit(BRW_OPCODE_CONTINUE);
1864 break;
1865
1866 case nir_jump_return:
1867 /* fall through */
1868 default:
1869 unreachable("unknown jump");
1870 }
1871 }
1872
1873 enum ir_texture_opcode
1874 ir_texture_opcode_for_nir_texop(nir_texop texop)
1875 {
1876 enum ir_texture_opcode op;
1877
1878 switch (texop) {
1879 case nir_texop_lod: op = ir_lod; break;
1880 case nir_texop_query_levels: op = ir_query_levels; break;
1881 case nir_texop_texture_samples: op = ir_texture_samples; break;
1882 case nir_texop_tex: op = ir_tex; break;
1883 case nir_texop_tg4: op = ir_tg4; break;
1884 case nir_texop_txb: op = ir_txb; break;
1885 case nir_texop_txd: op = ir_txd; break;
1886 case nir_texop_txf: op = ir_txf; break;
1887 case nir_texop_txf_ms: op = ir_txf_ms; break;
1888 case nir_texop_txl: op = ir_txl; break;
1889 case nir_texop_txs: op = ir_txs; break;
1890 case nir_texop_samples_identical: op = ir_samples_identical; break;
1891 default:
1892 unreachable("unknown texture opcode");
1893 }
1894
1895 return op;
1896 }
1897 const glsl_type *
1898 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
1899 unsigned components)
1900 {
1901 return glsl_type::get_instance(brw_glsl_base_type_for_nir_type(alu_type),
1902 components, 1);
1903 }
1904
1905 void
1906 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
1907 {
1908 unsigned texture = instr->texture_index;
1909 unsigned sampler = instr->sampler_index;
1910 src_reg texture_reg = brw_imm_ud(texture);
1911 src_reg sampler_reg = brw_imm_ud(sampler);
1912 src_reg coordinate;
1913 const glsl_type *coord_type = NULL;
1914 src_reg shadow_comparator;
1915 src_reg offset_value;
1916 src_reg lod, lod2;
1917 src_reg sample_index;
1918 src_reg mcs;
1919
1920 const glsl_type *dest_type =
1921 glsl_type_for_nir_alu_type(instr->dest_type,
1922 nir_tex_instr_dest_size(instr));
1923 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
1924
1925 /* The hardware requires a LOD for buffer textures */
1926 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
1927 lod = brw_imm_d(0);
1928
1929 /* Load the texture operation sources */
1930 uint32_t constant_offset = 0;
1931 for (unsigned i = 0; i < instr->num_srcs; i++) {
1932 switch (instr->src[i].src_type) {
1933 case nir_tex_src_comparator:
1934 shadow_comparator = get_nir_src(instr->src[i].src,
1935 BRW_REGISTER_TYPE_F, 1);
1936 break;
1937
1938 case nir_tex_src_coord: {
1939 unsigned src_size = nir_tex_instr_src_size(instr, i);
1940
1941 switch (instr->op) {
1942 case nir_texop_txf:
1943 case nir_texop_txf_ms:
1944 case nir_texop_samples_identical:
1945 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
1946 src_size);
1947 coord_type = glsl_type::ivec(src_size);
1948 break;
1949
1950 default:
1951 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1952 src_size);
1953 coord_type = glsl_type::vec(src_size);
1954 break;
1955 }
1956 break;
1957 }
1958
1959 case nir_tex_src_ddx:
1960 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1961 nir_tex_instr_src_size(instr, i));
1962 break;
1963
1964 case nir_tex_src_ddy:
1965 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1966 nir_tex_instr_src_size(instr, i));
1967 break;
1968
1969 case nir_tex_src_lod:
1970 switch (instr->op) {
1971 case nir_texop_txs:
1972 case nir_texop_txf:
1973 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1974 break;
1975
1976 default:
1977 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
1978 break;
1979 }
1980 break;
1981
1982 case nir_tex_src_ms_index: {
1983 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1984 break;
1985 }
1986
1987 case nir_tex_src_offset: {
1988 nir_const_value *const_offset =
1989 nir_src_as_const_value(instr->src[i].src);
1990 if (!const_offset ||
1991 !brw_texture_offset(const_offset->i32,
1992 nir_tex_instr_src_size(instr, i),
1993 &constant_offset)) {
1994 offset_value =
1995 get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
1996 }
1997 break;
1998 }
1999
2000 case nir_tex_src_texture_offset: {
2001 /* The highest texture which may be used by this operation is
2002 * the last element of the array. Mark it here, because the generator
2003 * doesn't have enough information to determine the bound.
2004 */
2005 uint32_t array_size = instr->texture_array_size;
2006 uint32_t max_used = texture + array_size - 1;
2007 if (instr->op == nir_texop_tg4) {
2008 max_used += prog_data->base.binding_table.gather_texture_start;
2009 } else {
2010 max_used += prog_data->base.binding_table.texture_start;
2011 }
2012
2013 brw_mark_surface_used(&prog_data->base, max_used);
2014
2015 /* Emit code to evaluate the actual indexing expression */
2016 src_reg src = get_nir_src(instr->src[i].src, 1);
2017 src_reg temp(this, glsl_type::uint_type);
2018 emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
2019 texture_reg = emit_uniformize(temp);
2020 break;
2021 }
2022
2023 case nir_tex_src_sampler_offset: {
2024 /* Emit code to evaluate the actual indexing expression */
2025 src_reg src = get_nir_src(instr->src[i].src, 1);
2026 src_reg temp(this, glsl_type::uint_type);
2027 emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
2028 sampler_reg = emit_uniformize(temp);
2029 break;
2030 }
2031
2032 case nir_tex_src_projector:
2033 unreachable("Should be lowered by do_lower_texture_projection");
2034
2035 case nir_tex_src_bias:
2036 unreachable("LOD bias is not valid for vertex shaders.\n");
2037
2038 default:
2039 unreachable("unknown texture source");
2040 }
2041 }
2042
2043 if (instr->op == nir_texop_txf_ms ||
2044 instr->op == nir_texop_samples_identical) {
2045 assert(coord_type != NULL);
2046 if (devinfo->gen >= 7 &&
2047 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
2048 mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
2049 } else {
2050 mcs = brw_imm_ud(0u);
2051 }
2052 }
2053
2054 /* Stuff the channel select bits in the top of the texture offset */
2055 if (instr->op == nir_texop_tg4) {
2056 if (instr->component == 1 &&
2057 (key_tex->gather_channel_quirk_mask & (1 << texture))) {
2058 /* gather4 sampler is broken for green channel on RG32F --
2059 * we must ask for blue instead.
2060 */
2061 constant_offset |= 2 << 16;
2062 } else {
2063 constant_offset |= instr->component << 16;
2064 }
2065 }
2066
2067 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
2068
2069 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
2070 shadow_comparator,
2071 lod, lod2, sample_index,
2072 constant_offset, offset_value, mcs,
2073 texture, texture_reg, sampler_reg);
2074 }
2075
2076 void
2077 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
2078 {
2079 nir_ssa_values[instr->def.index] = dst_reg(VGRF, alloc.allocate(1));
2080 }
2081
2082 }