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