nir/i965/anv/radv/gallium: make shader info a pointer
[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 const vec4_builder bld =
743 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
744
745 /* Get some metadata from the image intrinsic. */
746 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
747
748 /* Get the arguments of the atomic intrinsic. */
749 src_reg offset = get_nir_src(instr->src[0], nir_type_int,
750 instr->num_components);
751 const src_reg surface = brw_imm_ud(surf_index);
752 const src_reg src0 = (info->num_srcs >= 2
753 ? get_nir_src(instr->src[1]) : src_reg());
754 const src_reg src1 = (info->num_srcs >= 3
755 ? get_nir_src(instr->src[2]) : src_reg());
756
757 src_reg tmp;
758
759 dest = get_nir_dest(instr->dest);
760
761 if (instr->intrinsic == nir_intrinsic_atomic_counter_read) {
762 tmp = emit_untyped_read(bld, surface, offset, 1, 1);
763 } else {
764 tmp = emit_untyped_atomic(bld, surface, offset,
765 src0, src1,
766 1, 1,
767 get_atomic_counter_op(instr->intrinsic));
768 }
769
770 bld.MOV(retype(dest, tmp.type), tmp);
771 brw_mark_surface_used(stage_prog_data, surf_index);
772 break;
773 }
774
775 case nir_intrinsic_load_ubo: {
776 nir_const_value *const_block_index = nir_src_as_const_value(instr->src[0]);
777 src_reg surf_index;
778
779 dest = get_nir_dest(instr->dest);
780
781 if (const_block_index) {
782 /* The block index is a constant, so just emit the binding table entry
783 * as an immediate.
784 */
785 const unsigned index = prog_data->base.binding_table.ubo_start +
786 const_block_index->u32[0];
787 surf_index = brw_imm_ud(index);
788 brw_mark_surface_used(&prog_data->base, index);
789 } else {
790 /* The block index is not a constant. Evaluate the index expression
791 * per-channel and add the base UBO index; we have to select a value
792 * from any live channel.
793 */
794 surf_index = src_reg(this, glsl_type::uint_type);
795 emit(ADD(dst_reg(surf_index), get_nir_src(instr->src[0], nir_type_int,
796 instr->num_components),
797 brw_imm_ud(prog_data->base.binding_table.ubo_start)));
798 surf_index = emit_uniformize(surf_index);
799
800 /* Assume this may touch any UBO. It would be nice to provide
801 * a tighter bound, but the array information is already lowered away.
802 */
803 brw_mark_surface_used(&prog_data->base,
804 prog_data->base.binding_table.ubo_start +
805 nir->info->num_ubos - 1);
806 }
807
808 src_reg offset;
809 nir_const_value *const_offset = nir_src_as_const_value(instr->src[1]);
810 if (const_offset) {
811 offset = brw_imm_ud(const_offset->u32[0] & ~15);
812 } else {
813 offset = get_nir_src(instr->src[1], nir_type_int, 1);
814 }
815
816 src_reg packed_consts = src_reg(this, glsl_type::vec4_type);
817 packed_consts.type = dest.type;
818
819 emit_pull_constant_load_reg(dst_reg(packed_consts),
820 surf_index,
821 offset,
822 NULL, NULL /* before_block/inst */);
823
824 packed_consts.swizzle = brw_swizzle_for_size(instr->num_components);
825 if (const_offset) {
826 packed_consts.swizzle += BRW_SWIZZLE4(const_offset->u32[0] % 16 / 4,
827 const_offset->u32[0] % 16 / 4,
828 const_offset->u32[0] % 16 / 4,
829 const_offset->u32[0] % 16 / 4);
830 }
831
832 emit(MOV(dest, packed_consts));
833 break;
834 }
835
836 case nir_intrinsic_memory_barrier: {
837 const vec4_builder bld =
838 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
839 const dst_reg tmp = bld.vgrf(BRW_REGISTER_TYPE_UD, 2);
840 bld.emit(SHADER_OPCODE_MEMORY_FENCE, tmp)
841 ->size_written = 2 * REG_SIZE;
842 break;
843 }
844
845 case nir_intrinsic_shader_clock: {
846 /* We cannot do anything if there is an event, so ignore it for now */
847 const src_reg shader_clock = get_timestamp();
848 const enum brw_reg_type type = brw_type_for_base_type(glsl_type::uvec2_type);
849
850 dest = get_nir_dest(instr->dest, type);
851 emit(MOV(dest, shader_clock));
852 break;
853 }
854
855 default:
856 unreachable("Unknown intrinsic");
857 }
858 }
859
860 void
861 vec4_visitor::nir_emit_ssbo_atomic(int op, nir_intrinsic_instr *instr)
862 {
863 dst_reg dest;
864 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
865 dest = get_nir_dest(instr->dest);
866
867 src_reg surface;
868 nir_const_value *const_surface = nir_src_as_const_value(instr->src[0]);
869 if (const_surface) {
870 unsigned surf_index = prog_data->base.binding_table.ssbo_start +
871 const_surface->u32[0];
872 surface = brw_imm_ud(surf_index);
873 brw_mark_surface_used(&prog_data->base, surf_index);
874 } else {
875 surface = src_reg(this, glsl_type::uint_type);
876 emit(ADD(dst_reg(surface), get_nir_src(instr->src[0]),
877 brw_imm_ud(prog_data->base.binding_table.ssbo_start)));
878
879 /* Assume this may touch any UBO. This is the same we do for other
880 * UBO/SSBO accesses with non-constant surface.
881 */
882 brw_mark_surface_used(&prog_data->base,
883 prog_data->base.binding_table.ssbo_start +
884 nir->info->num_ssbos - 1);
885 }
886
887 src_reg offset = get_nir_src(instr->src[1], 1);
888 src_reg data1 = get_nir_src(instr->src[2], 1);
889 src_reg data2;
890 if (op == BRW_AOP_CMPWR)
891 data2 = get_nir_src(instr->src[3], 1);
892
893 /* Emit the actual atomic operation operation */
894 const vec4_builder bld =
895 vec4_builder(this).at_end().annotate(current_annotation, base_ir);
896
897 src_reg atomic_result = emit_untyped_atomic(bld, surface, offset,
898 data1, data2,
899 1 /* dims */, 1 /* rsize */,
900 op,
901 BRW_PREDICATE_NONE);
902 dest.type = atomic_result.type;
903 bld.MOV(dest, atomic_result);
904 }
905
906 static unsigned
907 brw_swizzle_for_nir_swizzle(uint8_t swizzle[4])
908 {
909 return BRW_SWIZZLE4(swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
910 }
911
912 static enum brw_conditional_mod
913 brw_conditional_for_nir_comparison(nir_op op)
914 {
915 switch (op) {
916 case nir_op_flt:
917 case nir_op_ilt:
918 case nir_op_ult:
919 return BRW_CONDITIONAL_L;
920
921 case nir_op_fge:
922 case nir_op_ige:
923 case nir_op_uge:
924 return BRW_CONDITIONAL_GE;
925
926 case nir_op_feq:
927 case nir_op_ieq:
928 case nir_op_ball_fequal2:
929 case nir_op_ball_iequal2:
930 case nir_op_ball_fequal3:
931 case nir_op_ball_iequal3:
932 case nir_op_ball_fequal4:
933 case nir_op_ball_iequal4:
934 return BRW_CONDITIONAL_Z;
935
936 case nir_op_fne:
937 case nir_op_ine:
938 case nir_op_bany_fnequal2:
939 case nir_op_bany_inequal2:
940 case nir_op_bany_fnequal3:
941 case nir_op_bany_inequal3:
942 case nir_op_bany_fnequal4:
943 case nir_op_bany_inequal4:
944 return BRW_CONDITIONAL_NZ;
945
946 default:
947 unreachable("not reached: bad operation for comparison");
948 }
949 }
950
951 bool
952 vec4_visitor::optimize_predicate(nir_alu_instr *instr,
953 enum brw_predicate *predicate)
954 {
955 if (!instr->src[0].src.is_ssa ||
956 instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
957 return false;
958
959 nir_alu_instr *cmp_instr =
960 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
961
962 switch (cmp_instr->op) {
963 case nir_op_bany_fnequal2:
964 case nir_op_bany_inequal2:
965 case nir_op_bany_fnequal3:
966 case nir_op_bany_inequal3:
967 case nir_op_bany_fnequal4:
968 case nir_op_bany_inequal4:
969 *predicate = BRW_PREDICATE_ALIGN16_ANY4H;
970 break;
971 case nir_op_ball_fequal2:
972 case nir_op_ball_iequal2:
973 case nir_op_ball_fequal3:
974 case nir_op_ball_iequal3:
975 case nir_op_ball_fequal4:
976 case nir_op_ball_iequal4:
977 *predicate = BRW_PREDICATE_ALIGN16_ALL4H;
978 break;
979 default:
980 return false;
981 }
982
983 unsigned size_swizzle =
984 brw_swizzle_for_size(nir_op_infos[cmp_instr->op].input_sizes[0]);
985
986 src_reg op[2];
987 assert(nir_op_infos[cmp_instr->op].num_inputs == 2);
988 for (unsigned i = 0; i < 2; i++) {
989 op[i] = get_nir_src(cmp_instr->src[i].src,
990 nir_op_infos[cmp_instr->op].input_types[i], 4);
991 unsigned base_swizzle =
992 brw_swizzle_for_nir_swizzle(cmp_instr->src[i].swizzle);
993 op[i].swizzle = brw_compose_swizzle(size_swizzle, base_swizzle);
994 op[i].abs = cmp_instr->src[i].abs;
995 op[i].negate = cmp_instr->src[i].negate;
996 }
997
998 emit(CMP(dst_null_d(), op[0], op[1],
999 brw_conditional_for_nir_comparison(cmp_instr->op)));
1000
1001 return true;
1002 }
1003
1004 static void
1005 emit_find_msb_using_lzd(const vec4_builder &bld,
1006 const dst_reg &dst,
1007 const src_reg &src,
1008 bool is_signed)
1009 {
1010 vec4_instruction *inst;
1011 src_reg temp = src;
1012
1013 if (is_signed) {
1014 /* LZD of an absolute value source almost always does the right
1015 * thing. There are two problem values:
1016 *
1017 * * 0x80000000. Since abs(0x80000000) == 0x80000000, LZD returns
1018 * 0. However, findMSB(int(0x80000000)) == 30.
1019 *
1020 * * 0xffffffff. Since abs(0xffffffff) == 1, LZD returns
1021 * 31. Section 8.8 (Integer Functions) of the GLSL 4.50 spec says:
1022 *
1023 * For a value of zero or negative one, -1 will be returned.
1024 *
1025 * * Negative powers of two. LZD(abs(-(1<<x))) returns x, but
1026 * findMSB(-(1<<x)) should return x-1.
1027 *
1028 * For all negative number cases, including 0x80000000 and
1029 * 0xffffffff, the correct value is obtained from LZD if instead of
1030 * negating the (already negative) value the logical-not is used. A
1031 * conditonal logical-not can be achieved in two instructions.
1032 */
1033 temp = src_reg(bld.vgrf(BRW_REGISTER_TYPE_D));
1034
1035 bld.ASR(dst_reg(temp), src, brw_imm_d(31));
1036 bld.XOR(dst_reg(temp), temp, src);
1037 }
1038
1039 bld.LZD(retype(dst, BRW_REGISTER_TYPE_UD),
1040 retype(temp, BRW_REGISTER_TYPE_UD));
1041
1042 /* LZD counts from the MSB side, while GLSL's findMSB() wants the count
1043 * from the LSB side. Subtract the result from 31 to convert the MSB count
1044 * into an LSB count. If no bits are set, LZD will return 32. 31-32 = -1,
1045 * which is exactly what findMSB() is supposed to return.
1046 */
1047 inst = bld.ADD(dst, retype(src_reg(dst), BRW_REGISTER_TYPE_D),
1048 brw_imm_d(31));
1049 inst->src[0].negate = true;
1050 }
1051
1052 void
1053 vec4_visitor::nir_emit_alu(nir_alu_instr *instr)
1054 {
1055 vec4_instruction *inst;
1056
1057 dst_reg dst = get_nir_dest(instr->dest.dest,
1058 nir_op_infos[instr->op].output_type);
1059 dst.writemask = instr->dest.write_mask;
1060
1061 src_reg op[4];
1062 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
1063 op[i] = get_nir_src(instr->src[i].src,
1064 nir_op_infos[instr->op].input_types[i], 4);
1065 op[i].swizzle = brw_swizzle_for_nir_swizzle(instr->src[i].swizzle);
1066 op[i].abs = instr->src[i].abs;
1067 op[i].negate = instr->src[i].negate;
1068 }
1069
1070 switch (instr->op) {
1071 case nir_op_imov:
1072 case nir_op_fmov:
1073 inst = emit(MOV(dst, op[0]));
1074 inst->saturate = instr->dest.saturate;
1075 break;
1076
1077 case nir_op_vec2:
1078 case nir_op_vec3:
1079 case nir_op_vec4:
1080 unreachable("not reached: should be handled by lower_vec_to_movs()");
1081
1082 case nir_op_i2f:
1083 case nir_op_u2f:
1084 inst = emit(MOV(dst, op[0]));
1085 inst->saturate = instr->dest.saturate;
1086 break;
1087
1088 case nir_op_f2i:
1089 case nir_op_f2u:
1090 inst = emit(MOV(dst, op[0]));
1091 break;
1092
1093 case nir_op_fadd:
1094 /* fall through */
1095 case nir_op_iadd:
1096 inst = emit(ADD(dst, op[0], op[1]));
1097 inst->saturate = instr->dest.saturate;
1098 break;
1099
1100 case nir_op_fmul:
1101 inst = emit(MUL(dst, op[0], op[1]));
1102 inst->saturate = instr->dest.saturate;
1103 break;
1104
1105 case nir_op_imul: {
1106 if (devinfo->gen < 8) {
1107 nir_const_value *value0 = nir_src_as_const_value(instr->src[0].src);
1108 nir_const_value *value1 = nir_src_as_const_value(instr->src[1].src);
1109
1110 /* For integer multiplication, the MUL uses the low 16 bits of one of
1111 * the operands (src0 through SNB, src1 on IVB and later). The MACH
1112 * accumulates in the contribution of the upper 16 bits of that
1113 * operand. If we can determine that one of the args is in the low
1114 * 16 bits, though, we can just emit a single MUL.
1115 */
1116 if (value0 && value0->u32[0] < (1 << 16)) {
1117 if (devinfo->gen < 7)
1118 emit(MUL(dst, op[0], op[1]));
1119 else
1120 emit(MUL(dst, op[1], op[0]));
1121 } else if (value1 && value1->u32[0] < (1 << 16)) {
1122 if (devinfo->gen < 7)
1123 emit(MUL(dst, op[1], op[0]));
1124 else
1125 emit(MUL(dst, op[0], op[1]));
1126 } else {
1127 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1128
1129 emit(MUL(acc, op[0], op[1]));
1130 emit(MACH(dst_null_d(), op[0], op[1]));
1131 emit(MOV(dst, src_reg(acc)));
1132 }
1133 } else {
1134 emit(MUL(dst, op[0], op[1]));
1135 }
1136 break;
1137 }
1138
1139 case nir_op_imul_high:
1140 case nir_op_umul_high: {
1141 struct brw_reg acc = retype(brw_acc_reg(8), dst.type);
1142
1143 if (devinfo->gen >= 8)
1144 emit(MUL(acc, op[0], retype(op[1], BRW_REGISTER_TYPE_UW)));
1145 else
1146 emit(MUL(acc, op[0], op[1]));
1147
1148 emit(MACH(dst, op[0], op[1]));
1149 break;
1150 }
1151
1152 case nir_op_frcp:
1153 inst = emit_math(SHADER_OPCODE_RCP, dst, op[0]);
1154 inst->saturate = instr->dest.saturate;
1155 break;
1156
1157 case nir_op_fexp2:
1158 inst = emit_math(SHADER_OPCODE_EXP2, dst, op[0]);
1159 inst->saturate = instr->dest.saturate;
1160 break;
1161
1162 case nir_op_flog2:
1163 inst = emit_math(SHADER_OPCODE_LOG2, dst, op[0]);
1164 inst->saturate = instr->dest.saturate;
1165 break;
1166
1167 case nir_op_fsin:
1168 inst = emit_math(SHADER_OPCODE_SIN, dst, op[0]);
1169 inst->saturate = instr->dest.saturate;
1170 break;
1171
1172 case nir_op_fcos:
1173 inst = emit_math(SHADER_OPCODE_COS, dst, op[0]);
1174 inst->saturate = instr->dest.saturate;
1175 break;
1176
1177 case nir_op_idiv:
1178 case nir_op_udiv:
1179 emit_math(SHADER_OPCODE_INT_QUOTIENT, dst, op[0], op[1]);
1180 break;
1181
1182 case nir_op_umod:
1183 case nir_op_irem:
1184 /* According to the sign table for INT DIV in the Ivy Bridge PRM, it
1185 * appears that our hardware just does the right thing for signed
1186 * remainder.
1187 */
1188 emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1189 break;
1190
1191 case nir_op_imod: {
1192 /* Get a regular C-style remainder. If a % b == 0, set the predicate. */
1193 inst = emit_math(SHADER_OPCODE_INT_REMAINDER, dst, op[0], op[1]);
1194
1195 /* Math instructions don't support conditional mod */
1196 inst = emit(MOV(dst_null_d(), src_reg(dst)));
1197 inst->conditional_mod = BRW_CONDITIONAL_NZ;
1198
1199 /* Now, we need to determine if signs of the sources are different.
1200 * When we XOR the sources, the top bit is 0 if they are the same and 1
1201 * if they are different. We can then use a conditional modifier to
1202 * turn that into a predicate. This leads us to an XOR.l instruction.
1203 *
1204 * Technically, according to the PRM, you're not allowed to use .l on a
1205 * XOR instruction. However, emperical experiments and Curro's reading
1206 * of the simulator source both indicate that it's safe.
1207 */
1208 src_reg tmp = src_reg(this, glsl_type::ivec4_type);
1209 inst = emit(XOR(dst_reg(tmp), op[0], op[1]));
1210 inst->predicate = BRW_PREDICATE_NORMAL;
1211 inst->conditional_mod = BRW_CONDITIONAL_L;
1212
1213 /* If the result of the initial remainder operation is non-zero and the
1214 * two sources have different signs, add in a copy of op[1] to get the
1215 * final integer modulus value.
1216 */
1217 inst = emit(ADD(dst, src_reg(dst), op[1]));
1218 inst->predicate = BRW_PREDICATE_NORMAL;
1219 break;
1220 }
1221
1222 case nir_op_ldexp:
1223 unreachable("not reached: should be handled by ldexp_to_arith()");
1224
1225 case nir_op_fsqrt:
1226 inst = emit_math(SHADER_OPCODE_SQRT, dst, op[0]);
1227 inst->saturate = instr->dest.saturate;
1228 break;
1229
1230 case nir_op_frsq:
1231 inst = emit_math(SHADER_OPCODE_RSQ, dst, op[0]);
1232 inst->saturate = instr->dest.saturate;
1233 break;
1234
1235 case nir_op_fpow:
1236 inst = emit_math(SHADER_OPCODE_POW, dst, op[0], op[1]);
1237 inst->saturate = instr->dest.saturate;
1238 break;
1239
1240 case nir_op_uadd_carry: {
1241 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1242
1243 emit(ADDC(dst_null_ud(), op[0], op[1]));
1244 emit(MOV(dst, src_reg(acc)));
1245 break;
1246 }
1247
1248 case nir_op_usub_borrow: {
1249 struct brw_reg acc = retype(brw_acc_reg(8), BRW_REGISTER_TYPE_UD);
1250
1251 emit(SUBB(dst_null_ud(), op[0], op[1]));
1252 emit(MOV(dst, src_reg(acc)));
1253 break;
1254 }
1255
1256 case nir_op_ftrunc:
1257 inst = emit(RNDZ(dst, op[0]));
1258 inst->saturate = instr->dest.saturate;
1259 break;
1260
1261 case nir_op_fceil: {
1262 src_reg tmp = src_reg(this, glsl_type::float_type);
1263 tmp.swizzle =
1264 brw_swizzle_for_size(instr->src[0].src.is_ssa ?
1265 instr->src[0].src.ssa->num_components :
1266 instr->src[0].src.reg.reg->num_components);
1267
1268 op[0].negate = !op[0].negate;
1269 emit(RNDD(dst_reg(tmp), op[0]));
1270 tmp.negate = true;
1271 inst = emit(MOV(dst, tmp));
1272 inst->saturate = instr->dest.saturate;
1273 break;
1274 }
1275
1276 case nir_op_ffloor:
1277 inst = emit(RNDD(dst, op[0]));
1278 inst->saturate = instr->dest.saturate;
1279 break;
1280
1281 case nir_op_ffract:
1282 inst = emit(FRC(dst, op[0]));
1283 inst->saturate = instr->dest.saturate;
1284 break;
1285
1286 case nir_op_fround_even:
1287 inst = emit(RNDE(dst, op[0]));
1288 inst->saturate = instr->dest.saturate;
1289 break;
1290
1291 case nir_op_fquantize2f16: {
1292 /* See also vec4_visitor::emit_pack_half_2x16() */
1293 src_reg tmp16 = src_reg(this, glsl_type::uvec4_type);
1294 src_reg tmp32 = src_reg(this, glsl_type::vec4_type);
1295 src_reg zero = src_reg(this, glsl_type::vec4_type);
1296
1297 /* Check for denormal */
1298 src_reg abs_src0 = op[0];
1299 abs_src0.abs = true;
1300 emit(CMP(dst_null_f(), abs_src0, brw_imm_f(ldexpf(1.0, -14)),
1301 BRW_CONDITIONAL_L));
1302 /* Get the appropriately signed zero */
1303 emit(AND(retype(dst_reg(zero), BRW_REGISTER_TYPE_UD),
1304 retype(op[0], BRW_REGISTER_TYPE_UD),
1305 brw_imm_ud(0x80000000)));
1306 /* Do the actual F32 -> F16 -> F32 conversion */
1307 emit(F32TO16(dst_reg(tmp16), op[0]));
1308 emit(F16TO32(dst_reg(tmp32), tmp16));
1309 /* Select that or zero based on normal status */
1310 inst = emit(BRW_OPCODE_SEL, dst, zero, tmp32);
1311 inst->predicate = BRW_PREDICATE_NORMAL;
1312 inst->saturate = instr->dest.saturate;
1313 break;
1314 }
1315
1316 case nir_op_fmin:
1317 case nir_op_imin:
1318 case nir_op_umin:
1319 inst = emit_minmax(BRW_CONDITIONAL_L, dst, op[0], op[1]);
1320 inst->saturate = instr->dest.saturate;
1321 break;
1322
1323 case nir_op_fmax:
1324 case nir_op_imax:
1325 case nir_op_umax:
1326 inst = emit_minmax(BRW_CONDITIONAL_GE, dst, op[0], op[1]);
1327 inst->saturate = instr->dest.saturate;
1328 break;
1329
1330 case nir_op_fddx:
1331 case nir_op_fddx_coarse:
1332 case nir_op_fddx_fine:
1333 case nir_op_fddy:
1334 case nir_op_fddy_coarse:
1335 case nir_op_fddy_fine:
1336 unreachable("derivatives are not valid in vertex shaders");
1337
1338 case nir_op_flt:
1339 case nir_op_ilt:
1340 case nir_op_ult:
1341 case nir_op_fge:
1342 case nir_op_ige:
1343 case nir_op_uge:
1344 case nir_op_feq:
1345 case nir_op_ieq:
1346 case nir_op_fne:
1347 case nir_op_ine:
1348 emit(CMP(dst, op[0], op[1],
1349 brw_conditional_for_nir_comparison(instr->op)));
1350 break;
1351
1352 case nir_op_ball_fequal2:
1353 case nir_op_ball_iequal2:
1354 case nir_op_ball_fequal3:
1355 case nir_op_ball_iequal3:
1356 case nir_op_ball_fequal4:
1357 case nir_op_ball_iequal4: {
1358 unsigned swiz =
1359 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1360
1361 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1362 brw_conditional_for_nir_comparison(instr->op)));
1363 emit(MOV(dst, brw_imm_d(0)));
1364 inst = emit(MOV(dst, brw_imm_d(~0)));
1365 inst->predicate = BRW_PREDICATE_ALIGN16_ALL4H;
1366 break;
1367 }
1368
1369 case nir_op_bany_fnequal2:
1370 case nir_op_bany_inequal2:
1371 case nir_op_bany_fnequal3:
1372 case nir_op_bany_inequal3:
1373 case nir_op_bany_fnequal4:
1374 case nir_op_bany_inequal4: {
1375 unsigned swiz =
1376 brw_swizzle_for_size(nir_op_infos[instr->op].input_sizes[0]);
1377
1378 emit(CMP(dst_null_d(), swizzle(op[0], swiz), swizzle(op[1], swiz),
1379 brw_conditional_for_nir_comparison(instr->op)));
1380
1381 emit(MOV(dst, brw_imm_d(0)));
1382 inst = emit(MOV(dst, brw_imm_d(~0)));
1383 inst->predicate = BRW_PREDICATE_ALIGN16_ANY4H;
1384 break;
1385 }
1386
1387 case nir_op_inot:
1388 if (devinfo->gen >= 8) {
1389 op[0] = resolve_source_modifiers(op[0]);
1390 }
1391 emit(NOT(dst, op[0]));
1392 break;
1393
1394 case nir_op_ixor:
1395 if (devinfo->gen >= 8) {
1396 op[0] = resolve_source_modifiers(op[0]);
1397 op[1] = resolve_source_modifiers(op[1]);
1398 }
1399 emit(XOR(dst, op[0], op[1]));
1400 break;
1401
1402 case nir_op_ior:
1403 if (devinfo->gen >= 8) {
1404 op[0] = resolve_source_modifiers(op[0]);
1405 op[1] = resolve_source_modifiers(op[1]);
1406 }
1407 emit(OR(dst, op[0], op[1]));
1408 break;
1409
1410 case nir_op_iand:
1411 if (devinfo->gen >= 8) {
1412 op[0] = resolve_source_modifiers(op[0]);
1413 op[1] = resolve_source_modifiers(op[1]);
1414 }
1415 emit(AND(dst, op[0], op[1]));
1416 break;
1417
1418 case nir_op_b2i:
1419 case nir_op_b2f:
1420 emit(MOV(dst, negate(op[0])));
1421 break;
1422
1423 case nir_op_f2b:
1424 emit(CMP(dst, op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1425 break;
1426
1427 case nir_op_i2b:
1428 emit(CMP(dst, op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1429 break;
1430
1431 case nir_op_fnoise1_1:
1432 case nir_op_fnoise1_2:
1433 case nir_op_fnoise1_3:
1434 case nir_op_fnoise1_4:
1435 case nir_op_fnoise2_1:
1436 case nir_op_fnoise2_2:
1437 case nir_op_fnoise2_3:
1438 case nir_op_fnoise2_4:
1439 case nir_op_fnoise3_1:
1440 case nir_op_fnoise3_2:
1441 case nir_op_fnoise3_3:
1442 case nir_op_fnoise3_4:
1443 case nir_op_fnoise4_1:
1444 case nir_op_fnoise4_2:
1445 case nir_op_fnoise4_3:
1446 case nir_op_fnoise4_4:
1447 unreachable("not reached: should be handled by lower_noise");
1448
1449 case nir_op_unpack_half_2x16_split_x:
1450 case nir_op_unpack_half_2x16_split_y:
1451 case nir_op_pack_half_2x16_split:
1452 unreachable("not reached: should not occur in vertex shader");
1453
1454 case nir_op_unpack_snorm_2x16:
1455 case nir_op_unpack_unorm_2x16:
1456 case nir_op_pack_snorm_2x16:
1457 case nir_op_pack_unorm_2x16:
1458 unreachable("not reached: should be handled by lower_packing_builtins");
1459
1460 case nir_op_pack_uvec4_to_uint:
1461 unreachable("not reached");
1462
1463 case nir_op_pack_uvec2_to_uint: {
1464 dst_reg tmp1 = dst_reg(this, glsl_type::uint_type);
1465 tmp1.writemask = WRITEMASK_X;
1466 op[0].swizzle = BRW_SWIZZLE_YYYY;
1467 emit(SHL(tmp1, op[0], src_reg(brw_imm_ud(16u))));
1468
1469 dst_reg tmp2 = dst_reg(this, glsl_type::uint_type);
1470 tmp2.writemask = WRITEMASK_X;
1471 op[0].swizzle = BRW_SWIZZLE_XXXX;
1472 emit(AND(tmp2, op[0], src_reg(brw_imm_ud(0xffffu))));
1473
1474 emit(OR(dst, src_reg(tmp1), src_reg(tmp2)));
1475 break;
1476 }
1477
1478 case nir_op_unpack_half_2x16:
1479 /* As NIR does not guarantee that we have a correct swizzle outside the
1480 * boundaries of a vector, and the implementation of emit_unpack_half_2x16
1481 * uses the source operand in an operation with WRITEMASK_Y while our
1482 * source operand has only size 1, it accessed incorrect data producing
1483 * regressions in Piglit. We repeat the swizzle of the first component on the
1484 * rest of components to avoid regressions. In the vec4_visitor IR code path
1485 * this is not needed because the operand has already the correct swizzle.
1486 */
1487 op[0].swizzle = brw_compose_swizzle(BRW_SWIZZLE_XXXX, op[0].swizzle);
1488 emit_unpack_half_2x16(dst, op[0]);
1489 break;
1490
1491 case nir_op_pack_half_2x16:
1492 emit_pack_half_2x16(dst, op[0]);
1493 break;
1494
1495 case nir_op_unpack_unorm_4x8:
1496 emit_unpack_unorm_4x8(dst, op[0]);
1497 break;
1498
1499 case nir_op_pack_unorm_4x8:
1500 emit_pack_unorm_4x8(dst, op[0]);
1501 break;
1502
1503 case nir_op_unpack_snorm_4x8:
1504 emit_unpack_snorm_4x8(dst, op[0]);
1505 break;
1506
1507 case nir_op_pack_snorm_4x8:
1508 emit_pack_snorm_4x8(dst, op[0]);
1509 break;
1510
1511 case nir_op_bitfield_reverse:
1512 emit(BFREV(dst, op[0]));
1513 break;
1514
1515 case nir_op_bit_count:
1516 emit(CBIT(dst, op[0]));
1517 break;
1518
1519 case nir_op_ufind_msb:
1520 emit_find_msb_using_lzd(vec4_builder(this).at_end(), dst, op[0], false);
1521 break;
1522
1523 case nir_op_ifind_msb: {
1524 vec4_builder bld = vec4_builder(this).at_end();
1525 src_reg src(dst);
1526
1527 if (devinfo->gen < 7) {
1528 emit_find_msb_using_lzd(bld, dst, op[0], true);
1529 } else {
1530 emit(FBH(retype(dst, BRW_REGISTER_TYPE_UD), op[0]));
1531
1532 /* FBH counts from the MSB side, while GLSL's findMSB() wants the
1533 * count from the LSB side. If FBH didn't return an error
1534 * (0xFFFFFFFF), then subtract the result from 31 to convert the MSB
1535 * count into an LSB count.
1536 */
1537 bld.CMP(dst_null_d(), src, brw_imm_d(-1), BRW_CONDITIONAL_NZ);
1538
1539 inst = bld.ADD(dst, src, brw_imm_d(31));
1540 inst->predicate = BRW_PREDICATE_NORMAL;
1541 inst->src[0].negate = true;
1542 }
1543 break;
1544 }
1545
1546 case nir_op_find_lsb: {
1547 vec4_builder bld = vec4_builder(this).at_end();
1548
1549 if (devinfo->gen < 7) {
1550 dst_reg temp = bld.vgrf(BRW_REGISTER_TYPE_D);
1551
1552 /* (x & -x) generates a value that consists of only the LSB of x.
1553 * For all powers of 2, findMSB(y) == findLSB(y).
1554 */
1555 src_reg src = src_reg(retype(op[0], BRW_REGISTER_TYPE_D));
1556 src_reg negated_src = src;
1557
1558 /* One must be negated, and the other must be non-negated. It
1559 * doesn't matter which is which.
1560 */
1561 negated_src.negate = true;
1562 src.negate = false;
1563
1564 bld.AND(temp, src, negated_src);
1565 emit_find_msb_using_lzd(bld, dst, src_reg(temp), false);
1566 } else {
1567 bld.FBL(dst, op[0]);
1568 }
1569 break;
1570 }
1571
1572 case nir_op_ubitfield_extract:
1573 case nir_op_ibitfield_extract:
1574 unreachable("should have been lowered");
1575 case nir_op_ubfe:
1576 case nir_op_ibfe:
1577 op[0] = fix_3src_operand(op[0]);
1578 op[1] = fix_3src_operand(op[1]);
1579 op[2] = fix_3src_operand(op[2]);
1580
1581 emit(BFE(dst, op[2], op[1], op[0]));
1582 break;
1583
1584 case nir_op_bfm:
1585 emit(BFI1(dst, op[0], op[1]));
1586 break;
1587
1588 case nir_op_bfi:
1589 op[0] = fix_3src_operand(op[0]);
1590 op[1] = fix_3src_operand(op[1]);
1591 op[2] = fix_3src_operand(op[2]);
1592
1593 emit(BFI2(dst, op[0], op[1], op[2]));
1594 break;
1595
1596 case nir_op_bitfield_insert:
1597 unreachable("not reached: should have been lowered");
1598
1599 case nir_op_fsign:
1600 /* AND(val, 0x80000000) gives the sign bit.
1601 *
1602 * Predicated OR ORs 1.0 (0x3f800000) with the sign bit if val is not
1603 * zero.
1604 */
1605 emit(CMP(dst_null_f(), op[0], brw_imm_f(0.0f), BRW_CONDITIONAL_NZ));
1606
1607 op[0].type = BRW_REGISTER_TYPE_UD;
1608 dst.type = BRW_REGISTER_TYPE_UD;
1609 emit(AND(dst, op[0], brw_imm_ud(0x80000000u)));
1610
1611 inst = emit(OR(dst, src_reg(dst), brw_imm_ud(0x3f800000u)));
1612 inst->predicate = BRW_PREDICATE_NORMAL;
1613 dst.type = BRW_REGISTER_TYPE_F;
1614
1615 if (instr->dest.saturate) {
1616 inst = emit(MOV(dst, src_reg(dst)));
1617 inst->saturate = true;
1618 }
1619 break;
1620
1621 case nir_op_isign:
1622 /* ASR(val, 31) -> negative val generates 0xffffffff (signed -1).
1623 * -> non-negative val generates 0x00000000.
1624 * Predicated OR sets 1 if val is positive.
1625 */
1626 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_G));
1627 emit(ASR(dst, op[0], brw_imm_d(31)));
1628 inst = emit(OR(dst, src_reg(dst), brw_imm_d(1)));
1629 inst->predicate = BRW_PREDICATE_NORMAL;
1630 break;
1631
1632 case nir_op_ishl:
1633 emit(SHL(dst, op[0], op[1]));
1634 break;
1635
1636 case nir_op_ishr:
1637 emit(ASR(dst, op[0], op[1]));
1638 break;
1639
1640 case nir_op_ushr:
1641 emit(SHR(dst, op[0], op[1]));
1642 break;
1643
1644 case nir_op_ffma:
1645 op[0] = fix_3src_operand(op[0]);
1646 op[1] = fix_3src_operand(op[1]);
1647 op[2] = fix_3src_operand(op[2]);
1648
1649 inst = emit(MAD(dst, op[2], op[1], op[0]));
1650 inst->saturate = instr->dest.saturate;
1651 break;
1652
1653 case nir_op_flrp:
1654 inst = emit_lrp(dst, op[0], op[1], op[2]);
1655 inst->saturate = instr->dest.saturate;
1656 break;
1657
1658 case nir_op_bcsel:
1659 enum brw_predicate predicate;
1660 if (!optimize_predicate(instr, &predicate)) {
1661 emit(CMP(dst_null_d(), op[0], brw_imm_d(0), BRW_CONDITIONAL_NZ));
1662 switch (dst.writemask) {
1663 case WRITEMASK_X:
1664 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_X;
1665 break;
1666 case WRITEMASK_Y:
1667 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Y;
1668 break;
1669 case WRITEMASK_Z:
1670 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_Z;
1671 break;
1672 case WRITEMASK_W:
1673 predicate = BRW_PREDICATE_ALIGN16_REPLICATE_W;
1674 break;
1675 default:
1676 predicate = BRW_PREDICATE_NORMAL;
1677 break;
1678 }
1679 }
1680 inst = emit(BRW_OPCODE_SEL, dst, op[1], op[2]);
1681 inst->predicate = predicate;
1682 break;
1683
1684 case nir_op_fdot_replicated2:
1685 inst = emit(BRW_OPCODE_DP2, dst, op[0], op[1]);
1686 inst->saturate = instr->dest.saturate;
1687 break;
1688
1689 case nir_op_fdot_replicated3:
1690 inst = emit(BRW_OPCODE_DP3, dst, op[0], op[1]);
1691 inst->saturate = instr->dest.saturate;
1692 break;
1693
1694 case nir_op_fdot_replicated4:
1695 inst = emit(BRW_OPCODE_DP4, dst, op[0], op[1]);
1696 inst->saturate = instr->dest.saturate;
1697 break;
1698
1699 case nir_op_fdph_replicated:
1700 inst = emit(BRW_OPCODE_DPH, dst, op[0], op[1]);
1701 inst->saturate = instr->dest.saturate;
1702 break;
1703
1704 case nir_op_fabs:
1705 case nir_op_iabs:
1706 case nir_op_fneg:
1707 case nir_op_ineg:
1708 case nir_op_fsat:
1709 unreachable("not reached: should be lowered by lower_source mods");
1710
1711 case nir_op_fdiv:
1712 unreachable("not reached: should be lowered by DIV_TO_MUL_RCP in the compiler");
1713
1714 case nir_op_fmod:
1715 unreachable("not reached: should be lowered by MOD_TO_FLOOR in the compiler");
1716
1717 case nir_op_fsub:
1718 case nir_op_isub:
1719 unreachable("not reached: should be handled by ir_sub_to_add_neg");
1720
1721 default:
1722 unreachable("Unimplemented ALU operation");
1723 }
1724
1725 /* If we need to do a boolean resolve, replace the result with -(x & 1)
1726 * to sign extend the low bit to 0/~0
1727 */
1728 if (devinfo->gen <= 5 &&
1729 (instr->instr.pass_flags & BRW_NIR_BOOLEAN_MASK) ==
1730 BRW_NIR_BOOLEAN_NEEDS_RESOLVE) {
1731 dst_reg masked = dst_reg(this, glsl_type::int_type);
1732 masked.writemask = dst.writemask;
1733 emit(AND(masked, src_reg(dst), brw_imm_d(1)));
1734 src_reg masked_neg = src_reg(masked);
1735 masked_neg.negate = true;
1736 emit(MOV(retype(dst, BRW_REGISTER_TYPE_D), masked_neg));
1737 }
1738 }
1739
1740 void
1741 vec4_visitor::nir_emit_jump(nir_jump_instr *instr)
1742 {
1743 switch (instr->type) {
1744 case nir_jump_break:
1745 emit(BRW_OPCODE_BREAK);
1746 break;
1747
1748 case nir_jump_continue:
1749 emit(BRW_OPCODE_CONTINUE);
1750 break;
1751
1752 case nir_jump_return:
1753 /* fall through */
1754 default:
1755 unreachable("unknown jump");
1756 }
1757 }
1758
1759 enum ir_texture_opcode
1760 ir_texture_opcode_for_nir_texop(nir_texop texop)
1761 {
1762 enum ir_texture_opcode op;
1763
1764 switch (texop) {
1765 case nir_texop_lod: op = ir_lod; break;
1766 case nir_texop_query_levels: op = ir_query_levels; break;
1767 case nir_texop_texture_samples: op = ir_texture_samples; break;
1768 case nir_texop_tex: op = ir_tex; break;
1769 case nir_texop_tg4: op = ir_tg4; break;
1770 case nir_texop_txb: op = ir_txb; break;
1771 case nir_texop_txd: op = ir_txd; break;
1772 case nir_texop_txf: op = ir_txf; break;
1773 case nir_texop_txf_ms: op = ir_txf_ms; break;
1774 case nir_texop_txl: op = ir_txl; break;
1775 case nir_texop_txs: op = ir_txs; break;
1776 case nir_texop_samples_identical: op = ir_samples_identical; break;
1777 default:
1778 unreachable("unknown texture opcode");
1779 }
1780
1781 return op;
1782 }
1783 const glsl_type *
1784 glsl_type_for_nir_alu_type(nir_alu_type alu_type,
1785 unsigned components)
1786 {
1787 switch (alu_type) {
1788 case nir_type_float:
1789 return glsl_type::vec(components);
1790 case nir_type_int:
1791 return glsl_type::ivec(components);
1792 case nir_type_uint:
1793 return glsl_type::uvec(components);
1794 case nir_type_bool:
1795 return glsl_type::bvec(components);
1796 default:
1797 return glsl_type::error_type;
1798 }
1799
1800 return glsl_type::error_type;
1801 }
1802
1803 void
1804 vec4_visitor::nir_emit_texture(nir_tex_instr *instr)
1805 {
1806 unsigned texture = instr->texture_index;
1807 unsigned sampler = instr->sampler_index;
1808 src_reg texture_reg = brw_imm_ud(texture);
1809 src_reg sampler_reg = brw_imm_ud(sampler);
1810 src_reg coordinate;
1811 const glsl_type *coord_type = NULL;
1812 src_reg shadow_comparitor;
1813 src_reg offset_value;
1814 src_reg lod, lod2;
1815 src_reg sample_index;
1816 src_reg mcs;
1817
1818 const glsl_type *dest_type =
1819 glsl_type_for_nir_alu_type(instr->dest_type,
1820 nir_tex_instr_dest_size(instr));
1821 dst_reg dest = get_nir_dest(instr->dest, instr->dest_type);
1822
1823 /* The hardware requires a LOD for buffer textures */
1824 if (instr->sampler_dim == GLSL_SAMPLER_DIM_BUF)
1825 lod = brw_imm_d(0);
1826
1827 /* Load the texture operation sources */
1828 uint32_t constant_offset = 0;
1829 for (unsigned i = 0; i < instr->num_srcs; i++) {
1830 switch (instr->src[i].src_type) {
1831 case nir_tex_src_comparitor:
1832 shadow_comparitor = get_nir_src(instr->src[i].src,
1833 BRW_REGISTER_TYPE_F, 1);
1834 break;
1835
1836 case nir_tex_src_coord: {
1837 unsigned src_size = nir_tex_instr_src_size(instr, i);
1838
1839 switch (instr->op) {
1840 case nir_texop_txf:
1841 case nir_texop_txf_ms:
1842 case nir_texop_samples_identical:
1843 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D,
1844 src_size);
1845 coord_type = glsl_type::ivec(src_size);
1846 break;
1847
1848 default:
1849 coordinate = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1850 src_size);
1851 coord_type = glsl_type::vec(src_size);
1852 break;
1853 }
1854 break;
1855 }
1856
1857 case nir_tex_src_ddx:
1858 lod = 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_ddy:
1863 lod2 = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F,
1864 nir_tex_instr_src_size(instr, i));
1865 break;
1866
1867 case nir_tex_src_lod:
1868 switch (instr->op) {
1869 case nir_texop_txs:
1870 case nir_texop_txf:
1871 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1872 break;
1873
1874 default:
1875 lod = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_F, 1);
1876 break;
1877 }
1878 break;
1879
1880 case nir_tex_src_ms_index: {
1881 sample_index = get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 1);
1882 break;
1883 }
1884
1885 case nir_tex_src_offset: {
1886 nir_const_value *const_offset =
1887 nir_src_as_const_value(instr->src[i].src);
1888 if (const_offset) {
1889 constant_offset = brw_texture_offset(const_offset->i32, 3);
1890 } else {
1891 offset_value =
1892 get_nir_src(instr->src[i].src, BRW_REGISTER_TYPE_D, 2);
1893 }
1894 break;
1895 }
1896
1897 case nir_tex_src_texture_offset: {
1898 /* The highest texture which may be used by this operation is
1899 * the last element of the array. Mark it here, because the generator
1900 * doesn't have enough information to determine the bound.
1901 */
1902 uint32_t array_size = instr->texture_array_size;
1903 uint32_t max_used = texture + array_size - 1;
1904 if (instr->op == nir_texop_tg4) {
1905 max_used += prog_data->base.binding_table.gather_texture_start;
1906 } else {
1907 max_used += prog_data->base.binding_table.texture_start;
1908 }
1909
1910 brw_mark_surface_used(&prog_data->base, max_used);
1911
1912 /* Emit code to evaluate the actual indexing expression */
1913 src_reg src = get_nir_src(instr->src[i].src, 1);
1914 src_reg temp(this, glsl_type::uint_type);
1915 emit(ADD(dst_reg(temp), src, brw_imm_ud(texture)));
1916 texture_reg = emit_uniformize(temp);
1917 break;
1918 }
1919
1920 case nir_tex_src_sampler_offset: {
1921 /* Emit code to evaluate the actual indexing expression */
1922 src_reg src = get_nir_src(instr->src[i].src, 1);
1923 src_reg temp(this, glsl_type::uint_type);
1924 emit(ADD(dst_reg(temp), src, brw_imm_ud(sampler)));
1925 sampler_reg = emit_uniformize(temp);
1926 break;
1927 }
1928
1929 case nir_tex_src_projector:
1930 unreachable("Should be lowered by do_lower_texture_projection");
1931
1932 case nir_tex_src_bias:
1933 unreachable("LOD bias is not valid for vertex shaders.\n");
1934
1935 default:
1936 unreachable("unknown texture source");
1937 }
1938 }
1939
1940 if (instr->op == nir_texop_txf_ms ||
1941 instr->op == nir_texop_samples_identical) {
1942 assert(coord_type != NULL);
1943 if (devinfo->gen >= 7 &&
1944 key_tex->compressed_multisample_layout_mask & (1 << texture)) {
1945 mcs = emit_mcs_fetch(coord_type, coordinate, texture_reg);
1946 } else {
1947 mcs = brw_imm_ud(0u);
1948 }
1949 }
1950
1951 /* Stuff the channel select bits in the top of the texture offset */
1952 if (instr->op == nir_texop_tg4) {
1953 if (instr->component == 1 &&
1954 (key_tex->gather_channel_quirk_mask & (1 << texture))) {
1955 /* gather4 sampler is broken for green channel on RG32F --
1956 * we must ask for blue instead.
1957 */
1958 constant_offset |= 2 << 16;
1959 } else {
1960 constant_offset |= instr->component << 16;
1961 }
1962 }
1963
1964 ir_texture_opcode op = ir_texture_opcode_for_nir_texop(instr->op);
1965
1966 emit_texture(op, dest, dest_type, coordinate, instr->coord_components,
1967 shadow_comparitor,
1968 lod, lod2, sample_index,
1969 constant_offset, offset_value, mcs,
1970 texture, texture_reg, sampler_reg);
1971 }
1972
1973 void
1974 vec4_visitor::nir_emit_undef(nir_ssa_undef_instr *instr)
1975 {
1976 nir_ssa_values[instr->def.index] = dst_reg(VGRF, alloc.allocate(1));
1977 }
1978
1979 }