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