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