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