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