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